'tag'); public $shuffle = FALSE; // Tag elements, biggest and smallest values protected $elements; protected $biggest; protected $smallest; /** * Construct a new tagcloud. The elements must be passed in as an array, * with each entry in the array having a "title" ,"link", and "count" key. * Font sizes will be applied via the "style" attribute as a percentage. * * @param array elements of the tagcloud * @param integer minimum font size * @param integer maximum font size * @return void */ public function __construct(array $elements, $min_size = NULL, $max_size = NULL, $shuffle = FALSE) { $this->elements = $elements; if($shuffle !== FALSE) { $this->shuffle = TRUE; } $counts = array(); foreach ($elements as $data) { $counts[] = $data['count']; } // Find the biggest and smallest values of the elements $this->biggest = max($counts); $this->smallest = min($counts); if ($min_size !== NULL) { $this->min_size = $min_size; } if ($max_size !== NULL) { $this->max_size = $max_size; } } /** * Magic __toString method. Returns all of the links as a single string. * * @return string */ public function __toString() { return implode("\n", $this->render()); } /** * Renders the elements of the tagcloud into an array of links. * * @return array */ public function render() { if ($this->shuffle === TRUE) { shuffle($this->elements); } // Minimum values must be 1 to prevent divide by zero errors $range = max($this->biggest - $this->smallest, 1); $scale = max($this->max_size - $this->min_size, 1); // Import the attributes locally to prevent overwrites $attr = $this->attributes; $output = array(); foreach ($this->elements as $data) { if (strpos($data['title'], ' ') !== FALSE) { // Replace spaces with non-breaking spaces to prevent line wrapping // in the middle of a link $data['title'] = str_replace(' ', ' ', $data['title']); } // Determine the size based on the min/max scale and the smallest/biggest range $size = ((($data['count'] - $this->smallest) * $scale) / $range) + $this->min_size; $attr['style'] = 'font-size: '.round($size, 0).'%'; $output[] = html::anchor($data['link'], $data['title'], $attr)."\n"; } return $output; } } // End Tagcloud