croogo /Taxonomy/Model/Behavior/TaxonomizableBehavior.php

Language PHP Lines 266
MD5 Hash deddf3d67b0b91bc0ee34c19e33f2bb5
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
<?php

App::uses('ModelBehavior', 'Model/Behavior');

/**
 * TaxonomizableBehavior
 *
 * @category Taxonomy.Model.Behavior
 * @package  Croogo.Taxonomy.Model.Behavior
 * @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 TaxonomizableBehavior extends ModelBehavior {

/**
 * Taxonomy instance
 */
	protected $_Taxonomy = null;

/**
 * Setup behavior
 *
 * @return void
 */
	public function setup(Model $model, $config = array()) {
		$this->settings[$model->alias] = $config;

		$this->_setupRelationships($model);
		$this->_setupEvents($model);

		$this->_Taxonomy = $model->Taxonomy;
	}

/**
 * Setup relationships
 *
 * @return void
 */
	protected function _setupRelationships(Model $model) {
		$model->bindModel(array(
			'hasAndBelongsToMany' => array(
				'Taxonomy' => array(
					'className' => 'Taxonomy.Taxonomy',
					'with' => 'Taxonomy.ModelTaxonomy',
					'foreignKey' => 'foreign_key',
					'associationForeignKey' => 'taxonomy_id',
					'unique' => true,
					'conditions' => array(
						'model' => $model->alias,
					),
				),
			),
		), false);

		$model->Taxonomy->bindModel(array(
			'hasAndBelongsToMany' => array(
				$model->alias => array(
					'className' => $model->plugin . '.' . $model->alias,
					'with' => 'Taxonomy.ModelTaxonomy',
					'foreignKey' => 'foreign_key',
					'associationForeignKey' => 'taxonomy_id',
					'unique' => true,
					'conditions' => array(
						'model' => $model->alias,
					),
				),
			),
		), false);
	}

/**
 * Setup Event handlers
 *
 * @return void
 */
	protected function _setupEvents($model) {
		$callback = array($this, 'onBeforeSaveNode');
		$eventManager = $model->getEventManager();
		$eventManager->attach($callback, 'Model.Node.beforeSaveNode');
	}

/**
 * Get selected terms from data
 */
	protected function _getSelectedTerms($data) {
		if (isset($data['Taxonomy']['Taxonomy'])) {
			return array_filter($data['Taxonomy']['Taxonomy']);
		} elseif (isset($data['Taxonomy'])) {
			return Hash::extract($data['Taxonomy'], '{n}.taxonomy_id');
		} else {
			return array();
		}
	}

/**
 * Validate Taxonomy data
 */
	public function validateTaxonomyData(Model $model) {
		$typeField = 'type';
		$data =& $model->data;

		if (isset($data[$model->alias][$typeField])) {
			$typeAlias = $data[$model->alias][$typeField];
		} elseif (isset($model->type)) {
			$typeAlias = $model->type;
		} else {
			$this->log('Unable to determine type for model ' . $model->alias);
			return false;
		}

		$type = $this->_Taxonomy->Vocabulary->Type->find('first', array(
			'fields' => array('id', 'title', 'alias'),
			'contain' => array(
				'Vocabulary' => array(
					'fields' => array('id', 'title', 'alias', 'required', 'multiple'),
				),
			),
			'conditions' => array(
				'alias' => $typeAlias,
			),
		));

		if (empty($type)) {
			$this->log('Type ' . $typeAlias . ' cannot be found');
			return true;
		}

		$selectedTerms = $this->_getSelectedTerms($data);

		$result = true;
		$requiredError = __d('croogo', 'Please select at least 1 value');
		$multipleError = __d('croogo', 'Please select at most 1 value');
		foreach ($type['Vocabulary'] as $vocabulary) {
			$fieldName = 'TaxonomyData.' . $vocabulary['id'];
			$terms = $this->_Taxonomy->find('all', array(
				'recursive' => -1,
				'fields' => 'term_id',
				'conditions' => array(
					'vocabulary_id' => $vocabulary['id'],
				),
			));
			$terms = Hash::extract($terms, '{n}.Taxonomy.term_id');
			$selected = count(array_intersect($selectedTerms, $terms));
			if ($vocabulary['required']) {
				if ($selected == 0) {
					$model->invalidate($fieldName, $requiredError);
					$result = false;
				}
			}
			if (!$vocabulary['multiple']) {
				if ($selected > 1) {
					$model->invalidate($fieldName, $multipleError);
					$result = false;
				}
			}
		}
		return $result;
	}

/**
 * Transform TaxonomyData array to a format that can be used for save operation
 *
 * @param array $data Array containing relevant Taxonomy data
 * @param string $typeAlias string Node type alias
 * @return array Formatted data
 * @throws InvalidArgumentException
 */
	public function formatTaxonomyData(Model $model, &$data, $typeAlias) {
		$type = $model->Taxonomy->Vocabulary->Type->findByAlias($typeAlias);
		if (empty($type)) {
			throw new InvalidArgumentException(__d('croogo', 'Invalid Content Type'));
		}
		if (empty($data[$model->alias]['type'])) {
			$data[$model->alias]['type'] = $typeAlias;
		}
		$model->type = $type['Type']['alias'];

		if (!$model->Behaviors->enabled('Tree')) {
			$model->Behaviors->attach('Tree', array(
				'scope' => array(
					$model->escapeField('type') => $model->type,
				),
			));
		}

		if (array_key_exists('TaxonomyData', $data)) {
			$foreignKey = $model->id;
			if (isset($data[$model->alias][$model->primaryKey])) {
				$foreignKey = $data[$model->alias][$model->primaryKey];
			}
			$data['Taxonomy'] = array();
			foreach ($data['TaxonomyData'] as $vocabularyId => $taxonomyIds) {
				if (empty($taxonomyIds)) {
					continue;
				}
				foreach ((array)$taxonomyIds as $taxonomyId) {
					$join = array(
						'model' => $model->alias,
						'foreign_key' => $foreignKey,
						'taxonomy_id' => $taxonomyId,
					);
					$data['Taxonomy'][] = $join;
				}
			}
			unset($data['TaxonomyData']);
		}

		$this->cacheTerms($model, $data);
	}

/**
 * Handle Model.Node.beforeSaveNode event
 *
 * @param CakeEvent $event Event containing `data` and `typeAlias`
 */
	public function onBeforeSaveNode($event) {
		$data = $event->data['data'];
		$typeAlias = $event->data['typeAlias'];
		$this->formatTaxonomyData($event->subject, $data, $typeAlias);
		$event->data['data'] = $data;
	}

/**
 * beforeSave
 *
 * @return bool
 */
	public function beforeSave(Model $model, $options = array()) {
		if (!empty($options['fieldList'])) {
			return true;
		}
		$result = $this->validateTaxonomyData($model);
		if ($result !== true) {
			return $result;
		}
		return true;
	}

/**
 * Caches Term in `terms` field
 *
 * @param Model model
 * @param array $data
 * @return void
 */
	public function cacheTerms(Model $model, &$data = null) {
		if ($data === null) {
			$data =& $model->data;
		}
		$taxonomyIds = $this->_getSelectedTerms($data);
		$taxonomies = $model->Taxonomy->find('all', array(
			'conditions' => array(
				'Taxonomy.id' => $taxonomyIds,
			),
		));
		$terms = Hash::combine($taxonomies, '{n}.Term.id', '{n}.Term.slug');
		$data[$model->alias]['terms'] = $model->encodeData($terms, array(
			'trim' => false,
			'json' => true,
		));
	}

}
Back to Top