package WebGUI::Widget::MessageBoard;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001 Plain Black Software.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Privilege;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::Utility;
use WebGUI::Widget;
#-------------------------------------------------------------------
sub _getBoardProperties {
my (%board);
%board = WebGUI::SQL->quickHash("select widget.title, widget.displayTitle, widget.description, MessageBoard.groupToPost, MessageBoard.messagesPerPage, MessageBoard.editTimeout from widget left join MessageBoard on (widget.widgetId=MessageBoard.widgetId) where widget.widgetId=$_[0]",$session{dbh});
return %board;
}
#-------------------------------------------------------------------
sub _traverseReplyTree {
my ($sth, @data, $html, $depth, $i);
for ($i=0;$i<=$_[1];$i++) {
$depth .= " ";
}
$sth = WebGUI::SQL->read("select messageId,substring(subject,1,30),username,date_format(dateOfPost,'%c/%e %l:%i%p') from message where pid=$_[0] order by messageId", $session{dbh});
while (@data = $sth->array) {
$html .= '
'.$data[1].'| '.$data[2].' | '.$data[3].' |
';
$html .= _traverseReplyTree($data[0],$_[1]+1);
}
$sth->finish;
return $html;
}
#-------------------------------------------------------------------
sub purge {
WebGUI::SQL->write("delete from message where widgetId=$_[0]",$_[1]);
WebGUI::SQL->write("delete from MessageBoard where widgetId=$_[0]",$_[1]);
purgeWidget($_[0],$_[1]);
}
#-------------------------------------------------------------------
sub widgetName {
return "Message Board";
}
#-------------------------------------------------------------------
sub www_add {
my ($output, %hash);
tie %hash, "Tie::IxHash";
if (WebGUI::Privilege::canEditPage()) {
$output = '
Add Message Board
';
return $output;
} else {
return WebGUI::Privilege::insufficient();
}
return $output;
}
#-------------------------------------------------------------------
sub www_addSave {
my ($widgetId);
if (WebGUI::Privilege::canEditPage()) {
$widgetId = create();
WebGUI::SQL->write("insert into MessageBoard set widgetId=$widgetId, groupToPost=$session{form}{groupToPost}, messagesPerPage=$session{form}{messagesPerPage}, editTimeout=$session{form}{editTimeout}",$session{dbh});
return "";
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_edit {
my ($output, %board, %hash, @array);
tie %hash, "Tie::IxHash";
if (WebGUI::Privilege::canEditPage()) {
%board = _getBoardProperties($session{form}{wid});
$output = '
Edit Message Board
';
return $output;
} else {
return WebGUI::Privilege::insufficient();
}
return $output;
}
#-------------------------------------------------------------------
sub www_editSave {
if (WebGUI::Privilege::canEditPage()) {
update();
WebGUI::SQL->write("update MessageBoard set groupToPost=$session{form}{groupToPost}, messagesPerPage=$session{form}{messagesPerPage}, editTimeout=$session{form}{editTimeout} where widgetId=$session{form}{wid}",$session{dbh});
return "";
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_editMessage {
my ($html, %board, %message);
%board = _getBoardProperties($session{form}{wid});
if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) {
%message = WebGUI::SQL->quickHash("select * from message where messageId=$session{form}{mid}",$session{dbh});
$html .= '| ';
if ($board{displayTitle}) {
$html .= $board{title};
}
$html .= ' |
';
$html .= '';
$html .= www_showMessage();
} else {
$html = WebGUI::Privilege::insufficient();
}
return $html;
}
#-------------------------------------------------------------------
sub www_editMessageSave {
my (%board);
%board = _getBoardProperties($session{form}{wid});
if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) {
if ($session{form}{subject} eq "") {
$session{form}{subject} = 'no subject';
}
if ($session{form}{message} eq "") {
$session{form}{subject} .= ' (eom)';
}
WebGUI::SQL->write("update message set subject=".quote($session{form}{subject}).", message=".quote("\n --- (Edited at ".localtime(time).") --- \n\n".$session{form}{message})." where messageId=$session{form}{mid}",$session{dbh});
return www_showMessage();
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_postNewMessage {
my ($html, %board);
%board = _getBoardProperties($session{form}{wid});
if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) {
$html .= '| ';
if ($board{displayTitle}) {
$html .= $board{title};
}
$html .= ' |
';
$html .= '';
} else {
$html = WebGUI::Privilege::insufficient();
}
return $html;
}
#-------------------------------------------------------------------
sub www_postNewMessageSave {
my ($mid, %board);
%board = _getBoardProperties($session{form}{wid});
if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) {
if ($session{form}{subject} eq "") {
$session{form}{subject} = 'no subject';
}
if ($session{form}{message} eq "") {
$session{form}{subject} .= ' (eom)';
}
$mid = getNextId("messageId");
WebGUI::SQL->write("insert into message set messageId=$mid, userId=$session{user}{userId}, username=".quote($session{user}{username}).", subject=".quote($session{form}{subject}).", message=".quote($session{form}{message}).", widgetId=$session{form}{wid}, pid=0, dateOfPost=now()",$session{dbh});
WebGUI::SQL->write("update message set rid=$mid where messageId=$mid",$session{dbh});
return "";
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_postReply {
my ($html, %board, $subject);
%board = _getBoardProperties($session{form}{wid});
if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) {
($subject) = WebGUI::SQL->quickArray("select subject from message where messageId=$session{form}{mid}", $session{dbh});
$subject = "Re: ".$subject;
$html .= '| ';
if ($board{displayTitle}) {
$html .= $board{title};
}
$html .= ' |
';
$html .= '';
$html .= www_showMessage();
} else {
$html = WebGUI::Privilege::insufficient();
}
return $html;
}
#-------------------------------------------------------------------
sub www_postReplySave {
my ($rid, %board, $mid);
%board = _getBoardProperties($session{form}{wid});
if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) {
if ($session{form}{subject} eq "") {
$session{form}{subject} = 'no subject';
}
if ($session{form}{message} eq "") {
$session{form}{subject} .= ' (eom)';
}
$mid = getNextId("messageId");
($rid) = WebGUI::SQL->quickArray("select rid from message where messageId=$session{form}{mid}",$session{dbh});
WebGUI::SQL->write("insert into message set messageId=$mid, userId=$session{user}{userId}, username=".quote($session{user}{username}).", subject=".quote($session{form}{subject}).", message=".quote($session{form}{message}).", rid=$rid, widgetId=$session{form}{wid}, pid=$session{form}{mid}, dateOfPost=now()", $session{dbh});
return www_showMessage();
} else {
return WebGUI::Privilege::insufficient();
}
}
#-------------------------------------------------------------------
sub www_showMessage {
my (@data, $html, %board, %message);
%message = WebGUI::SQL->quickHash("select * from message where messageId=$session{form}{mid}",$session{dbh});
%board = _getBoardProperties($session{form}{wid});
$html .= '| ';
if ($board{displayTitle}) {
$html .= $board{title};
}
$html .= ' |
';
$html .= '';
$html .= '
';
$message{message} =~ s/\n/\ /g;
$html .= $message{message};
$html .= ' |
';
$html .= '
';
@data = WebGUI::SQL->quickArray("select messageId,substring(subject,1,30),username,date_format(dateOfPost,'%c/%e %l:%i%p') from message where messageId=$message{rid}",$session{dbh});
$html .= ''.$data[1].'| '.$data[2].' | '.$data[3].' |
';
$html .= _traverseReplyTree($message{rid},1);
$html .= "
";
return $html;
}
#-------------------------------------------------------------------
sub www_view {
my ($sth, @data, $html, %board, $itemsPerPage, $i, @row, $pn);
%board = _getBoardProperties($_[0]);
$itemsPerPage = $board{messagesPerPage};
if ($board{description} ne "") {
$html .= $board{description}.'';
}
$html .= '
| ';
if ($board{displayTitle}) {
$html .= $board{title};
}
$html .= ' |
';
$html .= '';
$html .= '
';
$sth = WebGUI::SQL->read("select messageId,substring(subject,1,30),count(messageId)-1,username,date_format(dateOfPost,'%c/%e %l:%i%p'),date_format(max(dateOfPost),'%c/%e %l:%i%p'),max(messageId) from message where widgetId=$_[0] group by rid order by messageId desc", $session{dbh});
while (@data = $sth->array) {
$row[$i] = '| '.$data[1].' | '.$data[3].' | '.$data[4].' | '.$data[2].' | '.$data[5].' |
';
$i++;
}
if ($session{form}{pn} < 1) {
$pn = 0;
} else {
$pn = $session{form}{pn};
}
for ($i=($itemsPerPage*$pn); $i<($itemsPerPage*($pn+1));$i++) {
$html .= $row[$i];
}
$html .= '
';
$html .= '';
return $html;
}
1;