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

/Taxonomy/View/Helper/TaxonomiesHelper.php

https://github.com/kareypowell/croogo
PHP | 196 lines | 126 code | 19 blank | 51 comment | 15 complexity | cd266eb4d3005a9e7bee7a4c3081f714 MD5 | raw file
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. /**
  4. * Taxonomies Helper
  5. *
  6. * @category Taxonomy.View/Helper
  7. * @package Croogo.Taxonomy
  8. * @version 1.0
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class TaxonomiesHelper extends AppHelper {
  14. public $helpers = array(
  15. 'Html',
  16. );
  17. /**
  18. * constructor
  19. */
  20. public function __construct(View $view, $settings = array()) {
  21. parent::__construct($view);
  22. $this->_setupEvents();
  23. }
  24. /**
  25. * setup events
  26. */
  27. protected function _setupEvents() {
  28. $events = array(
  29. 'Helper.Layout.beforeFilter' => array(
  30. 'callable' => 'filter', 'passParams' => true,
  31. ),
  32. );
  33. $eventManager = $this->_View->getEventManager();
  34. foreach ($events as $name => $config) {
  35. $eventManager->attach(array($this, 'filter'), $name, $config);
  36. }
  37. }
  38. /**
  39. * beforeRender
  40. */
  41. public function beforeRender($viewFile) {
  42. if (isset($this->request->params['admin']) && !$this->request->is('ajax')) {
  43. $this->_adminTabs();
  44. }
  45. }
  46. /**
  47. * Hook admin tabs when $taxonomy is set
  48. */
  49. protected function _adminTabs() {
  50. $controller = Inflector::camelize($this->request->params['controller']);
  51. if (empty($this->_View->viewVars['taxonomy']) || $controller == 'Terms') {
  52. return;
  53. }
  54. $title = __d('croogo', 'Terms');
  55. $element = 'Taxonomy.admin/terms_tab';
  56. Croogo::hookAdminTab("$controller/admin_add", $title, $element);
  57. Croogo::hookAdminTab("$controller/admin_edit", $title, $element);
  58. }
  59. /**
  60. * Filter content for Vocabularies
  61. *
  62. * Replaces [vocabulary:vocabulary_alias] or [v:vocabulary_alias] with Terms list
  63. *
  64. * @param string $content
  65. * @return string
  66. */
  67. public function filter(&$content, $options = array()) {
  68. preg_match_all('/\[(vocabulary|v):([A-Za-z0-9_\-]*)(.*?)\]/i', $content, $tagMatches);
  69. for ($i = 0, $ii = count($tagMatches[1]); $i < $ii; $i++) {
  70. $regex = '/(\S+)=[\'"]?((?:.(?![\'"]?\s+(?:\S+)=|[>\'"]))+.)[\'"]?/i';
  71. preg_match_all($regex, $tagMatches[3][$i], $attributes);
  72. $vocabularyAlias = $tagMatches[2][$i];
  73. $options = array();
  74. for ($j = 0, $jj = count($attributes[0]); $j < $jj; $j++) {
  75. $options[$attributes[1][$j]] = $attributes[2][$j];
  76. }
  77. $content = str_replace($tagMatches[0][$i], $this->vocabulary($vocabularyAlias, $options), $content);
  78. }
  79. return $content;
  80. }
  81. /**
  82. * Show Vocabulary by Alias
  83. *
  84. * @param string $vocabularyAlias Vocabulary alias
  85. * @param array $options (optional)
  86. * @return string
  87. */
  88. public function vocabulary($vocabularyAlias, $options = array()) {
  89. $_options = array(
  90. 'tag' => 'ul',
  91. 'tagAttributes' => array(),
  92. 'type' => null,
  93. 'link' => true,
  94. 'plugin' => 'nodes',
  95. 'controller' => 'nodes',
  96. 'action' => 'term',
  97. 'element' => 'Taxonomy.vocabulary',
  98. );
  99. $options = array_merge($_options, $options);
  100. $output = '';
  101. if (isset($this->_View->viewVars['vocabularies_for_layout'][$vocabularyAlias]['threaded'])) {
  102. $vocabulary = $this->_View->viewVars['vocabularies_for_layout'][$vocabularyAlias];
  103. $output .= $this->_View->element($options['element'], array(
  104. 'vocabulary' => $vocabulary,
  105. 'options' => $options,
  106. ));
  107. }
  108. return $output;
  109. }
  110. /**
  111. * Nested Terms
  112. *
  113. * @param array $terms
  114. * @param array $options
  115. * @param integer $depth
  116. */
  117. public function nestedTerms($terms, $options, $depth = 1) {
  118. $_options = array();
  119. $options = array_merge($_options, $options);
  120. $output = '';
  121. foreach ($terms as $term) {
  122. if ($options['link']) {
  123. $termAttr = array(
  124. 'id' => 'term-' . $term['Term']['id'],
  125. );
  126. $termOutput = $this->Html->link($term['Term']['title'], array(
  127. 'plugin' => $options['plugin'],
  128. 'controller' => $options['controller'],
  129. 'action' => $options['action'],
  130. 'type' => $options['type'],
  131. 'slug' => $term['Term']['slug'],
  132. ), $termAttr);
  133. } else {
  134. $termOutput = $term['Term']['title'];
  135. }
  136. if (isset($term['children']) && count($term['children']) > 0) {
  137. $termOutput .= $this->nestedTerms($term['children'], $options, $depth + 1);
  138. }
  139. $termOutput = $this->Html->tag('li', $termOutput);
  140. $output .= $termOutput;
  141. }
  142. if ($output != null) {
  143. $output = $this->Html->tag($options['tag'], $output, $options['tagAttributes']);
  144. }
  145. return $output;
  146. }
  147. /**
  148. * Generate string of type links
  149. *
  150. * @param array $typeData Array of Type records
  151. * @param array $termData Array of Term records
  152. * @return string
  153. */
  154. public function generateTypeLinks($typeData, $termData) {
  155. $typeLinks = '';
  156. if (count($typeData) <= 1) {
  157. return $typeLinks;
  158. }
  159. $typeLink = array();
  160. $typeLink[] = ' (';
  161. foreach ($typeData as $type) {
  162. $typeLink[] = $this->Html->link($type['title'], array(
  163. 'admin' => false,
  164. 'plugin' => 'nodes',
  165. 'controller' => 'nodes',
  166. 'action' => 'term',
  167. 'type' => $type['alias'],
  168. 'slug' => $termData['Term']['slug']
  169. ), array(
  170. 'target' => '_blank',
  171. ));
  172. }
  173. $typeLink[] = ')';
  174. $typeLinks = implode(' ', $typeLink);
  175. return $typeLinks;
  176. }
  177. }