package WebGUI::Wobject; =head1 LEGAL ------------------------------------------------------------------- WebGUI is Copyright 2001-2002 Plain Black LLC. ------------------------------------------------------------------- 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 ------------------------------------------------------------------- =cut use DBI; use strict qw(subs vars); use Tie::IxHash; use WebGUI::DateTime; use WebGUI::HTML; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; use WebGUI::Macro; use WebGUI::Node; use WebGUI::Session; use WebGUI::SQL; use WebGUI::Template; use WebGUI::URL; use WebGUI::Utility; =head1 NAME Package WebGUI::Wobject =head1 SYNOPSIS use WebGUI::Wobject; our @ISA = qw(WebGUI::Wobject); See the subclasses in lib/WebGUI/Wobjects for details. =head1 DESCRIPTION An abstract class for all other wobjects to extend. =head1 METHODS These methods are available from this class: =cut #------------------------------------------------------------------- sub _reorderWobjects { my ($sth, $i, $wid); $sth = WebGUI::SQL->read("select wobjectId from wobject where pageId=$_[0] order by templatePosition,sequenceNumber"); while (($wid) = $sth->array) { $i++; WebGUI::SQL->write("update wobject set sequenceNumber='$i' where wobjectId=$wid"); } $sth->finish; } #------------------------------------------------------------------- sub _getNextSequenceNumber { my ($sequenceNumber); ($sequenceNumber) = WebGUI::SQL->quickArray("select max(sequenceNumber) from wobject where pageId='$_[0]'"); return ($sequenceNumber+1); } #------------------------------------------------------------------- =head2 confirm ( message, yesURL, [ , noURL, vitalComparison ] ) =item message A string containing the message to prompt the user for this action. =item yesURL A URL to the web method to execute if the user confirms the action. =item noURL A URL to the web method to execute if the user denies the action. Defaults back to the current page. =item vitalComparison A comparison expression to be used when checking whether the action should be allowed to continue. Typically this is used when the action is a delete of some sort. =cut sub confirm { my ($output, $noURL); if ($_[4]) { return WebGUI::Privilege::vitalComponent(); } elsif (WebGUI::Privilege::canEditPage()) { $noURL = $_[3] || WebGUI::URL::page(); $output = '
'; $output .= '
'; return $output; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 deleteCollateral ( tableName, keyName, keyValue ) Deletes a row of collateral data. =item tableName The name of the table you wish to delete the data from. =item keyName The name of the column that is the primary key in the table. =item keyValue An integer containing the key value. =cut sub deleteCollateral { WebGUI::SQL->write("delete from $_[1] where $_[2]=".quote($_[3])); WebGUI::ErrorHandler::audit("deleted ".$_[2]." ".$_[3]); } #------------------------------------------------------------------- =head2 description ( ) Returns this instance's description if it exists. =cut sub description { if ($_[0]->get("description")) { return $_[0]->get("description").''; } } #------------------------------------------------------------------- =head2 discussionProperties ( ) Returns a formRow list of discussion properties, which may be attached to any Wobject. =cut sub discussionProperties { my ($f,$editTimeout,$interval, $units, $groupToModerate,%moderationType,$moderationType); %moderationType = (before=>WebGUI::International::get(567),after=>WebGUI::International::get(568)); $f = WebGUI::HTMLForm->new; if ($_[0]->get("wobjectId") eq "new") { $editTimeout = 3600; $moderationType = 'after'; } else { $editTimeout = $_[0]->get("editTimeout"); $moderationType = $_[0]->get("moderationType"); } $groupToModerate = $_[0]->get("groupToModerate") || 4; $f->group( -name=>"groupToPost", -label=>WebGUI::International::get(564), -value=>[$_[0]->get("groupToPost")], -uiLevel=>7 ); ($interval, $units) = WebGUI::DateTime::secondsToInterval($editTimeout); $f->interval( -name=>"editTimeout", -label=>WebGUI::International::get(566), -intervalValue=>$interval, -unitsValue=>$units, -uiLevel=>7 ); if ($session{setting}{useKarma} && $session{user}{uiLevel} <= 7) { $f->integer("karmaPerPost",WebGUI::International::get(541),$_[0]->get("karmaPerPost")); } else { $f->hidden("karmaPerPost",$_[0]->get("karmaPerPost")); } $f->group( -name=>"groupToModerate", -label=>WebGUI::International::get(565), -value=>[$groupToModerate], -uiLevel=>7 ); $f->select( -name=>"moderationType", -options=>\%moderationType, -label=>WebGUI::International::get(569), -value=>[$moderationType], -uiLevel=>7 ); return $f->printRowsOnly; } #------------------------------------------------------------------- =head2 displayTitle ( ) Returns this instance's title if displayTitle is set to yes. =cut sub displayTitle { if ($_[0]->get("displayTitle")) { return "
'; $output .= '
'; return $output; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_deleteConfirm ( ) Moves this instance to the trash. =cut sub www_deleteConfirm { if (WebGUI::Privilege::canEditPage()) { $_[0]->set({pageId=>3, templatePosition=>1}); WebGUI::ErrorHandler::audit("moved Wobject ".$_[0]->{_property}{wobjectId}." to the trash."); _reorderWobjects($_[0]->get("pageId")); return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_deleteFile ( ) Displays a confirmation message relating to the deletion of a file. =cut sub www_deleteFile { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); return $_[0]->confirm(WebGUI::International::get(728), WebGUI::URL::page('func=deleteFileConfirm&wid='.$_[0]->get("wobjectId").'&file='.$session{form}{file}), WebGUI::URL::page('func=edit&wid='.$_[0]->get("wobjectId")) ); } #------------------------------------------------------------------- =head2 www_deleteFileConfirm ( ) Deletes a file from this instance. =cut sub www_deleteFileConfirm { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); $_[0]->set({$session{form}{file}=>''}); return $_[0]->www_edit(); } #------------------------------------------------------------------- =head2 www_deleteMessage ( ) Displays a message asking for confirmation to delete a message from a discussion. =cut sub www_deleteMessage { if (WebGUI::Discussion::canEditMessage($_[0],$session{form}{mid})) { return WebGUI::Discussion::deleteMessage(); } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_deleteMessageConfirm ( ) Deletes a message from a discussion. =cut sub www_deleteMessageConfirm { if (WebGUI::Discussion::canEditMessage($_[0],$session{form}{mid})) { return WebGUI::Discussion::deleteMessageConfirm(); } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_denyPost ( ) Sets the status flag on a discussion message to "denied". =cut sub www_denyPost { if (WebGUI::Privilege::isInGroup($_[0]->get("groupToModerate"))) { return WebGUI::Discussion::denyPost(); } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_edit ( formRows ) Displays the common properties of any/all wobjects. NOTE: This method should be extended by all wobjects. =item formRows The custom form rows from the wobject subclass edit page. =cut sub www_edit { my ($f, $startDate, $displayTitle, $title, $templatePosition, $endDate); if ($_[0]->get("wobjectId") eq "new") { $displayTitle = 1; } else { $displayTitle = $_[0]->get("displayTitle"); } $title = $_[0]->get("title") || $_[0]->get("namespace"); $templatePosition = $_[0]->get("templatePosition") || 1; $startDate = $_[0]->get("startDate") || $session{page}{startDate}; $endDate = $_[0]->get("endDate") || $session{page}{endDate}; $f = WebGUI::HTMLForm->new; $f->hidden("wid",$_[0]->get("wobjectId")); $f->hidden("namespace",$_[0]->get("namespace")) if ($_[0]->get("wobjectId") eq "new"); $f->hidden("func","editSave"); $f->submit if ($_[0]->get("wobjectId") ne "new"); $f->readOnly( -value=>$_[0]->get("wobjectId"), -label=>WebGUI::International::get(499), -uiLevel=>3 ); $f->text("title",WebGUI::International::get(99),$title); $f->yesNo( -name=>"displayTitle", -label=>WebGUI::International::get(174), -value=>$displayTitle, -uiLevel=>5 ); $f->yesNo( -name=>"processMacros", -label=>WebGUI::International::get(175), -value=>$_[0]->get("processMacros"), -uiLevel=>5 ); $f->select( -name=>"templatePosition", -label=>WebGUI::International::get(363), -value=>[$templatePosition], -uiLevel=>5, -options=>WebGUI::Template::getPositions($session{page}{templateId}), -subtext=>WebGUI::Template::draw($session{page}{templateId}) ); $f->date( -name=>"startDate", -label=>WebGUI::International::get(497), -value=>$startDate, -uiLevel=>9 ); $f->date( -name=>"endDate", -label=>WebGUI::International::get(498), -value=>$endDate, -uiLevel=>9 ); $f->HTMLArea("description",WebGUI::International::get(85),$_[0]->get("description")); $f->raw($_[1]); $f->submit; return $f->print; } #------------------------------------------------------------------- =head2 www_editSave ( ) Saves the default properties of any/all wobjects. NOTE: This method should be extended by all subclasses. =cut sub www_editSave { my ($title, $templatePosition, $startDate, $endDate); $title = $session{form}{title} || $_[0]->get("namespace"); $templatePosition = $session{form}{templatePosition} || 1; $startDate = setToEpoch($session{form}{startDate}) || $session{page}{startDate}; $endDate = setToEpoch($session{form}{endDate}) || $session{page}{endDate}; $session{form}{description} = WebGUI::HTML::cleanSegment($session{form}{description}); $session{form}{karmaPerPost} ||= 0; $session{form}{groupToPost} ||= 2; $session{form}{editTimeout} = WebGUI::DateTime::intervalToSeconds($session{form}{editTimeout_interval},$session{form}{editTimeout_units}) || 0; $session{form}{groupToModerate} ||= 3; $session{form}{moderationType} ||= "after"; $_[0]->set({ title=>$title, displayTitle=>$session{form}{displayTitle}, processMacros=>$session{form}{processMacros}, templatePosition=>$templatePosition, startDate=>$startDate, endDate=>$endDate, description=>$session{form}{description}, karmaPerPost=>$session{form}{karmaPerPost}, groupToPost=>$session{form}{groupToPost}, groupToModerate=>$session{form}{groupToModerate}, editTimeout=>$session{form}{editTimeout}, moderationType=>$session{form}{moderationType}, %{$_[1]} }); return ""; } #------------------------------------------------------------------- =head2 www_lockThread ( ) Locks a discussion thread from the current message down. =cut sub www_lockThread { if (WebGUI::Privilege::isInGroup($_[0]->get("groupToModerate"))) { WebGUI::Discussion::lockThread(); return $_[0]->www_showMessage; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_moveBottom ( ) Moves this instance to the bottom of the page. =cut sub www_moveBottom { if (WebGUI::Privilege::canEditPage()) { $_[0]->set({sequenceNumber=>99999}); _reorderWobjects($_[0]->get("pageId")); return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_moveDown ( ) Moves this instance down one spot on the page. =cut sub www_moveDown { my ($wid, $thisSeq); if (WebGUI::Privilege::canEditPage()) { ($thisSeq) = WebGUI::SQL->quickArray("select sequenceNumber from wobject where wobjectId=".$_[0]->get("wobjectId")); ($wid) = WebGUI::SQL->quickArray("select wobjectId from wobject where pageId=".$_[0]->get("pageId") ." and sequenceNumber=".($thisSeq+1)); if ($wid ne "") { WebGUI::SQL->write("update wobject set sequenceNumber=sequenceNumber+1 where wobjectId=".$_[0]->get("wobjectId")); WebGUI::SQL->write("update wobject set sequenceNumber=sequenceNumber-1 where wobjectId=$wid"); _reorderWobjects($_[0]->get("pageId")); } return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_moveTop ( ) Moves this instance to the top of the page. =cut sub www_moveTop { if (WebGUI::Privilege::canEditPage()) { $_[0]->set({sequenceNumber=>0}); _reorderWobjects($_[0]->get("pageId")); return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_moveUp ( ) Moves this instance up one spot on the page. =cut sub www_moveUp { my ($wid, $thisSeq); if (WebGUI::Privilege::canEditPage()) { ($thisSeq) = WebGUI::SQL->quickArray("select sequenceNumber from wobject where wobjectId=".$_[0]->get("wobjectId")); ($wid) = WebGUI::SQL->quickArray("select wobjectId from wobject where pageId=".$_[0]->get("pageId") ." and sequenceNumber=".($thisSeq-1)); if ($wid ne "") { WebGUI::SQL->write("update wobject set sequenceNumber=sequenceNumber-1 where wobjectId=".$_[0]->get("wobjectId")); WebGUI::SQL->write("update wobject set sequenceNumber=sequenceNumber+1 where wobjectId=$wid"); _reorderWobjects($_[0]->get("pageId")); } return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_paste ( ) Moves this instance from the clipboard to the current page. =cut sub www_paste { my ($output, $nextSeq); if (WebGUI::Privilege::canEditPage()) { ($nextSeq) = WebGUI::SQL->quickArray("select max(sequenceNumber) from wobject where pageId=$session{page}{pageId}"); $nextSeq += 1; $_[0]->set({sequenceNumber=>$nextSeq, pageId=>$session{page}{pageId}, templatePosition=>1}); return ""; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_post ( ) Displays a discussion message post form. =cut sub www_post { if (WebGUI::Privilege::isInGroup($_[0]->get("groupToPost"))) { return WebGUI::Discussion::post(); } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_post ( ) Saves a message post to a discussion. =cut sub www_postSave { if (WebGUI::Privilege::isInGroup($_[0]->get("groupToPost"))) { WebGUI::Discussion::postSave($_[0]); return $_[0]->www_showMessage(); } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_search ( ) Searches an attached discussion. =cut sub www_search { return WebGUI::Discussion::search(); } #------------------------------------------------------------------- =head2 www_showMessage ( [menuItem] ) Shows a message from a discussion. =item menuItem You can optionally extend this method by passing in an HTML string of menu items to be added to the menu of this display. =cut sub www_showMessage { my ($output, $defaultMid); ($defaultMid) = WebGUI::SQL->quickArray("select min(messageId) from discussion where wobjectId=".$_[0]->get("wobjectId")); $session{form}{mid} = $session{form}{mid} || $defaultMid || 0; $output = WebGUI::Discussion::showMessage($_[1],$_[0]); $output .= WebGUI::Discussion::showThreads(); return $output; } #------------------------------------------------------------------- =head2 www_unlockThread ( ) Unlocks a discussion thread from the current message on down. =cut sub www_unlockThread { if (WebGUI::Privilege::isInGroup($_[0]->get("groupToModerate"))) { WebGUI::Discussion::unlockThread(); return $_[0]->www_showMessage; } else { return WebGUI::Privilege::insufficient(); } } #------------------------------------------------------------------- =head2 www_view ( ) The default display mechanism for any wobject. This web method MUST be overridden. =cut sub www_view { my ($output); $output = $_[0]->displayTitle; $output .= $_[0]->description; $output = $_[0]->processMacros($output); return $output; } 1;