PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Tag/Cloud.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 410 lines | 199 code | 52 blank | 159 comment | 32 complexity | 34e11407278e4d3b4269d4504d8e3b7e MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Tag
  17. * @subpackage Cloud
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Cloud.php 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  21. */
  22. /**
  23. * @see Zend_Tag_Item
  24. */
  25. require_once 'Zend/Tag/Item.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Tag
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Tag_Cloud
  33. {
  34. /**
  35. * Decorator for the cloud
  36. *
  37. * @var Zend_Tag_Cloud_Decorator_Cloud
  38. */
  39. protected $_cloudDecorator = null;
  40. /**
  41. * Decorator for the tags
  42. *
  43. * @var Zend_Tag_Cloud_Decorator_Tag
  44. */
  45. protected $_tagDecorator = null;
  46. /**
  47. * List of all tags
  48. *
  49. * @var Zend_Tag_ItemList
  50. */
  51. protected $_tags = null;
  52. /**
  53. * Plugin loader for decorators
  54. *
  55. * @var Zend_Loader_PluginLoader
  56. */
  57. protected $_pluginLoader = null;
  58. /**
  59. * Option keys to skip when calling setOptions()
  60. *
  61. * @var array
  62. */
  63. protected $_skipOptions = array(
  64. 'options',
  65. 'config',
  66. );
  67. /**
  68. * Create a new tag cloud with options
  69. *
  70. * @param mixed $options
  71. */
  72. public function __construct($options = null)
  73. {
  74. if ($options instanceof Zend_Config) {
  75. $this->setConfig($options);
  76. }
  77. if (is_array($options)) {
  78. $this->setOptions($options);
  79. }
  80. }
  81. /**
  82. * Set options from Zend_Config
  83. *
  84. * @param Zend_Config $config
  85. * @return Zend_Tag_Cloud
  86. */
  87. public function setConfig(Zend_Config $config)
  88. {
  89. $this->setOptions($config->toArray());
  90. return $this;
  91. }
  92. /**
  93. * Set options from array
  94. *
  95. * @param array $options Configuration for Zend_Tag_Cloud
  96. * @return Zend_Tag_Cloud
  97. */
  98. public function setOptions(array $options)
  99. {
  100. if (isset($options['prefixPath'])) {
  101. $this->addPrefixPaths($options['prefixPath']);
  102. unset($options['prefixPath']);
  103. }
  104. foreach ($options as $key => $value) {
  105. if (in_array(strtolower($key), $this->_skipOptions)) {
  106. continue;
  107. }
  108. $method = 'set' . ucfirst($key);
  109. if (method_exists($this, $method)) {
  110. $this->$method($value);
  111. }
  112. }
  113. return $this;
  114. }
  115. /**
  116. * Set the tags for the tag cloud.
  117. *
  118. * $tags should be an array containing single tags as array. Each tag
  119. * array should at least contain the keys 'title' and 'weight'. Optionally
  120. * you may supply the key 'url', to which the tag links to. Any additional
  121. * parameter in the array is silently ignored and can be used by custom
  122. * decorators.
  123. *
  124. * @param array $tags
  125. * @return Zend_Tag_Cloud
  126. */
  127. public function setTags(array $tags)
  128. {
  129. // Validate and cleanup the tags
  130. $itemList = $this->getItemList();
  131. foreach ($tags as $tag) {
  132. if ($tag instanceof Zend_Tag_Taggable) {
  133. $itemList[] = $tag;
  134. } else if (is_array($tag)) {
  135. $itemList[] = new Zend_Tag_Item($tag);
  136. } else {
  137. require_once 'Zend/Tag/Cloud/Exception.php';
  138. throw new Zend_Tag_Cloud_Exception('Tag must be an instance of Zend_Tag_Taggable or an array');
  139. }
  140. }
  141. return $this;
  142. }
  143. /**
  144. * Append a single tag to the cloud
  145. *
  146. * @param Zend_Tag_Taggable|array $tag
  147. * @return Zend_Tag_Cloud
  148. */
  149. public function appendTag($tag)
  150. {
  151. $tags = $this->getItemList();
  152. if ($tag instanceof Zend_Tag_Taggable) {
  153. $tags[] = $tag;
  154. } else if (is_array($tag)) {
  155. $tags[] = new Zend_Tag_Item($tag);
  156. } else {
  157. require_once 'Zend/Tag/Cloud/Exception.php';
  158. throw new Zend_Tag_Cloud_Exception('Tag must be an instance of Zend_Tag_Taggable or an array');
  159. }
  160. return $this;
  161. }
  162. /**
  163. * Set the item list
  164. *
  165. * @param Zend_Tag_ItemList $itemList
  166. * @return Zend_Tag_Cloud
  167. */
  168. public function setItemList(Zend_Tag_ItemList $itemList)
  169. {
  170. $this->_tags = $itemList;
  171. return $this;
  172. }
  173. /**
  174. * Retrieve the item list
  175. *
  176. * If item list is undefined, creates one.
  177. *
  178. * @return Zend_Tag_ItemList
  179. */
  180. public function getItemList()
  181. {
  182. if (null === $this->_tags) {
  183. require_once 'Zend/Tag/ItemList.php';
  184. $this->setItemList(new Zend_Tag_ItemList());
  185. }
  186. return $this->_tags;
  187. }
  188. /**
  189. * Set the decorator for the cloud
  190. *
  191. * @param mixed $decorator
  192. * @return Zend_Tag_Cloud
  193. */
  194. public function setCloudDecorator($decorator)
  195. {
  196. $options = null;
  197. if (is_array($decorator)) {
  198. if (isset($decorator['options'])) {
  199. $options = $decorator['options'];
  200. }
  201. if (isset($decorator['decorator'])) {
  202. $decorator = $decorator['decorator'];
  203. }
  204. }
  205. if (is_string($decorator)) {
  206. $classname = $this->getPluginLoader()->load($decorator);
  207. $decorator = new $classname($options);
  208. }
  209. if (!($decorator instanceof Zend_Tag_Cloud_Decorator_Cloud)) {
  210. require_once 'Zend/Tag/Cloud/Exception.php';
  211. throw new Zend_Tag_Cloud_Exception('Decorator is no instance of Zend_Tag_Cloud_Decorator_Cloud');
  212. }
  213. $this->_cloudDecorator = $decorator;
  214. return $this;
  215. }
  216. /**
  217. * Get the decorator for the cloud
  218. *
  219. * @return Zend_Tag_Cloud_Decorator_Cloud
  220. */
  221. public function getCloudDecorator()
  222. {
  223. if (null === $this->_cloudDecorator) {
  224. $this->setCloudDecorator('htmlCloud');
  225. }
  226. return $this->_cloudDecorator;
  227. }
  228. /**
  229. * Set the decorator for the tags
  230. *
  231. * @param mixed $decorator
  232. * @return Zend_Tag_Cloud
  233. */
  234. public function setTagDecorator($decorator)
  235. {
  236. $options = null;
  237. if (is_array($decorator)) {
  238. if (isset($decorator['options'])) {
  239. $options = $decorator['options'];
  240. }
  241. if (isset($decorator['decorator'])) {
  242. $decorator = $decorator['decorator'];
  243. }
  244. }
  245. if (is_string($decorator)) {
  246. $classname = $this->getPluginLoader()->load($decorator);
  247. $decorator = new $classname($options);
  248. }
  249. if (!($decorator instanceof Zend_Tag_Cloud_Decorator_Tag)) {
  250. require_once 'Zend/Tag/Cloud/Exception.php';
  251. throw new Zend_Tag_Cloud_Exception('Decorator is no instance of Zend_Tag_Cloud_Decorator_Tag');
  252. }
  253. $this->_tagDecorator = $decorator;
  254. return $this;
  255. }
  256. /**
  257. * Get the decorator for the tags
  258. *
  259. * @return Zend_Tag_Cloud_Decorator_Tag
  260. */
  261. public function getTagDecorator()
  262. {
  263. if (null === $this->_tagDecorator) {
  264. $this->setTagDecorator('htmlTag');
  265. }
  266. return $this->_tagDecorator;
  267. }
  268. /**
  269. * Set plugin loaders for use with decorators
  270. *
  271. * @param Zend_Loader_PluginLoader_Interface $loader
  272. * @return Zend_Tag_Cloud
  273. */
  274. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
  275. {
  276. $this->_pluginLoader = $loader;
  277. return $this;
  278. }
  279. /**
  280. * Get the plugin loader for decorators
  281. *
  282. * @return Zend_Loader_PluginLoader
  283. */
  284. public function getPluginLoader()
  285. {
  286. if ($this->_pluginLoader === null) {
  287. $prefix = 'Zend_Tag_Cloud_Decorator_';
  288. $pathPrefix = 'Zend/Tag/Cloud/Decorator/';
  289. require_once 'Zend/Loader/PluginLoader.php';
  290. $this->_pluginLoader = new Zend_Loader_PluginLoader(array($prefix => $pathPrefix));
  291. }
  292. return $this->_pluginLoader;
  293. }
  294. /**
  295. * Add many prefix paths at once
  296. *
  297. * @param array $paths
  298. * @return Zend_Tag_Cloud
  299. */
  300. public function addPrefixPaths(array $paths)
  301. {
  302. if (isset($paths['prefix']) && isset($paths['path'])) {
  303. return $this->addPrefixPath($paths['prefix'], $paths['path']);
  304. }
  305. foreach ($paths as $path) {
  306. if (!isset($path['prefix']) || !isset($path['path'])) {
  307. continue;
  308. }
  309. $this->addPrefixPath($path['prefix'], $path['path']);
  310. }
  311. return $this;
  312. }
  313. /**
  314. * Add prefix path for plugin loader
  315. *
  316. * @param string $prefix
  317. * @param string $path
  318. * @return Zend_Tag_Cloud
  319. */
  320. public function addPrefixPath($prefix, $path)
  321. {
  322. $loader = $this->getPluginLoader();
  323. $loader->addPrefixPath($prefix, $path);
  324. return $this;
  325. }
  326. /**
  327. * Render the tag cloud
  328. *
  329. * @return string
  330. */
  331. public function render()
  332. {
  333. $tags = $this->getItemList();
  334. if (count($tags) === 0) {
  335. return '';
  336. }
  337. $tagsResult = $this->getTagDecorator()->render($tags);
  338. $cloudResult = $this->getCloudDecorator()->render($tagsResult);
  339. return $cloudResult;
  340. }
  341. /**
  342. * Render the tag cloud
  343. *
  344. * @return string
  345. */
  346. public function __toString()
  347. {
  348. try {
  349. $result = $this->render();
  350. return $result;
  351. } catch (Exception $e) {
  352. $message = "Exception caught by tag cloud: " . $e->getMessage()
  353. . "\nStack Trace:\n" . $e->getTraceAsString();
  354. trigger_error($message, E_USER_WARNING);
  355. return '';
  356. }
  357. }
  358. }