croogo /Taxonomy/View/Helper/TaxonomiesHelper.php

Language PHP Lines 197
MD5 Hash cd266eb4d3005a9e7bee7a4c3081f714
Repository https://github.com/kareypowell/croogo.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

App::uses('AppHelper', 'View/Helper');

/**
 * Taxonomies Helper
 *
 * @category Taxonomy.View/Helper
 * @package  Croogo.Taxonomy
 * @version  1.0
 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class TaxonomiesHelper extends AppHelper {

	public $helpers = array(
		'Html',
	);

/**
 * constructor
 */
	public function __construct(View $view, $settings = array()) {
		parent::__construct($view);
		$this->_setupEvents();
	}

/**
 * setup events
 */
	protected function _setupEvents() {
		$events = array(
			'Helper.Layout.beforeFilter' => array(
				'callable' => 'filter', 'passParams' => true,
			),
		);
		$eventManager = $this->_View->getEventManager();
		foreach ($events as $name => $config) {
			$eventManager->attach(array($this, 'filter'), $name, $config);
		}
	}

/**
 * beforeRender
 */
	public function beforeRender($viewFile) {
		if (isset($this->request->params['admin']) && !$this->request->is('ajax')) {
			$this->_adminTabs();
		}
	}

/**
 * Hook admin tabs when $taxonomy is set
 */
	protected function _adminTabs() {
		$controller = Inflector::camelize($this->request->params['controller']);
		if (empty($this->_View->viewVars['taxonomy']) || $controller == 'Terms') {
			return;
		}
		$title = __d('croogo', 'Terms');
		$element = 'Taxonomy.admin/terms_tab';
		Croogo::hookAdminTab("$controller/admin_add", $title, $element);
		Croogo::hookAdminTab("$controller/admin_edit", $title, $element);
	}

/**
 * Filter content for Vocabularies
 *
 * Replaces [vocabulary:vocabulary_alias] or [v:vocabulary_alias] with Terms list
 *
 * @param string $content
 * @return string
 */
	public function filter(&$content, $options = array()) {
		preg_match_all('/\[(vocabulary|v):([A-Za-z0-9_\-]*)(.*?)\]/i', $content, $tagMatches);
		for ($i = 0, $ii = count($tagMatches[1]); $i < $ii; $i++) {
			$regex = '/(\S+)=[\'"]?((?:.(?![\'"]?\s+(?:\S+)=|[>\'"]))+.)[\'"]?/i';
			preg_match_all($regex, $tagMatches[3][$i], $attributes);
			$vocabularyAlias = $tagMatches[2][$i];
			$options = array();
			for ($j = 0, $jj = count($attributes[0]); $j < $jj; $j++) {
				$options[$attributes[1][$j]] = $attributes[2][$j];
			}
			$content = str_replace($tagMatches[0][$i], $this->vocabulary($vocabularyAlias, $options), $content);
		}
		return $content;
	}

/**
 * Show Vocabulary by Alias
 *
 * @param string $vocabularyAlias Vocabulary alias
 * @param array $options (optional)
 * @return string
 */
	public function vocabulary($vocabularyAlias, $options = array()) {
		$_options = array(
			'tag' => 'ul',
			'tagAttributes' => array(),
			'type' => null,
			'link' => true,
			'plugin' => 'nodes',
			'controller' => 'nodes',
			'action' => 'term',
			'element' => 'Taxonomy.vocabulary',
		);
		$options = array_merge($_options, $options);

		$output = '';
		if (isset($this->_View->viewVars['vocabularies_for_layout'][$vocabularyAlias]['threaded'])) {
			$vocabulary = $this->_View->viewVars['vocabularies_for_layout'][$vocabularyAlias];
			$output .= $this->_View->element($options['element'], array(
				'vocabulary' => $vocabulary,
				'options' => $options,
			));
		}
		return $output;
	}

/**
 * Nested Terms
 *
 * @param array   $terms
 * @param array   $options
 * @param integer $depth
 */
	public function nestedTerms($terms, $options, $depth = 1) {
		$_options = array();
		$options = array_merge($_options, $options);

		$output = '';
		foreach ($terms as $term) {
			if ($options['link']) {
				$termAttr = array(
					'id' => 'term-' . $term['Term']['id'],
				);
				$termOutput = $this->Html->link($term['Term']['title'], array(
					'plugin' => $options['plugin'],
					'controller' => $options['controller'],
					'action' => $options['action'],
					'type' => $options['type'],
					'slug' => $term['Term']['slug'],
				), $termAttr);
			} else {
				$termOutput = $term['Term']['title'];
			}
			if (isset($term['children']) && count($term['children']) > 0) {
				$termOutput .= $this->nestedTerms($term['children'], $options, $depth + 1);
			}
			$termOutput = $this->Html->tag('li', $termOutput);
			$output .= $termOutput;
		}
		if ($output != null) {
			$output = $this->Html->tag($options['tag'], $output, $options['tagAttributes']);
		}

		return $output;
	}

/**
 * Generate string of type links
 *
 * @param array $typeData Array of Type records
 * @param array $termData Array of Term records
 * @return string
 */
	public function generateTypeLinks($typeData, $termData) {
		$typeLinks = '';
		if (count($typeData) <= 1) {
			return $typeLinks;
		}

		$typeLink = array();
		$typeLink[] = ' (';

		foreach ($typeData as $type) {
			$typeLink[] = $this->Html->link($type['title'], array(
				'admin' => false,
				'plugin' => 'nodes',
				'controller' => 'nodes',
				'action' => 'term',
				'type' => $type['alias'],
				'slug' => $termData['Term']['slug']
			), array(
				'target' => '_blank',
			));
		}

		$typeLink[] = ')';
		$typeLinks = implode(' ', $typeLink);

		return $typeLinks;
	}

}
Back to Top