I did it again.
Here is the [http://twiki.org/cgi-bin/view/Plugins/MessageBoardPlugin|MessageBoardPlugin], a [http://www.twiki.org|TWiki] [http://twiki.org/cgi-bin/view/Plugins|Plugin] for posting messages like a coffee message board, where users can post anything they need to. Hope that the fellows like reading and using this.
Code follows:Filename: MessageBoardPlugin.txt
%META:TOPICINFO{author="lcampos" date="1087408800" format="1.0" version="1.9"}%
---+ Message Board Plugin
This is a database based message board plugin for the
* Set PENCIL_ICON_LINK =
* Database Settings
* Set DB_DRIVER = mysql
* Set DB_SERVER = _IP or hostname here_
* Set DB_SERVER_PORT = 3306
* Set DB_DATABASE = message_board
* Set DB_TABLE = message
* Set DB_USER = board
* Set DB_PASSWORD = _password_
* Plugin Messages (so you can internationalize them):
* Error opening connection to the database
* Set MSG_DB_CONNECT_ERROR = %X% %RED% Error connecting to the database%ENDCOLOR%
* Error closing connection to the database
* Set MSG_DB_CLOSE_ERROR = %X% %RED% Error closing the database connection%ENDCOLOR%
* Error during query preparation
* Set MSG_DB_PREPARE_ERROR = %X% %RED% Error preparing query%ENDCOLOR%
* Error during data fetch
* Set MSG_DB_FETCH_ERROR = %X% %RED% Error fetching data from database%ENDCOLOR%
* Error during query execution
* Set MSG_DB_EXECUTE_ERROR = %X% %RED% Error executing query%ENDCOLOR%
* No data found error
* Set MSG_DB_NO_DATA_ERROR = %X% %RED% No data found%ENDCOLOR%
* Update error message
* Set MSG_DB_UPDATE_ERROR = %X% %RED% Error updating data%ENDCOLOR%
* Insert error message
* Set MSG_DB_INSERT_ERROR = %X% %RED% Error inserting data%ENDCOLOR%
* Debug plugin: (See output in =data/debug.txt=)
* Set DEBUG = 0
---++ Plugin Installation Instructions
__Note:__ You do not need to install anything on the browser to use this plugin. The following instructions are for the administrator who installs the plugin on the server where TWiki is running.
* Download the ZIP file from the Plugin web (see below)
* Unzip ==%TOPIC%.zip== in your twiki installation directory. Content:
| *File:* | *Description:* |
| ==data/TWiki/%TOPIC%.txt== | Plugin topic |
| ==data/TWiki/%TOPIC%.txt,v== | Plugin topic repository |
| ==lib/TWiki/Plugins/%TOPIC%.pm== | Plugin Perl module |
| ==pub/TWiki/MessageBoardPlugin/trashcan.png== | Trash Can Icon |
| ==pub/TWiki/MessageBoardPlugin/editicon.png== | Pencil and Paper Icon |
* Create a table into your database using (this is mysql SQL, the only DB supported at the moment)
CREATE DATABASE message_board;
CREATE TABLE message(
id INT PRIMARY KEY AUTO_INCREMENT,
author VARCHAR(50) NOT NULL,
due DATETIME NOT NULL,
posted DATETIME NOT NULL,
msg TEXT NOT NULL,
dropped ENUM( 'Y', 'N' ) NOT NULL DEFAULT 'N'
)
* Create a new user and give it appropriated permissions:
GRANT SELECT, INSERT, UPDATE, DELETE
ON messsage_board.message
TO board IDENTIFIED BY 'b0aRd'
* Test if the plugin is correctly installed:
* Open a Sandbox.MessageBoardTestArea and insert %
Filename: MessageBoardPlugin.pm
# Plugin for TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 2004 Luis Campos de Carvalho, monsieur_champs@yahoo.com.br
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
#
# =========================
package TWiki::Plugins::MessageBoardPlugin;
use strict;
use warnings;
use DBI;
# =========================
use vars qw(
$web $topic $user $installWeb $VERSION $pluginName
$debug %db %i18nMessage %color
);
$VERSION = '1.000';
$pluginName = 'MessageBoardPlugin';
# =========================
sub initPlugin
{
( $topic, $web, $user, $installWeb ) = @_;
# check for Plugins.pm versions
if( $TWiki::Plugins::VERSION < 1 ) {
TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" );
return 0;
}
# Get plugin debug flag
$debug = TWiki::Func::getPreferencesFlag( "\U$pluginName\E_DEBUG" );
# Get plugin color settings
%color =
map { $_->[0] => &TWiki::Prefs::getPreferencesValue( $_->[1] ) }
( [ TABLE_HEAD => "\U$pluginName\E_TABLE_HEAD_COLOR" ],
[ REVERSE_LINE => "\U$pluginName\E_REVERSE_LINE_COLOR" ],
);
# Get plugin messages (so you can internationalize and customize
# them at will )
%i18nMessage =
map { $_->[0] => &TWiki::Prefs::getPreferencesValue( $_->[1] ) }
( [ DB_CONNECT_ERROR => "\U$pluginName\E_MSG_DB_CONNECT_ERROR" ],
[ DB_CLOSE_ERROR => "\U$pluginName\E_MSG_DB_CLOSE_ERROR" ],
[ DB_PREPARE_ERROR => "\U$pluginName\E_MSG_DB_PREPARE_ERROR" ],
[ DB_FETCH_ERROR => "\U$pluginName\E_MSG_DB_FETCH_ERROR" ],
[ DB_EXECUTE_ERROR => "\U$pluginName\E_MSG_DB_EXECUTE_ERROR" ],
[ DB_NO_DATA_ERROR => "\U$pluginName\E_MSG_DB_NO_DATA_ERROR" ],
[ DB_UPDATE_ERROR => "\U$pluginName\E_MSG_DB_UPDATE_ERROR" ],
[ DB_INSERT_ERROR => "\U$pluginName\E_MSG_DB_INSERT_ERROR" ],
);
# Get plugin database meta-data
%db =
map { $_->[0] => &TWiki::Prefs::getPreferencesValue( $_->[1] ) }
( [ driver => "\U$pluginName\E_DB_DRIVER" ],
[ host => "\U$pluginName\E_DB_SERVER" ],
[ port => "\U$pluginName\E_DB_SERVER_PORT" ],
[ database => "\U$pluginName\E_DB_DATABASE" ],
[ table => "\U$pluginName\E_DB_TABLE" ],
[ user => "\U$pluginName\E_DB_USER" ],
[ passwd => "\U$pluginName\E_DB_PASSWORD" ],
);
$db{dbh} = eval{
DBI->connect( 'dbi:'.$db{driver}.
':database='.$db{database}.
';hostname='.$db{host}.
';port='.$db{port},
$db{user},
$db{passwd},
{ RaiseError => 1, PrintError => 0 } )
};
$db{connection_error} = $@ if $@;
# Plugin correctly initialized
# TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ) if $debug;
return 1;
}
# =========================
sub commonTagsHandler{
### my ( $text, $topic, $web ) = @_;
# do not uncomment, use $_[0], $_[1]... instead
# TWiki::Func::writeDebug("- ${pluginName}::commonTagsHandler( $_[2].$_[1] )") if $debug;
# This is the place to define customized tags and variables
# Called by sub handleCommonTags, after %INCLUDE:"..."%
$_[0] =~ s/(%MESSAGE_BOARD(?:{[^}]+})?%)/&board($1)/ge;
}
# =========================
sub display{
my $msgOrder = shift;
my $sth = eval {
$db{dbh}->prepare( q{ SELECT id
, author
, DATE_FORMAT( due, '%T
%d/%m/%Y' ) as due
, DATE_FORMAT( posted, '%T
%d/%m/%Y' ) as posted
, msg
FROM message
WHERE dropped = 'N'
AND due >= NOW()
ORDER BY id
} . uc $msgOrder
);
};
return $i18nMessage{DB_PREPARE_ERROR}. ": $@." if $@;
eval { $sth->execute; };
return $i18nMessage{DB_EXECUTE_ERROR}. ": $@." if $@;
my $result = eval { $sth->fetchall_arrayref( {} ); };
return $i18nMessage{DB_FETCH_ERROR}. ": $@." if $@;
my $trash_icon = &TWiki::Func::expandCommonVariables( &TWiki::Prefs::getPreferencesValue( "\U$pluginName\E_TRASH_CAN_ICON_LINK" ), $topic, $web );
my $pencil_icon = &TWiki::Func::expandCommonVariables( &TWiki::Prefs::getPreferencesValue( "\U$pluginName\E_PENCIL_ICON_LINK" ), $topic, $web );
# Build Message Board Table and return it.
return
qq{| Due Date | Posted Date | Author | Drop & Change |
Message |
| ' . $_->{due} . ' | ' . $_->{posted} . ' | ' . $_->{author} . ' | ' . $pencil_icon . '' . $trash_icon . ' | ' . $_->{msg} . ' |
| '.&display( $messageOrder ).' |
| ' . &inputBox( &TWiki::Func::getViewUrl( $web, $topic ), undef, 'new', undef ) . ' |
Threre are also two images that are needed to display query results properly. You can fetch them from [http://www.TWiki.org/|TWiki]'s website.
-- [monsieur_champs]
perlmonks.org content © perlmonks.org and monsieur_champs
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03