Alpha Framework alpha--model
[ class tree: alpha--model ] [ index: alpha--model ] [ all elements ]

Source for file TagObject.inc

Documentation is available at TagObject.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/model/AlphaDAO.inc';
  4. require_once $config->get('sysRoot').'alpha/util/helpers/AlphaValidator.inc';
  5.  
  6. /**
  7.  *
  8.  * The tag class used in tag clouds and search
  9.  * 
  10.  * @package alpha::model
  11.  * @since 1.0
  12.  * @author John Collins <dev@alphaframework.org>
  13.  * @version $Id: TagObject.inc 1456 2011-12-04 16:05:22Z johnc $
  14.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  15.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  16.  *  All rights reserved.
  17.  * 
  18.  *  <pre>
  19.  *  Redistribution and use in source and binary forms, with or
  20.  *  without modification, are permitted provided that the
  21.  *  following conditions are met:
  22.  * 
  23.  *  * Redistributions of source code must retain the above
  24.  *    copyright notice, this list of conditions and the
  25.  *    following disclaimer.
  26.  *  * Redistributions in binary form must reproduce the above
  27.  *    copyright notice, this list of conditions and the
  28.  *    following disclaimer in the documentation and/or other
  29.  *    materials provided with the distribution.
  30.  *  * Neither the name of the Alpha Framework nor the names
  31.  *    of its contributors may be used to endorse or promote
  32.  *    products derived from this software without specific
  33.  *    prior written permission.
  34.  *   
  35.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  36.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  37.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  40.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  45.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  46.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  *  </pre>
  49.  *  
  50.  */
  51. class TagObject extends AlphaDAO {
  52.     /**
  53.      * The name of the class of the object which is tagged
  54.      *
  55.      * @var String 
  56.      * @since 1.0
  57.      */
  58.     protected $taggedClass;
  59.     
  60.     /**
  61.      * The OID of the object which is tagged
  62.      *
  63.      * @var Integer 
  64.      * @since 1.0
  65.      */
  66.     protected $taggedOID;
  67.     
  68.     /**
  69.      * The content of the tag
  70.      *
  71.      * @var String 
  72.      * @since 1.0
  73.      */
  74.     protected $content;
  75.     
  76.     /**
  77.      * An array of data display labels for the class properties
  78.      * 
  79.      * @var array 
  80.      * @since 1.0
  81.      */
  82.     protected $dataLabels = array("OID"=>"Tag ID#","taggedClass"=>"Class Name","taggedOID"=>"Tagged Object ID#","content"=>"Tag");
  83.     
  84.     /**
  85.      * The name of the database table for the class
  86.      * 
  87.      * @var string 
  88.      * @since 1.0
  89.      */
  90.     const TABLE_NAME = 'Tag';
  91.     
  92.     /**
  93.      * Trace logger
  94.      * 
  95.      * @var Logger 
  96.      * @since 1.0
  97.      */
  98.     private static $logger null;
  99.     
  100.     /**
  101.      * The constructor
  102.      * 
  103.      * @since 1.0
  104.      */
  105.     public function __construct({
  106.         self::$logger new Logger('TagObject');
  107.             
  108.         // ensure to call the parent constructor
  109.         parent::__construct();
  110.         $this->taggedClass = new String();
  111.         $this->taggedOID = new Integer();
  112.         $this->content = new String();
  113.         
  114.         $this->markUnique('taggedClass''taggedOID''content');
  115.     }
  116.     
  117.     /**
  118.      * Returns an array of TagObjects matching the class and OID provided
  119.      * 
  120.      * @param $taggedClass The class name of the DAO that has been tagged.
  121.      * @param $taggedOID The Object ID of the DAO that has been tagged.
  122.      * @return array 
  123.      * @since 1.0
  124.      * @throws AlphaException
  125.      * @throws IllegalArguementException
  126.      */
  127.     public function loadTags($taggedClass$taggedOID{
  128.             
  129.         global $config;
  130.         
  131.         if($taggedClass == '' || $taggedOID == '')
  132.             throw new IllegalArguementException('The taggedClass or taggedOID provided are empty');
  133.         
  134.         $provider AlphaDAOProviderFactory::getInstance($config->get('sysDBProviderName')$this);
  135.         
  136.         try {
  137.             $tags $provider->loadAllByAttributes(array('taggedOID','taggedClass')array($taggedOID$taggedClass));
  138.             return $tags;
  139.         }catch(BONotFoundException $bonf{
  140.             return array();
  141.         }catch(Exception $e{
  142.             self::$logger->error($e->getMessage());
  143.             throw new AlphaException($e->getMessage());
  144.         }
  145.     }
  146.     
  147.     /**
  148.      * Returns a hash array of the most popular tags based on their occurence in the database,
  149.      * ordered by alphabet and restricted to the a count matching the $limit supplied.  The
  150.      * returned has array uses the tag content as a key and the database value as a value.
  151.      *  
  152.      * @param $limit 
  153.      * @return array 
  154.      * @since 1.0
  155.      * @throws AlphaException
  156.      */
  157.     public static function getPopularTagsArray($limit{
  158.         global $config;
  159.         
  160.         $provider AlphaDAOProviderFactory::getInstance($config->get('sysDBProviderName')new TagObject());
  161.         
  162.         $sqlQuery "SELECT content, count(*) as count FROM ".TagObject::TABLE_NAME." GROUP BY content ORDER BY count DESC LIMIT $limit";
  163.         
  164.         try{
  165.             $result $provider->query($sqlQuery);            
  166.         }catch(CustomQueryException $e{
  167.             throw new AlphaException('Failed to query the tags table, error is ['.$e->getMessage().']');
  168.             return array();
  169.         }
  170.         
  171.         // now build an array of tags to be returned
  172.         $popTags array();        
  173.         
  174.         foreach($result as $row{
  175.             $popTags[$row['content']] $row['count'];            
  176.         }
  177.         
  178.         // sort the array by content key before returning
  179.         ksort($popTags);        
  180.         return $popTags;
  181.     }
  182.  
  183.     /**
  184.      * Splits the passed content by spaces, filters (removes) stop words from stopwords.ini,
  185.      * and returns an array of TagObject instances.
  186.      * 
  187.      * @param $content 
  188.      * @param $taggedClass Optionally provide a BO class name
  189.      * @param $taggedOID Optionally provide a BO instance OID
  190.      * @param $applyStopwords Defaults true, set to false if you want to ignore the stopwords.
  191.      * @return array 
  192.      * @throws AlphaException
  193.      * @since 1.0
  194.      */
  195.     public static function tokenize($content$taggedClass=''$taggedOID=''$applyStopwords=true{
  196.         if(self::$logger == null)
  197.             self::$logger new Logger('TagObject');
  198.             
  199.         global $config;
  200.         
  201.         // apply stop words
  202.         $lowerWords preg_split("/[\s,.:-]+/"$content);
  203.         
  204.         array_walk($lowerWords'TagObject::lowercaseArrayElement');
  205.         
  206.         if($applyStopwords{
  207.             if(file_exists($config->get('sysRoot').'config/stopwords-'.$config->get('sysStopwordsSize').'.ini')) {        
  208.                 $stopwords file($config->get('sysRoot').'config/stopwords-'.$config->get('sysStopwordsSize').'.ini'FILE_IGNORE_NEW_LINES);
  209.             }elseif(file_exists($config->get('sysRoot').'alpha/stopwords-'.$config->get('sysStopwordsSize').'.ini')) {
  210.                 $stopwords file($config->get('sysRoot').'alpha/stopwords-'.$config->get('sysStopwordsSize').'.ini'FILE_IGNORE_NEW_LINES);
  211.             }else{
  212.                 throw new AlphaException('Unable to find a stopwords-'.$config->get('sysStopwordsSize').'.ini file in the application!');
  213.             }
  214.             
  215.             array_walk($stopwords'TagObject::lowercaseArrayElement');
  216.             
  217.             $filtered array_diff($lowerWords$stopwords);
  218.         }else{
  219.             $filtered $lowerWords;
  220.         }
  221.         
  222.         $tagObjects array();
  223.         $tagContents array();
  224.         foreach($filtered as $tagContent{
  225.             // we only want to create word tags
  226.             if(AlphaValidator::isAlpha($tagContent)) {
  227.                 // just making sure that we haven't added this one in already
  228.                 if(!in_array($tagContent$tagContents&& !empty($tagContent)) {
  229.                     $tag new TagObject();
  230.                     $tag->set('content'trim(strtolower($tagContent)));
  231.                     if(!empty($taggedClass))
  232.                         $tag->set('taggedClass'$taggedClass);
  233.                     if(!empty($taggedOID))
  234.                         $tag->set('taggedOID'$taggedOID);
  235.                     
  236.                     array_push($tagObjects$tag);
  237.                     array_push($tagContents$tagContent);
  238.                 }
  239.             }
  240.         }
  241.         
  242.         self::$logger->debug('Tags generated: ['.var_export($tagContentstrue).']');
  243.         return $tagObjects;
  244.     }
  245.     
  246.     /**
  247.      * Applies trim() and strtolower to the array element passed by reference
  248.      * 
  249.      * @param $element 
  250.      * @param $key (not required)
  251.      */
  252.     private static function lowercaseArrayElement(&$element$key{
  253.         $element trim(strtolower($element));
  254.     }
  255.     
  256.     /**
  257.      * Cleans tag content by removing white spaces and converting to lowercase.
  258.      * 
  259.      * @param $content 
  260.      * @return string 
  261.      */
  262.     public static function cleanTagContent($content{
  263.         return trim(strtolower(str_replace(' '''$content)));
  264.     }
  265. }
  266.  
  267. ?>

Documentation generated on Tue, 13 Dec 2011 20:27:37 +0000 by phpDocumentor 1.4.3