Route and Results buttons updated.
[speedfreak] / Server / system / libraries / Tagcloud.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * [Tag cloud][ref-tcl] creation library.
4  *
5  * [ref-tcl]: http://en.wikipedia.org/wiki/Tag_cloud
6  *
7  * $Id: Tagcloud.php 3824 2008-12-20 17:13:06Z samsoir $
8  *
9  * @package    Tagcloud
10  * @author     Kohana Team
11  * @copyright  (c) 2008 Kohana Team
12  * @license    http://kohanaphp.com/license.html
13  */
14 class Tagcloud_Core {
15
16         /**
17          * Creates a new Tagcloud instance and returns it.
18          *
19          * @chainable
20          * @param   array    elements of the tagcloud
21          * @param   integer  minimum font size
22          * @param   integer  maximum font size
23          * @return  Tagcloud
24          */
25         public static function factory(array $elements, $min_size = NULL, $max_size = NULL, $shuffle = FALSE)
26         {
27                 return new Tagcloud($elements, $min_size, $max_size, $shuffle);
28         }
29
30         public $min_size   = 80;
31         public $max_size   = 140;
32         public $attributes = array('class' => 'tag');
33         public $shuffle    = FALSE;
34
35         // Tag elements, biggest and smallest values
36         protected $elements;
37         protected $biggest;
38         protected $smallest;
39
40         /**
41          * Construct a new tagcloud. The elements must be passed in as an array,
42          * with each entry in the array having a "title" ,"link", and "count" key.
43          * Font sizes will be applied via the "style" attribute as a percentage.
44          *
45          * @param   array    elements of the tagcloud
46          * @param   integer  minimum font size
47          * @param   integer  maximum font size
48          * @return  void
49          */
50         public function __construct(array $elements, $min_size = NULL, $max_size = NULL, $shuffle = FALSE)
51         {
52                 $this->elements = $elements;
53                 
54                 if($shuffle !== FALSE)
55                 {
56                         $this->shuffle = TRUE;
57                 }
58
59                 $counts = array();
60                 foreach ($elements as $data)
61                 {
62                         $counts[] = $data['count'];
63                 }
64
65                 // Find the biggest and smallest values of the elements
66                 $this->biggest  = max($counts);
67                 $this->smallest = min($counts);
68
69                 if ($min_size !== NULL)
70                 {
71                         $this->min_size = $min_size;
72                 }
73
74                 if ($max_size !== NULL)
75                 {
76                         $this->max_size = $max_size;
77                 }
78         }
79
80         /**
81          * Magic __toString method. Returns all of the links as a single string.
82          *
83          * @return  string
84          */
85         public function __toString()
86         {
87                 return implode("\n", $this->render());
88         }
89
90         /**
91          * Renders the elements of the tagcloud into an array of links.
92          *
93          * @return  array
94          */
95         public function render()
96         {
97                 if ($this->shuffle === TRUE)
98                 {
99                         shuffle($this->elements);
100                 }
101
102                 // Minimum values must be 1 to prevent divide by zero errors
103                 $range = max($this->biggest  - $this->smallest, 1);
104                 $scale = max($this->max_size - $this->min_size, 1);
105
106                 // Import the attributes locally to prevent overwrites
107                 $attr = $this->attributes;
108
109                 $output = array();
110                 foreach ($this->elements as $data)
111                 {
112                         if (strpos($data['title'], ' ') !== FALSE)
113                         {
114                                 // Replace spaces with non-breaking spaces to prevent line wrapping
115                                 // in the middle of a link
116                                 $data['title'] = str_replace(' ', '&nbsp;', $data['title']);
117                         }
118
119                         // Determine the size based on the min/max scale and the smallest/biggest range
120                         $size = ((($data['count'] - $this->smallest) * $scale) / $range) + $this->min_size;
121
122                         $attr['style'] = 'font-size: '.round($size, 0).'%';
123
124                         $output[] = html::anchor($data['link'], $data['title'], $attr)."\n";
125                 }
126
127                 return $output;
128         }
129
130 } // End Tagcloud