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

Source for file BitArticleTopic.php

Documentation is available at BitArticleTopic.php

  1. <?php
  2. /**
  3.  * @version $Header$
  4.  * @package articles
  5.  *
  6.  * @copyright Copyright (c) 2004-2006, bitweaver.org
  7.  *  All Rights Reserved. See below for details and a complete list of authors.
  8.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details.
  9.  */
  10.  
  11. /**
  12.  * Required setup
  13.  */
  14. global $gBitSystem;
  15. require_once( KERNEL_PKG_PATH."BitBase.php" );
  16. require_once( ARTICLES_PKG_PATH.'BitArticle.php' );
  17.  
  18. /**
  19.  * @package articles
  20.  */
  21. class BitArticleTopic extends BitBase
  22. {
  23.     public $mTopicId;
  24.  
  25.     public function __construct($iTopicId = NULL, $iTopicName = NULL)
  26.     {
  27.         $this->mTopicId = NULL;
  28.         parent::__construct();
  29.         if ($iTopicId || $iTopicName) {
  30.             $this->loadTopic(array('topic_id'=>$iTopicId, 'topic_name'=>$iTopicName));
  31.         }
  32.     }
  33.  
  34.     public function isValid()
  35.     {
  36.         return ($this->verifyId($this->mTopicId));
  37.     }
  38.  
  39.     public function loadTopic($iParamHash = NULL)
  40.     {
  41.         $whereSQL = ' WHERE artt.';
  42.         $ret = NULL;
  43.  
  44.         if (@$this->verifyId($iParamHash['topic_id']) || !empty($iParamHash['topic_name'])) {
  45.             $whereSQL .= "`".((@$this->verifyId($iParamHash['topic_id']) || $this->mTopicId) ? 'topic_id' : 'topic_name')."` = ?";
  46.             $bindVars = array((@$this->verifyId($iParamHash['topic_id']) ? (int) $iParamHash['topic_id'] : ($this->mTopicId ? $this->mTopicId : $iParamHash['topic_name'])) );
  47.  
  48.             $sql = "SELECT artt.*".
  49.                    "FROM `".BIT_DB_PREFIX."article_topics` artt ".
  50.                       $whereSQL;
  51.             $this->mInfo = $this->mDb->getRow($sql, $bindVars);
  52.  
  53.             if( !empty( $this->mInfo['topic_id'] ) ) {
  54.                 $this->mTopicId = $this->mInfo['topic_id'];
  55.  
  56.                 if ($this->mInfo['has_topic_image']) {
  57.                     $this->mInfo['topic_image_url'] = $this->getTopicImageStorageUrl(NULL, FALSE, TRUE);
  58.                 } else {
  59.                     $this->mInfo['topic_image_url'] = NULL;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         return $ret;
  65.     }
  66.  
  67.     public function verify(&$iParamHash)
  68.     {
  69.         // Validate the (optional) topic_id parameter
  70.         if (@$this->verifyId($iParamHash['topic_id'])) {
  71.             $cleanHash['topic_id'] = (int) $iParamHash['topic_id'];
  72.         } else {
  73.             $cleanHash['topic_id'] = NULL;
  74.         }
  75.  
  76.         // Was an acceptable name given?
  77.         if (empty($iParamHash['topic_name']) || ($iParamHash['topic_name'] == '')) {
  78.             $this->mErrors['topic_name'] = tra("Invalid or blank topic name supplied");
  79.         } elseif (empty($iParamHash['topic_id'])) {
  80.             $ret = $this->getTopicList( array( 'topic_name' => $iParamHash['topic_name'] ) );
  81.             if ( sizeof( $ret ) ) {
  82.                 $this->mErrors['topic_name'] = 'Topic "'.$iParamHash['topic_name'].'" already exists. Please choose a different name.';
  83.             } else {
  84.                 $cleanHash['topic_name'] = $iParamHash['topic_name'];
  85.             }
  86.         } else {
  87.             $cleanHash['topic_name'] = $iParamHash['topic_name'];
  88.         }
  89.  
  90.         // Whether the topic is active or not
  91.         if ( empty($iParamHash['active_topic']) || (strtoupper($iParamHash['active_topic']) != 'CHECKED' && strtoupper($iParamHash['active_topic']) != 'ON' && strtoupper($iParamHash['active_topic']) != 'Y')) {
  92.             if (@$this->verifyId($cleanHash['topic_id'])) {
  93.                 $cleanHash['active_topic'] = 'n';
  94.             } else {
  95.                 // Probably a new topic so lets go ahead and enable it
  96.                 $cleanHash['active_topic'] = 'y';
  97.             }
  98.         } else {
  99.             $cleanHash['active_topic'] = 'y';
  100.         }
  101.  
  102.         if (empty($iParamHash['created'])) {
  103.             global $gBitSystem;
  104.             $cleanHash['created'] = $gBitSystem->getUTCTime();
  105.         }
  106.  
  107.         $iParamHash = $cleanHash;
  108.  
  109.         return(count($this->mErrors) == 0);
  110.     }
  111.  
  112.     public function storeTopic($iParamHash = NULL)
  113.     {
  114.         global $gLibertySystem;
  115.         global $gBitUser;
  116.  
  117.         if ($this->verify($iParamHash)) {
  118.             if (!$iParamHash['topic_id']) {
  119.                 $topicId = $this->mDb->GenID('article_topics_id_seq');
  120.             } else {
  121.                 $topicId = $this->mTopicId;
  122.             }
  123.  
  124.             if( !empty( $_FILES['upload'] ) && $_FILES['upload']['tmp_name'] ) {
  125.                 $checkFunc = liberty_get_function( 'can_thumbnail' );
  126.                 if( $checkFunc( $_FILES['upload']['type'] )) {
  127.                     $fileHash = $_FILES['upload'];
  128.                     $fileHash['dest_branch'] = $this->getTopicImageBaseUrl( $topicId );
  129.                     $fileHash['source_file'] = $fileHash['tmp_name'];
  130.                     liberty_clear_thumbnails( $fileHash );
  131.                     liberty_generate_thumbnails( $fileHash );
  132.                     $iParamHash['has_topic_image'] = 'y';
  133.                 } else {
  134.                     $this->mErrors = tra( "The file you uploaded doesn't appear to be a valid image. The reported mime type is" ).": ".$_FILES['upload']['type'];
  135.                 }
  136.             }
  137.  
  138.             if( $iParamHash['topic_id'] ) {
  139.                 $this->mDb->associateUpdate( BIT_DB_PREFIX."article_topics", $iParamHash, array( 'topic_id' => $iParamHash['topic_id'] ) );
  140.             } else {
  141.                 $iParamHash['topic_id'] = $topicId;
  142.                 $this->mDb->associateInsert( BIT_DB_PREFIX."article_topics", $iParamHash );
  143.             }
  144.         }
  145.         $this->mTopicId = $iParamHash['topic_id'];
  146.     }
  147.  
  148.     /**
  149.     * Work out the path to the image for this article
  150.     * @param $pTopicId id of the article we need the image path for
  151.     * @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
  152.     * @return path on success, FALSE on failure
  153.     * @access public
  154.     ***/
  155.     public function getTopicImageBaseUrl($pTopicId = NULL)
  156.     {
  157.         $ret = FALSE;
  158.         if ( !@BitBase::verifyId( $pTopicId ) && $this->isValid() ) {
  159.             $pTopicId = $this->mTopicId;
  160.         }
  161.  
  162.         if( @BitBase::verifyId( $pTopicId )) {
  163.             $ret = LibertyMime::getStorageUrl( 'topics/'.$pTopicId );
  164.         }
  165.         return $ret;
  166.     }
  167.  
  168.     /**
  169.      * Get the full URL to the needed thumbnail
  170.      *
  171.      * @param numeric $pTopicId Topic ID of topic in question
  172.      * @access public
  173.      * @return Path to thumbnail, FALSE on failure
  174.      */
  175.     public function getTopicImageThumbUrl($pTopicId = NULL)
  176.     {
  177.         global $gBitSystem;
  178.         $ret = FALSE;
  179.         if ( !@BitBase::verifyId( $pTopicId ) && $this->isValid() ) {
  180.             $pTopicId = $this->mTopicId;
  181.         }
  182.  
  183.         if ( @BitBase::verifyId( $pTopicId )) {
  184.             $ret = liberty_fetch_thumbnail_url( array(
  185.                 'source_file'   => BitArticleTopic::getTopicImageBaseUrl( $pTopicId ),
  186.                 'default_image' => $gBitSystem->getConfig( 'articles_image_size', 'small' )
  187.             ));
  188.         }
  189.         return $ret;
  190.     }
  191.  
  192.     public static function getTopicList($pOptionHash=NULL)
  193.     {
  194.         global $gBitSystem;
  195.  
  196.         $where = '';
  197.         $bindVars = array();
  198.         if ( !empty( $pOptionHash['active_topic'] ) ) {
  199.             $where = " WHERE artt.`active_topic` = 'y' ";
  200.         }
  201.         if ( !empty(  $pOptionHash['topic_name'] ) ) {
  202.             $where = " WHERE artt.`topic_name` = ? ";
  203.             $bindVars[] = $pOptionHash['topic_name'];
  204.         }
  205.  
  206.         $query = "SELECT artt.*
  207.                  FROM `".BIT_DB_PREFIX."article_topics` artt
  208.                  $where ORDER BY artt.`topic_name`";
  209.  
  210.         $result = $gBitSystem->mDb->query( $query, $bindVars );
  211.  
  212.         $ret = array();
  213.  
  214.         while ( $res = $result->fetchRow() ) {
  215.             $res["num_articles"] = $gBitSystem->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."articles` WHERE `topic_id`= ?", array( $res["topic_id"] ) );
  216.             if ( empty( $res['topic_image_url'] ) && $res['has_topic_image'] == 'y' ) {
  217.                 $res['topic_image_url'] = BitArticleTopic::getTopicImageStorageUrl( $res['topic_id'] );
  218.             }
  219.  
  220.             $ret[] = $res;
  221.         }
  222.  
  223.         return $ret;
  224.     }
  225.  
  226.     public function removeTopicImage()
  227.     {
  228.         if ($this->mTopicId) {
  229.             if ( file_exists($this->getTopicImageStoragePath() ) ) {
  230.                 @unlink( $this->getTopicImageStoragePath() );
  231.             }
  232.             $sql = "UPDATE `".BIT_DB_PREFIX."article_topics` SET `has_topic_image` = 'n' WHERE `topic_id` = ?";
  233.             $rs = $this->mDb->query($sql, array($this->mTopicId));
  234.             $this->mInfo['has_topic_image'] = 'n';
  235.         }
  236.     }
  237.  
  238.     public function activateTopic()
  239.     {
  240.         $this->setActivation(TRUE);
  241.     }
  242.  
  243.     public function deactivateTopic()
  244.     {
  245.         $this->setActivation(FALSE);
  246.     }
  247.  
  248.     public function setActivation($iIsActive = FALSE)
  249.     {
  250.         $sql = "UPDATE `".BIT_DB_PREFIX."article_topics` SET `active_topic` = '".($iIsActive ? 'y' : 'n')."' WHERE `topic_id` = ?";
  251.         $rs = $this->mDb->query($sql, array($this->mTopicId));
  252.         $this->mInfo['active_topic'] = ($iIsActive ? 'y' : 'n');
  253.     }
  254.  
  255.     public function getTopicArticles()
  256.     {
  257.         if (!$this->mTopicId) {
  258.             return NULL;
  259.         }
  260.  
  261.         $sql = "SELECT `article_id` FROM `".BIT_DB_PREFIX."articles` WHERE `topic_id` = ?";
  262.         $rs = $this->mDb->query($sql, array($this->mTopicId));
  263.  
  264.         $ret = array();
  265.         while ($row = $rs->fetchRow()) {
  266.             $tmpArticle = new BitArticle($row['article_id']);
  267.             $tmpArticle->load();
  268.             $ret[] = $tmpArticle;
  269.         }
  270.     }
  271.  
  272.     public function removeTopic($iRemoveArticles = FALSE)
  273.     {
  274.         if (!$this->mTopicId) {
  275.             return NULL;
  276.         }
  277.  
  278.         $this->removeTopicImage();
  279.  
  280.         if ($iRemoveArticles == TRUE) {
  281.             $topicArticles = $this->getTopicArticles();
  282.             for ($articleCount = 0; $articleCount < count($topicArticles); $articleCount++) {
  283.                 $topicArticles[$articleCount]->expunge();
  284.             }
  285.         } else {
  286.             $sql = "UPDATE `".BIT_DB_PREFIX."articles` SET `topic_id` = ? WHERE `topic_id` = ?";
  287.             $rs = $this->mDb->query($sql, array(NULL, $this->mTopicId));
  288.         }
  289.  
  290.         $sql = "DELETE FROM `".BIT_DB_PREFIX."article_topics` WHERE `topic_id` = ?";
  291.         $rs = $this->mDb->query($sql, array($this->mTopicId));
  292.     }
  293.  
  294.  
  295.  
  296.  
  297.     /*****************************************************************************
  298.      * Image functions needed for backward compatability - these are needed to   *
  299.      * handle old article image style images that are not attachments. generally *
  300.      * these functions are deprecated but needed for legacy code                 *
  301.      ****************************************************************************/
  302.  
  303.     /**
  304.      * Get the name of the article image file
  305.      *
  306.      * @param array $pTopicId article id
  307.      * @access public
  308.      * @return TRUE on success, FALSE on failure
  309.      */
  310.     public function getTopicImageStorageName($pTopicId = NULL)
  311.     {
  312.         if ( !@BitBase::verifyId( $pTopicId ) ) {
  313.             if ( $this->isValid() ) {
  314.                 $pTopicId = $this->mTopicId;
  315.             } else {
  316.                 return NULL;
  317.             }
  318.         }
  319.  
  320.         global $gBitSystem;
  321.         return "topic_$pTopicId.".$gBitSystem->getConfig( 'liberty_thumbnail_format', 'jpg' );
  322.     }
  323.  
  324.     /**
  325.     * Work out the path to the image for this article
  326.     * @param $pTopicId id of the article we need the image path for
  327.     * @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
  328.     * @return path on success, FALSE on failure
  329.     * @access public
  330.     ***/
  331.     public function getTopicImageStoragePath($pTopicId = NULL, $pBasePathOnly = FALSE)
  332.     {
  333.         $path = BitArticleTopic::getArticleImageStoragePath( NULL, TRUE );
  334.  
  335.         if ($pBasePathOnly) {
  336.             return $path;
  337.         }
  338.  
  339.         if ( !@BitBase::verifyId( $pTopicId ) ) {
  340.             if( $this->isValid() ) {
  341.                 $pTopicId = $this->mTopicId;
  342.             } else {
  343.                 return NULL;
  344.             }
  345.         }
  346.  
  347.         if ( !empty( $pTopicId ) ) {
  348.             return $path.BitArticleTopic::getTopicImageStorageName( $pTopicId );
  349.         } else {
  350.             return FALSE;
  351.         }
  352.     }
  353.  
  354.     /**
  355.     * Work out the URL to the image for this article
  356.     * @param $pTopicId id of the article we need the image path for
  357.     * @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
  358.     * @return URL on success, FALSE on failure
  359.     * @access public
  360.     ***/
  361.     public function getTopicImageStorageUrl($pTopicId = NULL, $pBasePathOnly = FALSE, $pForceRefresh = FALSE)
  362.     {
  363.         global $gBitSystem;
  364.         $ret = FALSE;
  365.  
  366.         // first we check to see if this is a new type thumbnail. if that fails we'll use the old method
  367.         if ( !( $ret = BitArticleTopic::getTopicImageThumbUrl( $pTopicId ))) {
  368.             $url = BitArticleTopic::getArticleImageStorageUrl( NULL, TRUE );
  369.             if ($pBasePathOnly) {
  370.                 return $url;
  371.             }
  372.  
  373.             if ( !@BitBase::verifyId( $pTopicId ) ) {
  374.                 if ( $this->isValid() ) {
  375.                     $pTopicId = $this->mTopicId;
  376.                 } else {
  377.                     return NULL;
  378.                 }
  379.             }
  380.  
  381.             if ( is_file( BitArticleTopic::getTopicImageStoragePath( NULL, TRUE ).BitArticleTopic::getTopicImageStorageName( $pTopicId ))) {
  382.                 $ret = $url.BitArticleTopic::getTopicImageStorageName( $pTopicId ).( $pForceRefresh ? "?".$gBitSystem->getUTCTime() : '' );
  383.             }
  384.         }
  385.  
  386.         return str_replace( "//", "/", $ret );
  387.     }
  388.  
  389.  
  390.  
  391.  
  392.     /*****************************************************************************
  393.      * Image functions needed for backward compatability - these are needed to   *
  394.      * handle old article image style images that are not attachments. generally *
  395.      * these functions are deprecated but needed for legacy code                 *
  396.      *                                                                           *
  397.      * the legacy code below here should go at some point. this code is old and  *
  398.      * fugly. In fact, a lot of the code in here is fugly. we should use         *
  399.      * pigoenholes to do this topic thing, now that pigoenholes can have primary *
  400.      * attachments.                                                              *
  401.      ****************************************************************************/
  402.  
  403.     /**
  404.      * Get the name of the article image file
  405.      *
  406.      * @param array $pArticleId article id
  407.      * @access public
  408.      * @return TRUE on success, FALSE on failure
  409.      */
  410.     public function getArticleImageStorageName($pArticleId = NULL)
  411.     {
  412.         if ( !@BitBase::verifyId( $pArticleId ) ) {
  413.             if ( $this->isValid() ) {
  414.                 $pArticleId = $this->mArticleId;
  415.             } else {
  416.                 return NULL;
  417.             }
  418.         }
  419.  
  420.         return "article_$pArticleId.jpg";
  421.     }
  422.  
  423.     /**
  424.     * Work out the path to the image for this article
  425.     * @param $pArticleId id of the article we need the image path for
  426.     * @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
  427.     * @return path on success, FALSE on failure
  428.     * @access public
  429.     ***/
  430.     public function getArticleImageStoragePath($pArticleId = NULL, $pBasePathOnly = FALSE)
  431.     {
  432.         $path = STORAGE_PKG_PATH.ARTICLES_PKG_NAME.'/';
  433.         if ( !is_dir( $path ) ) {
  434.             mkdir_p( $path );
  435.         }
  436.  
  437.         if ($pBasePathOnly) {
  438.             return $path;
  439.         }
  440.  
  441.         if( !@BitBase::verifyId( $pArticleId ) ) {
  442.             if( $this->isValid() ) {
  443.                 $pArticleId = $this->mArticleId;
  444.             } else {
  445.                 return NULL;
  446.             }
  447.         }
  448.  
  449.         if ( !empty( $pArticleId ) ) {
  450.             return $path.BitArticleTopic::getArticleImageStorageName( $pArticleId );
  451.         } else {
  452.             return FALSE;
  453.         }
  454.     }
  455.  
  456.     /**
  457.     * Work out the URL to the image for this article
  458.     * @param $pArticleId id of the article we need the image path for
  459.     * @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
  460.     * @return URL on success, FALSE on failure
  461.     * @access public
  462.     ***/
  463.     public function getArticleImageStorageUrl($pArticleId = NULL, $pBasePathOnly = FALSE, $pForceRefresh = FALSE)
  464.     {
  465.         global $gBitSystem;
  466.         $url = STORAGE_PKG_URL.ARTICLES_PKG_NAME.'/';
  467.         if ($pBasePathOnly) {
  468.             return $url;
  469.         }
  470.  
  471.         if( !@BitBase::verifyId( $pArticleId ) ) {
  472.             if( $this->isValid() ) {
  473.                 $pArticleId = $this->mArticleId;
  474.             } else {
  475.                 return NULL;
  476.             }
  477.         }
  478.  
  479.         if ( is_file( BitArticleTopic::getArticleImageStoragePath( NULL, TRUE ).BitArticleTopic::getArticleImageStorageName( $pArticleId ) ) ) {
  480.             return $url.BitArticleTopic::getArticleImageStorageName( $pArticleId ).( $pForceRefresh ? "?".$gBitSystem->getUTCTime() : '' );
  481.         } else {
  482.             return FALSE;
  483.         }
  484.     }
  485. }

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