Tag cloud view helper

Here's a little view helper to display a tag cloud. All you have to do is supply an array of tags, with the tag name being the index and how many times it's used as the value, and the url you'd like the tags to go to.

PHP:
  1. <?php
  2.  
  3. /**
  4. * Display a tag cloud
  5. *
  6. * @category   Amnuts
  7. * @package    Amnuts_View
  8. * @subpackage Amnuts_View_Helper
  9. * @copyright  Copyright (c) 2008 Andrew Collington (http://www.amnuts.com/)
  10. * @license    http://framework.zend.com/license/new-bsd     New BSD License
  11. */
  12. class Amnuts_View_Helper_TagCloud
  13. {
  14.     public $view;
  15.  
  16.     /**
  17.      * Output the tag cloud.
  18.      *
  19.      * The $tags parameter is expected to be an array with the tag being the
  20.      * index and the number of times it's used as the value.  For example:
  21.      *
  22.      *     array(
  23.      *         'foo' => 3,
  24.      *         'bar' => 1,
  25.      *         'dog' => 5,
  26.      *         'cat' => 1
  27.      *     )
  28.      *
  29.      * The url will have the tag text appended to it, so that if you supply
  30.      * '/filter/by/tag/' as the url, then given the above array you will have:
  31.      *
  32.      *     <a href="/filter/by/tag/foo">foo</a>
  33.      *     <a href="/filter/by/tag/bar">bar</a>
  34.      *
  35.      * and so on.
  36.      *
  37.      * @param array $tags The tag array
  38.      * @param string $url The link for each tag (with the tag name appended)
  39.      * @param int|string $minFont The minimum font value
  40.      * @param int|string $maxFont The maximum font value
  41.      * @param string $unit The unit of size type (%, em, px, etc.)
  42.      * @return string
  43.      */
  44.     public function tagCloud(array $tags, $url, $minFont = 100, $maxFont = 150, $unit = '%')
  45.     {
  46.         $xhtml = '';
  47.         $cloud = array();
  48.        
  49.         if (!empty($tags)) {
  50.             $min  = min(array_values($tags));
  51.             $max  = max(array_values($tags));
  52.             $diff = $max - $min;
  53.             if (!$diff) {
  54.                 ++$diff;
  55.             }
  56.  
  57.             foreach ($tags as $tag => $count) {
  58.                 $size = $minFont + ($count - $min) * ($maxFont - $minFont) / $diff;
  59.                 $cloud[] = '<a href="' . $url . urlencode($tag)
  60.                          . '" style="font-size:' . $size . $unit . ';">'
  61.                          . $this->view->escape($tag) . '</a>';
  62.             }
  63.             $xhtml .= '<div class="tagCloudContainer"><h4>Tag cloud</h4>' . join(', ', $cloud) . "</div>\n";
  64.         }
  65.         return $xhtml;
  66.     }
  67.  
  68.     /**
  69.      * Set the view object
  70.      *
  71.      * @param Zend_View_Interface $view
  72.      */
  73.     public function setView(Zend_View_Interface $view)
  74.     {
  75.         $this->view = $view;
  76.     }
  77. }
  78.  
  79. ?>

0 Responses to “Tag cloud view helper”


  1. No Comments

Leave a Reply

You must login to post a comment.




Mp3 sparks Allofmp3