PageRenderTime 26ms CodeModel.GetById 47ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Tag/Cloud.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 427 lines | 199 code | 59 blank | 169 comment | 32 complexity | 2907b5878ba19be07a64ebeff94fa324 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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. *
  86. * @return Zend_Tag_Cloud
  87. */
  88. public function setConfig (Zend_Config $config)
  89. {
  90. $this->setOptions($config->toArray());
  91. return $this;
  92. }
  93. /**
  94. * Set options from array
  95. *
  96. * @param array $options Configuration for Zend_Tag_Cloud
  97. *
  98. * @return Zend_Tag_Cloud
  99. */
  100. public function setOptions (array $options)
  101. {
  102. if (isset( $options['prefixPath'] )) {
  103. $this->addPrefixPaths($options['prefixPath']);
  104. unset( $options['prefixPath'] );
  105. }
  106. foreach ($options as $key => $value) {
  107. if (in_array(strtolower($key), $this->_skipOptions)) {
  108. continue;
  109. }
  110. $method = 'set' . ucfirst($key);
  111. if (method_exists($this, $method)) {
  112. $this->$method($value);
  113. }
  114. }
  115. return $this;
  116. }
  117. /**
  118. * Set the tags for the tag cloud.
  119. *
  120. * $tags should be an array containing single tags as array. Each tag
  121. * array should at least contain the keys 'title' and 'weight'. Optionally
  122. * you may supply the key 'url', to which the tag links to. Any additional
  123. * parameter in the array is silently ignored and can be used by custom
  124. * decorators.
  125. *
  126. * @param array $tags
  127. *
  128. * @return Zend_Tag_Cloud
  129. */
  130. public function setTags (array $tags)
  131. {
  132. // Validate and cleanup the tags
  133. $itemList = $this->getItemList();
  134. foreach ($tags as $tag) {
  135. if ($tag instanceof Zend_Tag_Taggable) {
  136. $itemList[] = $tag;
  137. } else if (is_array($tag)) {
  138. $itemList[] = new Zend_Tag_Item( $tag );
  139. } else {
  140. require_once 'Zend/Tag/Cloud/Exception.php';
  141. throw new Zend_Tag_Cloud_Exception( 'Tag must be an instance of Zend_Tag_Taggable or an array' );
  142. }
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Append a single tag to the cloud
  148. *
  149. * @param Zend_Tag_Taggable|array $tag
  150. *
  151. * @return Zend_Tag_Cloud
  152. */
  153. public function appendTag ($tag)
  154. {
  155. $tags = $this->getItemList();
  156. if ($tag instanceof Zend_Tag_Taggable) {
  157. $tags[] = $tag;
  158. } else if (is_array($tag)) {
  159. $tags[] = new Zend_Tag_Item( $tag );
  160. } else {
  161. require_once 'Zend/Tag/Cloud/Exception.php';
  162. throw new Zend_Tag_Cloud_Exception( 'Tag must be an instance of Zend_Tag_Taggable or an array' );
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Set the item list
  168. *
  169. * @param Zend_Tag_ItemList $itemList
  170. *
  171. * @return Zend_Tag_Cloud
  172. */
  173. public function setItemList (Zend_Tag_ItemList $itemList)
  174. {
  175. $this->_tags = $itemList;
  176. return $this;
  177. }
  178. /**
  179. * Retrieve the item list
  180. *
  181. * If item list is undefined, creates one.
  182. *
  183. * @return Zend_Tag_ItemList
  184. */
  185. public function getItemList ()
  186. {
  187. if (null === $this->_tags) {
  188. require_once 'Zend/Tag/ItemList.php';
  189. $this->setItemList(new Zend_Tag_ItemList());
  190. }
  191. return $this->_tags;
  192. }
  193. /**
  194. * Set the decorator for the cloud
  195. *
  196. * @param mixed $decorator
  197. *
  198. * @return Zend_Tag_Cloud
  199. */
  200. public function setCloudDecorator ($decorator)
  201. {
  202. $options = null;
  203. if (is_array($decorator)) {
  204. if (isset( $decorator['options'] )) {
  205. $options = $decorator['options'];
  206. }
  207. if (isset( $decorator['decorator'] )) {
  208. $decorator = $decorator['decorator'];
  209. }
  210. }
  211. if (is_string($decorator)) {
  212. $classname = $this->getPluginLoader()->load($decorator);
  213. $decorator = new $classname( $options );
  214. }
  215. if (!( $decorator instanceof Zend_Tag_Cloud_Decorator_Cloud )) {
  216. require_once 'Zend/Tag/Cloud/Exception.php';
  217. throw new Zend_Tag_Cloud_Exception( 'Decorator is no instance of Zend_Tag_Cloud_Decorator_Cloud' );
  218. }
  219. $this->_cloudDecorator = $decorator;
  220. return $this;
  221. }
  222. /**
  223. * Get the decorator for the cloud
  224. *
  225. * @return Zend_Tag_Cloud_Decorator_Cloud
  226. */
  227. public function getCloudDecorator ()
  228. {
  229. if (null === $this->_cloudDecorator) {
  230. $this->setCloudDecorator('htmlCloud');
  231. }
  232. return $this->_cloudDecorator;
  233. }
  234. /**
  235. * Set the decorator for the tags
  236. *
  237. * @param mixed $decorator
  238. *
  239. * @return Zend_Tag_Cloud
  240. */
  241. public function setTagDecorator ($decorator)
  242. {
  243. $options = null;
  244. if (is_array($decorator)) {
  245. if (isset( $decorator['options'] )) {
  246. $options = $decorator['options'];
  247. }
  248. if (isset( $decorator['decorator'] )) {
  249. $decorator = $decorator['decorator'];
  250. }
  251. }
  252. if (is_string($decorator)) {
  253. $classname = $this->getPluginLoader()->load($decorator);
  254. $decorator = new $classname( $options );
  255. }
  256. if (!( $decorator instanceof Zend_Tag_Cloud_Decorator_Tag )) {
  257. require_once 'Zend/Tag/Cloud/Exception.php';
  258. throw new Zend_Tag_Cloud_Exception( 'Decorator is no instance of Zend_Tag_Cloud_Decorator_Tag' );
  259. }
  260. $this->_tagDecorator = $decorator;
  261. return $this;
  262. }
  263. /**
  264. * Get the decorator for the tags
  265. *
  266. * @return Zend_Tag_Cloud_Decorator_Tag
  267. */
  268. public function getTagDecorator ()
  269. {
  270. if (null === $this->_tagDecorator) {
  271. $this->setTagDecorator('htmlTag');
  272. }
  273. return $this->_tagDecorator;
  274. }
  275. /**
  276. * Set plugin loaders for use with decorators
  277. *
  278. * @param Zend_Loader_PluginLoader_Interface $loader
  279. *
  280. * @return Zend_Tag_Cloud
  281. */
  282. public function setPluginLoader (Zend_Loader_PluginLoader_Interface $loader)
  283. {
  284. $this->_pluginLoader = $loader;
  285. return $this;
  286. }
  287. /**
  288. * Get the plugin loader for decorators
  289. *
  290. * @return Zend_Loader_PluginLoader
  291. */
  292. public function getPluginLoader ()
  293. {
  294. if ($this->_pluginLoader === null) {
  295. $prefix = 'Zend_Tag_Cloud_Decorator_';
  296. $pathPrefix = 'Zend/Tag/Cloud/Decorator/';
  297. require_once 'Zend/Loader/PluginLoader.php';
  298. $this->_pluginLoader = new Zend_Loader_PluginLoader( array( $prefix => $pathPrefix ) );
  299. }
  300. return $this->_pluginLoader;
  301. }
  302. /**
  303. * Add many prefix paths at once
  304. *
  305. * @param array $paths
  306. *
  307. * @return Zend_Tag_Cloud
  308. */
  309. public function addPrefixPaths (array $paths)
  310. {
  311. if (isset( $paths['prefix'] ) && isset( $paths['path'] )) {
  312. return $this->addPrefixPath($paths['prefix'], $paths['path']);
  313. }
  314. foreach ($paths as $path) {
  315. if (!isset( $path['prefix'] ) || !isset( $path['path'] )) {
  316. continue;
  317. }
  318. $this->addPrefixPath($path['prefix'], $path['path']);
  319. }
  320. return $this;
  321. }
  322. /**
  323. * Add prefix path for plugin loader
  324. *
  325. * @param string $prefix
  326. * @param string $path
  327. *
  328. * @return Zend_Tag_Cloud
  329. */
  330. public function addPrefixPath ($prefix, $path)
  331. {
  332. $loader = $this->getPluginLoader();
  333. $loader->addPrefixPath($prefix, $path);
  334. return $this;
  335. }
  336. /**
  337. * Render the tag cloud
  338. *
  339. * @return string
  340. */
  341. public function render ()
  342. {
  343. $tags = $this->getItemList();
  344. if (count($tags) === 0) {
  345. return '';
  346. }
  347. $tagsResult = $this->getTagDecorator()->render($tags);
  348. $cloudResult = $this->getCloudDecorator()->render($tagsResult);
  349. return $cloudResult;
  350. }
  351. /**
  352. * Render the tag cloud
  353. *
  354. * @return string
  355. */
  356. public function __toString ()
  357. {
  358. try {
  359. $result = $this->render();
  360. return $result;
  361. } catch (Exception $e) {
  362. $message = "Exception caught by tag cloud: " . $e->getMessage()
  363. . "\nStack Trace:\n" . $e->getTraceAsString();
  364. trigger_error($message, E_USER_WARNING);
  365. return '';
  366. }
  367. }
  368. }