croogo /Taxonomy/Model/Taxonomy.php

Language PHP Lines 174
MD5 Hash 7c7f7383c137b1e7817a882d551ae1c5
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
<?php

App::uses('TaxonomyAppModel', 'Taxonomy.Model');

/**
 * Taxonomy
 *
 * @category Taxonomy.Model
 * @package  Croogo.Taxonomy.Model
 * @since    1.3.1
 * @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 Taxonomy extends TaxonomyAppModel {

/**
 * Model name
 *
 * @var string
 * @access public
 */
	public $name = 'Taxonomy';

/**
 * Behaviors used by the Model
 *
 * @var array
 * @access public
 */
	public $actsAs = array(
		'Tree',
		'Croogo.Cached' => array(
			'groups' => array(
				'nodes',
				'taxonomy',
			),
		),
	);

/**
 * Model associations: belongsTo
 *
 * @var array
 * @access public
 */
	public $belongsTo = array(
		'Term' => array(
			'className' => 'Taxonomy.Term',
			'foreignKey' => 'term_id',
			'conditions' => '',
			'fields' => '',
			'order' => '',
		),
		'Vocabulary' => array(
			'className' => 'Taxonomy.Vocabulary',
			'foreignKey' => 'vocabulary_id',
			'conditions' => '',
			'fields' => '',
			'order' => '',
		),
	);

/**
 * Generates a tree of terms for a vocabulary
 *
 * @param  string $alias   Vocabulary alias (e.g., categories)
 * @param  array  $options
 * @return array
 */
	public function getTree($alias, $options = array()) {
		$_options = array(
			'key' => 'slug', // Term.slug
			'value' => 'title', // Term.title
			'taxonomyId' => false,
			'cache' => false,
		);
		$options = array_merge($_options, $options);

		// Check if cached
		if ($this->useCache && isset($options['cache']['config'])) {
			if (isset($options['cache']['prefix'])) {
				$cacheName = $options['cache']['prefix'] . '_' . md5($alias . serialize($options));
			} elseif (isset($options['cache']['name'])) {
				$cacheName = $options['cache']['name'];
			}

			if (isset($cacheName)) {
				$cacheName .= '_' . Configure::read('Config.language');
				$cachedResult = Cache::read($cacheName, $options['cache']['config']);
				if ($cachedResult) {
					return $cachedResult;
				}
			}
		}

		$vocabulary = $this->Vocabulary->findByAlias($alias);
		if (!isset($vocabulary['Vocabulary']['id'])) {
			return false;
		}
		$this->Behaviors->attach('Tree', array(
			'scope' => array(
				$this->alias . '.vocabulary_id' => $vocabulary['Vocabulary']['id'],
			),
		));
		$treeConditions = array(
			$this->alias . '.vocabulary_id' => $vocabulary['Vocabulary']['id'],
		);
		$tree = $this->generateTreeList($treeConditions, '{n}.' . $this->alias . '.term_id', '{n}.' . $this->alias . '.id');
		$termsIds = array_keys($tree);
		$terms = $this->Term->find('list', array(
			'conditions' => array(
				'Term.id' => $termsIds,
			),
			'fields' => array(
				$options['key'],
				$options['value'],
				'id',
			),
		));

		$termsTree = array();
		foreach ($tree as $termId => $tvId) {
			if (isset($terms[$termId])) {
				$term = $terms[$termId];
				$key = array_keys($term);
				$key = $key['0'];
				$value = $term[$key];
				if (strstr($tvId, '_')) {
					$tvIdN = str_replace('_', '', $tvId);
					$tvIdE = explode($tvIdN, $tvId);
					$value = $tvIdE['0'] . $value;
				}

				if (!$options['taxonomyId']) {
					$termsTree[$key] = $value;
				} else {
					$termsTree[str_replace('_', '', $tvId)] = $value;
				}
			}
		}

		// Write cache
		if (isset($cacheName)) {
			Cache::write($cacheName, $termsTree, $options['cache']['config']);
		}

		return $termsTree;
	}

/**
 * Check if Term HABTM Vocabulary.
 *
 * If yes, return Taxonomy ID
 * otherwise, return false
 *
 * @param integer $termId
 * @param integer $vocabularyId
 * @return boolean
 */
	public function termInVocabulary($termId, $vocabularyId) {
		$taxonomy = $this->find('first', array(
			'conditions' => array(
				$this->alias . '.term_id' => $termId,
				$this->alias . '.vocabulary_id' => $vocabularyId,
			),
		));
		if (isset($taxonomy[$this->alias]['id'])) {
			return $taxonomy[$this->alias]['id'];
		}
		return false;
	}
}
Back to Top