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

Source for file BitBlog.php

Documentation is available at BitBlog.php

  1. <?php
  2. /**
  3.  * @version $Header$
  4.  * @version  $Revision$
  5.  * @package blogs
  6.  */
  7.  
  8. /**
  9.  * required setup
  10.  */
  11. require_once( BLOGS_PKG_PATH.'BitBlogPost.php');
  12. require_once( LIBERTY_PKG_PATH.'LibertyComment.php');
  13.  
  14. define( 'BITBLOG_CONTENT_TYPE_GUID', 'bitblog' );
  15.  
  16. /**
  17.  * @package blogs
  18.  */
  19. class BitBlog extends LibertyMime {
  20.     var $mBlogId;
  21.  
  22.     function BitBlog( $pBlogId=NULL, $pContentId=NULL ) {
  23.         $this->mBlogId = @$this->verifyId( $pBlogId ) ? $pBlogId : NULL;
  24.         parent::__construct( $pContentId );
  25.         $this->registerContentType( BITBLOG_CONTENT_TYPE_GUID, array(
  26.             'content_type_guid' => BITBLOG_CONTENT_TYPE_GUID,
  27.             'content_name' => 'Blog',
  28.             'handler_class' => 'BitBlog',
  29.             'handler_package' => 'blogs',
  30.             'handler_file' => 'BitBlog.php',
  31.             'maintainer_url' => 'http://www.bitweaver.org'
  32.         ) );
  33.         $this->mContentId = $pContentId;
  34.         $this->mContentTypeGuid = BITBLOG_CONTENT_TYPE_GUID;
  35.  
  36.         // Permission setup
  37.         $this->mViewContentPerm  = 'p_blogs_view';
  38.         $this->mUpdateContentPerm  = 'p_blogs_update';
  39.         $this->mCreateContentPerm  = 'p_blogs_create';
  40.         $this->mAdminContentPerm = 'p_blogs_admin';
  41.     }
  42.  
  43.     function get_num_user_blogs($user_id) {
  44.         $ret = NULL;
  45.         if ($user_id) {
  46.             $sql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."blogs` WHERE `user_id` = ?";
  47.             $ret = $this->mDb->getOne($sql, array( $user_id ));
  48.         }
  49.         return $ret;
  50.     }
  51.  
  52.     public static function getDisplayUrlFromHash( &$pParamHash ) {
  53.         global $gBitSystem;
  54.         $ret = NULL;
  55.  
  56.         if ( BitBase::verifyId( $pParamHash['blog_id'] ) ) {
  57.             if( $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ) {
  58.                 $ret = BLOGS_PKG_URL.'view/'.$pParamHash['blog_id'];
  59.             } elseif( $gBitSystem->isFeatureActive( 'pretty_urls' ) ) {
  60.                 $ret = BLOGS_PKG_URL.$pParamHash['blog_id'];
  61.             } else {
  62.                 $ret = BLOGS_PKG_URL.'view.php?blog_id='.$pParamHash['blog_id'];
  63.             }
  64.         } else {
  65.             $ret = parent::getDisplayUrlFromHash( $pParamHash );
  66.         }
  67.         return $ret;
  68.     }
  69.  
  70.     function getDisplayUrl() {
  71.         $ret = NULL;
  72.         if( $this->isValid() ) {
  73.             $hash = array ( 'blog_id' => $this->mBlogId );
  74.             $ret = self::getDisplayUrlFromHash( $hash );
  75.         }
  76.         return $ret;
  77.     }
  78.  
  79.     /**
  80.     * Check if there is an article loaded
  81.     * @return bool TRUE on success, FALSE on failure
  82.     * @access public
  83.     ***/
  84.     function isValid() {
  85.         return( $this->verifyId( $this->mBlogId ) && $this->verifyId( $this->mContentId ) );
  86.     }
  87.  
  88.     function load( $pContentId = NULL, $pPluginParams = NULL ) {
  89.         if ( $this->getBlog( $this->mBlogId, $this->mContentId ) ) {
  90.             $this->mContentId = $this->getField( 'content_id' );
  91.             $this->mBlogId = $this->getField('blog_id');
  92.         }
  93.     }
  94.  
  95.  
  96.     /*shared*/
  97.     function getBlog( $pBlogId, $pContentId = NULL ) {
  98.         global $gBitSystem;
  99.         $ret = NULL;
  100.  
  101.         $lookupId = (!empty( $pBlogId ) ? $pBlogId : $pContentId);
  102.         $lookupColumn = (!empty( $pBlogId ) ? 'blog_id' : 'content_id');
  103.  
  104.         $bindVars = array( (int)$lookupId );
  105.         $selectSql = ''; $joinSql = ''; $whereSql = '';
  106.         $this->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  107.  
  108.         if ( BitBase::verifyId( $lookupId ) ) {
  109.             $query = "
  110.                 SELECT b.*, lc.*, lch.`hits`, uu.`login`, uu.`login`, uu.`user_id`, uu.`real_name`,
  111.                     lfa.`file_name` as `avatar_file_name`, lfa.`mime_type` AS `avatar_mime_type`, laa.`attachment_id` AS `avatar_attachment_id`,
  112.                     lfp.`file_name` AS `image_file_name`, lfp.`mime_type` AS `image_mime_type`, lap.`attachment_id` AS `image_attachment_id`
  113.                     $selectSql
  114.                 FROM `".BIT_DB_PREFIX."blogs` b
  115.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id` = b.`content_id`)
  116.                     INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id` = lc.`user_id`)
  117.                     $joinSql
  118.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON (lc.`content_id` = lch.`content_id`)
  119.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments`    laa ON (uu.`user_id` = laa.`user_id` AND laa.`attachment_id` = uu.`avatar_attachment_id`)
  120.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files`        lfa ON lfa.`file_id`           = laa.`foreign_id`
  121.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments`  lap ON lap.`content_id`        = lc.`content_id` AND lap.`is_primary` = 'y'
  122.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files`        lfp ON lfp.`file_id`           = lap.`foreign_id`
  123.                 WHERE b.`$lookupColumn`= ? $whereSql";
  124.  
  125.             if( $this->mInfo = $this->mDb->getRow($query,$bindVars) ) {
  126.                 $this->mContentId = $this->getField( 'content_id' );
  127.                 $this->mBlogId = $this->getField('blog_id');
  128.                 foreach( array( 'avatar', 'image' ) as $img ) {
  129.                     $this->mInfo[$img] = liberty_fetch_thumbnails( array(
  130.                         'source_file' => $this->getSourceFile( array( 'user_id'=>$this->getField( 'user_id' ), 'package'=>liberty_mime_get_storage_sub_dir_name( array( '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' ) ) )
  131.                     ));
  132.                 }
  133.                 parent::load();
  134.                 $this->mInfo['postscant'] = $this->getPostsCount( $this->mContentId );
  135.             }
  136.         }
  137.         return count( $this->mInfo ) != 0;
  138.     }
  139.  
  140.     function verify( &$pParamHash ) {
  141.         global $gBitUser;
  142.  
  143.         $pParamHash['blog_store']['max_posts'] = !empty( $pParamHash['max_posts'] ) && is_numeric( $pParamHash['max_posts'] ) ? $pParamHash['max_posts'] : NULL;
  144.         $pParamHash['blog_store']['use_title'] = isset( $pParamHash['use_title'] ) ? 'y' : 'n';
  145.         $pParamHash['blog_store']['allow_comments'] = isset( $pParamHash['allow_comments'] ) ? 'y' : 'n';
  146.         $pParamHash['blog_store']['use_find'] = isset( $pParamHash['use_find'] ) ? 'y' : 'n';
  147.  
  148.         // if we have an error we get them all by checking parent classes for additional errors
  149.         if( count( $this->mErrors ) > 0 ){
  150.             parent::verify( $pParamHash );
  151.         }
  152.  
  153.         return( count( $this->mErrors ) == 0 );
  154.     }
  155.  
  156.     function store( &$pParamHash ) {
  157.         global $gBitSystem;
  158.         $this->mDb->StartTrans();
  159.         if( $this->verify( $pParamHash ) && parent::store( $pParamHash ) ) {
  160.             $table = BIT_DB_PREFIX."blogs";
  161.             if( $this->isValid() ) {
  162.                 $result = $this->mDb->associateUpdate( $table, $pParamHash['blog_store'], array( "blog_id" => $pParamHash['blog_id'] ) );
  163.             } else {
  164.                 // DEPRECATED - this looks stupid -wjames5
  165.                 //$pParamHash['blog_store']['posts'] = 0;
  166.                 $pParamHash['blog_store']['content_id'] = $this->mContentId;
  167.                 if( isset( $pParamHash['blog_id'] )&& is_numeric( $pParamHash['blog_id'] ) ) {
  168.                     // if pParamHash['blog_id'] is set, someone is requesting a particular blog_id. Use with caution!
  169.                     $pParamHash['blog_store']['blog_id'] = $pParamHash['blog_id'];
  170.                 } else {
  171.                     $pParamHash['blog_store']['blog_id'] = $this->mDb->GenID( 'blogs_blog_id_seq' );
  172.                 }
  173.                 $this->mBlogId = $pParamHash['blog_store']['blog_id'];
  174.                 $result = $this->mDb->associateInsert( $table, $pParamHash['blog_store'] );
  175.             }
  176.             $this->mDb->CompleteTrans();
  177.         }
  178.         return( count( $this->mErrors ) == 0 );
  179.     }
  180.  
  181.     function expunge() {
  182.         $ret = FALSE;
  183.         if ( $this->isValid() ) {
  184.             $this->mDb->StartTrans();
  185.  
  186.             // remove all references in blogs_posts_map where post_content_id = content_id
  187.             $query_map = "DELETE FROM `".BIT_DB_PREFIX."blogs_posts_map` WHERE `blog_content_id` = ?";
  188.             $result = $this->mDb->query( $query_map, array( $this->mContentId ) );
  189.  
  190.             $query = "DELETE from `".BIT_DB_PREFIX."blogs` where `content_id`=?";
  191.             $result = $this->mDb->query( $query, array( (int)$this->mContentId ) );
  192.  
  193.             if( parent::expunge() ) {
  194.                 $ret = TRUE;
  195.                 $this->mDb->CompleteTrans();
  196.             } else {
  197.                 $this->mDb->RollbackTrans();
  198.             }
  199.             $this->mDb->CompleteTrans();
  200.         }
  201.         return $ret;
  202.     }
  203.  
  204.     function getPost( $pListHash=array() ) {
  205.         $ret = NULL;
  206.         $bindVars = array();
  207.  
  208.         $blogId = (!empty( $pListHash['blog_id'] ) ? $pListHash['blog_id'] : $this->mBlogId);
  209.  
  210.         if ( BitBase::verifyId( $blogId ) ) {
  211.             $this->prepGetList( $pListHash );
  212.             $sql = "SELECT bp.`post_id`
  213.                     FROM `".BIT_DB_PREFIX."blog_posts` bp
  214.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id`=bp.`content_id`)
  215.                         INNER JOIN `".BIT_DB_PREFIX."blogs_posts_map` bpm ON (bp.`content_id`=bpm.`post_content_id`)
  216.                         INNER JOIN `".BIT_DB_PREFIX."blogs` b on (bpm.`blog_content_id`=b.`content_id`)
  217.                     WHERE b.`blog_id` = ? ORDER BY ".$this->mDb->convertSortMode( $pListHash['sort_mode'] );
  218.             if( $postId = $this->mDb->getOne($sql, array( $blogId ) ) ) {
  219.                 $blogPost = new BitBlogPost( $postId );
  220.                 $blogPost->load( NULL, $pListHash );
  221.                 $ret = $blogPost;
  222.             }
  223.         }
  224.         return $ret;
  225.     }
  226.  
  227.     // BLOG METHODS ////
  228.     function getList( &$pParamHash ) {
  229.         global $gBitSystem;
  230.  
  231.         LibertyContent::prepGetList( $pParamHash );
  232.  
  233.         $selectSql = ''; $joinSql = ''; $whereSql = '';
  234.         $bindVars = array();
  235. //        array_push( $bindVars, $this->mContentTypeGuid );
  236.         $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  237.  
  238.         // You can use a title or an array of blog_id
  239.         if (!empty($pParamHash['find'])) {
  240.             if (is_array($pParamHash['find'])) {
  241.                 $whereSql .= " AND b.`blog_id` IN ( ".implode( ',',array_fill( 0,count( $pParamHash['find'] ),'?' ) ).") ";
  242.                 $bindVars = array_merge($bindVars, $pParamHash['find']);
  243.             }
  244.             else {
  245.                 $findesc = '%' . strtoupper( $pParamHash['find'] ) . '%';
  246.                 $whereSql = " AND (UPPER(lc.`title`) like ? or UPPER(lc.`data`) like ?) ";
  247.                 $bindVars=array($findesc,$findesc);
  248.             }
  249.         }
  250.         if( @$this->verifyId( $pParamHash['user_id'] ) ) {
  251.             $whereSql .= " AND uu.`user_id` = ? ";
  252.             $bindVars[] = $pParamHash['user_id'];
  253.         }
  254.  
  255.         $this->getServicesSql( 'content_user_collection_function', $selectSql, $joinSql, $whereSql, $bindVars, $this, $pListHash );
  256.  
  257.         if( !empty( $pParamHash['is_active'] ) ) {
  258.             $whereSql .= " AND b.`activity` IS NOT NULL";
  259.         }
  260.  
  261.         if( !empty( $pParamHash['is_hit'] ) ) {
  262.             $whereSql .= " AND lch.`hits` IS NOT NULL";
  263.         }
  264.  
  265.         if( !empty( $pParamHash['content_perm_name'] ) ) {
  266.             $this->getContentPermissionsSql( $pParamHash['content_perm_name'], $selectSql, $joinSql, $whereSql, $bindVars );
  267.         }
  268.  
  269.         if( !empty( $whereSql ) ) {
  270.             $whereSql = preg_replace( '/^[\s]*AND/', ' WHERE ', $whereSql );
  271.         }
  272.  
  273.  
  274.         $ret = array();
  275.  
  276.         // Return a data array, even if empty
  277.         $pParamHash["data"] = array();
  278.  
  279.         # Get count of total number of items available
  280.         $query_cant = "
  281.             SELECT COUNT(b.`blog_id`)
  282.                 FROM `".BIT_DB_PREFIX."blogs` b
  283.                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id` = b.`content_id`)
  284.                 INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id` = lc.`user_id`)
  285.                 $joinSql
  286.             $whereSql";
  287.         $pParamHash["cant"] = $this->mDb->getOne( $query_cant, $bindVars );
  288.  
  289.         # Check for offset out of range
  290.         if ( $pParamHash['offset'] < 0 ) {
  291.             $pParamHash['offset'] = 0;
  292.             }
  293.         elseif ( $pParamHash['offset']    > $pParamHash["cant"] ) {
  294.             $lastPageNumber = ceil ( $pParamHash["cant"] / $pParamHash['max_records'] ) - 1;
  295.             $pParamHash['offset'] = $pParamHash['max_records'] * $lastPageNumber;
  296.             }
  297.  
  298.         $query = "
  299.             SELECT b.`content_id` AS `hash_key`,
  300.                 b.`blog_id`, b.`is_public`, b.`max_posts`, b.`activity`, b.`use_find`, b.`use_title`,
  301.                 b.`add_date`, b.`add_poster`, b.`allow_comments`,
  302.                 uu.`login`,    uu.`real_name`, lc.*, lch.hits $selectSql
  303.             FROM `".BIT_DB_PREFIX."blogs` b
  304.                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id` = b.`content_id`)
  305.                 INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id` = lc.`user_id`)
  306.                 $joinSql
  307.                 LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON (lc.`content_id` = lch.`content_id`)
  308.             $whereSql order by ".$this->mDb->convertSortmode($pParamHash['sort_mode']);
  309.  
  310.         $result = $this->mDb->query( $query, $bindVars, $pParamHash['max_records'], $pParamHash['offset'] );
  311.         $ret = array ();
  312.         while ($res = $result->fetchRow()) {
  313.             $blogContentId = $res['content_id'];
  314.             $ret[$blogContentId] = $res;
  315.             $ret[$blogContentId]['blog_url'] = $this->getDisplayUrl( $res['blog_id'] );
  316.             //get count of post in each blog
  317.             $ret[$blogContentId]['postscant'] = $this->getPostsCount( $res['content_id'] );
  318.             // deal with the parsing
  319.             $parseHash['format_guid']   = $res['format_guid'];
  320.             $parseHash['content_id']    = $res['content_id'];
  321.             $parseHash['data']     = $res['data'];
  322.             $ret[$blogContentId]['parsed'] = $this->parseData( $parseHash );
  323.         }
  324.  
  325.         LibertyContent::postGetList( $pParamHash );
  326.  
  327.         return $ret;
  328.     }
  329.  
  330.     function getPostsCount($pBlogContentId){
  331.         global $gBitSystem;
  332.         $ret = NULL;
  333.         if( @$this->verifyId( $pBlogContentId ) ) {
  334.             $whereSql = 'bpm.`blog_content_id` = ?';
  335.             $bindVars = array((int)$pBlogContentId);
  336.             BitBlogPost::getDateRestrictions(array(), $whereSql, $bindVars);
  337.             $query = "SELECT COUNT(*)
  338.                 FROM `".BIT_DB_PREFIX."blogs_posts_map` bpm
  339.                 INNER JOIN `".BIT_DB_PREFIX."blog_posts` bp ON (bpm.`post_content_id`=bp.`content_id`)
  340.                 WHERE $whereSql";
  341.  
  342.             $ret = $this->mDb->getOne( $query, $bindVars );
  343.         } else {
  344.             $this->mErrors['content_id'] = "Invalid blog content id.";
  345.         }
  346.         return $ret;
  347.     }
  348.  
  349.     //This doesnt even appear to be used in blogs before this refactoring -wjames5
  350.     function viewerCanPostIntoBlog() {
  351.         global $gBitUser;
  352.         return ($this->getField('user_id') == $gBitUser->mUserId || $gBitUser->isAdmin() || $this->getField('is_public') == 'y' );
  353.     }
  354.  
  355.     function hasPostPermission() {
  356.         $ret = FALSE;
  357.         if( $this->isValid() ) {
  358.             // for now just check edit permission, however eventually we'll want to separate this notion so blog editors and posters can be distinguished
  359.             $ret = $this->hasUpdatePermission();
  360.         }
  361.         return $ret;
  362.     }
  363.  
  364.     function viewerHasPermission($pPermName = NULL) {
  365.         global $gBitUser;
  366.         $ret = FALSE;
  367.         if ($gBitUser->mUserId && $pPermName) {
  368.             $ret = $gBitUser->object_has_permission( $gBitUser->mUserId, $this->mInfo['blog_id'], $this->getContentType(), $pPermName );
  369.         }
  370.         return $ret;
  371.     }
  372.  
  373.     function getViewTemplate( $pAction ){
  374.         $ret = null;
  375.         switch ( $pAction ){
  376.             case "view":
  377.                 $ret = "bitpackage:blogs/center_".$pAction."_blog_posts.tpl";
  378.                 break;
  379.             case "list":
  380.                 $ret = "bitpackage:liberty/center_".$pAction."_generic.tpl";
  381.                 break;
  382.         }
  383.         return $ret;
  384.     }
  385.  
  386.     /**
  387.      * getContentStatus
  388.      *
  389.      * @access public
  390.      * @return an array of content_status_id, content_status_names the current
  391.      *  user can use on this content.
  392.      */
  393.     function getAvailableContentStatuses( $pUserMinimum=-100, $pUserMaximum=100 ) {
  394.         global $gBitUser;
  395.         $ret = NULL;
  396.          // return NULL for all but admins
  397.         if( $gBitUser->hasPermission( 'p_liberty_edit_all_status' )) {
  398.             $ret = LibertyMime::getAvailableContentStatuses();
  399.         }
  400.         return $ret;
  401.     }
  402. }
  403.  
  404. function blogs_module_display(&$pParamHash){
  405.     global $gBitThemes, $gBitSmarty, $gBitSystem;
  406.     if( $gBitThemes->isModuleLoaded( 'bitpackage:blogs/center_list_blog_posts.tpl', 'c' ) && $gBitSystem->isFeatureActive( 'blog_ajax_more' ) && $gBitThemes->isJavascriptEnabled() ) {
  407.         $gBitSmarty->assign( 'ajax_more', TRUE );
  408.         $gBitThemes->loadAjax( 'mochikit', array( 'Iter.js', 'DOM.js', 'Style.js', 'Color.js', 'Position.js', 'Visual.js' ));
  409.     }
  410. }
  411. ?>

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