PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Menus/Controller/LinksController.php

https://github.com/kareypowell/croogo
PHP | 353 lines | 226 code | 26 blank | 101 comment | 27 complexity | feac1ace9577dde23a2be6c04a068981 MD5 | raw file
  1. <?php
  2. App::uses('MenusAppController', 'Menus.Controller');
  3. /**
  4. * Links Controller
  5. *
  6. * @category Controller
  7. * @package Croogo.Menus.Controller
  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 LinksController extends MenusAppController {
  14. /**
  15. * Controller name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Links';
  21. /**
  22. * Models used by the Controller
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $uses = array(
  28. 'Menus.Link',
  29. 'Users.Role',
  30. );
  31. /**
  32. * Components
  33. *
  34. * @var array
  35. */
  36. public $components = array(
  37. 'Croogo.BulkProcess',
  38. );
  39. /**
  40. * Menu ID
  41. *
  42. * holds the current menu ID (if any)
  43. *
  44. * @var string
  45. * @access public
  46. */
  47. public $menuId = '';
  48. /**
  49. * afterConstruct
  50. */
  51. public function afterConstruct() {
  52. parent::afterConstruct();
  53. $this->_setupAclComponent();
  54. }
  55. /**
  56. * beforeFilter
  57. *
  58. * @return void
  59. * @access public
  60. */
  61. public function beforeFilter() {
  62. parent::beforeFilter();
  63. $this->Security->unlockedActions[] = 'admin_toggle';
  64. }
  65. /**
  66. * Toggle Link status
  67. *
  68. * @param $id string Link id
  69. * @param $status integer Current Link status
  70. * @return void
  71. */
  72. public function admin_toggle($id = null, $status = null) {
  73. $this->Croogo->fieldToggle($this->Link, $id, $status);
  74. }
  75. /**
  76. * Admin index
  77. *
  78. * @return void
  79. * @access public
  80. */
  81. public function admin_index() {
  82. if (isset($this->request->query['menu_id'])) {
  83. $menuId = $this->request->query['menu_id'];
  84. }
  85. if (empty($menuId)) {
  86. return $this->redirect(array(
  87. 'controller' => 'menus',
  88. 'action' => 'index',
  89. ));
  90. }
  91. $menu = $this->Link->Menu->findById($menuId);
  92. if (!isset($menu['Menu']['id'])) {
  93. return $this->redirect(array(
  94. 'controller' => 'menus',
  95. 'action' => 'index',
  96. ));
  97. }
  98. $this->set('title_for_layout', __d('croogo', 'Links: %s', $menu['Menu']['title']));
  99. $this->Link->recursive = 0;
  100. $linksTree = $this->Link->generateTreeList(array(
  101. 'Link.menu_id' => $menuId,
  102. ));
  103. $linksStatus = $this->Link->find('list', array(
  104. 'conditions' => array(
  105. 'Link.menu_id' => $menuId,
  106. ),
  107. 'fields' => array(
  108. 'Link.id',
  109. 'Link.status',
  110. ),
  111. ));
  112. $this->set(compact('linksTree', 'linksStatus', 'menu'));
  113. if ($this->request->ext === 'json') {
  114. $this->set('_serialize', array('linksTree', 'menu', 'linksStatus'));
  115. }
  116. }
  117. /**
  118. * Admin add
  119. *
  120. * @param integer $menuId
  121. * @return void
  122. * @access public
  123. */
  124. public function admin_add($menuId = null) {
  125. $this->set('title_for_layout', __d('croogo', 'Add Link'));
  126. if (!empty($this->request->data)) {
  127. $this->Link->create();
  128. $this->request->data['Link']['visibility_roles'] = $this->Link->encodeData($this->request->data['Role']['Role']);
  129. $this->Link->setTreeScope($this->request->data['Link']['menu_id']);
  130. if ($this->Link->save($this->request->data)) {
  131. $this->Session->setFlash(__d('croogo', 'The Link has been saved'), 'default', array('class' => 'success'));
  132. if (isset($this->request->data['apply'])) {
  133. return $this->redirect(array('action' => 'edit', $this->Link->id));
  134. } else {
  135. return $this->redirect(array(
  136. 'action' => 'index',
  137. '?' => array(
  138. 'menu_id' => $this->request->data['Link']['menu_id']
  139. )
  140. ));
  141. }
  142. } else {
  143. $this->Session->setFlash(__d('croogo', 'The Link could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  144. }
  145. }
  146. $menus = $this->Link->Menu->find('list');
  147. $roles = $this->Role->find('list');
  148. $parentLinks = $this->Link->generateTreeList(array(
  149. 'Link.menu_id' => $menuId,
  150. ));
  151. $this->set(compact('menus', 'roles', 'parentLinks', 'menuId'));
  152. }
  153. /**
  154. * Admin edit
  155. *
  156. * @param integer $id
  157. * @return void
  158. * @access public
  159. */
  160. public function admin_edit($id = null) {
  161. $this->set('title_for_layout', __d('croogo', 'Edit Link'));
  162. if (!$id && empty($this->request->data)) {
  163. $this->Session->setFlash(__d('croogo', 'Invalid Link'), 'default', array('class' => 'error'));
  164. return $this->redirect(array(
  165. 'controller' => 'menus',
  166. 'action' => 'index',
  167. ));
  168. }
  169. if (!empty($this->request->data)) {
  170. $this->request->data['Link']['visibility_roles'] = $this->Link->encodeData($this->request->data['Role']['Role']);
  171. if ($this->Link->save($this->request->data)) {
  172. $this->Session->setFlash(__d('croogo', 'The Link has been saved'), 'default', array('class' => 'success'));
  173. if (isset($this->request->data['apply'])) {
  174. return $this->redirect(array('action' => 'edit', $this->Link->id));
  175. } else {
  176. return $this->redirect(array(
  177. 'action' => 'index',
  178. '?' => array(
  179. 'menu_id' => $this->request->data['Link']['menu_id']
  180. )
  181. ));
  182. }
  183. } else {
  184. $this->Session->setFlash(__d('croogo', 'The Link could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  185. }
  186. }
  187. if (empty($this->request->data)) {
  188. $data = $this->Link->read(null, $id);
  189. $data['Role']['Role'] = $this->Link->decodeData($data['Link']['visibility_roles']);
  190. $this->request->data = $data;
  191. }
  192. $menus = $this->Link->Menu->find('list');
  193. $roles = $this->Role->find('list');
  194. $menu = $this->Link->Menu->findById($this->request->data['Link']['menu_id']);
  195. $parentLinks = $this->Link->generateTreeList(array(
  196. 'Link.menu_id' => $menu['Menu']['id'],
  197. ));
  198. $menuId = $menu['Menu']['id'];
  199. $this->set(compact('menus', 'roles', 'parentLinks', 'menuId'));
  200. }
  201. /**
  202. * Admin delete
  203. *
  204. * @param integer $id
  205. * @return void
  206. * @access public
  207. */
  208. public function admin_delete($id = null) {
  209. if (!$id) {
  210. $this->Session->setFlash(__d('croogo', 'Invalid id for Link'), 'default', array('class' => 'error'));
  211. return $this->redirect(array(
  212. 'controller' => 'menus',
  213. 'action' => 'index',
  214. ));
  215. }
  216. $link = $this->Link->findById($id);
  217. if (!isset($link['Link']['id'])) {
  218. $this->Session->setFlash(__d('croogo', 'Invalid id for Link'), 'default', array('class' => 'error'));
  219. return $this->redirect(array(
  220. 'controller' => 'menus',
  221. 'action' => 'index',
  222. ));
  223. }
  224. $this->Link->setTreeScope($link['Link']['menu_id']);
  225. if ($this->Link->delete($id)) {
  226. $this->Session->setFlash(__d('croogo', 'Link deleted'), 'default', array('class' => 'success'));
  227. return $this->redirect(array(
  228. 'action' => 'index',
  229. '?' => array(
  230. 'menu_id' => $link['Link']['menu_id'],
  231. ),
  232. ));
  233. }
  234. }
  235. /**
  236. * Admin moveup
  237. *
  238. * @param integer $id
  239. * @param integer $step
  240. * @return void
  241. * @access public
  242. */
  243. public function admin_moveup($id, $step = 1) {
  244. $link = $this->Link->findById($id);
  245. if (!isset($link['Link']['id'])) {
  246. $this->Session->setFlash(__d('croogo', 'Invalid id for Link'), 'default', array('class' => 'error'));
  247. return $this->redirect(array(
  248. 'controller' => 'menus',
  249. 'action' => 'index',
  250. ));
  251. }
  252. $this->Link->setTreeScope($link['Link']['menu_id']);
  253. if ($this->Link->moveUp($id, $step)) {
  254. Cache::clearGroup('menus','croogo_menus');
  255. $this->Session->setFlash(__d('croogo', 'Moved up successfully'), 'default', array('class' => 'success'));
  256. } else {
  257. $this->Session->setFlash(__d('croogo', 'Could not move up'), 'default', array('class' => 'error'));
  258. }
  259. return $this->redirect(array(
  260. 'action' => 'index',
  261. '?' => array(
  262. 'menu_id' => $link['Link']['menu_id'],
  263. ),
  264. ));
  265. }
  266. /**
  267. * Admin movedown
  268. *
  269. * @param integer $id
  270. * @param integer $step
  271. * @return void
  272. * @access public
  273. */
  274. public function admin_movedown($id, $step = 1) {
  275. $link = $this->Link->findById($id);
  276. if (!isset($link['Link']['id'])) {
  277. $this->Session->setFlash(__d('croogo', 'Invalid id for Link'), 'default', array('class' => 'error'));
  278. return $this->redirect(array(
  279. 'controller' => 'menus',
  280. 'action' => 'index',
  281. ));
  282. }
  283. $this->Link->setTreeScope($link['Link']['menu_id']);
  284. if ($this->Link->moveDown($id, $step)) {
  285. Cache::clearGroup('menus','croogo_menus');
  286. $this->Session->setFlash(__d('croogo', 'Moved down successfully'), 'default', array('class' => 'success'));
  287. } else {
  288. $this->Session->setFlash(__d('croogo', 'Could not move down'), 'default', array('class' => 'error'));
  289. }
  290. return $this->redirect(array(
  291. 'action' => 'index',
  292. '?' => array(
  293. 'menu_id' => $link['Link']['menu_id'],
  294. ),
  295. ));
  296. }
  297. /**
  298. * Admin process
  299. *
  300. * @param integer $menuId
  301. * @return void
  302. * @access public
  303. */
  304. public function admin_process($menuId = null) {
  305. $Link = $this->{$this->modelClass};
  306. list($action, $ids) = $this->BulkProcess->getRequestVars($Link->alias);
  307. $redirect = array('action' => 'index');
  308. $menu = $this->Link->Menu->findById($menuId);
  309. if (isset($menu['Menu']['id'])) {
  310. $redirect['?'] = array('menu_id' => $menuId);
  311. }
  312. $this->Link->setTreeScope($menuId);
  313. $multiple = array('copy' => false);
  314. $messageMap = array(
  315. 'delete' => __d('croogo', 'Links deleted'),
  316. 'publish' => __d('croogo', 'Links published'),
  317. 'unpublish' => __d('croogo', 'Links unpublished'),
  318. );
  319. $options = compact('multiple', 'redirect', 'messageMap');
  320. return $this->BulkProcess->process($this->Link, $action, $ids, $options);
  321. }
  322. public function admin_link_chooser() {
  323. Croogo::dispatchEvent('Controller.Links.setupLinkChooser', $this);
  324. $linkChoosers = Configure::read('Menus.linkChoosers');
  325. $this->set(compact('linkChoosers'));
  326. }
  327. }