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

Source for file BitBlogPost.php

Documentation is available at BitBlogPost.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * @copyright (c) 2004 bitweaver.org
  6.  *  All Rights Reserved. See below for details and a complete list of authors.
  7.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  8.  *
  9.  *  Virtual base class (as much as one can have such things in PHP) for all
  10.  *  derived tikiwiki classes that require database access.
  11.  *
  12.  * @date created 2004/10/20
  13.  *
  14.  * @author drewslater <andrew@andrewslater.com>, spiderr <spider@steelsun.com>
  15.  *
  16.  * @version $Revision$
  17.  * @package blogs
  18.  */
  19.  
  20. /**
  21.  * required setup
  22.  */
  23. require_once( LIBERTY_PKG_PATH.'LibertyComment.php');
  24. require_once( LIBERTY_PKG_PATH.'LibertyMime.php');
  25. require_once( BLOGS_PKG_PATH.'BitBlog.php');
  26.  
  27. define( 'BITBLOGPOST_CONTENT_TYPE_GUID', 'bitblogpost' );
  28.  
  29. /**
  30.  * @package blogs
  31.  */
  32. class BitBlogPost extends LibertyMime {
  33.     var $mPostId;
  34.  
  35.     function BitBlogPost( $pPostId=NULL, $pContentId=NULL ) {
  36.         parent::__construct();
  37.         $this->registerContentType( BITBLOGPOST_CONTENT_TYPE_GUID, array(
  38.             'content_type_guid' => BITBLOGPOST_CONTENT_TYPE_GUID,
  39.             'content_name' => 'Blog Post',
  40.             'handler_class' => 'BitBlogPost',
  41.             'handler_package' => 'blogs',
  42.             'handler_file' => 'BitBlogPost.php',
  43.             'maintainer_url' => 'http://www.bitweaver.org'
  44.         ) );
  45.         $this->mPostId = (int)$pPostId;
  46.         $this->mContentId = (int)$pContentId;
  47.  
  48.         // Permission setup
  49.         $this->mViewContentPerm  = 'p_blogs_view';
  50.         $this->mCreateContentPerm  = 'p_blogs_post';
  51.         $this->mUpdateContentPerm  = 'p_blogs_update';
  52.         $this->mAdminContentPerm = 'p_blogs_admin';
  53.     }
  54.  
  55.     /**
  56.      * Load a Blog Post section
  57.      */
  58.     function load( $pContentId = NULL, $pPluginParams = NULL ) {
  59.         if( $this->verifyId( $this->mPostId ) || $this->verifyId( $this->mContentId ) ) {
  60.             global $gBitSystem, $gBitUser, $gLibertySystem;
  61.  
  62.             $bindVars = array(); $selectSql = ''; $joinSql = ''; $whereSql = '';
  63.             $lookupColumn = $this->verifyId( $this->mPostId )? 'post_id' : 'content_id';
  64.             $lookupId = $this->verifyId( $this->mPostId )? $this->mPostId : $this->mContentId;
  65.             array_push( $bindVars, $lookupId );
  66.             $this->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  67.  
  68.             $query = "
  69.                 SELECT bp.*, lc.*, lcds.`data` AS `summary`, lch.`hits`, uu.`login`, uu.`real_name`,
  70.                     lfa.`file_name` as `avatar_file_name`, lfa.`mime_type` AS `avatar_mime_type`, laa.`attachment_id` AS `avatar_attachment_id`,
  71.                     lfp.`file_name` AS `image_file_name`, lfp.`mime_type` AS `image_mime_type`, lap.`attachment_id` AS `image_attachment_id`
  72.                     $selectSql
  73.                 FROM `".BIT_DB_PREFIX."blog_posts` bp
  74.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id` = bp.`content_id`)
  75.                     INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON( uu.`user_id` = lc.`user_id` )
  76.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_data` lcds ON (lc.`content_id` = lcds.`content_id` AND lcds.`data_type`='summary')
  77.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON( lch.`content_id` = lc.`content_id` )
  78.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` laa ON (uu.`user_id` = laa.`user_id` AND uu.`avatar_attachment_id`=laa.`attachment_id`)
  79.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` lfa ON (lfa.`file_id` = laa.`foreign_id`)
  80.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` lap ON( lap.`content_id` = lc.`content_id` AND lap.`is_primary` = 'y' )
  81.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` lfp ON( lfp.`file_id` = lap.`foreign_id` )
  82.                     $joinSql
  83.                 WHERE bp.`$lookupColumn`=? $whereSql ";
  84.  
  85.             if( $this->mInfo = $this->mDb->getRow( $query, $bindVars ) ) {
  86.                 $this->mPostId = $this->mInfo['post_id'];
  87.                 $this->mContentId = $this->mInfo['content_id'];
  88.                 $this->mInfo['blogs'] = $this->getBlogMemberships( $this->mContentId );
  89.                 // this is bad news right here, 'url' is wrong, standard is 'display_url'
  90.                 // we should remove this now that display_url is added
  91.                 $this->mInfo['url'] = BitBlogPost::getDisplayUrlFromHash( $this->mContentId, $this->mInfo );
  92.                 $this->mInfo['display_url'] = BitBlogPost::getDisplayUrlFromHash( $this->mContentId, $this->mInfo );
  93.                 foreach( array( 'avatar', 'image' ) as $img ) {
  94.                     $this->mInfo[$img] = liberty_fetch_thumbnails( array(
  95.                         'source_file' => $this->getSourceFile( array( 'user_id'=>$this->getField( 'user_id' ), 'package'=>liberty_mime_get_storage_sub_dir_name( array( 'mime_type' => $this->getField( $img.'_mime_type' ), 'name' =>  $this->getField( $img.'_file_name' ) ) ), 'file_name' => basename( $this->mInfo[$img.'_file_name'] ), 'sub_dir' =>  $this->getField( $img.'_attachment_id' ) ) )
  96.                     ));
  97.                 }
  98.  
  99.                 $this->mInfo['raw'] = $this->mInfo['data'];
  100.                 //for two text field auto split
  101.                 if( $gBitSystem->isFeatureActive( 'blog_posts_autosplit' ) && preg_match( LIBERTY_SPLIT_REGEX, $this->mInfo['raw'] )){
  102.                     $format = $this->mInfo['format_guid'];
  103.                     $linebreak = $gLibertySystem->mPlugins[$format]['linebreak'];
  104.                     if ( preg_match( "/\.{3}split\.{3}(".preg_quote( $linebreak, "/" )."){2}/i", $this->mInfo['raw'] ) ){
  105.                         $parts = preg_split( "/\.{3}split\.{3}(".preg_quote( $linebreak, "/" )."){2}/i", $this->mInfo['raw'] );
  106.                     }else{
  107.                         $parts = preg_split( "/\.{3}split\.{3}/i", $this->mInfo['raw'] );
  108.                     }
  109.                     $this->mInfo['raw'] = isset( $parts[0] )? $parts[0] : $this->mInfo['raw'];
  110.                     $this->mInfo['raw_more'] = isset( $parts[1] )? $parts[1] : NULL ;
  111.                 }
  112.  
  113.                 $this->mInfo['data'] = preg_replace( LIBERTY_SPLIT_REGEX, "", $this->mInfo['data'] );
  114.                 $this->mInfo['use_title'] = $gBitUser->getPreference( 'user_blog_posts_use_title', 'y', $this->mInfo['user_id'] ) ;
  115.  
  116.                 if( isset($pPluginParams['load_comments']) and $pPluginParams['load_comments'] ) {
  117.                     $comment = new LibertyComment();
  118.                     $comment->mRootObj = $this;
  119.                     $this->mInfo['num_comments'] = $comment->getNumComments($this->mInfo['content_id']);
  120.                     // Get the comments associated with this post
  121.                     $this->mInfo['comments'] = $comment->getComments($this->mInfo['content_id'], $gBitSystem->getConfig( 'comments_per_page', 10 ) );
  122.                 }
  123.  
  124.                 if (!$this->mInfo['trackbacks_from'] || $this->mInfo['trackbacks_from']===null)
  125.                     $this->mInfo['trackbacks_from'] = serialize(array());
  126.  
  127.                 if (!$this->mInfo['trackbacks_to'] || $this->mInfo['trackbacks_to']===null)
  128.                     $this->mInfo['trackbacks_to'] = serialize(array());
  129.  
  130.                 $this->mInfo['trackbacks_from_count'] = count(array_keys(unserialize($this->mInfo['trackbacks_from'])));
  131.                 $this->mInfo['trackbacks_from'] = unserialize($this->mInfo['trackbacks_from']);
  132.                 $this->mInfo['trackbacks_to'] = unserialize($this->mInfo['trackbacks_to']);
  133.                 $this->mInfo['trackbacks_to_count'] = count($this->mInfo['trackbacks_to']);
  134.  
  135.                 LibertyMime::load();
  136.                 if( $this->mStorage ) {
  137.                     foreach( array_keys( $this->mStorage ) as $key ) {
  138.                         $this->mStorage[$key]['wiki_plugin_link'] = '{attachment id='.$key.'}';
  139.                     }
  140.                 }
  141.             } else {
  142.                 $this->mPostId = NULL;
  143.                 $this->mContentId = NULL;
  144.             }
  145.         }
  146.         return( count( $this->mInfo ) );
  147.     }
  148.  
  149.     public static function getTitleFromHash( $pHash, $pDefault=TRUE ) {
  150.         global $gBitSystem;
  151.         $ret = NULL;
  152.         if( !empty( $pHash['title'] ) ) {
  153.             $ret = $pHash['title'];
  154.         } elseif( !is_null( $pHash ) ) {
  155.             $date_format = $gBitSystem->get_long_date_format();
  156.             if( $gBitSystem->get_display_offset() ) {
  157.                 $date_format = preg_replace( "/ ?%Z/", "", $date_format );
  158.             } else {
  159.                 $date_format = preg_replace( "/%Z/", "UTC", $date_format );
  160.             }
  161.             $date_string = $gBitSystem->mServerTimestamp->getDisplayDateFromUTC( !empty($pHash['created']) ? $pHash['created'] : $gBitSystem->getUTCTime());
  162.             $ret = $gBitSystem->mServerTimestamp->strftime( $date_format, $date_string, true );
  163.         }
  164.  
  165.         return $ret;
  166.     }
  167.  
  168.     function getBlogMemberships( $pPostContentId ){
  169.         global $gBitSystem;
  170.         $ret = NULL;
  171.         if( @$this->verifyId( $pPostContentId ) ) {
  172.             $bindVars = array( (int)$pPostContentId );
  173.             $query = "SELECT b.`content_id` AS hash_key, bpm.*, b.*, lc.*
  174.                 FROM `".BIT_DB_PREFIX."blogs_posts_map` bpm
  175.                 INNER JOIN        `".BIT_DB_PREFIX."blogs`                 b ON b.`content_id` = bpm.`blog_content_id`
  176.                 INNER JOIN        `".BIT_DB_PREFIX."liberty_content`        lc ON lc.`content_id` = b.`content_id`
  177.                 WHERE bpm.post_content_id = ?";
  178.  
  179.             if( $ret = $this->mDb->getAssoc( $query, $bindVars ) ) {
  180.                 foreach( array_keys( $ret ) as $blogContentId ) {
  181.                     $ret[$blogContentId]['blog_url'] = BitBlog::getDisplayUrlFromHash( $ret[$blogContentId]['blog_id'] );
  182.                 }
  183.             }
  184.         } else {
  185.             $this->mErrors['post_id'] = "Invalid post id.";
  186.         }
  187.         return $ret;
  188.     }
  189.  
  190.     /**
  191.     * Get the URL for any given post image
  192.     * @param $pParamHash pass in full set of data returned from post query
  193.     * @return url to image
  194.     * @access public
  195.     ***/
  196.     function getImageThumbnails( $pParamHash ) {
  197.         global $gBitSystem, $gThumbSizes;
  198.         $ret = NULL;
  199.         if( !empty( $pParamHash['image_file_name'] )) {
  200.             $thumbHash = array(
  201.                 'mime_image'   => FALSE,
  202.                 'source_file' => $pParamHash['image_file_name']
  203.             );
  204.             $ret = liberty_fetch_thumbnails( $thumbHash );
  205.             $ret['original'] = BIT_ROOT_URL.$pParamHash['image_file_name'];
  206.         }
  207.         return $ret;
  208.     }
  209.  
  210.     /**
  211.     * Deal with images and text, modify them apprpriately that they can be returned to the form.
  212.     * @param $previewData data submitted by form - generally $_REQUEST
  213.     * @return array of data compatible with article form
  214.     * @access public
  215.     ***/
  216.     function preparePreview( $pParamHash ) {
  217.         global $gBitSystem, $gBitUser;
  218.  
  219.         $data = $pParamHash;
  220.         // preserve our split data if we are using to text fields cause it gets merged in verify
  221.         $data['raw'] = $data['edit'];
  222.         $data['raw_more'] = (!empty($data['edit_body'])?$data['edit_body']:'');
  223.         $this->verify( $data );
  224.  
  225.         if( empty( $data['user_id'] ) ) {
  226.             $data['user_id'] = $gBitUser->mUserId;
  227.         }
  228.  
  229.         if( empty( $data['hits'] ) ) {
  230.             $data['hits'] = 0;
  231.         }
  232.  
  233.         if( empty( $data['publish_date'] ) ) {
  234.             $data['publish_date'] = $gBitSystem->getUTCTime();
  235.         }
  236.  
  237.         // preserve checked blogs
  238.         if( !empty($pParamHash['blog_content_id']) ){
  239.             foreach($pParamHash['blog_content_id'] as $blog_content_id) {
  240.                 $this->mInfo['blogs'][$blog_content_id] = $blog_content_id;
  241.             }
  242.         }
  243.  
  244.         $data['use_title'] = $gBitUser->getPreference( 'user_blog_posts_use_title', 'y', $data['user_id'] );
  245.         $data['title'] = $this->getTitle($pParamHash);
  246.  
  247.         if( empty( $data['parsed_data'] ) ) {
  248.             $data['no_cache']    = TRUE;
  249.             /* this is already taken care of by calling verify above
  250.             if (isset($data['edit_body'])){
  251.                 $data['edit'] .= "...split...".$data['edit_body'];
  252.             }
  253.             */
  254.             $data['parsed_data'] = $this->parseData( $data['edit'], (!empty($data['format_guid']) ? $data['format_guid'] : 'tikiwiki' ));
  255.             //$data['parsed_data'] = $this->parseData( $data );
  256.             // replace the split syntax with a horizontal rule
  257.             $data['parsed_data'] = preg_replace( LIBERTY_SPLIT_REGEX, "<hr />", $data['parsed_data'] );
  258.         }
  259.  
  260.         return $data;
  261.     }
  262.  
  263.  
  264.  
  265.     /**
  266.     * Make sure the data is safe to store
  267.     * @param pParamHash be sure to pass by reference in case we need to make modifcations to the hash
  268.     *  This function is responsible for data integrity and validation before any operations are performed with the $pParamHash
  269.     *  NOTE: This is a PRIVATE METHOD!!!! do not call outside this class, under penalty of death!
  270.     *
  271.     * @param array pParams reference to hash of values that will be used to store the page, they will be modified where necessary
  272.     *
  273.     * @return bool TRUE on success, FALSE if verify failed. If FALSE, $this->mErrors will have reason why
  274.     *
  275.     * @access private
  276.     ***/
  277.     function verify( &$pParamHash ) {
  278.         global $gBitUser, $gBitSystem, $gLibertySystem;
  279.  
  280.         // make sure we're all loaded up of we have a mPostId
  281.         if( $this->verifyId( $this->mPostId ) && empty( $this->mInfo ) ) {
  282.             $this->load();
  283.         }
  284.  
  285.         if( @$this->verifyId( $this->mInfo['content_id'] ) ) {
  286.             $pParamHash['content_id'] = $this->mInfo['content_id'];
  287.         }
  288.  
  289.         // It is possible a derived class set this to something different
  290.         if( empty( $pParamHash['content_type_guid'] )&& !empty( $this->mContentTypeGuid ) ) {
  291.             $pParamHash['content_type_guid'] = $this->mContentTypeGuid;
  292.         }
  293.  
  294.         if( !empty( $pParamHash['data'] ) ) {
  295.             $pParamHash['edit'] = $pParamHash['data'];
  296.         }
  297.  
  298.         // for two text field auto split
  299.         if (!empty($pParamHash['edit_body'])){
  300.             $linebreak = $gLibertySystem->mPlugins[$pParamHash['format_guid']]['linebreak'];
  301.             // we need two line breaks to simulate a paragraph break
  302.             $pParamHash['edit'] .= "...split...".$linebreak.$linebreak.$pParamHash['edit_body'];
  303.         }
  304.  
  305.         // truncate length if too long
  306.         if( !empty( $pParamHash['title'] ) ) {
  307.             $pParamHash['title'] = substr( $pParamHash['title'], 0, 160 );
  308.         }
  309.  
  310.         if( !empty( $pParamHash['publish_Month'] ) ) {
  311.             //$dateString = $pParamHash['publish_Year'].'-'.$pParamHash['publish_Month'].'-'.$pParamHash['publish_Day'].' '.$pParamHash['publish_Hour'].':'.$pParamHash['publish_Minute'];
  312.  
  313.             //old way
  314.             //$timestamp = $gBitSystem->mServerTimestamp->getUTCFromDisplayDate( strtotime( $dateString ) );
  315.             //new way
  316.             $this->mDate = new BitDate(0);
  317.             $offset = $this->mDate->get_display_offset();
  318.  
  319.             $dateString = $this->mDate->gmmktime(
  320.                 $pParamHash['publish_Hour'],
  321.                 $pParamHash['publish_Minute'],
  322.                 isset($pParamHash['publish_Second']) ? $pParamHash['publish_Second'] : 0,
  323.                 $pParamHash['publish_Month'],
  324.                 $pParamHash['publish_Day'],
  325.                 $pParamHash['publish_Year']
  326.             );
  327.  
  328.             $timestamp = $this->mDate->getUTCFromDisplayDate( $dateString );
  329.  
  330.             if( $timestamp !== -1 ) {
  331.                 $pParamHash['publish_date'] = $timestamp;
  332.             }
  333.         }
  334.         if( !empty( $pParamHash['publish_date'] ) ) {
  335.             $pParamHash['post_store']['publish_date'] = $pParamHash['publish_date'];
  336.         }else{
  337.             $pParamHash['post_store']['publish_date'] = $gBitSystem->getUTCTime();
  338.         }
  339.  
  340.         if( !empty( $pParamHash['expire_Month'] ) ) {
  341.             $dateString = $pParamHash['expire_Year'].'-'.$pParamHash['expire_Month'].'-'.$pParamHash['expire_Day'].' '.$pParamHash['expire_Hour'].':'.$pParamHash['expire_Minute'];
  342.  
  343.             //old way
  344.             //$timestamp = $gBitSystem->mServerTimestamp->getUTCFromDisplayDate( strtotime( $dateString ) );
  345.             //new way
  346.             $this->mDate = new BitDate(0);
  347.             $offset = $this->mDate->get_display_offset();
  348.  
  349.             $dateString = $this->mDate->gmmktime(
  350.                 $pParamHash['expire_Hour'],
  351.                 $pParamHash['expire_Minute'],
  352.                 isset($pParamHash['expire_Second']) ? $pParamHash['expire_Second'] : 0,
  353.                 $pParamHash['expire_Month'],
  354.                 $pParamHash['expire_Day'],
  355.                 $pParamHash['expire_Year']
  356.             );
  357.  
  358.             $timestamp = $this->mDate->getUTCFromDisplayDate( $dateString );
  359.  
  360.             if( $timestamp !== -1 ) {
  361.                 $pParamHash['expire_date'] = $timestamp;
  362.             }
  363.         }
  364.         if( !empty( $pParamHash['expire_date'] ) ) {
  365.             $pParamHash['post_store']['expire_date'] = $pParamHash['expire_date'];
  366.         }else{
  367.             $pParamHash['post_store']['expire_date'] = $gBitSystem->getUTCTime();
  368.         }
  369.  
  370.         // if we have an error we get them all by checking parent classes for additional errors
  371.         if( count( $this->mErrors ) > 0 ){
  372.             parent::verify( $pParamHash );
  373.         }
  374.  
  375.         return( count( $this->mErrors )== 0 );
  376.     }
  377.  
  378.     /**
  379.      * Check that the class has a valid blog loaded
  380.      */
  381.     function isValid() {
  382.         return( $this->verifyId( $this->mPostId ) && is_numeric( $this->mPostId ) && $this->mPostId > 0 );
  383.     }
  384.  
  385.     /**
  386.      * Check if the current user is the blog owner
  387.      */
  388.      function isBlogOwner( $pUserId=NULL ) {
  389.         $ret = FALSE;
  390.         global $gBitUser;
  391.         if( empty( $pUserId ) && $gBitUser->isValid() ) {
  392.             $pUserId = $gBitUser->mUserId;
  393.         }
  394.         if( $this->isValid() && ($pUserId == $this->mInfo["blog_user_id"]) ) {
  395.             $ret = 'y';
  396.         }
  397.         return $ret;
  398.     }
  399.  
  400.  
  401.     /**
  402.      * Check if the current post can have comments attached to it
  403.      */
  404.     function isCommentable(){
  405.         global $gBitSystem;
  406.         return $gBitSystem->isFeatureActive( 'blog_posts_comments' );
  407.     }
  408.  
  409.     /**
  410.      * Store a Blog Post
  411.      * @todo users_watches is a legacy package and needs refactoring
  412.      */
  413.     function store( &$pParamHash ) {
  414.         global $gBitSystem;
  415.         $this->mDb->StartTrans();
  416.         if( $this->verify( $pParamHash )&& LibertyMime::store( $pParamHash ) ) {
  417.             $table = BIT_DB_PREFIX."blog_posts";
  418.  
  419.             // Send trackbacks recovering only successful trackbacks
  420.             if ( !empty( $pParamHash['trackback'] ) ){
  421.                 $trackbacks = serialize( $this->sendTrackbacks( $pParamHash['trackback'] ) );
  422.             }
  423.  
  424.             if( $this->isValid() ) {
  425.                 $locId = array( "content_id" => $this->mContentId );
  426.                 $result = $this->mDb->associateUpdate( $table, $pParamHash['post_store'], $locId );
  427.             } else {
  428.                 $pParamHash['post_store']['content_id'] = $pParamHash['content_id'];
  429.                 if( @$this->verifyId( $pParamHash['post_id'] ) ) {
  430.                     // if pParamHash['post_id'] is set, someone is requesting a particular post_id. Use with caution!
  431.                     $pParamHash['post_store']['post_id'] = $pParamHash['post_id'];
  432.                 } else {
  433.                     $pParamHash['post_store']['post_id'] = $this->mDb->GenID( 'blog_posts_post_id_seq' );
  434.                 }
  435.                 $this->mPostId = $pParamHash['post_store']['post_id'];
  436.                 //store the new post
  437.                 $result = $this->mDb->associateInsert( $table, $pParamHash['post_store'] );
  438.             }
  439.  
  440.             // let's reload to get a full mInfo hash which is needed below
  441.             $this->load();
  442.  
  443.             // if blog_content_id, then map the post to the relative blogs
  444.             if( !empty( $pParamHash['blog_content_id'] )){
  445.                 $this->storePostMap( $this->mInfo, $pParamHash['blog_content_id'], NULL, TRUE );
  446.             }
  447.  
  448.             // Update post with trackbacks successfully sent
  449.             // Can this be moved below into similar function below? -wjames5
  450.             // this throws an error on site population because post_id is not defined in pParamHash - wjames5
  451.             $query = "UPDATE `".BIT_DB_PREFIX."blog_posts` SET `trackbacks_from`=?, `trackbacks_to` = ? WHERE `post_id`=?";
  452.             if( @BitBase::verifyId( $pParamHash['post_id'] )) {
  453.                 $this->mDb->query( $query, array( serialize( array() ), $trackbacks, (int) $pParamHash['post_id'] ));
  454.             }
  455.  
  456.             if( $gBitSystem->isFeatureActive( 'users_watches' ) ) {
  457.                 global $gBitUser, $gBitSmarty;
  458.                 if( isset( $this->mInfo['blog_id'] ) &&  $nots = $gBitUser->getEventWatches( 'blog_post', $this->mInfo['blog_id'] ) ) {
  459.                     foreach ($nots as $not) {
  460.                         $gBitSmarty->assign('mail_site', $_SERVER["SERVER_NAME"]);
  461.                         $gBitSmarty->assign('mail_title', $this->mInfo['title']);
  462.                         $gBitSmarty->assign('mail_blogid', $this->mInfo['blog_id']);
  463.                         $gBitSmarty->assign('mail_postid', $this->mPostId);
  464.                         $gBitSmarty->assign('mail_date', $gBitSystem->getUTCTime());
  465.                         $gBitSmarty->assign('mail_user', $this->mInfo['login']);
  466.                         $gBitSmarty->assign('mail_data', $this->mInfo['data']);
  467.                         $gBitSmarty->assign('mail_hash', $not['hash']);
  468.                         $foo = parse_url($_SERVER["REQUEST_URI"]);
  469.                         $machine = httpPrefix(). $foo["path"];
  470.                         $gBitSmarty->assign('mail_machine', $machine);
  471.                         $parts = explode('/', $foo['path']);
  472.  
  473.                         if (count($parts) > 1)
  474.                             unset ($parts[count($parts) - 1]);
  475.  
  476.                         $gBitSmarty->assign('mail_machine_raw', httpPrefix(). implode('/', $parts));
  477.                         $mail_data = $gBitSmarty->fetch('bitpackage:blogs/user_watch_blog_post.tpl');
  478.                         @mail($not['email'], tra('Blog post'). ' ' . $title, $mail_data, "From: ".$gBitSystem->getPrefence( 'site_sender_email' )."\r\nContent-type: text/plain;charset=utf-8\r\n");
  479.                     }
  480.                 }
  481.             }
  482.  
  483.             //is this nearly identical to the above and can they be consolodated? -wjames5
  484.             // should this be $pParamHash['trackback'] or the above $pParamHash['trackbacks'] ? - xing
  485.             if( !empty( $pParamHash['trackbacks'] ) ) {
  486.                 $query = "update `".BIT_DB_PREFIX."blog_posts` set `trackbacks_to`=? where `post_id`=?";
  487.                 $result = $this->mDb->query($query,array($trackbacks, $user_id, $post_id));
  488.             }
  489.  
  490.             $this->mDb->CompleteTrans();
  491.             $this->load();
  492.         }
  493.         return ( count( $this->mErrors ) == 0 );
  494.     }
  495.  
  496.  
  497.     function loadPostMap( $pPostContentId, $pBlogContentId){
  498.         $ret = NULL;
  499.         if( @BitBase::verifyId( $pPostContentId ) ){
  500.             $this->mDb->StartTrans();
  501.             $result = $this->mDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."blogs_posts_map` WHERE `post_content_id`=? AND `blog_content_id`=?", array( $pPostContentId, $pBlogContentId ) );
  502.             $this->mDb->CompleteTrans();
  503.             if ( !empty( $result ) ){
  504.                 $ret = $result;
  505.             };
  506.         }
  507.         return $ret;
  508.     }
  509.  
  510.     /**
  511.      * Map a Post to a Blog or multiple Blogs
  512.      * @param pPost a Post hash.
  513.      * @param pBlogMixed the content_id or and array of ids of the blogs we want the post to show up in.
  514.      * @param pCrosspostNote text to display with the blog post when viewed in the blog crossposted to.
  515.      * @param pAutoProcess a bool to distinguish if we are storing from the crosspost interface or from the blog posting interface.
  516.      */
  517.     function storePostMap( $pPost, $pBlogMixed, $pCrosspostNote = NULL, $pAutoProcess = FALSE ) {
  518.         global $gBitSystem, $gBitUser;
  519.         $postContentId = $pPost['content_id'];
  520.         if( @$this->verifyId( $postContentId ) ) {
  521.             $this->mDb->StartTrans();
  522.             //this is to set the time we add a post to a blog.
  523.             $currTime = $gBitSystem->getUTCTime();
  524.             $postTime = $pPost['publish_date'];
  525.             $timeStamp = ( $postTime > $currTime )?$postTime : $currTime;
  526.             $blogIds = array();
  527.  
  528.             if( !empty( $pBlogMixed )){
  529.                 if (!is_array( $pBlogMixed ) && !is_numeric( $pBlogMixed ) ){
  530.                     $blogIds = explode( ",", $pBlogMixed );
  531.                 }elseif ( is_array( $pBlogMixed ) ) {
  532.                     $blogIds = $pBlogMixed;
  533.                 }elseif ( is_numeric( $pBlogMixed ) ) {
  534.                     $blogIds = array( $pBlogMixed );
  535.                 }
  536.             }
  537.             $currentMappings = array();
  538.             if( $allMappings = $this->mDb->getCol( "SELECT `blog_content_id` FROM `".BIT_DB_PREFIX."blogs_posts_map` WHERE `post_content_id`=?", array( $postContentId ) ) ) {
  539.                 // whiddle down all mappings to just those we have perm to
  540.                 foreach( $allMappings as $blogContentId ) {
  541.                     if( $this->checkContentPermission( array( 'user_id' => $gBitUser->mUserId, 'perm_name'=>'p_blogs_post', 'content_id'=>$blogContentId ) ) ) {
  542.                         $currentMappings[] = $blogContentId;
  543.                     }
  544.                 }
  545.             }
  546.  
  547.             // Add new mappings for this post
  548.             $newBlogIds = array_diff( $blogIds, $currentMappings );
  549.             foreach( $newBlogIds as $blogContentId ) {
  550.                 if( $this->verifyId( $blogContentId ) && $this->checkContentPermission( array( 'user_id' => $gBitUser->mUserId, 'perm_name'=>'p_blogs_post', 'content_id'=>$blogContentId ) ) ) {
  551.                     $result = $this->mDb->associateInsert( BIT_DB_PREFIX."blogs_posts_map", array(
  552.                         'post_content_id' => $postContentId,
  553.                         'blog_content_id' => (int)$blogContentId,
  554.                         'date_added' => $timeStamp,
  555.                         'crosspost_note' => $pCrosspostNote,
  556.                     ));
  557.                 }
  558.             }
  559.  
  560.             /* if we are coming form the crossposting form then we
  561.              * want to update any change to the crosspost note.
  562.              * we dont want to if we are coming from the blog posting form.
  563.              */
  564.             if( !$pAutoProcess ){
  565.                 // Update existing mappings
  566.                 $updateBlogIds = array_intersect( $blogIds, $currentMappings );
  567.                 foreach( $updateBlogIds as $blogContentId ) {
  568.                     if( $this->verifyId( $blogContentId ) && $this->checkContentPermission( array( 'user_id' => $gBitUser->mUserId, 'perm_name'=>'p_blogs_post', 'content_id'=>$blogContentId ) ) ) {
  569.                         $result = $this->mDb->associateUpdate( BIT_DB_PREFIX."blogs_posts_map", array(
  570.                             'crosspost_note' => $pCrosspostNote,
  571.                         ), array(
  572.                             'post_content_id' => $postContentId,
  573.                             'blog_content_id' => (int)$blogContentId,
  574.                         ));
  575.                     }
  576.                 }
  577.             }
  578.             $this->mDb->CompleteTrans();
  579.  
  580.             /* if we are coming from the blog posting form we
  581.              * want to automatically drop any crossposting if
  582.              * we have unchecked them there. we ignore this when
  583.              * coming from the crossposting form.
  584.              */
  585.             if ( $pAutoProcess ){
  586.                 // Remove mappings for this post
  587.                 $removedBlogIds = array_diff( $currentMappings, $blogIds );
  588.                 $this->expungePostMap( $postContentId, $removedBlogIds );
  589.             }
  590.         }
  591.  
  592.         return ( count( $this->mErrors ) == 0 );
  593.     }
  594.  
  595.     function expungePostMap( $pPostContentId, $pBlogContentIds ){
  596.         $this->mDb->StartTrans();
  597.         if ( !empty($pBlogContentIds) ){
  598.             foreach( $pBlogContentIds as $blogContentId ) {
  599.                 $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."blogs_posts_map` WHERE `blog_content_id`=? AND `post_content_id`=?", array( $blogContentId, $pPostContentId ) );
  600.             }
  601.         }
  602.         $this->mDb->CompleteTrans();
  603.         return ( count( $this->mErrors ) == 0 );
  604.     }
  605.  
  606.     /**
  607.      * Remove complete blog post set and any comments
  608.      */
  609.     function expunge() {
  610.         $ret = FALSE;
  611.         // let's force a full load to make sure everything is loaded.
  612.         // lets not -wjames5
  613.         //$this->load();
  614.         if( $this->isValid() ) {
  615.             $this->mDb->StartTrans();
  616.  
  617.             // remove all references in blogs_posts_map where post_content_id = content_id
  618.             $query_map = "DELETE FROM `".BIT_DB_PREFIX."blogs_posts_map` WHERE `post_content_id` = ?";
  619.             $result = $this->mDb->query( $query_map, array( $this->mContentId ) );
  620.  
  621.             $query = "DELETE FROM `".BIT_DB_PREFIX."blog_posts` WHERE `content_id` = ?";
  622.             $result = $this->mDb->query( $query, array( $this->mContentId ) );
  623.  
  624.             // Do this last so foreign keys won't complain (not the we have them... yet ;-)
  625.             if( LibertyMime::expunge() ) {
  626.                 $ret = TRUE;
  627.                 $this->mDb->CompleteTrans();
  628.             } else {
  629.                 $this->mDb->RollbackTrans();
  630.             }
  631.         }
  632.         return $ret;
  633.     }
  634.  
  635.     /**
  636.      * Return a summary for this content base on
  637.      *
  638.      * @param    object    PostId of the item to use
  639.      * @return    object    Url String
  640.      */
  641.     function getDescription() {
  642.         if( !($ret = $this->getField( 'summary' )) ) {
  643.             $ret = $this->getField( 'data' );
  644.         }
  645.         return $ret;
  646.     }
  647.  
  648.  
  649.     /**
  650.      * Generate a valid url for the Blog
  651.      *
  652.      * @param    object    PostId of the item to use
  653.      * @return    object    Url String
  654.      */
  655.     public static function getDisplayUrlFromHash( &$pParamHash ) {
  656.         global $gBitSystem;
  657.  
  658.         $ret = NULL;
  659.         if( @BitBase::verifyId( $pParamHash['content_id'] )) {
  660.             $rewrite_tag = $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ? 'view/' : '';
  661.             if( $gBitSystem->isFeatureActive( 'pretty_urls' ) || $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ) {
  662.                 if( !empty( $pParamHash['post_id'] ) ) {
  663.                     $ret = BLOGS_PKG_URL.$rewrite_tag.'post/'.$pParamHash['post_id'];
  664.                 } else {
  665.                     $ret = BLOGS_PKG_URL.$rewrite_tag.'content/'.$pParamHash['content_id'];
  666.                 }
  667.             } else {
  668.                 $ret = BLOGS_PKG_URL.'view_post.php?content_id='.$pParamHash['content_id'];
  669.             }
  670.         }
  671.         return $ret;
  672.     }
  673.  
  674.     /**
  675.      * Generate a valid display link for the Blog
  676.      *
  677.      * @param    object    PostId of the item to use
  678.      * @param    array    Not used
  679.      * @return    object    Fully formatted html link for use by Liberty
  680.      */
  681.     function getDisplayLink( $pTitle=NULL, $pMixed=NULL, $pAnchor=NULL ) {
  682.         global $gBitSystem;
  683.         if( empty( $pTitle ) && !empty( $this ) ) {
  684.             $pTitle = $this->getField( 'title', $this->getContentTypeName() );
  685.         }
  686.  
  687.         if( empty( $pMixed ) && !empty( $this ) ) {
  688.             $pMixed = $this->mInfo;
  689.         }
  690.  
  691.         $ret = $pTitle;
  692.         if( $gBitSystem->isPackageActive( 'blogs' ) ) {
  693.             $ret = '<a title="'.htmlspecialchars( BitBlogPost::getTitleFromHash( $pMixed ) ).'" href="'.BitBlogPost::getDisplayUrlFromHash( $pMixed['content_id'] ).'">'.htmlspecialchars( BitBlogPost::getTitleFromHash( $pMixed  ) ).'</a>';
  694.         }
  695.  
  696.         return $ret;
  697.     }
  698.  
  699.     /**
  700.     * Returns include file that will
  701.     * @return the fully specified path to file to be included
  702.     */
  703.     function getRenderFile() {
  704.         return( BLOGS_PKG_PATH.'display_bitblogpost_inc.php' );
  705.     }
  706.  
  707.     function sendTrackbacks( $pTrackbacks ) {
  708.         $ret = array();
  709.         if( $this->isValid() && !empty( $pTrackbacks ) ) {
  710.         // Split to get each URI
  711.         $tracks = explode(',', $pTrackbacks);
  712.  
  713.         //Build uri for post
  714.         $parts = parse_url($_SERVER['REQUEST_URI']);
  715.         $uri = httpPrefix(). str_replace('post',
  716.             'view_post', $parts['path']). '?post_id=' . $this->mPostId . '&amp;blog_id=' . $this->mInfo['blog_id'];
  717.         include_once ( UTIL_PKG_PATH.'Snoopy.class.inc' );
  718.         $snoopy = new Snoopy;
  719.  
  720.         foreach ($tracks as $track) {
  721.             @$fp = fopen($track, 'r');
  722.  
  723.             if ($fp) {
  724.                 $data = '';
  725.  
  726.                 while (!feof($fp)) {
  727.                     $data .= fread($fp, 32767);
  728.                 }
  729.  
  730.                 fclose ($fp);
  731.                 preg_match("/trackback:ping=(\"|\'|\s*)(.+)(\"|\'\s)/", $data, $reqs);
  732.  
  733.                 if (!isset($reqs[2]))
  734.                 return $ret;
  735.  
  736.                 @$fp = fopen($reqs[2], 'r');
  737.  
  738.                     if ($fp) {
  739.                         fclose ($fp);
  740.  
  741.                         $submit_url = $reqs[2];
  742.                         $submit_vars["url"] = $uri;
  743.                         $submit_vars["blog_name"] = $this->mInfo['blogtitle'];
  744.                         $submit_vars["title"] = $this->mInfo['title'] ? $this->mInfo['title'] : date("d/m/Y [h:i]", $this->mInfo['created']);
  745.                         $submit_vars["title"] .= ' ' . tra('by'). ' ' . BitUser::getDisplayNameFromHash( FALSE, $this->mInfo );
  746.                         $submit_vars["excerpt"] = substr($post_info['data'], 0, 200);
  747.                         $snoopy->submit($submit_url, $submit_vars);
  748.                         $back = $snoopy->results;
  749.  
  750.                         if (!strstr('<error>1</error>', $back)) {
  751.                             $ret[] = $track;
  752.                         }
  753.                     }
  754.                 }
  755.             }
  756.         }
  757.         return $ret;
  758.     }
  759.  
  760.     function getList( &$pListHash ) {
  761.         global $gBitUser, $gBitSystem;
  762.  
  763.         $this->prepGetList( $pListHash );
  764.  
  765.         $selectSql = ''; $joinSql = ''; $whereSql = '';
  766.         $bindVars = array();
  767.         array_push( $bindVars, $this->mContentTypeGuid );
  768.  
  769.         $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, NULL, $pListHash );
  770.  
  771.         if( @$this->verifyId( $pListHash['blog_id'] ) ) {
  772.             $selectSql .= ', bpm.crosspost_note';
  773.             array_push( $bindVars, (int)$pListHash['blog_id'] );
  774.             $joinSql .= " LEFT OUTER JOIN `".BIT_DB_PREFIX."blogs_posts_map` bpm ON ( bpm.`post_content_id` = bp.`content_id` ) ";
  775.             $joinSql .= " LEFT OUTER JOIN `".BIT_DB_PREFIX."blogs` b ON ( bpm.`blog_content_id`=b.`content_id` ) ";
  776.                                 //    " ON ( b.`content_id` = bpm.`blog_content_id` AND bp.`content_id` = bpm.`post_content_id` )";
  777.             $whereSql .= ' AND b.`blog_id` = ? ';
  778.             $pListHash['sort_mode'] = 'publish_date_desc';
  779.         }
  780.  
  781.         if( @$this->verifyId( $pListHash['post_id_gt'] ) ) {
  782.             array_push( $bindVars, (int)$pListHash['post_id_gt'] );
  783.             $whereSql .= ' AND bp.`post_id` > ? ';
  784.         }
  785.  
  786.         if( @$this->verifyId( $pListHash['post_id_lt'] ) ) {
  787.             array_push( $bindVars, (int)$pListHash['post_id_lt'] );
  788.             $whereSql .= ' AND bp.`post_id` < ? ';
  789.         }
  790.  
  791.         if( @$this->verifyId( $pListHash['user_id'] ) ) {
  792.             array_push( $bindVars, (int)$pListHash['user_id'] );
  793.             $whereSql .= ' AND lc.`user_id` = ? ';
  794.         }
  795.  
  796.         $this->getServicesSql( 'content_user_collection_function', $selectSql, $joinSql, $whereSql, $bindVars, NULL, $pListHash );
  797.  
  798.         // map user to login in case we used one instead of the other
  799.         if( !empty( $pListHash['user'] ) ) {
  800.             $pListHash['login'] = $pListHash['user'];
  801.         }
  802.  
  803.         if( !empty( $pListHash['login'] ) ) {
  804.             array_push( $bindVars, $pListHash['login'] );
  805.             $whereSql .= ' AND uu.`login` = ? ';
  806.         }
  807.  
  808.         if( $pListHash['find'] ) {
  809.             $findesc = '%' . strtoupper( $pListHash['find'] ) . '%';
  810.              $whereSql .= "AND (UPPER(lc.`data`) like ?) ";
  811.             $bindVars[] =$findesc;
  812.         }
  813.  
  814.         if( !empty( $pListHash['date'] ) && is_numeric( $pListHash['date'] ) ) {
  815.             $whereSql .= " AND  lc.`created`<=? ";
  816.             $bindVars[]= $pListHash['date'];
  817.         }
  818.  
  819.         if( !empty( $pListHash['date_start'] ) && is_numeric( $pListHash['date_start'] ) ) {
  820.             $whereSql .= " AND  lc.`created`>=? ";
  821.             $bindVars[]= $pListHash['date_start'];
  822.         }
  823.         if( !empty( $pListHash['date_end'] ) && is_numeric( $pListHash['date_end'] ) ) {
  824.             $whereSql .= " AND  lc.`created`<=? ";
  825.             $bindVars[]= $pListHash['date_end'];
  826.         }
  827.  
  828.         if( !empty( $pListHash['content_perm_name'] ) ) {
  829.             $this->getContentListPermissionsSql( $pListHash['content_perm_name'], $selectSql, $joinSql, $whereSql, $bindVars );
  830.         }
  831.  
  832.         /* Check if the post wants to be viewed before / after respective dates
  833.          * Note: expiring posts are determined by the expired date being greater than the publish date
  834.          */
  835.         static::getDateRestrictions($pListHash, $whereSql, $bindVars);
  836.  
  837.         /* sort_mode is never empty due to call to prepGetList above
  838.          * I think this will have to be perminently removed and default
  839.          * set before passing the list hash in if a different default is
  840.          * desired from that in prepGetList. -wjames5
  841.          */
  842.         /*
  843.         if( empty( $pListHash['sort_mode'] ) ) {
  844.             $pListHash['sort_mode'] = 'publish_date_desc';
  845.             $sortModePrefix = 'bp';
  846.             //$pListHash['sort_mode'] = 'created_desc';
  847.         } else {
  848.         */
  849.         $sortModePrefix = '';
  850.         if( !empty( $pListHash['sort_mode'] ) && !strpos( $pListHash['sort_mode'], '.' ) ) {
  851.             switch( $pListHash['sort_mode'] ) {
  852.                 case 'publish_date_asc':
  853.                 case 'publish_date_desc':
  854.                 case 'post_id_desc':
  855.                 case 'post_id_asc':
  856.                     $sortModePrefix = 'bp.';
  857.                     break;
  858.                 case 'date_added_desc':
  859.                     $sortModePrefix = 'bpm.';
  860.                     break;
  861.                 case 'hits_asc':
  862.                 case 'hits_desc':
  863.                     $sortModePrefix = 'lch.';
  864.                     break;
  865.                 case 'sort_date_asc':
  866.                 case 'sort_date_desc':
  867.                     break;
  868.                 case 'real_name_asc':
  869.                 case 'real_name_desc':
  870.                     $sortModePrefix = 'uu.';
  871.                     break;
  872.                 // these technicall are not correct, however, we do not double join on users_users, so we sort by creator real_name
  873.                 case 'creator_real_name_asc':
  874.                 case 'modifier_real_name_asc':
  875.                     $sortModePrefix = 'uu.';
  876.                     $pListHash['sort_mode'] = 'real_name_asc';
  877.                     break;
  878.                 case 'registration_date_desc':
  879.                     $sortModePrefix = 'uu.';
  880.                     $pListHash['sort_mode'] = 'registration_date_desc';
  881.                     break;
  882.                 case 'creator_real_name_desc':
  883.                 case 'modifier_real_name_desc':
  884.                     $sortModePrefix = 'uu.';
  885.                     $pListHash['sort_mode'] = 'real_name_desc';
  886.                     break;
  887.                 default:
  888.                     $sortModePrefix = 'lc.';
  889.                     break;
  890.             }
  891.         }
  892.  
  893.         $secondarySortMode = ($pListHash['sort_mode'] != 'last_modified_desc') ? ', last_modified DESC': '';
  894.         $sort_mode = $sortModePrefix . $this->mDb->convertSortmode( $pListHash['sort_mode'] ).$secondarySortMode;
  895.  
  896.         $query = "
  897.             SELECT
  898.                 bp.`post_id`, bp.`publish_date`, bp.`expire_date`, bp.`trackbacks_to`, bp.`trackbacks_from`,
  899.                 lc.*, lch.`hits`, lcds.`data` AS `summary`, COALESCE( bp.`publish_date`, lc.`last_modified` ) AS sort_date,
  900.                 uu.`email`, uu.`login`, uu.`real_name`,
  901.                     lfa.`file_name` as `avatar_file_name`, lfa.`mime_type` AS `avatar_mime_type`, laa.`attachment_id` AS `avatar_attachment_id`,
  902.                     lfp.`file_name` AS `image_file_name`, lfp.`mime_type` AS `image_mime_type`, lap.`attachment_id` AS `image_attachment_id`
  903.             FROM `".BIT_DB_PREFIX."blog_posts` bp
  904.                 INNER JOIN      `".BIT_DB_PREFIX."liberty_content`       lc ON lc.`content_id`         = bp.`content_id`
  905.                 INNER JOIN        `".BIT_DB_PREFIX."users_users`             uu ON uu.`user_id`               = lc.`user_id`
  906.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON lc.`content_id`         = lch.`content_id`
  907.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_data` lcds ON (lc.`content_id` = lcds.`content_id` AND lcds.`data_type`='summary')
  908.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments`    laa ON (uu.`user_id` = laa.`user_id` AND laa.`attachment_id` = uu.`avatar_attachment_id`)
  909.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files`        lfa ON lfa.`file_id`           = laa.`foreign_id`
  910.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments`  lap ON lap.`content_id`        = lc.`content_id` AND lap.`is_primary` = 'y'
  911.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files`        lfp ON lfp.`file_id`           = lap.`foreign_id`
  912.                 $joinSql
  913.             WHERE lc.`content_type_guid` = ? $whereSql
  914.             ORDER BY $sort_mode";
  915.  
  916.         # Get count of total number of items available
  917.         $query_cant = "
  918.             SELECT COUNT( * )
  919.             FROM `".BIT_DB_PREFIX."blog_posts` bp
  920.                 INNER JOIN      `".BIT_DB_PREFIX."liberty_content`       lc ON lc.`content_id` = bp.`content_id`
  921.                 INNER JOIN        `".BIT_DB_PREFIX."users_users`             uu ON uu.`user_id`               = lc.`user_id`
  922.                 $joinSql
  923.             WHERE lc.`content_type_guid` = ? $whereSql ";
  924.  
  925.         $cant = $this->mDb->getOne($query_cant,$bindVars);
  926.         $pListHash["cant"] = $cant;
  927.  
  928.         # Check for offset out of range
  929.         if ( $pListHash['offset'] < 0 ) {
  930.             $pListHash['offset'] = 0;
  931.         } elseif ( $pListHash['offset']    > $pListHash["cant"] ) {
  932.             $lastPageNumber = ceil ( $pListHash["cant"] / $pListHash['max_records'] ) - 1;
  933.             $pListHash['offset'] = $pListHash['max_records'] * $lastPageNumber;
  934.         }
  935.  
  936.  
  937.         $result = $this->mDb->query($query,$bindVars,$pListHash['max_records'],$pListHash['offset']);
  938.         $ret = array();
  939.  
  940.         $comment = new LibertyComment();
  941.         while ($res = $result->fetchRow()) {
  942.             $res['no_fatal'] = TRUE;
  943.             $accessError = $this->invokeServices( 'content_verify_access', $res, FALSE );
  944.             if( empty( $accessError ) ) {
  945.  
  946.                 foreach( array( 'avatar', 'image' ) as $img ) {
  947.                     $res[$img] = liberty_fetch_thumbnails( array(
  948.                         'source_file' => liberty_mime_get_source_file( array( 'user_id'=>$res['user_id'], 'package'=>liberty_mime_get_storage_sub_dir_name( array( 'mime_type' => $res[$img.'_mime_type'], 'name'=>$res[$img.'_file_name'] ) ), 'file_name'=>basename( $res[$img.'_file_name'] ), 'sub_dir'=>$res[$img.'_attachment_id'] ) )
  949.                     ));
  950.                 }
  951.                 $res['thumbnail_url'] = BitBlogPost::getImageThumbnails( $res );
  952.                 $res['num_comments'] = $comment->getNumComments( $res['content_id'] );
  953.                 $res['post_url'] = BitBlogPost::getDisplayUrlFromHash( $res );
  954.                 $res['display_url'] = $res['post_url'];
  955.                 $res['display_link'] = $this->getDisplayLink( $res['title'], $res );
  956.                 $res['blogs'] = $this->getBlogMemberships( $res['content_id'] );
  957.  
  958.                 // trackbacks
  959.                 if($res['trackbacks_from']!=null)
  960.                     $res['trackbacks_from'] = unserialize($res['trackbacks_from']);
  961.  
  962.                 if (!is_array($res['trackbacks_from']))
  963.                     $res['trackbacks_from'] = array();
  964.  
  965.                 $res['trackbacks_from_count'] = count(array_keys($res['trackbacks_from']));
  966.                 if($res['trackbacks_to']!=null)
  967.                     $res['trackbacks_to'] = unserialize($res['trackbacks_to']);
  968.                 if ($res['user_id'] == $gBitUser->mUserId) {
  969.                     $res['ownsblog'] = 'y';
  970.                 } else {
  971.                     $res['ownsblog'] = 'n';
  972.                 }
  973.                 $res['trackbacks_to_count'] = count($res['trackbacks_to']);
  974.  
  975.                 $res['pages'] = $this->getNumberOfPages( $res['data'] );
  976.  
  977.                 // deal with the parsing
  978.                 $parseHash['format_guid']     = $res['format_guid'];
  979.                 $parseHash['content_id']      = $res['content_id'];
  980.                 $parseHash['user_id']          = $res['user_id'];
  981.                 // support for ...split... and auto split
  982.                 if( !empty( $pListHash['full_data'] ) ) {
  983.                     $parseHash['data'] = $res['data'];
  984.                     $res['parsed'] = $this->parseData( $parseHash );
  985.                 } else {
  986.                     $parseHash['data'] = $res['data'];
  987.                     $parseHash['no_cache'] = TRUE;
  988.                     $splitArray = $this->parseSplit($parseHash, $gBitSystem->getConfig( 'blog_posts_description_length', 500));
  989.                     $res = array_merge($res, $splitArray);
  990.                 }
  991.                 if( !empty( $this->mInfo['summary'] ) ) {
  992.                     $res['summary'] = $parseHash['data'] = $this->mInfo['summary'];
  993.                     $parseHash['no_cache'] = TRUE;
  994.                     $res['parsed_summary'] = $this->parsedData( $parseHash );
  995.                 }
  996.                 if( !empty( $res['crosspost_note'] ) ){
  997.                     $res['crosspost_note_raw'] = $parseHash['data'] = $res['crosspost_note'];
  998.                     $parseHash['no_cache'] = TRUE;
  999.                     $res['crosspost_note'] = $this->parseData( $parseHash );
  1000.                 }
  1001.  
  1002.                 $ret[] = $res;
  1003.  
  1004.             } elseif( !empty( $accessError ) ) {
  1005.                 if( !empty( $accessError['access_control'] ) ) {
  1006.                     $res['post_url'] = BitBlogPost::getDisplayUrlFromHash( $res['content_id'], $res );
  1007.                     $res['display_url'] = $res['post_url'];
  1008.                     /* this needs to be part of loop that gets all blogs post is in
  1009.                     $res['blog_url'] = BitBlog::getDisplayUrlFromHash( $res['blog_content_id'] );
  1010.                     */
  1011.                     $res["parsed_data"] = $accessError['access_control'];
  1012.                     $ret[] = $res;
  1013.                 }
  1014.             } else {
  1015.             }
  1016.         }
  1017.  
  1018.         $pListHash["data"] = $ret;
  1019.  
  1020.         LibertyContent::postGetList( $pListHash );
  1021.  
  1022.         return $pListHash;
  1023.     }
  1024.  
  1025.     /**
  1026.      * alters the whereSql and bindVars to limit the posts returned based on the dates
  1027.      * expects the blog_psots table to be aliased as bp
  1028.      */
  1029.     public static function getDateRestrictions( $pListHash, &$whereSql, &$bindVars ) {
  1030.         global $gBitSystem, $gBitUser;
  1031.  
  1032.         $now = $gBitSystem->getUTCTime();
  1033.         if( !empty( $pListHash['show_future'] ) && !empty( $pListHash['show_expired'] ) && $gBitUser->hasPermission( 'p_blog_posts_read_future' ) && $gBitUser->hasPermission( 'p_blog_posts_read_expired' ) ) {
  1034.         // this will show all post at once - future, current and expired
  1035.         } elseif( !empty( $pListHash['show_future'] ) && $gBitUser->hasPermission( 'p_blog_posts_read_future' ) ) {
  1036.             // hide expired posts but show future
  1037.             $whereSql .= " AND ( bp.`expire_date` <= bp.`publish_date` OR bp.`expire_date` > ? ) ";
  1038.             $bindVars[] = ( int )$now;
  1039.         } elseif( !empty( $pListHash['show_expired'] ) && $gBitUser->hasPermission( 'p_blog_posts_read_expired' ) ) {
  1040.             // hide future posts but show expired
  1041.             $whereSql .= " AND bp.`publish_date` < ?";
  1042.             $bindVars[] = ( int )$now;
  1043.         } elseif( !empty( $pListHash['get_future'] ) && $gBitUser->hasPermission( 'p_blog_posts_read_future' ) ) {
  1044.             // show only future
  1045.             $whereSql .= " AND bp.`publish_date` > ?";
  1046.             $bindVars[] = ( int )$now;
  1047.         } elseif( !empty( $pListHash['get_expired'] ) && $gBitUser->hasPermission( 'p_blog_posts_read_expired' ) ) {
  1048.             // show only expired posts
  1049.             $whereSql .= " AND bp.`expire_date` < ? AND bp.`expire_date` > bp.`publish_date` ";
  1050.             $bindVars[] = ( int )$now;
  1051.         } else {
  1052.             // hide future and expired posts
  1053.             $whereSql .= " AND ((bp.`publish_date` IS NULL AND bp.`expire_date` IS NULL) OR (bp.`publish_date` <= ? AND ((bp.`expire_date` IS NULL) OR ( bp.`expire_date` <= bp.`publish_date` ) OR ( bp.`expire_date` > ? ))))";
  1054.             $bindVars[] = ( int )$now;
  1055.             $bindVars[] = ( int )$now;
  1056.         }
  1057.     }
  1058.  
  1059.     /**
  1060.      * Get a list of posts that are to be published in the future
  1061.      *
  1062.      * @param array $pParamHash contains listing options - same as getList()
  1063.      * @access public
  1064.      * @return array of posts
  1065.      */
  1066.     function getFutureList( &$pParamHash ) {
  1067.         $pParamHash['get_future'] = TRUE;
  1068.         return( $this->getList( $pParamHash ));
  1069.     }
  1070.  
  1071.  
  1072.     /**
  1073.      * Get list of posts that have expired and are not displayed on the site anymore
  1074.      *
  1075.      * @param array $pParamHash contains listing options - same as getList()
  1076.      * @access public
  1077.      * @return array of posts
  1078.      */
  1079.     function getExpiredList( &$pParamHash ) {
  1080.         $pParamHash['get_expired'] = TRUE;
  1081.         return( $this->getList( $pParamHash ));
  1082.     }
  1083.  
  1084.  
  1085.     /**
  1086.      *
  1087.      */
  1088.     function addTrackbackFrom( $url, $title = '', $excerpt = '', $blog_name = '') {
  1089.         if( $this->isValid() ) {
  1090.             $tbs = $this->getTrackbacksFrom( $this->mPostId );
  1091.             $aux = array(
  1092.                 'title' => $title,
  1093.                 'excerpt' => $excerpt,
  1094.                 'blog_name' => $blog_name
  1095.             );
  1096.  
  1097.             $tbs[$url] = $aux;
  1098.             $st = serialize($tbs);
  1099.             $query = "update `".BIT_DB_PREFIX."blog_posts` set `trackbacks_from`=? where `post_id`=?";
  1100.             $this->mDb->query( $query, array( $st, $this->mPostId ) );
  1101.             return true;
  1102.         }
  1103.     }
  1104.  
  1105.     /**
  1106.      *
  1107.      */
  1108.     function getTrackbacksFrom() {
  1109.         if( $this->isValid() ) {
  1110.             $st = $this->mDb->getOne("select `trackbacks_from` from `".BIT_DB_PREFIX."blog_posts` where `post_id`=?",array( $this->mPostId ) );
  1111.             return unserialize($st);
  1112.         }
  1113.     }
  1114.  
  1115.     /**
  1116.      *
  1117.      */
  1118.     function getTrackbacksTo() {
  1119.         if( $this->isValid() ) {
  1120.             $st = $this->mDb->getOne("select `trackbacks_to` from `".BIT_DB_PREFIX."blog_posts` where `post_id`=?", array( $this->mPostId ) );
  1121.             return unserialize($st);
  1122.         }
  1123.     }
  1124.  
  1125.     /**
  1126.      *
  1127.      */
  1128.     function clearTrackbacksFrom() {
  1129.         if( $this->isValid() ) {
  1130.             $empty = serialize(array());
  1131.             $query = "update `".BIT_DB_PREFIX."blog_posts` set `trackbacks_from` = ? where `post_id`=?";
  1132.             $this->mDb->query( $query, array( $empty, $this->mPostId ) );
  1133.         }
  1134.     }
  1135.  
  1136.     /**
  1137.      *
  1138.      */
  1139.     function clearTrackbacksTo() {
  1140.          if( $this->isValid() ) {
  1141.              $empty = serialize(array());
  1142.             $query = "update `".BIT_DB_PREFIX."blog_posts` set `trackbacks_to` = ? where `post_id`=?";
  1143.             $this->mDb->query( $query, array( $empty, $this->mPostId ) );
  1144.         }
  1145.     }
  1146.  
  1147.     function getViewTemplate( $pAction ){
  1148.         $ret = null;
  1149.         switch ( $pAction ){
  1150.             case "view":
  1151.                 $ret = "bitpackage:liberty/center_".$pAction."_generic.tpl";
  1152.                 break;
  1153.             case "list":
  1154.                 $ret = "bitpackage:blogs/center_".$pAction."_blog_posts.tpl";
  1155.                 break;
  1156.         }
  1157.         return $ret;
  1158.     }
  1159.  
  1160.     /**
  1161.      * getContentStatus
  1162.      *
  1163.      * @access public
  1164.      * @return an array of content_status_id, content_status_names the current
  1165.      *  user can use on this content.
  1166.      *
  1167.      *  NOTE: pUserMinimum and pUserMaximum are currently NOT inclusive in parent funtion, so these are one beyond the limit we desire
  1168.      */
  1169.     function getAvailableContentStatuses( $pUserMinimum=-6, $pUserMaximum=51 ) {
  1170.         global $gBitUser;
  1171.         $ret = LibertyMime::getAvailableContentStatuses( $pUserMinimum, $pUserMaximum );
  1172.         // this is a little ugly as we manually trim the list to just what we need for blog posts for regular users
  1173.         if( !$gBitUser->hasPermission( 'p_liberty_edit_all_status' )) {
  1174.             if ( array_key_exists( -1, $ret ) ){
  1175.                 unset( $ret[-1] );
  1176.             }
  1177.             if ( array_key_exists( 50, $ret ) && $ret[50]=="Available" ){
  1178.                 $ret[50] = "Public";
  1179.             }
  1180.         }
  1181.         return $ret;
  1182.     }
  1183.  
  1184.     /**
  1185.     * Returns the create/edit url to a blog post
  1186.     * @param number $pContentId a valid content id
  1187.     * @param array $pMixed a hash of params to add to the url
  1188.     */
  1189.     function getEditUrl( $pContentId = NULL, $pMixed = NULL ){
  1190.         if( @BitBase::verifyId( $pContentId ) ) {
  1191.             $ret = BLOGS_PKG_URL.'post.php?content_id='.$pContentId;
  1192.         } elseif( $this->isValid() ) {
  1193.             $ret = BLOGS_PKG_URL.'post.php?content_id='.$this->mContentId;
  1194.         } else {
  1195.             $ret = BLOGS_PKG_URL.'post.php'.(!empty( $pMixed )?"?":"");
  1196.         }
  1197.         foreach( $pMixed as $key => $value ){
  1198.             if( $key != "content_id" || ( $key == "content_id" && @BitBase::verifyId( $value ) ) ) {
  1199.                 $ret .= (isset($amp)?"&":"").$key."=".$value;
  1200.             }
  1201.             $amp = TRUE;
  1202.         }
  1203.         return $ret;
  1204.     }
  1205.  
  1206. }
  1207. ?>

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