[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Tag / Cloud.php
blob293999d42d26f2fc01b6f7b811257cc0bea3b176
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Tag
17 * @subpackage Cloud
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 /**
24 * @see Zend_Tag_Item
26 require_once 'Zend/Tag/Item.php';
28 /**
29 * @category Zend
30 * @package Zend_Tag
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Tag_Cloud
36 /**
37 * Decorator for the cloud
39 * @var Zend_Tag_Cloud_Decorator_Cloud
41 protected $_cloudDecorator = null;
43 /**
44 * Decorator for the tags
46 * @var Zend_Tag_Cloud_Decorator_Tag
48 protected $_tagDecorator = null;
50 /**
51 * List of all tags
53 * @var Zend_Tag_ItemList
55 protected $_tags = null;
57 /**
58 * Plugin loader for decorators
60 * @var Zend_Loader_PluginLoader
62 protected $_pluginLoader = null;
64 /**
65 * Option keys to skip when calling setOptions()
67 * @var array
69 protected $_skipOptions = array(
70 'options',
71 'config',
74 /**
75 * Create a new tag cloud with options
77 * @param mixed $options
79 public function __construct($options = null)
81 if ($options instanceof Zend_Config) {
82 $this->setConfig($options);
85 if (is_array($options)) {
86 $this->setOptions($options);
90 /**
91 * Set options from Zend_Config
93 * @param Zend_Config $config
94 * @return Zend_Tag_Cloud
96 public function setConfig(Zend_Config $config)
98 $this->setOptions($config->toArray());
100 return $this;
104 * Set options from array
106 * @param array $options Configuration for Zend_Tag_Cloud
107 * @return Zend_Tag_Cloud
109 public function setOptions(array $options)
111 if (isset($options['prefixPath'])) {
112 $this->addPrefixPaths($options['prefixPath']);
113 unset($options['prefixPath']);
116 foreach ($options as $key => $value) {
117 if (in_array(strtolower($key), $this->_skipOptions)) {
118 continue;
121 $method = 'set' . ucfirst($key);
122 if (method_exists($this, $method)) {
123 $this->$method($value);
127 return $this;
131 * Set the tags for the tag cloud.
133 * $tags should be an array containing single tags as array. Each tag
134 * array should at least contain the keys 'title' and 'weight'. Optionally
135 * you may supply the key 'url', to which the tag links to. Any additional
136 * parameter in the array is silently ignored and can be used by custom
137 * decorators.
139 * @param array $tags
140 * @return Zend_Tag_Cloud
142 public function setTags(array $tags)
144 // Validate and cleanup the tags
145 $itemList = $this->getItemList();
147 foreach ($tags as $tag) {
148 if ($tag instanceof Zend_Tag_Taggable) {
149 $itemList[] = $tag;
150 } else if (is_array($tag)) {
151 $itemList[] = new Zend_Tag_Item($tag);
152 } else {
153 require_once 'Zend/Tag/Cloud/Exception.php';
154 throw new Zend_Tag_Cloud_Exception('Tag must be an instance of Zend_Tag_Taggable or an array');
158 return $this;
162 * Append a single tag to the cloud
164 * @param Zend_Tag_Taggable|array $tag
165 * @return Zend_Tag_Cloud
167 public function appendTag($tag)
169 $tags = $this->getItemList();
170 if ($tag instanceof Zend_Tag_Taggable) {
171 $tags[] = $tag;
172 } else if (is_array($tag)) {
173 $tags[] = new Zend_Tag_Item($tag);
174 } else {
175 require_once 'Zend/Tag/Cloud/Exception.php';
176 throw new Zend_Tag_Cloud_Exception('Tag must be an instance of Zend_Tag_Taggable or an array');
179 return $this;
183 * Set the item list
185 * @param Zend_Tag_ItemList $itemList
186 * @return Zend_Tag_Cloud
188 public function setItemList(Zend_Tag_ItemList $itemList)
190 $this->_tags = $itemList;
191 return $this;
195 * Retrieve the item list
197 * If item list is undefined, creates one.
199 * @return Zend_Tag_ItemList
201 public function getItemList()
203 if (null === $this->_tags) {
204 require_once 'Zend/Tag/ItemList.php';
205 $this->setItemList(new Zend_Tag_ItemList());
207 return $this->_tags;
211 * Set the decorator for the cloud
213 * @param mixed $decorator
214 * @return Zend_Tag_Cloud
216 public function setCloudDecorator($decorator)
218 $options = null;
220 if (is_array($decorator)) {
221 if (isset($decorator['options'])) {
222 $options = $decorator['options'];
225 if (isset($decorator['decorator'])) {
226 $decorator = $decorator['decorator'];
230 if (is_string($decorator)) {
231 $classname = $this->getPluginLoader()->load($decorator);
232 $decorator = new $classname($options);
235 if (!($decorator instanceof Zend_Tag_Cloud_Decorator_Cloud)) {
236 require_once 'Zend/Tag/Cloud/Exception.php';
237 throw new Zend_Tag_Cloud_Exception('Decorator is no instance of Zend_Tag_Cloud_Decorator_Cloud');
240 $this->_cloudDecorator = $decorator;
242 return $this;
246 * Get the decorator for the cloud
248 * @return Zend_Tag_Cloud_Decorator_Cloud
250 public function getCloudDecorator()
252 if (null === $this->_cloudDecorator) {
253 $this->setCloudDecorator('htmlCloud');
255 return $this->_cloudDecorator;
259 * Set the decorator for the tags
261 * @param mixed $decorator
262 * @return Zend_Tag_Cloud
264 public function setTagDecorator($decorator)
266 $options = null;
268 if (is_array($decorator)) {
269 if (isset($decorator['options'])) {
270 $options = $decorator['options'];
273 if (isset($decorator['decorator'])) {
274 $decorator = $decorator['decorator'];
278 if (is_string($decorator)) {
279 $classname = $this->getPluginLoader()->load($decorator);
280 $decorator = new $classname($options);
283 if (!($decorator instanceof Zend_Tag_Cloud_Decorator_Tag)) {
284 require_once 'Zend/Tag/Cloud/Exception.php';
285 throw new Zend_Tag_Cloud_Exception('Decorator is no instance of Zend_Tag_Cloud_Decorator_Tag');
288 $this->_tagDecorator = $decorator;
290 return $this;
294 * Get the decorator for the tags
296 * @return Zend_Tag_Cloud_Decorator_Tag
298 public function getTagDecorator()
300 if (null === $this->_tagDecorator) {
301 $this->setTagDecorator('htmlTag');
303 return $this->_tagDecorator;
307 * Set plugin loaders for use with decorators
309 * @param Zend_Loader_PluginLoader_Interface $loader
310 * @return Zend_Tag_Cloud
312 public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
314 $this->_pluginLoader = $loader;
315 return $this;
319 * Get the plugin loader for decorators
321 * @return Zend_Loader_PluginLoader
323 public function getPluginLoader()
325 if ($this->_pluginLoader === null) {
326 $prefix = 'Zend_Tag_Cloud_Decorator_';
327 $pathPrefix = 'Zend/Tag/Cloud/Decorator/';
329 require_once 'Zend/Loader/PluginLoader.php';
330 $this->_pluginLoader = new Zend_Loader_PluginLoader(array($prefix => $pathPrefix));
333 return $this->_pluginLoader;
337 * Add many prefix paths at once
339 * @param array $paths
340 * @return Zend_Tag_Cloud
342 public function addPrefixPaths(array $paths)
344 if (isset($paths['prefix']) && isset($paths['path'])) {
345 return $this->addPrefixPath($paths['prefix'], $paths['path']);
348 foreach ($paths as $path) {
349 if (!isset($path['prefix']) || !isset($path['path'])) {
350 continue;
353 $this->addPrefixPath($path['prefix'], $path['path']);
356 return $this;
360 * Add prefix path for plugin loader
362 * @param string $prefix
363 * @param string $path
364 * @return Zend_Tag_Cloud
366 public function addPrefixPath($prefix, $path)
368 $loader = $this->getPluginLoader();
369 $loader->addPrefixPath($prefix, $path);
371 return $this;
375 * Render the tag cloud
377 * @return string
379 public function render()
381 $tags = $this->getItemList();
383 if (count($tags) === 0) {
384 return '';
387 $tagsResult = $this->getTagDecorator()->render($tags);
388 $cloudResult = $this->getCloudDecorator()->render($tagsResult);
390 return $cloudResult;
394 * Render the tag cloud
396 * @return string
398 public function __toString()
400 try {
401 $result = $this->render();
402 return $result;
403 } catch (Exception $e) {
404 $message = "Exception caught by tag cloud: " . $e->getMessage()
405 . "\nStack Trace:\n" . $e->getTraceAsString();
406 trigger_error($message, E_USER_WARNING);
407 return '';