package WebGUI::Discussion; #------------------------------------------------------------------- # WebGUI is Copyright 2001-2002 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::Session; use WebGUI::Shortcut; use WebGUI::SQL; use WebGUI::URL; #------------------------------------------------------------------- sub _deleteReplyTree { my ($sth, %data, $messageId); $sth = WebGUI::SQL->read("select messageId from discussion where pid=$_[0] order by messageId"); while (%data = $sth->hash) { _deleteReplyTree($data{messageId}); WebGUI::SQL->write("delete from discussion where messageId=$data{messageId}"); } $sth->finish; } #------------------------------------------------------------------- sub _duplicateReplyTree { my ($sth, %data, $newMessageId); $sth = WebGUI::SQL->read("select * from discussion where pid=$_[0] order by messageId"); while (%data = $sth->hash) { $newMessageId = getNextId("messageId"); WebGUI::SQL->write("insert into discussion values ($newMessageId, $_[2], $_[3], $_[1], $data{userId}, " .quote($data{username}).", ".quote($data{subject}).", ".quote($data{message}). ", $data{dateOfPost}, $_[4])"); _duplicateReplyTree($data{messageId},$newMessageId,$_[2],$_[3],$_[4]); } $sth->finish; } #------------------------------------------------------------------- sub duplicate { my ($sth, %data, $newMessageId, $oldSubId, $newSubId); $oldSubId = $_[2] || 0; $newSubId = $_[3] || 0; $sth = WebGUI::SQL->read("select * from discussion where widgetId=$_[0] and pid=0 and subId=$oldSubId order by messageId"); while (%data = $sth->hash) { $newMessageId = getNextId("messageId"); WebGUI::SQL->write("insert into discussion values ($newMessageId, $newMessageId, $_[1], 0, $data{userId}, ".quote($data{username}).", ".quote($data{subject}).", ".quote($data{message}).", $data{dateOfPost}, $newSubId)"); _duplicateReplyTree($data{messageId},$newMessageId,$newMessageId,$_[1],$newSubId); } $sth->finish; } #------------------------------------------------------------------- sub deleteMessage { my ($output); $output = '

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

'; $output .= WebGUI::International::get(401); $output .= '

'; $output .= '

'; $output .= WebGUI::International::get(44); $output .= ''; $output .= '    '; $output .= WebGUI::International::get(45); $output .= '
'; return $output; } #------------------------------------------------------------------- sub deleteMessageConfirm { _deleteReplyTree($session{form}{mid}); WebGUI::SQL->write("delete from discussion where messageId=$session{form}{mid}"); return ""; } #------------------------------------------------------------------- sub editMessage { my ($html, %message); tie %message, 'Tie::CPHash'; %message = getMessage($session{form}{mid}); $html = '

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

'; $html .= formHeader().''; $html .= WebGUI::Form::hidden("func","editMessageSave"); $html .= WebGUI::Form::hidden("wid",$session{form}{wid}); $html .= WebGUI::Form::hidden("sid",$session{form}{sid}); $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 .= showMessage(); return $html; } #------------------------------------------------------------------- sub editMessageSave { 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 discussion set subject=".quote($session{form}{subject}). ", message=".quote("\n --- (Edited at ".localtime(time). " by $session{user}{username}) --- \n\n".$session{form}{message}). ", subId='$session{form}{sid}' where messageId=$session{form}{mid}"); return showMessage(); } #------------------------------------------------------------------- sub getMessage { my (%message); tie %message, 'Tie::CPHash'; %message = WebGUI::SQL->quickHash("select * from discussion where messageId='$_[0]'"); unless ($message{message} =~ /\/ig || $message{message} =~ /\/ig || $message{message} =~ /\/ig) { $message{message} =~ s/\n/\/g; } return %message; } #------------------------------------------------------------------- sub postNewMessage { my ($html); $html = '

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

'; $html .= formHeader().''; $html .= WebGUI::Form::hidden("func","postNewMessageSave"); $html .= WebGUI::Form::hidden("wid",$session{form}{wid}); $html .= WebGUI::Form::hidden("sid",$session{form}{sid}); $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)).'
'; return $html; } #------------------------------------------------------------------- sub postNewMessageSave { my ($mid); 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 discussion values ($mid, $mid, $session{form}{wid}, 0, $session{user}{userId}, ".quote($session{user}{username}).", ".quote($session{form}{subject}).", ".quote($session{form}{message}).", ".time().", '$session{form}{sid}')"); return ""; } #------------------------------------------------------------------- sub postReply { my ($html, $subject); ($subject) = WebGUI::SQL->quickArray("select subject from discussion where messageId=$session{form}{mid}"); $subject = "Re: ".$subject; $html = '

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

'; $html .= formHeader().''; $html .= WebGUI::Form::hidden("func","postReplySave"); $html .= WebGUI::Form::hidden("wid",$session{form}{wid}); $html .= WebGUI::Form::hidden("sid",$session{form}{sid}); $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 .= showMessage(); return $html; } #------------------------------------------------------------------- sub postReplySave { my ($rid, $mid); 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 discussion where messageId=$session{form}{mid}"); WebGUI::SQL->write("insert into discussion 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{form}{sid}')"); return ""; } #------------------------------------------------------------------- sub purgeWidget { WebGUI::SQL->write("delete from discussion where widgetId=$_[0]",$_[1]); } #------------------------------------------------------------------- sub showMessage { my ($html, %message); tie %message, 'Tie::CPHash'; %message = getMessage($session{form}{mid}); if ($message{messageId}) { $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 .= '
'; $html .= $message{message}; $html .= '
'; } else { $html = WebGUI::International::get(402); } return $html; } #------------------------------------------------------------------- sub showReplyTree { my (@data, $html, %message); tie %message, 'Tie::CPHash'; %message = getMessage($session{form}{mid}); if ($message{messageId}) { $html .= ''; $html .= ''; $html .= traverseReplyTree($message{rid},0); $html .= "
'.WebGUI::International::get(229). ''.WebGUI::International::get(244). ''.WebGUI::International::get(245).'
"; } return $html; } #------------------------------------------------------------------- sub traverseReplyTree { my ($sth, @data, $html, $depth, $i); for ($i=0;$i<=$_[1];$i++) { $depth .= "  "; } $sth = WebGUI::SQL->read("select messageId,subject,username,dateOfPost,userId from discussion where pid=$_[0] order by messageId"); 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; } 1;