# 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 ) . ' |