boards
[ class tree: boards ] [ index: boards ] [ all elements ]

Source for file BitBoardPost.php

Documentation is available at BitBoardPost.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  * $Id$
  5.  *
  6.  * Messageboards class to illustrate best practices when creating a new bitweaver package that
  7.  * builds on core bitweaver functionality, such as the Liberty CMS engine
  8.  *
  9.  * @author spider <spider@steelsun.com>
  10.  * @version $Revision$
  11.  * @package boards
  12.  */
  13.  
  14. /**
  15.  * required setup
  16.  */
  17. require_onceLIBERTY_PKG_PATH.'LibertyMime.php' );
  18. require_onceBOARDS_PKG_PATH.'BitBoardTopic.php' );
  19.  
  20. /**
  21.  * Class that handles posts to a board
  22.  *
  23.  * @package boards
  24.  */
  25. class BitBoardPost extends LibertyComment {
  26.     /**
  27.      * During initialisation, be sure to call our base constructors
  28.      */
  29.     function BitBoardPost($pCommentId NULL$pContentId NULL$pInfo NULL{
  30.         parent::__construct($pCommentId,$pContentId,$pInfo);
  31.  
  32.         // Permission setup
  33.         $this->mViewContentPerm  = 'p_boards_read';
  34.         $this->mUpdateContentPerm  = 'p_boards_post_update';
  35.         $this->mAdminContentPerm = 'p_boards_admin';
  36.     }
  37.  
  38.     /**
  39.      * This function verifies the data for a post
  40.      */
  41.     function verify&$pParamHash {
  42.         ifisset$pParamHash['is_approved') ) {
  43.             if!is_numeric$pParamHash['is_approved'|| $pParamHash['is_approved'|| $pParamHash['is_approved'{
  44.                 $this->mErrors[]=("Invalid post approved state");
  45.             else {
  46.                 $pParamHash['post_store']['is_approved'$pParamHash['is_approved'];
  47.             }
  48.         }
  49.         ifisset$pParamHash['is_warned') ) {
  50.             if!is_numeric$pParamHash['warned'|| $pParamHash['is_warned'|| $pParamHash['is_warned'{
  51.                 $this->mErrors[]=("Invalid warned state");
  52.             else {
  53.                 $pParamHash['post_store']['is_warned'$pParamHash['is_warned'];
  54.             }
  55.         }
  56.         if!empty$pParamHash['warned_message') ) {
  57.             $pParamHash['post_store']['warned_message'$pParamHash['warned_message'];
  58.         }
  59.         if!empty$pParamHash['warned_message') ) {
  60.             $pParamHash['post_store']['warned_message'$pParamHash['warned_message'];
  61.         }
  62.         if!empty$pParamHash['migrate_post_id') ) {
  63.             $pParamHash['post_store']['migrate_post_id'$pParamHash['migrate_post_id'];
  64.         }
  65.  
  66.         returncount$this->mErrors == && !empty$pParamHash['post_store') );
  67.     }
  68.  
  69.     /**
  70.      * This function stores a post
  71.      */
  72.     function store&$pParamHash {
  73.         global $gBitSystem;
  74.         $ret FALSE;
  75.         if$this->mCommentId && $this->verify$pParamHash ) ) {
  76.             //$gBitSystem->verifyPermission('p_boards_update');
  77.             //$pParamHash = (($pParamHash + 1)%2);
  78.             $query_sel "SELECT * FROM `".BIT_DB_PREFIX."boards_posts` WHERE `comment_id` = ?";
  79.             $isStored $this->mDb->getOne$query_selarray$this->mCommentId ) );
  80.             if$isStored {
  81.                 $result $this->mDb->associateUpdate'boards_posts'$pParamHash['post_store']array'comment_id' => $this->mCommentId ) );
  82.             else {
  83.                 $pParamHash['post_store']['comment_id'$this->mCommentId;
  84.                 $result $this->mDb->associateInsert'boards_posts'$pParamHash['post_store');
  85.             }
  86.             $ret TRUE;
  87.         }
  88.         return $ret;
  89.     }
  90.  
  91.     /**
  92.      * This function gets the meta data relating to a post
  93.      */
  94.     function loadMetaData({
  95.         if ($this->isValid()) {
  96.             if (!isset($this->mInfo['accepted'])) {
  97.                 $key array('comment_id' => $this->mCommentId);
  98.                 $query_sel "SELECT
  99.                 post.is_approved,
  100.                 post.is_warned,
  101.                 post.warned_message
  102.                 FROM `".BIT_DB_PREFIX."boards_posts` post WHERE comment_id=?";
  103.                 $data $this->mDb->getRow$query_sel array_values($key));
  104.                 if ($data{
  105.                     if (!empty($data['warned_message'])) {
  106.                         $data['warned_message'str_replace("\n","<br />\n",$data['warned_message']);
  107.                     }
  108.                     $this->mInfo=array_merge($this->mInfo,$data);
  109.                 }
  110.             }
  111.         }
  112.     }
  113.  
  114.     /**
  115.     * This function removes a bitboard entry
  116.     **/
  117.  
  118.     function expunge({
  119.         $ret FALSE;
  120.         if$this->isValid() ) {
  121.             $this->mDb->StartTrans();
  122.             // parent actually has deletion of rows in boards for constraint reasons
  123.             ifparent::expunge() ) {
  124.                 $this->mDb->CompleteTrans();
  125.                 $ret TRUE;
  126.             else {
  127.                 $this->mDb->RollbackTrans();
  128.             }
  129.         }
  130.         return $ret;
  131.     }
  132.  
  133.     /**
  134.      * This function gets all the post relating to a topic
  135.      */
  136.     function getComments$pContentId NULL$pMaxComments NULL$pOffset NULL$pSortOrder NULL$pDisplayMode NULL {
  137.         global $gBitUser$gBitSystem;
  138.  
  139.         $joinSql $selectSql $whereSql '';
  140.  
  141.         $ret array();
  142.         $contentId $this->mCommentId;
  143.  
  144.         $mid 'thread_forward_sequence  ASC';
  145.         if (!empty($pSortOrder)) {
  146.             if ($pSortOrder == 'commentDate_desc'{
  147.                 $mid 'created DESC';
  148.             else if ($pSortOrder == 'commentDate_asc'{
  149.                 $mid 'created ASC';
  150.             elseif ($pSortOrder == 'thread_asc'{
  151.                 $mid 'thread_forward_sequence  ASC';
  152.             // thread newest first is harder...
  153.             elseif ($pSortOrder == 'thread_desc'{
  154.                 $mid 'thread_reverse_sequence  ASC';
  155.             else {
  156.                 $mid $this->mDb->convertSortmode$pSortOrder );
  157.             }
  158.         }
  159.         $mid 'order by ' $mid;
  160.  
  161.         $bindVars array();
  162.         if (is_array$contentId ) ) {
  163.             $mid2 'in ('.implode(','array_fill(0count$pContentId )'?')).')';
  164.             $bindVars $contentId;
  165.             $select1 ', lcp.content_type_guid as parent_content_type_guid, lcp.title as parent_title ';
  166.             $join1 " LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lcp ON (lcp.content_id = lcom.parent_id) ";
  167.         elseif ($contentId{
  168.             $mid2 "lcom.`thread_forward_sequence` LIKE '".sprintf("%09d.",$contentId)."%'";
  169.             $select1 '';
  170.             $join1 '';
  171.         }
  172.  
  173.         if ($gBitSystem->isFeatureActive('boards_posts_anon_moderation'&& !($gBitUser->hasPermission('p_boards_update'|| $gBitUser->hasPermission('p_boards_post_update'))) {
  174.             $whereSql .= " AND ((post.`is_approved` = 1) OR (lc.`user_id` >= 0))";
  175.         }
  176.  
  177.         $pListHash array'content_id' => $contentId'max_records' => $pMaxComments'offset'=>$pOffset'sort_mode'=> $pSortOrder'display_mode' => $pDisplayMode'has_comment_view_perm' => TRUE );
  178.         $this->getServicesSql'content_list_sql_function'$selectSql$joinSql$whereSql$bindVars$this$pListHash );
  179.  
  180.         if ($pContentId{
  181.             $sql "SELECT lcom.`comment_id`, lcom.`parent_id`, lcom.`root_id`,
  182.             lcom.`thread_forward_sequence`, lcom.`thread_reverse_sequence`, lcom.`anon_name`, lc.*, uu.`email`, uu.`real_name`, uu.`login`,
  183.                 post.is_approved,
  184.                 post.is_warned,
  185.                 post.warned_message,
  186.                 uu.registration_date AS registration_date,
  187.                 tf_ava.`file_name` AS `avatar_file_name`, tf_ava.`mime_type` AS `avatar_mime_type`, tf_ava.`user_id` AS `avatar_user_id`, ta_ava.`attachment_id` AS `avatar_attachment_id`
  188.                 $selectSql $select1
  189.                     FROM `".BIT_DB_PREFIX."liberty_comments` lcom
  190.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
  191.                         INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (lc.`user_id` = uu.`user_id`)
  192.                          $joinSql $join1
  193.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` ta_ava ON ( uu.`avatar_attachment_id`=ta_ava.`attachment_id` )
  194.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` tf_ava ON ( tf_ava.`file_id`=ta_ava.`foreign_id` )
  195.                         LEFT JOIN `".BIT_DB_PREFIX."boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
  196.                     WHERE $mid2 $whereSql $mid";
  197.  
  198.             $flat_comments array();
  199.  
  200.             if$result $this->mDb->query$sql$bindVars$pMaxComments$pOffset ) ) {
  201.                 while$row $result->FetchRow() ) {
  202.                     if (empty($row['anon_name'])) {
  203.                         $row['anon_name'"Anonymous";
  204.                     }
  205.                     if$row['avatar_file_name'{
  206.                         $row['user_avatar_url'liberty_fetch_thumbnail_urlarray(
  207.                             'source_file' => liberty_mime_get_source_filearray'user_id'=>$row['avatar_user_id']'file_name'=>$row['avatar_file_name']'mime_type'=>$row['avatar_mime_type']'attachment_id'=>$row['avatar_attachment_id') ),
  208.                             'size' => 'avatar'
  209.                         ));
  210.                     else {
  211.                         $row['user_avatar_url'FALSE;
  212.                     }
  213.                     if (!empty($row['warned_message'])) {
  214.                         $row['warned_message'str_replace("\n","<br />\n",$row['warned_message']);
  215.                     }
  216.                     $row['data'trim$row['data');
  217.                     $row['user_url'BitUser::getDisplayUrlFromHash$row );
  218.                     $row['parsed_data'$this->parseData$row );
  219.                     $row['level'substr_count $row['thread_forward_sequence']'.' 1;
  220.                     $c new LibertyComment();
  221.                     $c->mInfo=$row;
  222.                     $row['is_editable'$c->userCanEdit();
  223.  
  224.                     if$gBitSystem->isFeatureActive'comments_allow_attachments' ) ){
  225.                         // get attachments for each comment
  226.                         global $gLibertySystem;
  227.                         $query "SELECT * FROM `".BIT_DB_PREFIX."liberty_attachments` la WHERE la.`content_id`=? ORDER BY la.`pos` ASC, la.`attachment_id` ASC";
  228.                         if$result2 $this->mDb->query$query,array(int)$row['content_id'))) {
  229.                             while$row2 $result2->fetchRow() ) {
  230.                                 if$func $gLibertySystem->getPluginFunction$row2['attachment_plugin_guid']'load_function''mime' )) {
  231.                                     // we will pass the preferences by reference that the plugin can easily update them
  232.                                     ifempty$row['storage'][$row2['attachment_id']] )) {
  233.                                         $row['storage'][$row2['attachment_id']] array();
  234.                                     }
  235.                                     $row['storage'][$row2['attachment_id']] $func$row2$row['storage'][$row2['attachment_id']] );
  236.                                 else {
  237.                                     print "No load_function for ".$row2['attachment_plugin_guid'];
  238.                                 }
  239.                             }
  240.                         }
  241.                         // end get attachements for each comment
  242.                     }
  243.  
  244.                     $flat_comments[$row['content_id']] $row;
  245.                     // vd($row);
  246.                 }
  247.             }
  248.  
  249.             # now select comments wanted
  250.             $ret $flat_comments;
  251.  
  252.         }
  253.         return $ret;
  254.     }
  255.  
  256.     /**
  257.      * This function gets a list of posts
  258.      */
  259.     function getList&$pListHash {
  260.         global $gBitUser$gBitSystem;
  261.  
  262.         $this->prepGetList$pListHash );
  263.  
  264.         $joinSql $selectSql $whereSql '';
  265.  
  266.         $ret array();
  267.         $contentId $this->mCommentId;
  268.  
  269. //        $mid = 'ORDER BY `thread_forward_sequence` ASC';
  270.  
  271.         $bindVars array();
  272.         if!empty$pListHash['content_id') ) {
  273.             if (is_array$contentId ) ) {
  274.                 $mid2 'in ('.implode(','array_fill(0count$pListHash['content_id')'?')).')';
  275.                 $bindVars $contentId;
  276.                 $selectSql ', lcp.content_type_guid as parent_content_type_guid, lcp.title as parent_title ';
  277.                 $joinSql .= " LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lcp ON (lcp.content_id = lcom.parent_id) ";
  278.             elseifis_numeric$contentId ) ) {
  279.                 $whereSql .= " AND `thread_forward_sequence` LIKE '".sprintf("%09d.",$contentId)."%'";
  280.             }
  281.         }
  282.  
  283.         if ($gBitSystem->isFeatureActive('boards_posts_anon_moderation'&& !($gBitUser->hasPermission('p_boards_update'|| $gBitUser->hasPermission('p_boards_post_update'))) {
  284.             $whereSql .= " AND ((post.`is_approved` = 1) OR (lc.`user_id` >= 0))";
  285.         }
  286.  
  287.         $this->getServicesSql'content_list_sql_function'$selectSql$joinSql$whereSql$bindVars$this );
  288.  
  289.         if!empty$pListHash['board_id') ) {
  290.             $joinSql .= "INNER JOIN `".BIT_DB_PREFIX."boards` b ON (b.`content_id` = bm.`board_content_id`)";
  291.             $whereSql .= ' AND b.`board_id`=? ';
  292.             array_push$bindVars(int)$pListHash['board_id');
  293.         }
  294.  
  295.         ifBitBase::verifyId$pListHash['user_id') ) {
  296.             $whereSql .= ' AND lc.`user_id`=? ';
  297.             array_push$bindVars$pListHash['user_id');
  298.         }
  299.  
  300.         if!empty$whereSql ) ) {
  301.             $whereSql preg_replace'/^[\s]*AND\b/i''WHERE '$whereSql );
  302.         }
  303.  
  304.         $sql "SELECT lcom.`comment_id`, lcom.`parent_id`, lcom.`root_id`, lcom.`thread_forward_sequence`, lcom.`thread_reverse_sequence`, lcom.`anon_name`, lc.*, uu.`email`, uu.`real_name`, uu.`login`, post.is_approved, post.is_warned, post.warned_message, uu.registration_date AS registration_date,
  305.                     tf_ava.`file_name` AS `avatar_file_name`, tf_ava.`mime_type` AS `avatar_mime_type`, tf_ava.`user_id` AS `avatar_user_id`, ta_ava.`attachment_id` AS `avatar_attachment_id`
  306.                     $selectSql
  307.                 FROM `".BIT_DB_PREFIX."liberty_comments` lcom
  308.                     INNER JOIN `".BIT_DB_PREFIX."boards_map` bm ON (lcom.`root_id` = bm.`topic_content_id`)
  309.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
  310.                     INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (lc.`user_id` = uu.`user_id`)
  311.                      $joinSql
  312.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` ta_ava ON ( uu.`avatar_attachment_id`=ta_ava.`attachment_id` )
  313.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` tf_ava ON ( tf_ava.`file_id`=ta_ava.`foreign_id` )
  314.                     LEFT JOIN `".BIT_DB_PREFIX."boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
  315.                 $whereSql ORDER BY ".$this->mDb->convertSortmode$pListHash['sort_mode');
  316.  
  317.         $ret array();
  318.  
  319.         if$result $this->mDb->query$sql$bindVars$pListHash['max_records']$pListHash['offset') ) {
  320.             while$row $result->FetchRow() ) {
  321.                 if (empty($row['anon_name'])) $row['anon_name'"Anonymous";
  322.                 if!empty$row['avatar_file_name')) {
  323.                     $row['user_avatar_url'liberty_fetch_thumbnail_urlarray(
  324.                         'source_file' => liberty_mime_get_source_filearray'user_id'=>$row['avatar_user_id']'file_name'=>$row['avatar_file_name']'mime_type'=>$row['avatar_mime_type']'attachment_id'=>$row['avatar_attachment_id') ),
  325.                         'size' => 'avatar'
  326.                     ));
  327.                 else {
  328.                     $row['user_avatar_url'FALSE;
  329.                 }
  330.                 unset($row['avatar_file_name']);
  331.                 if (!empty($row['warned_message'])) {
  332.                     $row['warned_message'str_replace("\n","<br />\n",$row['warned_message']);
  333.                 }
  334.                 $row['data'trim($row['data']);
  335.                 $row['user_url']=BitUser::getDisplayUrlFromHash($row);
  336.                 $row['parsed_data'$this->parseData$row );
  337.                 $row['level'substr_count $row['thread_forward_sequence']'.' 1;
  338.                 $row['topic_id'boards_get_topic_comment$row['thread_forward_sequence');
  339.                 $row['display_url'static::getDisplayUrlFromHash$row );
  340.                 $c new LibertyComment();
  341.                 $c->mInfo=$row;
  342.                 $row['is_editable'$c->userCanEdit();
  343.                 $ret[$row;
  344.                 //va($row);
  345.             }
  346.         }
  347.  
  348.         return $ret;
  349.     }
  350.  
  351.     /**
  352.      * This function counts the posts relating to a topic
  353.      */
  354.     function getNumComments($pContentId NULL{
  355.         $ret 0;
  356.  
  357.         $contentId $this->mCommentId;
  358.  
  359.         $bindVars array();
  360.  
  361.         $joinSql $selectSql $whereSql '';
  362.         $paramHash array'include_comments' => TRUE );
  363.         $this->getServicesSql'content_list_sql_function'$selectSql$joinSql$whereSql$bindVars$this$paramHash );
  364.  
  365.         if ($pContentId{
  366.             $sql "SELECT COUNT(*)
  367.                     FROM `".BIT_DB_PREFIX."liberty_comments` lcom
  368.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
  369.                         INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (lc.`user_id` = uu.`user_id`) $joinSql
  370.                         LEFT JOIN `".BIT_DB_PREFIX."boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
  371.                     WHERE lcom.`thread_forward_sequence` LIKE '".sprintf("%09d.",$contentId)."%' $whereSql
  372.             ";
  373.             $ret $this->mDb->getOne$sql$bindVars );
  374.         }
  375.         return $ret;
  376.     }
  377.  
  378.     /**
  379.      * This function generates a valid lookup URL
  380.      */
  381.     function getDisplayUrl({
  382.         $ret NULL;
  383.         if$this->isValid() ) {
  384.             $urlHash['comment_id'$this->mCommentId;
  385.             $urlHash['topic_id'$this->getTopicId();
  386.             $ret static::getDisplayUrlFromHash$urlHash );
  387.         }
  388.         return $ret;
  389.     }
  390.  
  391.     /**
  392.      * Generates the URL to the bitboard page
  393.      * @return the link to display the page.
  394.      */
  395.     public static function getDisplayUrlFromHash&$pParamHash {
  396.         global $gBitSystem;
  397.  
  398.         $ret NULL;
  399.         ifstatic::verifyId$pParamHash['comment_id') ) {
  400.             if$gBitSystem->isFeatureActive'pretty_urls' || $gBitSystem->isFeatureActive'pretty_urls_extended' ) ) {
  401.                 $rewrite_tag $gBitSystem->isFeatureActive'pretty_urls_extended' 'view/' '';
  402.                 $ret BOARDS_PKG_URL.$rewrite_tag."topic/".$pParamHash['topic_id'];
  403.             else {
  404.                 $ret BOARDS_PKG_URL."index.php?t=".$pParamHash['topic_id'];
  405.             }
  406.  
  407.             if$pParamHash['comment_id'!= $pParamHash['topic_id'{
  408.                 $ret .= '#comment_'.$pParamHash['comment_id'];
  409.             }
  410.         }
  411.         return $ret;
  412.     }
  413.  
  414.     function getTopicId({
  415.         return boards_get_topic_comment$this->getField'thread_forward_sequence') );
  416.     }
  417.  
  418.     function modApprove({
  419.         $data['is_approved'1;
  420.         $this->setMetaData($data);
  421.     }
  422.  
  423.     function modReject({
  424.         $this->deleteComment();
  425.     }
  426.  
  427.     function modWarn($message{
  428.         global $gBitSystem$gBitUser;
  429.  
  430.         if (empty($message)) {
  431.             $gBitSystem->fatalError("No Warning Message Given. <br />A post cannot be warned without a message");
  432.         }
  433.         $data['is_warned']=1;
  434.         $data['warned_message']=$message;
  435.         $this->setMetaData($data);
  436.  
  437.         if ($gBitSystem->isPackageActive('messages')) {
  438.             require_once(MESSAGES_PKG_PATH.'Messages.php');
  439.  
  440.             $u new BitUser($this->mInfo['user_id']);
  441.             $u->load();
  442.             $userInfo $u->mInfo;
  443.  
  444.             $pm new Messages();
  445.             $message "Your post \"".$this->mInfo['title']."\" [http://".$_SERVER['HTTP_HOST'].$this->getContactUrl()."] has been warned with the following message:\n$message\n";
  446.             $msgHash array(
  447.                 'to_login' => $userInfo['login'],
  448.                 'to'       => $userInfo['real_name'],
  449.                 'subject'  => tra'Warned Post' ).': '.$this->mInfo['title'],
  450.                 'priority' => 4,
  451.             );
  452.             $pm->postMessage$msgHash );
  453.         }
  454.     }
  455.  
  456.     function setMetaData($data{
  457.         if ($this->isValid()) {
  458.             $key array('comment_id' => $this->mCommentId);
  459.             $query_sel "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."boards_posts` WHERE comment_id=?";
  460.             $c $this->mDb->getOne$query_sel array_values($key));
  461.             if ($c == 0{
  462.                 $data=array_merge($data,$key);
  463.                 $this->mDb->associateInsert(BIT_DB_PREFIX."boards_posts",$data);
  464.             else {
  465.  
  466.                 $this->mDb->associateUpdate(BIT_DB_PREFIX."boards_posts",$data,$key);
  467.             }
  468.         }
  469.     }
  470. }
  471. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:45 +0000 by phpDocumentor 1.5.0-lsces