PageRenderTime 34ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Contents/View/Helper/TagCloudHelper.php

http://github.com/infinitas/infinitas
PHP | 166 lines | 87 code | 19 blank | 60 comment | 12 complexity | e650f4ddf05d5b6b898105024b6c0398 MD5 | raw file
  1. <?php
  2. /**
  3. * CakePHP Tags Plugin
  4. *
  5. * Copyright 2009 - 2010, Cake Development Corporation
  6. * 1785 E. Sahara Avenue, Suite 490-423
  7. * Las Vegas, Nevada 89104
  8. *
  9. *
  10. *
  11. *
  12. * @copyright 2009 - 2010, Cake Development Corporation (http://cakedc.com)
  13. * @link http://github.com/CakeDC/Tags
  14. * @package Infinitas.Contents.Helper
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. /**
  18. * Tag cloud helper
  19. *
  20. * @package Infinitas.Contents.Helper
  21. *
  22. * @property HtmlHelper $Html
  23. * @property TextHelper $Text
  24. * @property DesignHelper $Design
  25. */
  26. class TagCloudHelper extends AppHelper {
  27. /**
  28. * Other helpers to load
  29. *
  30. * @var public $helpers
  31. */
  32. public $helpers = array(
  33. 'Html', 'Text',
  34. 'Libs.Design'
  35. );
  36. /**
  37. * Method to output a tag-cloud formatted based on the weight of the tags
  38. *
  39. * @param array $tags
  40. * @param array $options Display options. Valid keys are:
  41. * - shuffle: true to shuffle the tag list, false to display them in the same order than passed [default: true]
  42. * - extract: Set::extract() compatible format string. Path to extract weight values from the $tags array [default: {n}.GlobalTag.weight]
  43. * - before: string to be displayed before each generated link. "%size%" will be replaced with tag size calculated from the weight [default: empty]
  44. * - after: string to be displayed after each generated link. "%size%" will be replaced with tag size calculated from the weight [default: empty]
  45. * - maxSize: size of the heaviest tag [default: 160]
  46. * - minSize: size of the lightest tag [default: 80]
  47. * - url: an array containing the default url
  48. * - named: the named parameter used to send the tag [default: by]
  49. *
  50. * @return string
  51. */
  52. public function display($tags = null, $options = array()) {
  53. if (empty($tags) || !is_array($tags)) {
  54. return '';
  55. }
  56. $defaults = array(
  57. 'shuffle' => true,
  58. 'extract' => '{n}.GlobalTag.weight',
  59. 'between' => ' | ',
  60. 'maxSize' => 160,
  61. 'minSize' => 80,
  62. 'url' => array(
  63. 'plugin' => $this->request->params['plugin'],
  64. 'action' => 'index'
  65. ),
  66. 'named' => 'by',
  67. 'return' => false
  68. );
  69. $options = array_merge($defaults, array_filter($options));
  70. $weights = Set::extract($options['extract'], $tags);
  71. $maxWeight = $minWeight = 1;
  72. if ($weights) {
  73. $maxWeight = max($weights);
  74. $minWeight = min($weights);
  75. }
  76. // find the range of values
  77. $spread = $maxWeight - $minWeight;
  78. if (0 == $spread) {
  79. $spread = 1;
  80. }
  81. if ($options['shuffle'] == true) {
  82. shuffle($tags);
  83. }
  84. $cloud = array();
  85. foreach ($tags as $tag) {
  86. $options['url'] = array_merge(array(
  87. 'plugin' => $this->request->params['plugin']
  88. ), $options['url']);
  89. $options['url'][$options['named']] = $tag['GlobalTag']['keyname'];
  90. $url = EventCore::trigger($this, Inflector::camelize($options['url']['plugin']) . '.slugUrl', array('type' => 'tag', 'data' => $options['url']));
  91. $size = $options['minSize'] + (($tag['GlobalTag']['weight'] - $minWeight) * (($options['maxSize'] - $options['minSize']) / ($spread)));
  92. $size = ceil($size);
  93. $cloud[] = $this->Html->link(
  94. $tag['GlobalTag']['name'],
  95. current($url['slugUrl']),
  96. array(
  97. 'class' => 'tag tag-' . $tag['GlobalTag']['id']
  98. )
  99. );
  100. }
  101. if ($options['return']) {
  102. return $cloud;
  103. }
  104. return implode($options['between'], $cloud);
  105. }
  106. /**
  107. * convert an array of tags to a list of tags with links
  108. *
  109. * @param array $data the row of data from find
  110. * @param string $seperator what to seperate with
  111. * @param integer $limit max number of tags to show
  112. *
  113. * @return string
  114. */
  115. public function tagList($data, $seperator = 'and', $limit = 5) {
  116. if (empty($data['GlobalTagged'])) {
  117. return __d('contents', 'No tags');
  118. }
  119. $return = array();
  120. foreach ($data['GlobalTagged'] as $tagged) {
  121. if ($limit <= 0) {
  122. continue;
  123. }
  124. $return[] = $this->Html->link(
  125. $this->Design->icon('tag') . $tagged['GlobalTag']['name'],
  126. $this->here . '#',
  127. array(
  128. 'escape' => false,
  129. 'class' => 'icon tag'
  130. )
  131. );
  132. $limit--;
  133. }
  134. if ($seperator == ',') {
  135. return implode(', ', $return);
  136. }
  137. return $this->Text->toList($return, $seperator);
  138. }
  139. /**
  140. * Replaces %size% in strings with the calculated "size" of the tag
  141. *
  142. * @return string
  143. */
  144. protected function _replace($string, $size) {
  145. return str_replace("%size%", $size, $string);
  146. }
  147. }