croogo /Taxonomy/Model/Term.php

Language PHP Lines 287
MD5 Hash c999436e8be6a8d9c9031ad27deed4e3
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php

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

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

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

	public $findMethods = array(
		'byVocabulary' => true,
	);

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

/**
 * Validation
 *
 * @var array
 * @access public
 */
	public $validate = array(
		'slug' => array(
			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'This slug has already been taken.',
			),
			'minLength' => array(
				'rule' => array('minLength', 1),
				'message' => 'Slug cannot be empty.',
			),
		),
	);

/**
 * Model associations: hasAndBelongsToMany
 *
 * @var array
 * @access public
 */
	public $hasAndBelongsToMany = array(
		'Vocabulary' => array(
			'className' => 'Taxonomy.Vocabulary',
			'with' => 'Taxonomy',
			'joinTable' => 'taxonomy',
			'foreignKey' => 'term_id',
			'associationForeignKey' => 'vocabulary_id',
			'unique' => true,
		),
	);

/**
 * Save Term and return ID.
 * If another Term with same slug exists, return ID of that Term without saving.
 *
 * @param  array $data
 * @return integer
 */
	public function saveAndGetId($data) {
		$dataToPersist = $data;
		if (!array_key_exists($this->alias, $data)) {
			$dataToPersist = array($this->alias => $data);
		}
		$term = $this->find('first', array(
			'conditions' => array(
				$this->escapeField('slug') => $dataToPersist[$this->alias]['slug'],
			),
		));
		if (isset($term[$this->alias][$this->primaryKey])) {
			$id = $term[$this->alias][$this->primaryKey];
			$update = $dataToPersist[$this->alias];
			if ($id && isset($update['description'])) {
				$this->id = $id;
				$this->saveField('description', $update['description']);
			}
			return $id;
		}

		$this->id = false;
		if ($this->save($dataToPersist)) {
			return $this->id;
		}
		return false;
	}

/**
 * Allow delete on whether given Term has any association left with Taxonomy
 *
 * @return bool
 */
	public function beforeDelete($cascade = true) {
		$Taxonomy = ClassRegistry::init('Taxonomy.Taxonomy');
		$count = $Taxonomy->find('count', array(
			'recursive' => -1,
			'conditions' => array(
				$Taxonomy->escapeField('term_id') => $this->id,
			),
		));
		return $count === 0;
	}

/**
 * Convenience method to check whether term exists within a vocabulary
 *
 * @param integer $id Term Id
 * @param integer $vocabularyId Vocabulary Id
 * @param integer $taxonomyId Taxonomy Id
 * @return bool True if Term exists in Vocabulary
 */
	public function isInVocabulary($id, $vocabularyId, $taxonomyId = null) {
		$conditions = array('term_id' => $id, 'vocabulary_id' => $vocabularyId);
		if (!is_null($taxonomyId)) {
			$conditions['Taxonomy.id !='] = $taxonomyId;
		}
		return $this->Vocabulary->Taxonomy->hasAny($conditions);
	}

/**
 * Save term
 *
 * @see Term::_save()
 * @return array|bool Array of saved term or boolean false
 */
	public function add($data, $vocabularyId) {
		return $this->_save($data, $vocabularyId);
	}

/**
 * Edit term
 *
 * @see Term::_save()
 * @return array|bool Array of saved term or boolean false
 */
	public function edit($data, $vocabularyId) {
		$id = $data[$this->alias][$this->primaryKey];
		$slug = $data[$this->alias]['slug'];

		if ($this->hasSlugChanged($id, $slug) && $this->slugExists($slug)) {
			$edited = false;
		} else {
			$taxonomyId = !empty($data['Taxonomy']['id']) ? $data['Taxonomy']['id'] : null;
			$edited = $this->_save($data, $vocabularyId, $taxonomyId);
		}
		return $edited;
	}

/**
 * Checks wether slug has changed for given Term id
 *
 * @param int $id Term Id
 * @param string $slug Slug
 * @return bool True if slug has changed
 * @throws NotFoundException
 */
	public function hasSlugChanged($id, $slug) {
		if (!is_numeric($id) || !$this->exists($id)) {
			throw new NotFoundException(__d('croogo', 'Invalid Term Id'));
		}
		return $this->field('slug', array($this->escapeField() => $id)) != $slug;
	}

/**
 * Convenience check for slug
 *
 * @return bool
 */
	public function slugExists($slug) {
		return $this->hasAny(compact('slug'));
	}

/**
 * Remove term
 *
 * @param integer $id Term Id
 * @param integer $vocabularyId Vocabulary Id
 */
	public function remove($id, $vocabularyId) {
		$taxonomyId = $this->Vocabulary->Taxonomy->field('id', array(
			'term_id' => $id, 'vocabulary_id' => $vocabularyId
		));
		$this->setScopeForTaxonomy($vocabularyId);
		return $this->Taxonomy->delete($taxonomyId) && $this->delete($id);
	}

/**
 * Save new/updated term data
 *
 * @param array $data Term data
 * @param integer $vocabularyId Vocabulary Id
 * @param integer $taxonomyId Taxonomy Id
 */
	protected function _save($data, $vocabularyId, $taxonomyId = null) {
		$added = false;

		$termId = $this->saveAndGetId($data);
		if (!$this->isInVocabulary($termId, $vocabularyId, $taxonomyId)) {
			$this->setScopeForTaxonomy($vocabularyId);
			$dataToPersist = array(
				'parent_id' => $data['Taxonomy']['parent_id'],
				'term_id' => $termId,
				'vocabulary_id' => $vocabularyId,
			);
			if (!is_null($taxonomyId)) {
				$dataToPersist['id'] = $taxonomyId;
			}
			$added = $this->Taxonomy->save($dataToPersist);
		}
		return $added;
	}

	protected function _findByVocabulary($state, $query, $results = array()) {
		static $termsId = null;
		if (empty($query['vocabulary_id'])) {
			trigger_error(__d('croogo', '"vocabulary_id" key not found'));
		}
		if ($state == 'before') {
			$vocabularyAlias = $this->Vocabulary->field('alias', array('id' => $query['vocabulary_id']));
			$termsId = $this->Vocabulary->Taxonomy->getTree($vocabularyAlias, array('key' => 'id', 'value' => 'title'));
			$defaultQuery = array(
				'conditions' => array(
					$this->escapeField() => array_keys($termsId)
				)
			);
			return array_merge_recursive($defaultQuery, (array)$query);
		}

		$ordered = array_keys($termsId);
		$terms = array();
		foreach ($results as $tempTerm) {
			$term = $tempTerm;
			$id = $term[$this->alias][$this->primaryKey];
			$term[$this->alias]['indent'] = substr_count($termsId[$id], '_');
			$position = array_search($id, $ordered);
			$terms[$position] = $term;
		}
		ksort($terms);

		$termsId = null;
		return $terms;
	}

/**
 * Set Scope
 *
 * @param integer $vocabularyId Vocabulary Id
 */
	public function setScopeForTaxonomy($vocabularyId) {
		$scopeSettings = array('scope' => array(
			'Taxonomy.vocabulary_id' => $vocabularyId,
		));
		if ($this->Vocabulary->Taxonomy->Behaviors->loaded('Tree')) {
			$this->Vocabulary->Taxonomy->Behaviors->Tree->setup($this->Vocabulary->Taxonomy, $scopeSettings);
		} else {
			$this->Vocabulary->Taxonomy->Behaviors->load('Tree', $scopeSettings);
		}
	}
}
Back to Top