PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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. 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.'), 'default', array('class' => 'success'));
  81. return $this->redirect(array(
  82. 'action' => 'index',
  83. $vocabularyId,
  84. ));
  85. } else {
  86. $this->Session->setFlash(__d('croogo', 'Term could not be added to the vocabulary. Please try again.'), 'default', 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.'), 'default', array('class' => 'success'));
  120. return $this->redirect(array(
  121. 'action' => 'index',
  122. $vocabularyId,
  123. ));
  124. } else {
  125. $this->Session->setFlash(__d('croogo', 'Term could not be added to the vocabulary. Please try again.'), 'default', array('class' => 'error'));
  126. }
  127. } else {
  128. $this->request->data['Taxonomy'] = $taxonomy['Taxonomy'];
  129. $this->request->data['Term'] = $term['Term'];
  130. }
  131. $parentTree = $this->Term->Taxonomy->getTree($vocabulary['Vocabulary']['alias'], array('taxonomyId' => true));
  132. $this->set(compact('vocabulary', 'parentTree', 'term', 'taxonomy', 'vocabularyId'));
  133. }
  134. /**
  135. * Admin delete
  136. *
  137. * @param integer $id
  138. * @param integer $vocabularyId
  139. * @return void
  140. * @access public
  141. */
  142. public function admin_delete($id = null, $vocabularyId = null) {
  143. $redirectUrl = array('action' => 'index', $vocabularyId);
  144. $this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
  145. $this->__ensureTermExists($id, $redirectUrl);
  146. $taxonomyId = $this->Term->Taxonomy->termInVocabulary($id, $vocabularyId);
  147. $this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
  148. if ($this->Term->remove($id, $vocabularyId)) {
  149. $messageFlash = __d('croogo', 'Term deleted');
  150. $cssClass = array('class' => 'success');
  151. } else {
  152. $messageFlash = __d('croogo', 'Term could not be deleted. Please, try again.');
  153. $cssClass = array('class' => 'error');
  154. }
  155. $this->Session->setFlash($messageFlash, 'default', $cssClass);
  156. return $this->redirect($redirectUrl);
  157. }
  158. /**
  159. * Admin moveup
  160. *
  161. * @param integer $id
  162. * @param integer $vocabularyId
  163. * @param integer $step
  164. * @return void
  165. * @access public
  166. */
  167. public function admin_moveup($id = null, $vocabularyId = null, $step = 1) {
  168. $this->__move('up', $id, $vocabularyId, $step);
  169. }
  170. /**
  171. * Admin movedown
  172. *
  173. * @param integer $id
  174. * @param integer $vocabularyId
  175. * @param integer $step
  176. * @return void
  177. * @access public
  178. */
  179. public function admin_movedown($id = null, $vocabularyId = null, $step = 1) {
  180. $this->__move('down', $id, $vocabularyId, $step);
  181. }
  182. /**
  183. * __move
  184. *
  185. * @param string $direction either 'up' or 'down'
  186. * @param integer $id
  187. * @param integer $vocabularyId
  188. * @param integer $step
  189. * @return void
  190. * @access private
  191. */
  192. private function __move($direction, $id, $vocabularyId, $step) {
  193. $redirectUrl = array('action' => 'index', $vocabularyId);
  194. $this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
  195. $this->__ensureTermExists($id, $redirectUrl);
  196. $taxonomyId = $this->Term->Taxonomy->termInVocabulary($id, $vocabularyId);
  197. $this->__ensureVocabularyIdExists($vocabularyId, $redirectUrl);
  198. $this->Term->setScopeForTaxonomy($vocabularyId);
  199. if ($this->Term->Taxonomy->{'move' . ucfirst($direction)}($taxonomyId, $step)) {
  200. $messageFlash = __d('croogo', 'Moved %s successfully', $direction);
  201. $cssClass = array('class' => 'success');
  202. } else {
  203. $messageFlash = __d('croogo', 'Could not move %s', $direction);
  204. $cssClass = array('class' => 'error');
  205. }
  206. $this->Session->setFlash($messageFlash, 'default', $cssClass);
  207. return $this->redirect($redirectUrl);
  208. }
  209. /**
  210. * Get default type from Vocabulary
  211. */
  212. private function __getDefaultType($vocabulary) {
  213. $defaultType = null;
  214. if (isset($vocabulary['Type'][0])) {
  215. $defaultType = $vocabulary['Type'][0];
  216. }
  217. if (isset($this->params->query['type_id'])) {
  218. if (isset($vocabulary['Type'][$this->request->query['type_id']])) {
  219. $defaultType = $vocabulary['Type'][$this->request->query['type_id']];
  220. }
  221. }
  222. return $defaultType;
  223. }
  224. /**
  225. * Check that Term exists or flash and redirect to $url when it is not found
  226. *
  227. * @param integer $id Term Id
  228. * @param string $url Redirect Url
  229. * @return bool True if Term exists
  230. */
  231. private function __ensureTermExists($id, $url = null) {
  232. $redirectUrl = is_null($url) ? $this->_redirectUrl : $url;
  233. if (!$this->Term->exists($id)) {
  234. $this->Session->setFlash(__d('croogo', 'Invalid Term ID.'), 'default', array('class' => 'error'));
  235. return $this->redirect($redirectUrl);
  236. }
  237. }
  238. /**
  239. * Checks that Taxonomy exists or flash redirect to $url when it is not found
  240. *
  241. * @param integer $id Term Id
  242. * @param integer $vocabularyId Vocabulary Id
  243. * @param string $url Redirect Url
  244. * @return bool True if Term exists
  245. */
  246. private function __ensureTaxonomyExists($id, $vocabularyId, $url = null) {
  247. $redirectUrl = is_null($url) ? $this->_redirectUrl : $url;
  248. if (!$this->Term->Taxonomy->hasAny(array('term_id' => $id, 'vocabulary_id' => $vocabularyId))) {
  249. $this->Session->setFlash(__d('croogo', 'Invalid Taxonomy.'), 'default', array('class' => 'error'));
  250. return $this->redirect($redirectUrl);
  251. }
  252. }
  253. /**
  254. * Checks that Vocabulary exists or flash redirect to $url when it is not found
  255. *
  256. * @param integer $vocabularyId Vocabulary Id
  257. * @param string $url Redirect Url
  258. * @return bool True if Term exists
  259. */
  260. private function __ensureVocabularyIdExists($vocabularyId, $url = null) {
  261. $redirectUrl = is_null($url) ? $this->_redirectUrl : $url;
  262. if (!$vocabularyId) {
  263. return $this->redirect($redirectUrl);
  264. }
  265. if (!$this->Term->Vocabulary->exists($vocabularyId)) {
  266. $this->Session->setFlash(__d('croogo', 'Invalid Vocabulary ID.'), 'default', array('class' => 'error'));
  267. return $this->redirect($redirectUrl);
  268. }
  269. }
  270. }