PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Taxonomy/Controller/TermsController.php

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