PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Controller/TermsController.php

https://github.com/okatsuralau/croogo
PHP | 379 lines | 284 code | 17 blank | 78 comment | 46 complexity | f5cd671603e07cf961e574ce0a705cea MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Terms Controller
  4. *
  5. * PHP version 5
  6. *
  7. * @category Controller
  8. * @package Croogo
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class TermsController extends AppController {
  15. /**
  16. * Controller name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'Terms';
  22. /**
  23. * Models used by the Controller
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. public $uses = array('Term');
  29. /**
  30. * beforeFilter
  31. *
  32. * @return void
  33. * @access public
  34. */
  35. public function beforeFilter() {
  36. parent::beforeFilter();
  37. $this->vocabularyId = null;
  38. if (isset($this->request->params['named']['vocabulary'])) {
  39. $this->vocabularyId = $this->request->params['named']['vocabulary'];
  40. }
  41. $this->set('vocabulary', $this->vocabularyId);
  42. }
  43. /**
  44. * Admin index
  45. *
  46. * @param integer $vocabularyId
  47. * @return void
  48. * @access public
  49. */
  50. public function admin_index($vocabularyId = null) {
  51. if (!$vocabularyId) {
  52. $this->redirect(array(
  53. 'controller' => 'vocabularies',
  54. 'action' => 'index',
  55. ));
  56. }
  57. $vocabulary = $this->Term->Vocabulary->findById($vocabularyId);
  58. if (!isset($vocabulary['Vocabulary']['id'])) {
  59. $this->Session->setFlash(__('Invalid Vocabulary ID.'), 'default', array('class' => 'error'));
  60. $this->redirect(array(
  61. 'controller' => 'vocabularies',
  62. 'action' => 'index',
  63. ));
  64. }
  65. $this->set('title_for_layout', sprintf(__('Vocabulary: %s'), $vocabulary['Vocabulary']['title']));
  66. $termsTree = $this->Term->Taxonomy->getTree($vocabulary['Vocabulary']['alias'], array(
  67. 'key' => 'id',
  68. 'value' => 'title',
  69. ));
  70. $terms = $this->Term->find('all', array(
  71. 'conditions' => array(
  72. 'Term.id' => array_keys($termsTree),
  73. ),
  74. ));
  75. $terms = Set::combine($terms, '{n}.Term.id', '{n}.Term');
  76. $this->set(compact('termsTree', 'vocabulary', 'terms'));
  77. }
  78. /**
  79. * Admin add
  80. *
  81. * @param integer $vocabularyId
  82. * @return void
  83. * @access public
  84. */
  85. public function admin_add($vocabularyId = null) {
  86. if (!$vocabularyId) {
  87. $this->redirect(array(
  88. 'controller' => 'vocabularies',
  89. 'action' => 'index',
  90. ));
  91. }
  92. $vocabulary = $this->Term->Vocabulary->find('first', array(
  93. 'conditions' => array(
  94. 'Vocabulary.id' => $vocabularyId,
  95. ),
  96. ));
  97. if (!isset($vocabulary['Vocabulary']['id'])) {
  98. $this->redirect(array(
  99. 'controller' => 'vocabularies',
  100. 'action' => 'index',
  101. ));
  102. }
  103. $this->set('title_for_layout', sprintf(__('%s: Add Term'), $vocabulary['Vocabulary']['title']));
  104. if (!empty($this->request->data)) {
  105. $termId = $this->Term->saveAndGetId($this->request->data['Term']);
  106. if ($termId) {
  107. $termInVocabulary = $this->Term->Taxonomy->hasAny(array(
  108. 'Taxonomy.vocabulary_id' => $vocabularyId,
  109. 'Taxonomy.term_id' => $termId,
  110. ));
  111. if ($termInVocabulary) {
  112. $this->Session->setFlash(__('Term with same slug already exists in the vocabulary.'), 'default', array('class' => 'error'));
  113. } else {
  114. $this->Term->Taxonomy->Behaviors->attach('Tree', array(
  115. 'scope' => array(
  116. 'Taxonomy.vocabulary_id' => $vocabularyId,
  117. ),
  118. ));
  119. $taxonomy = array(
  120. 'parent_id' => $this->request->data['Taxonomy']['parent_id'],
  121. 'term_id' => $termId,
  122. 'vocabulary_id' => $vocabularyId,
  123. );
  124. if ($this->Term->Taxonomy->save($taxonomy)) {
  125. $this->Session->setFlash(__('Term saved successfuly.'), 'default', array('class' => 'success'));
  126. $this->redirect(array(
  127. 'action' => 'index',
  128. $vocabularyId,
  129. ));
  130. } else {
  131. $this->Session->setFlash(__('Term could not be added to the vocabulary. Please try again.'), 'default', array('class' => 'error'));
  132. }
  133. }
  134. } else {
  135. $this->Session->setFlash(__('Term could not be saved. Please try again.'), 'default', array('class' => 'error'));
  136. }
  137. }
  138. $parentTree = $this->Term->Taxonomy->getTree($vocabulary['Vocabulary']['alias'], array('taxonomyId' => true));
  139. $this->set(compact('vocabulary', 'parentTree', 'vocabularyId'));
  140. }
  141. /**
  142. * Admin edit
  143. *
  144. * @param integer $id
  145. * @param integer $vocabularyId
  146. * @return void
  147. * @access public
  148. */
  149. public function admin_edit($id = null, $vocabularyId = null) {
  150. if (!$vocabularyId) {
  151. $this->redirect(array(
  152. 'controller' => 'vocabularies',
  153. 'action' => 'index',
  154. ));
  155. }
  156. $vocabulary = $this->Term->Vocabulary->find('first', array(
  157. 'conditions' => array(
  158. 'Vocabulary.id' => $vocabularyId,
  159. ),
  160. ));
  161. if (!isset($vocabulary['Vocabulary']['id'])) {
  162. $this->redirect(array(
  163. 'controller' => 'vocabularies',
  164. 'action' => 'index',
  165. ));
  166. }
  167. $term = $this->Term->find('first', array(
  168. 'conditions' => array(
  169. 'Term.id' => $id,
  170. ),
  171. ));
  172. if (!isset($term['Term']['id'])) {
  173. $this->redirect(array(
  174. 'controller' => 'vocabularies',
  175. 'action' => 'index',
  176. ));
  177. }
  178. $taxonomy = $this->Term->Taxonomy->find('first', array(
  179. 'conditions' => array(
  180. 'Taxonomy.term_id' => $id,
  181. 'Taxonomy.vocabulary_id' => $vocabularyId,
  182. ),
  183. ));
  184. if (!isset($taxonomy['Taxonomy']['id'])) {
  185. $this->redirect(array(
  186. 'controller' => 'vocabularies',
  187. 'action' => 'index',
  188. ));
  189. }
  190. $this->set('title_for_layout', sprintf(__('%s: Edit Term'), $vocabulary['Vocabulary']['title']));
  191. if (!empty($this->request->data)) {
  192. if ($term['Term']['slug'] != $this->request->data['Term']['slug']) {
  193. if ($this->Term->hasAny(array('Term.slug' => $this->request->data['Term']['slug']))) {
  194. $termId = false;
  195. } else {
  196. $termId = $this->Term->saveAndGetId($this->request->data['Term']);
  197. }
  198. } else {
  199. $this->Term->id = $term['Term']['id'];
  200. if (!$this->Term->save($this->request->data['Term'])) {
  201. $termId = false;
  202. } else {
  203. $termId = $term['Term']['id'];
  204. }
  205. }
  206. if ($termId) {
  207. $termInVocabulary = $this->Term->Taxonomy->hasAny(array(
  208. 'Taxonomy.id !=' => $taxonomy['Taxonomy']['id'],
  209. 'Taxonomy.vocabulary_id' => $vocabularyId,
  210. 'Taxonomy.term_id' => $termId,
  211. ));
  212. if ($termInVocabulary) {
  213. $this->Session->setFlash(__('Term with same slug already exists in the vocabulary.'), 'default', array('class' => 'error'));
  214. } else {
  215. $this->Term->Taxonomy->Behaviors->attach('Tree', array(
  216. 'scope' => array(
  217. 'Taxonomy.vocabulary_id' => $vocabularyId,
  218. ),
  219. ));
  220. $taxonomy = array(
  221. 'id' => $taxonomy['Taxonomy']['id'],
  222. 'parent_id' => $this->request->data['Taxonomy']['parent_id'],
  223. 'term_id' => $termId,
  224. 'vocabulary_id' => $vocabularyId,
  225. );
  226. if ($this->Term->Taxonomy->save($taxonomy)) {
  227. $this->Session->setFlash(__('Term saved successfuly.'), 'default', array('class' => 'success'));
  228. $this->redirect(array(
  229. 'action' => 'index',
  230. $vocabularyId,
  231. ));
  232. } else {
  233. $this->Session->setFlash(__('Term could not be added to the vocabulary. Please try again.'), 'default', array('class' => 'error'));
  234. }
  235. }
  236. } else {
  237. $this->Session->setFlash(__('Term could not be saved. Please try again.'), 'default', array('class' => 'error'));
  238. }
  239. } else {
  240. $this->request->data['Taxonomy'] = $taxonomy['Taxonomy'];
  241. $this->request->data['Term'] = $term['Term'];
  242. }
  243. $parentTree = $this->Term->Taxonomy->getTree($vocabulary['Vocabulary']['alias'], array('taxonomyId' => true));
  244. $this->set(compact('vocabulary', 'parentTree', 'term', 'taxonomy', 'vocabularyId'));
  245. }
  246. /**
  247. * Admin delete
  248. *
  249. * @param integer $id
  250. * @param integer $vocabularyId
  251. * @return void
  252. * @access public
  253. */
  254. public function admin_delete($id = null, $vocabularyId = null) {
  255. if (!$id || !$vocabularyId) {
  256. $this->Session->setFlash(__('Invalid id for Term'), 'default', array('class' => 'error'));
  257. $this->redirect(array(
  258. 'action' => 'index',
  259. $vocabularyId,
  260. ));
  261. }
  262. $taxonomyId = $this->Term->Taxonomy->termInVocabulary($id, $vocabularyId);
  263. if (!$taxonomyId) {
  264. $this->redirect(array(
  265. 'action' => 'index',
  266. $vocabularyId,
  267. ));
  268. }
  269. $this->Term->Taxonomy->Behaviors->attach('Tree', array(
  270. 'scope' => array(
  271. 'Taxonomy.vocabulary_id' => $vocabularyId,
  272. ),
  273. ));
  274. if ($this->Term->Taxonomy->delete($taxonomyId)) {
  275. $this->Session->setFlash(__('Term deleted'), 'default', array('class' => 'success'));
  276. } else {
  277. $this->Session->setFlash(__('Term could not be deleted. Please, try again.'), 'default', array('class' => 'error'));
  278. }
  279. $this->redirect(array(
  280. 'action' => 'index',
  281. $vocabularyId,
  282. ));
  283. }
  284. /**
  285. * Admin moveup
  286. *
  287. * @param integer $id
  288. * @param integer $vocabularyId
  289. * @param integer $step
  290. * @return void
  291. * @access public
  292. */
  293. public function admin_moveup($id = null, $vocabularyId = null, $step = 1) {
  294. if (!$id || !$vocabularyId) {
  295. $this->Session->setFlash(__('Invalid id for Term'), 'default', array('class' => 'error'));
  296. $this->redirect(array(
  297. 'action' => 'index',
  298. $vocabularyId,
  299. ));
  300. }
  301. $taxonomyId = $this->Term->Taxonomy->termInVocabulary($id, $vocabularyId);
  302. if (!$taxonomyId) {
  303. $this->redirect(array(
  304. 'action' => 'index',
  305. $vocabularyId,
  306. ));
  307. }
  308. $this->Term->Taxonomy->Behaviors->attach('Tree', array(
  309. 'scope' => array(
  310. 'Taxonomy.vocabulary_id' => $vocabularyId,
  311. ),
  312. ));
  313. if ($this->Term->Taxonomy->moveUp($taxonomyId, $step)) {
  314. $this->Session->setFlash(__('Moved up successfully'), 'default', array('class' => 'success'));
  315. } else {
  316. $this->Session->setFlash(__('Could not move up'), 'default', array('class' => 'error'));
  317. }
  318. $this->redirect(array(
  319. 'action' => 'index',
  320. $vocabularyId,
  321. ));
  322. }
  323. /**
  324. * Admin movedown
  325. *
  326. * @param integer $id
  327. * @param integer $vocabularyId
  328. * @param integer $step
  329. * @return void
  330. * @access public
  331. */
  332. public function admin_movedown($id = null, $vocabularyId = null, $step = 1) {
  333. if (!$id || !$vocabularyId) {
  334. $this->Session->setFlash(__('Invalid id for Term'), 'default', array('class' => 'error'));
  335. $this->redirect(array(
  336. 'action' => 'index',
  337. $vocabularyId,
  338. ));
  339. }
  340. $taxonomyId = $this->Term->Taxonomy->termInVocabulary($id, $vocabularyId);
  341. if (!$taxonomyId) {
  342. $this->redirect(array(
  343. 'action' => 'index',
  344. $vocabularyId,
  345. ));
  346. }
  347. $this->Term->Taxonomy->Behaviors->attach('Tree', array(
  348. 'scope' => array(
  349. 'Taxonomy.vocabulary_id' => $vocabularyId,
  350. ),
  351. ));
  352. if ($this->Term->Taxonomy->moveDown($taxonomyId, $step)) {
  353. $this->Session->setFlash(__('Moved down successfully'), 'default', array('class' => 'success'));
  354. } else {
  355. $this->Session->setFlash(__('Could not move down'), 'default', array('class' => 'error'));
  356. }
  357. $this->redirect(array(
  358. 'action' => 'index',
  359. $vocabularyId,
  360. ));
  361. }
  362. }