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