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 Tie::CPHash; use WebGUI::DateTime; use WebGUI::International; use WebGUI::Macro; use WebGUI::Privilege; use WebGUI::Session; use WebGUI::SQL; use WebGUI::Utility; use WebGUI::Widget; #------------------------------------------------------------------- sub _getBoardProperties { my (%board); tie %board, 'Tie::CPHash'; %board = WebGUI::SQL->quickHash("select * from widget, MessageBoard where widget.widgetId=MessageBoard.widgetId and 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,subject,username,dateOfPost from message where pid=$_[0] order by messageId", $session{dbh}); while (@data = $sth->array) { $html .= ''.substr($data[1],0,30).''.$data[2].''.epochToHuman($data[3],"%M/%D %H:%n%p").''; $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 WebGUI::International::get(223); } #------------------------------------------------------------------- sub www_add { my ($output, %hash); tie %hash, "Tie::IxHash"; if (WebGUI::Privilege::canEditPage()) { $output = ''; $output .= '

'.WebGUI::International::get(222).'

'; $output .= '
'; $output .= WebGUI::Form::hidden("widget","MessageBoard"); $output .= WebGUI::Form::hidden("func","addSave"); $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; %hash = WebGUI::SQL->buildHash("select groupId,groupName from groups where groupName<>'Reserved' order by groupName",$session{dbh}); $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= '
'.WebGUI::International::get(99).''.WebGUI::Form::text("title",20,30,'Message Board').'
'.WebGUI::International::get(175).''.WebGUI::Form::checkbox("displayTitle",1,1).'
'.WebGUI::International::get(176).''.WebGUI::Form::checkbox("processMacros",1).'
'.WebGUI::International::get(85).''.WebGUI::Form::textArea("description",'').'
'.WebGUI::International::get(224).''.WebGUI::Form::selectList("groupToPost",\%hash,'',1).'
'.WebGUI::International::get(225).''.WebGUI::Form::text("messagesPerPage",20,2,30).'
'.WebGUI::International::get(226).''.WebGUI::Form::text("editTimeout",20,3,1).'
'.WebGUI::Form::submit(WebGUI::International::get(62)).'
'; 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 values ($widgetId, $session{form}{groupToPost}, '$session{form}{messagesPerPage}', '$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()) { tie %board, 'Tie::CPHash'; %board = _getBoardProperties($session{form}{wid}); $output = ''; $output .= '

'.WebGUI::International::get(227).'

'; $output .= '
'; $output .= WebGUI::Form::hidden("wid",$session{form}{wid}); $output .= WebGUI::Form::hidden("func","editSave"); $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; %hash = WebGUI::SQL->buildHash("select groupId,groupName from groups where groupName<>'Reserved' order by groupName",$session{dbh}); $array[0] = $board{groupToPost}; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= '
'.WebGUI::International::get(99).''.WebGUI::Form::text("title",20,30,$board{title}).'
'.WebGUI::International::get(175).''.WebGUI::Form::checkbox("displayTitle","1",$board{displayTitle}).'
'.WebGUI::International::get(176).''.WebGUI::Form::checkbox("processMacros","1",$board{processMacros}).'
'.WebGUI::International::get(85).''.WebGUI::Form::textArea("description",$board{description}).'
'.WebGUI::International::get(224).''.WebGUI::Form::selectList("groupToPost",\%hash,\@array,1).'
'.WebGUI::International::get(225).''.WebGUI::Form::text("messagesPerPage",20,2,$board{messagesPerPage}).'
'.WebGUI::International::get(226).''.WebGUI::Form::text("editTimeout",20,2,$board{editTimeout}).'
'.WebGUI::Form::submit(WebGUI::International::get(62)).'
'; 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); tie %message, 'Tie::CPHash'; tie %board, 'Tie::CPHash'; %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 .= ''.WebGUI::International::get(228).'
'; $html .= '
'; $html .= WebGUI::Form::hidden("func","editMessageSave"); $html .= WebGUI::Form::hidden("wid",$session{form}{wid}); $html .= WebGUI::Form::hidden("mid",$session{form}{mid}); $html .= ''; $html .= ''; $html .= ''; $html .= '
'.WebGUI::International::get(229).''.WebGUI::Form::text("subject",30,255,$message{subject}).'
'.WebGUI::International::get(230).''.WebGUI::Form::textArea("message",$message{message},50,6,1).'
'.WebGUI::Form::submit(WebGUI::International::get(62)).'
'; $html .= www_showMessage(); } else { $html = WebGUI::Privilege::insufficient(); } return $html; } #------------------------------------------------------------------- sub www_editMessageSave { my (%board); tie %board, 'Tie::CPHash'; %board = _getBoardProperties($session{form}{wid}); if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) { if ($session{form}{subject} eq "") { $session{form}{subject} = WebGUI::International::get(232); } if ($session{form}{message} eq "") { $session{form}{subject} .= ' '.WebGUI::International::get(233); } 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); tie %board, 'Tie::CPHash'; %board = _getBoardProperties($session{form}{wid}); if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) { $html .= '
'; if ($board{displayTitle}) { $html .= $board{title}; } $html .= ''.WebGUI::International::get(231).'
'; $html .= '
'; $html .= WebGUI::Form::hidden("func","postNewMessageSave"); $html .= WebGUI::Form::hidden("wid",$session{form}{wid}); $html .= ''; $html .= ''; $html .= ''; $html .= '
'.WebGUI::International::get(229).''.WebGUI::Form::text("subject",30,255).'
'.WebGUI::International::get(230).''.WebGUI::Form::textArea("message",'',50,6,1).'
'.WebGUI::Form::submit(WebGUI::International::get(62)).'
'; } else { $html = WebGUI::Privilege::insufficient(); } return $html; } #------------------------------------------------------------------- sub www_postNewMessageSave { my ($mid, %board); tie %board, 'Tie::CPHash'; %board = _getBoardProperties($session{form}{wid}); if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) { if ($session{form}{subject} eq "") { $session{form}{subject} = WebGUI::International::get(232); } if ($session{form}{message} eq "") { $session{form}{subject} .= ' '.WebGUI::International::get(233); } $mid = getNextId("messageId"); WebGUI::SQL->write("insert into message values ($mid, $mid, $session{form}{wid}, 0, $session{user}{userId}, ".quote($session{user}{username}).", ".quote($session{form}{subject}).", ".quote($session{form}{message}).", ".time().")",$session{dbh}); return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- sub www_postReply { my ($html, %board, $subject); tie %board, 'Tie::CPHash'; %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 .= ''.WebGUI::International::get(234).'
'; $html .= '
'; $html .= WebGUI::Form::hidden("func","postReplySave"); $html .= WebGUI::Form::hidden("wid",$session{form}{wid}); $html .= WebGUI::Form::hidden("mid",$session{form}{mid}); $html .= ''; $html .= ''; $html .= ''; $html .= '
'.WebGUI::International::get(229).''.WebGUI::Form::text("subject",30,255,$subject).'
'.WebGUI::International::get(230).''.WebGUI::Form::textArea("message",'',50,6,1).'
'.WebGUI::Form::submit(WebGUI::International::get(62)).'
'; $html .= www_showMessage(); } else { $html = WebGUI::Privilege::insufficient(); } return $html; } #------------------------------------------------------------------- sub www_postReplySave { my ($rid, %board, $mid); tie %board, 'Tie::CPHash'; %board = _getBoardProperties($session{form}{wid}); if (WebGUI::Privilege::isInGroup($board{groupToPost},$session{user}{userId})) { if ($session{form}{subject} eq "") { $session{form}{subject} = WebGUI::International::get(232); } if ($session{form}{message} eq "") { $session{form}{subject} .= ' '.WebGUI::International::get(233); } $mid = getNextId("messageId"); ($rid) = WebGUI::SQL->quickArray("select rid from message where messageId=$session{form}{mid}",$session{dbh}); WebGUI::SQL->write("insert into message values ($mid, $rid, $session{form}{wid}, $session{form}{mid}, $session{user}{userId}, ".quote($session{user}{username}).", ".quote($session{form}{subject}).", ".quote($session{form}{message}).", ".time().")", $session{dbh}); return www_showMessage(); } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- sub www_showMessage { my (@data, $html, %board, %message); tie %message, 'Tie::CPHash'; tie %board, 'Tie::CPHash'; %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 .= ''; if ((time()-$message{dateOfPost}) < 3600*$board{editTimeout} && $message{'userId'} eq $session{user}{userId}) { $html .= ''.WebGUI::International::get(235).' · '; } $html .= ''.WebGUI::International::get(236).'
'; $html .= ''; $html .= '
'; $html .= "".WebGUI::International::get(237)." ".$message{subject}."
"; $html .= "".WebGUI::International::get(238)." ".$message{username}."
"; $html .= "".WebGUI::International::get(239)." ".epochToHuman($message{dateOfPost},"%w, %c %D, %y at %H:%n%p")."
"; $html .= "".WebGUI::International::get(240)." ".$message{widgetId}."-".$message{rid}."-".$message{pid}."-".$message{messageId}."
"; $html .= '
'; $message{message} =~ s/\n/\/g; $html .= $message{message}; $html .= '

'; @data = WebGUI::SQL->quickArray("select max(messageId) from message where widgetId=$message{widgetId} and pid=0 and messageId<$message{rid}",$session{dbh}); if ($data[0] ne "") { $html .= '« '.WebGUI::International::get(241).''; } else { $html .= '« '.WebGUI::International::get(241).''; } $html .= ' · '.WebGUI::International::get(242).' · '; @data = WebGUI::SQL->quickArray("select min(messageId) from message where widgetId=$message{widgetId} and pid=0 and messageId>$message{rid}",$session{dbh}); if ($data[0] ne "") { $html .= ''.WebGUI::International::get(243).' »'; } else { $html .= WebGUI::International::get(243).' »'; } $html .= '
'; $html .= ''; @data = WebGUI::SQL->quickArray("select messageId,subject,username,dateOfPost from message where messageId=$message{rid}",$session{dbh}); $html .= ''.substr($data[1],0,30).''; $html .= _traverseReplyTree($message{rid},1); $html .= "
'.WebGUI::International::get(229).''.WebGUI::International::get(244).''.WebGUI::International::get(245).'
'.$data[2].''.epochToHuman($data[3],"%M/%D %H:%n%p").'
"; return $html; } #------------------------------------------------------------------- sub www_view { my ($sth, @data, $html, %board, $itemsPerPage, $i, $pn, $lastId, @last, $replies); tie %board, 'Tie::CPHash'; %board = _getBoardProperties($_[0]); $itemsPerPage = $board{messagesPerPage}; if ($session{form}{pn} < 1) { $pn = 0; } else { $pn = $session{form}{pn}; } if ($board{description} ne "") { $html = $board{description}.'

'; } if ($board{processMacros}) { $html = WebGUI::Macro::process($html); } $html .= '
'; if ($board{displayTitle}) { $html .= $board{title}; } $html .= ''.WebGUI::International::get(246).'
'; $html .= ''; $html .= ''; #$sth = WebGUI::SQL->read("select messageId,subject,count(*)-1,username,dateOfPost,max(dateOfPost),max(messageId) from message where widgetId=$_[0] group by rid order by messageId desc", $session{dbh}); $sth = WebGUI::SQL->read("select messageId,subject,username,dateOfPost from message where widgetId=$_[0] and pid=0 order by messageId desc", $session{dbh}); while (@data = $sth->array) { if ($i >= ($itemsPerPage*$pn) && $i < ($itemsPerPage*($pn+1))) { @last = WebGUI::SQL->quickArray("select messageId,dateOfPost,username,subject from message where widgetId=$_[0] and rid=$data[0] order by dateOfPost desc",$session{dbh}); ($replies) = WebGUI::SQL->quickArray("select count(*)-1 from message where rid=$data[0]",$session{dbh}); $html .= ''; } $i++; } $html .= '
'.WebGUI::International::get(229).''.WebGUI::International::get(244).''.WebGUI::International::get(247).''.WebGUI::International::get(248).''.WebGUI::International::get(249).'
'.substr($data[1],0,30).''.$data[2].''.epochToHuman($data[3],"%M/%D %H:%n%p").''.$replies.''.substr($last[3],0,30).' @ '.epochToHuman($last[1],"%M/%D %H:%n%p").' by '.$last[2].'
'; $html .= '

'; return $html; } 1;