PageRenderTime 94ms CodeModel.GetById 40ms app.highlight 15ms RepoModel.GetById 36ms app.codeStats 0ms

/Taxonomy/Controller/TermsController.php

https://github.com/kareypowell/croogo
PHP | 301 lines | 159 code | 31 blank | 111 comment | 22 complexity | 8a7f9a4b7b297cdb9b1373a6cd202cb2 MD5 | raw file
  1<?php
  2
  3App::uses('TaxonomyAppController', 'Taxonomy.Controller');
  4
  5/**
  6 * Terms Controller
  7 *
  8 * @category Taxonomy.Controller
  9 * @package  Croogo
 10 * @version  1.0
 11 * @author   Fahad Ibnay Heylaal <contact@fahad19.com>
 12 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 13 * @link     http://www.croogo.org
 14 */
 15class TermsController extends TaxonomyAppController {
 16
 17/**
 18 * Controller name
 19 *
 20 * @var string
 21 * @access public
 22 */
 23	public $name = 'Terms';
 24
 25	protected $_redirectUrl = array(
 26		'plugin' => 'taxonomy',
 27		'controller' => 'vocabularies',
 28		'action' => 'index',
 29		'admin' => true
 30	);
 31
 32/**
 33 * Models used by the Controller
 34 *
 35 * @var array
 36 * @access public
 37 */
 38	public $uses = array('Taxonomy.Term');
 39
 40/**
 41 * beforeFilter
 42 *
 43 * @return void
 44 * @access public
 45 */
 46	public function beforeFilter() {
 47		parent::beforeFilter();
 48		$this->vocabularyId = null;
 49		if (isset($this->request->params['named']['vocabulary'])) {
 50			$this->vocabularyId = $this->request->params['named']['vocabulary'];
 51		}
 52		$this->set('vocabulary', $this->vocabularyId);
 53	}
 54
 55/**
 56 * Admin index
 57 *
 58 * @param integer $vocabularyId
 59 * @return void
 60 * @access public
 61 */
 62	public function admin_index($vocabularyId = null) {
 63		$this->__ensureVocabularyIdExists($vocabularyId);
 64
 65		$vocabulary = $this->Term->Vocabulary->read(null, $vocabularyId);
 66		$defaultType = $this->__getDefaultType($vocabulary);
 67		$this->set('title_for_layout', __d('croogo', 'Vocabulary: %s', $vocabulary['Vocabulary']['title']));
 68
 69		$terms = $this->Term->find('byVocabulary', array('vocabulary_id' => $vocabularyId));
 70		$this->set(compact('vocabulary', 'terms', 'defaultType'));
 71
 72		if (isset($this->request->params['named']['links']) || isset($this->request->query['chooser'])) {
 73			$this->layout = 'admin_popup';
 74			$this->render('admin_chooser');
 75		}
 76	}
 77
 78/**
 79 * Admin add
 80 *
 81 * @param integer $vocabularyId
 82 * @return void
 83 * @access public
 84 */
 85	public function admin_add($vocabularyId) {
 86		$this->__ensureVocabularyIdExists($vocabularyId);
 87
 88		$vocabulary = $this->Term->Vocabulary->read(null, $vocabularyId);
 89		$this->set('title_for_layout', __d('croogo', '%s: Add Term', $vocabulary['Vocabulary']['title']));
 90
 91		if ($this->request->is('post')) {
 92			if ($this->Term->add($this->request->data, $vocabularyId)) {
 93				$this->Session->setFlash(__d('croogo', 'Term saved successfuly.'), 'default', array('class' => 'success'));
 94				return $this->redirect(array(
 95					'action' => 'index',
 96					$vocabularyId,
 97				));
 98			} else {
 99				$this->Session->setFlash(__d('croogo', 'Term could not be added to the vocabulary. Please try again.'), 'default', array('class' => 'error'));
100			}
101		}
102		$parentTree = $this->Term->Taxonomy->getTree($vocabulary['Vocabulary']['alias'], array('taxonomyId' => true));
103		$this->set(compact('vocabulary', 'parentTree', 'vocabularyId'));
104	}
105
106/**
107 * Admin edit
108 *
109 * @param integer $id
110 * @param integer $vocabularyId
111 * @return void
112 * @access public
113 */
114	public function admin_edit($id, $vocabularyId) {
115		$this->__ensureVocabularyIdExists($vocabularyId);
116		$this->__ensureTermExists($id);
117		$this->__ensureTaxonomyExists($id, $vocabularyId);
118
119		$vocabulary = $this->Term->Vocabulary->read(null, $vocabularyId);
120		$term = $this->Term->find('first', array(
121			'conditions' => array(
122				'Term.id' => $id,
123			),
124		));
125		$taxonomy = $this->Term->Taxonomy->find('first', array(
126			'conditions' => array(
127				'Taxonomy.term_id' => $id,
128				'Taxonomy.vocabulary_id' => $vocabularyId,
129			),
130		));
131
132		$this->set('title_for_layout', __d('croogo', '%s: Edit Term', $vocabulary['Vocabulary']['title']));
133
134		if ($this->request->is('post') || $this->request->is('put')) {
135			if ($this->Term->edit($this->request->data, $vocabularyId)) {
136				$this->Session->setFlash(__d('croogo', 'Term saved successfuly.'), 'default', array('class' => 'success'));
137				return $this->redirect(array(
138					'action' => 'index',
139					$vocabularyId,
140				));
141			} else {
142				$this->Session->setFlash(__d('croogo', 'Term could not be added to the vocabulary. Please try again.'), 'default', array('class' => 'error'));
143			}
144		} else {
145			$this->request->data['Taxonomy'] = $taxonomy['Taxonomy'];
146			$this->request->data['Term'] = $term['Term'];
147		}
148		$parentTree = $this->Term->Taxonomy->getTree($vocabulary['Vocabulary']['alias'], array('taxonomyId' => true));
149		$this->set(compact('vocabulary', 'parentTree', 'term', 'taxonomy', 'vocabularyId'));
150	}
151
152/**
153 * Admin delete
154 *
155 * @param integer $id
156 * @param integer $vocabularyId
157 * @return void
158 * @access public
159 */
160	public function admin_delete($id = null, $vocabularyId = null) {
161		$redirectUrl = array('action' => 'index', $vocabularyId);
162		$this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
163		$this->__ensureTermExists($id, $redirectUrl);
164		$taxonomyId = $this->Term->Taxonomy->termInVocabulary($id, $vocabularyId);
165		$this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
166
167		if ($this->Term->remove($id, $vocabularyId)) {
168			$messageFlash = __d('croogo', 'Term deleted');
169			$cssClass = array('class' => 'success');
170		} else {
171			$messageFlash = __d('croogo', 'Term could not be deleted. Please, try again.');
172			$cssClass = array('class' => 'error');
173		}
174
175		$this->Session->setFlash($messageFlash, 'default', $cssClass);
176		return $this->redirect($redirectUrl);
177	}
178
179/**
180 * Admin moveup
181 *
182 * @param integer $id
183 * @param integer $vocabularyId
184 * @param integer $step
185 * @return void
186 * @access public
187 */
188	public function admin_moveup($id = null, $vocabularyId = null, $step = 1) {
189		$this->__move('up', $id, $vocabularyId, $step);
190	}
191
192/**
193 * Admin movedown
194 *
195 * @param integer $id
196 * @param integer $vocabularyId
197 * @param integer $step
198 * @return void
199 * @access public
200 */
201	public function admin_movedown($id = null, $vocabularyId = null, $step = 1) {
202		$this->__move('down', $id, $vocabularyId, $step);
203	}
204
205/**
206 * __move
207 *
208 * @param string $direction either 'up' or 'down'
209 * @param integer $id
210 * @param integer $vocabularyId
211 * @param integer $step
212 * @return void
213 * @access private
214 */
215	private function __move($direction, $id, $vocabularyId, $step) {
216		$redirectUrl = array('action' => 'index', $vocabularyId);
217		$this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
218		$this->__ensureTermExists($id, $redirectUrl);
219		$taxonomyId = $this->Term->Taxonomy->termInVocabulary($id, $vocabularyId);
220		$this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
221
222		$this->Term->setScopeForTaxonomy($vocabularyId);
223
224		if ($this->Term->Taxonomy->{'move' . ucfirst($direction)}($taxonomyId, $step)) {
225			$messageFlash = __d('croogo', 'Moved %s successfully', $direction);
226			$cssClass = array('class' => 'success');
227		} else {
228			$messageFlash = __d('croogo', 'Could not move %s', $direction);
229			$cssClass = array('class' => 'error');
230		}
231		$this->Session->setFlash($messageFlash, 'default', $cssClass);
232		return $this->redirect($redirectUrl);
233	}
234
235/**
236 * Get default type from Vocabulary
237 */
238	private function __getDefaultType($vocabulary) {
239		$defaultType = null;
240		if (isset($vocabulary['Type'][0])) {
241			$defaultType = $vocabulary['Type'][0];
242		}
243		if (isset($this->params->query['type_id'])) {
244			if (isset($vocabulary['Type'][$this->request->query['type_id']])) {
245				$defaultType = $vocabulary['Type'][$this->request->query['type_id']];
246			}
247		}
248		return $defaultType;
249	}
250
251/**
252 * Check that Term exists or flash and redirect to $url when it is not found
253 *
254 * @param integer $id Term Id
255 * @param string $url Redirect Url
256 * @return bool True if Term exists
257 */
258	private function __ensureTermExists($id, $url = null) {
259		$redirectUrl = is_null($url) ? $this->_redirectUrl : $url;
260		if (!$this->Term->exists($id)) {
261			$this->Session->setFlash(__d('croogo', 'Invalid Term ID.'), 'default', array('class' => 'error'));
262			return $this->redirect($redirectUrl);
263		}
264	}
265
266/**
267 * Checks that Taxonomy exists or flash redirect to $url when it is not found
268 *
269 * @param integer $id Term Id
270 * @param integer $vocabularyId Vocabulary Id
271 * @param string $url Redirect Url
272 * @return bool True if Term exists
273 */
274	private function __ensureTaxonomyExists($id, $vocabularyId, $url = null) {
275		$redirectUrl = is_null($url) ? $this->_redirectUrl : $url;
276		if (!$this->Term->Taxonomy->hasAny(array('term_id' => $id, 'vocabulary_id' => $vocabularyId))) {
277			$this->Session->setFlash(__d('croogo', 'Invalid Taxonomy.'), 'default', array('class' => 'error'));
278			return $this->redirect($redirectUrl);
279		}
280	}
281
282/**
283 * Checks that Vocabulary exists or flash redirect to $url when it is not found
284 *
285 * @param integer $vocabularyId Vocabulary Id
286 * @param string $url Redirect Url
287 * @return bool True if Term exists
288 */
289	private function __ensureVocabularyIdExists($vocabularyId, $url = null) {
290		$redirectUrl = is_null($url) ? $this->_redirectUrl : $url;
291		if (!$vocabularyId) {
292			return $this->redirect($redirectUrl);
293		}
294
295		if (!$this->Term->Vocabulary->exists($vocabularyId)) {
296			$this->Session->setFlash(__d('croogo', 'Invalid Vocabulary ID.'), 'default', array('class' => 'error'));
297			return $this->redirect($redirectUrl);
298		}
299	}
300
301}