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

Source for file TagCloud.inc

Documentation is available at TagCloud.inc

  1. <?php
  2.  
  3. require_once $config->get('sysRoot').'alpha/util/AlphaErrorHandlers.inc';
  4. require_once $config->get('sysRoot').'alpha/util/Logger.inc';
  5. require_once $config->get('sysRoot').'alpha/model/TagObject.inc';
  6. require_once $config->get('sysRoot').'alpha/util/cache/AlphaCacheProviderFactory.inc';
  7.  
  8. /**
  9.  * A widget for rendering a tag cloud, based off the TagObject instances in the
  10.  * database.
  11.  * 
  12.  * @package alpha::view::widgets
  13.  * @since 1.0
  14.  * @author John Collins <dev@alphaframework.org>
  15.  * @version $Id: TagCloud.inc 1468 2011-12-13 19:52:58Z johnc $
  16.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  17.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  18.  *  All rights reserved.
  19.  * 
  20.  *  <pre>
  21.  *  Redistribution and use in source and binary forms, with or
  22.  *  without modification, are permitted provided that the
  23.  *  following conditions are met:
  24.  * 
  25.  *  * Redistributions of source code must retain the above
  26.  *    copyright notice, this list of conditions and the
  27.  *    following disclaimer.
  28.  *  * Redistributions in binary form must reproduce the above
  29.  *    copyright notice, this list of conditions and the
  30.  *    following disclaimer in the documentation and/or other
  31.  *    materials provided with the distribution.
  32.  *  * Neither the name of the Alpha Framework nor the names
  33.  *    of its contributors may be used to endorse or promote
  34.  *    products derived from this software without specific
  35.  *    prior written permission.
  36.  *   
  37.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  38.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  39.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  40.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  42.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  48.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  49.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50.  *  </pre>
  51.  *  
  52.  */
  53. class TagCloud {
  54.     
  55.     /**
  56.      * Trace logger
  57.      * 
  58.      * @var Logger 
  59.      * @since 1.0
  60.      */
  61.     private static $logger null;
  62.     
  63.     /**
  64.      * A hash array of popular tags
  65.      * 
  66.      * @var array 
  67.      * @since 1.0
  68.      */
  69.     private $popTags array();
  70.     
  71.     /**
  72.      * Constructor
  73.      * 
  74.      * @param $limit The maximum amount of tags to include in the cloud.
  75.      * @param $cacheKey Set this optional value to attempt to store the tag cloud array in the available cache for 24hrs (sysCacheProviderName).
  76.      * @since 1.0
  77.      */
  78.     public function __construct($limit$cacheKey ''{
  79.         self::$logger new Logger('TagCloud');
  80.         
  81.         global $config;
  82.         
  83.         if($cacheKey != '' && $config->get('sysCacheProviderName'!= ''{
  84.             $cache AlphaCacheProviderFactory::getInstance($config->get('sysCacheProviderName'));
  85.               $this->popTags $cache->get($cacheKey);
  86.               
  87.               // cache look-up failed, so add it for the next time
  88.               if(!$this->popTags{
  89.                   self::$logger->debug('Cache lookup on the key ['.$cacheKey.'] failed, regenerating popular tags...');
  90.                   
  91.                   $this->popTags TagObject::getPopularTagsArray($limit);
  92.                   
  93.                   $cache->set($cacheKey$this->popTags86400);
  94.               }else{
  95.                   $this->popTags array_slice($this->popTags0$limit);
  96.                   self::$logger->debug('Cache lookup on the key ['.$cacheKey.'] succeeded');
  97.               }
  98.         }else{
  99.             $this->popTags TagObject::getPopularTagsArray($limit);
  100.         }
  101.     }
  102.     
  103.     /**
  104.      * Render the tag cloud and return all of the HTML links in a single paragraph.
  105.      * 
  106.      * @param $minLinkSize The minimum font size for any tag link, in points.
  107.      * @param $maxLinkSize The maximum font size for any tag link, in points.
  108.      * @param $target The target attribute for the links
  109.      * @return string 
  110.      * @since 1.0
  111.      */
  112.     public function render($minLinkSize=8$maxLinkSize=20$target=''{
  113.         global $config;
  114.         
  115.         $html '<p>';
  116.         
  117.         foreach (array_keys($this->popTagsas $key{
  118.             $linkSize $this->popTags[$key];
  119.             if ($linkSize $minLinkSize)
  120.                 $linkSize $minLinkSize;
  121.             if ($linkSize $maxLinkSize)
  122.                 $linkSize $maxLinkSize;
  123.             $html .= '<a href="'.$config->get('sysURL').'search/q/'.$key.'" style="font-size:'.$linkSize.'pt;"'.(empty($target'' ' target="'.$target.'"').' rel="tag">'.$key.'</a> ';
  124.         }
  125.         
  126.         return $html.'</p>';
  127.     }
  128. }
  129.  
  130. ?>

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