PageRenderTime 208ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Blocks/Controller/BlocksController.php

https://github.com/kareypowell/croogo
PHP | 253 lines | 128 code | 25 blank | 100 comment | 18 complexity | 984a5483f3dd2fd13620b4a27bd6b01d MD5 | raw file
  1. <?php
  2. App::uses('BlocksAppController', 'Blocks.Controller');
  3. /**
  4. * Blocks Controller
  5. *
  6. * @category Blocks.Controller
  7. * @package Croogo.Blocks.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 BlocksController extends BlocksAppController {
  14. /**
  15. * Controller name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Blocks';
  21. /**
  22. * Components
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $components = array(
  28. 'Croogo.BulkProcess',
  29. 'Search.Prg' => array(
  30. 'presetForm' => array(
  31. 'paramType' => 'querystring',
  32. ),
  33. 'commonProcess' => array(
  34. 'paramType' => 'querystring',
  35. 'filterEmpty' => true,
  36. ),
  37. ),
  38. );
  39. /**
  40. * Preset Variables Search
  41. *
  42. * @var array
  43. * @access public
  44. */
  45. public $presetVars = true;
  46. /**
  47. * Models used by the Controller
  48. *
  49. * @var array
  50. * @access public
  51. */
  52. public $uses = array('Blocks.Block', 'Users.Role');
  53. /**
  54. * afterConstruct
  55. * @see AppController::afterConstruct()
  56. */
  57. public function afterConstruct() {
  58. parent::afterConstruct();
  59. $this->_setupAclComponent();
  60. }
  61. /**
  62. * beforeFilter
  63. *
  64. * @return void
  65. * @access public
  66. */
  67. public function beforeFilter() {
  68. parent::beforeFilter();
  69. $this->Security->unlockedActions[] = 'admin_toggle';
  70. }
  71. /**
  72. * Toggle Block status
  73. *
  74. * @param $id string Block id
  75. * @param $status integer Current Block status
  76. * @return void
  77. */
  78. public function admin_toggle($id = null, $status = null) {
  79. $this->Croogo->fieldToggle($this->Block, $id, $status);
  80. }
  81. /**
  82. * Admin index
  83. *
  84. * @return void
  85. * @access public
  86. * $searchField : Identify fields for search
  87. */
  88. public function admin_index() {
  89. $this->set('title_for_layout', __d('croogo', 'Blocks'));
  90. $this->Prg->commonProcess();
  91. $searchFields = array('region_id', 'title');
  92. $this->Block->recursive = 0;
  93. $this->paginate['Block']['order'] = array('Block.weight' => 'ASC');
  94. $criteria = $this->Block->parseCriteria($this->Prg->parsedParams());
  95. $this->set('blocks', $this->paginate($criteria));
  96. $this->set('regions', $this->Block->Region->find('list'));
  97. $this->set('searchFields', $searchFields);
  98. if (isset($this->request->query['chooser'])) {
  99. $this->layout = 'admin_popup';
  100. }
  101. }
  102. /**
  103. * Admin add
  104. *
  105. * @return void
  106. * @access public
  107. */
  108. public function admin_add() {
  109. $this->set('title_for_layout', __d('croogo', 'Add Block'));
  110. if (!empty($this->request->data)) {
  111. $this->Block->create();
  112. $this->request->data['Block']['visibility_roles'] = $this->Block->encodeData($this->request->data['Role']['Role']);
  113. $this->request->data['Block']['visibility_paths'] = $this->Block->encodeData(explode("\n", $this->request->data['Block']['visibility_paths']));
  114. if ($this->Block->save($this->request->data)) {
  115. $this->Session->setFlash(__d('croogo', 'The Block has been saved'), 'default', array('class' => 'success'));
  116. $this->Croogo->redirect(array('action' => 'edit', $this->Block->id));
  117. } else {
  118. $this->Session->setFlash(__d('croogo', 'The Block could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  119. }
  120. }
  121. $regions = $this->Block->Region->find('list');
  122. $roles = $this->Role->find('list');
  123. $this->set(compact('regions', 'roles'));
  124. }
  125. /**
  126. * Admin edit
  127. *
  128. * @param integer $id
  129. * @return void
  130. * @access public
  131. */
  132. public function admin_edit($id = null) {
  133. $this->set('title_for_layout', __d('croogo', 'Edit Block'));
  134. if (!$id && empty($this->request->data)) {
  135. $this->Session->setFlash(__d('croogo', 'Invalid Block'), 'default', array('class' => 'error'));
  136. return $this->redirect(array('action' => 'index'));
  137. }
  138. if (!empty($this->request->data)) {
  139. $this->request->data['Block']['visibility_roles'] = $this->Block->encodeData($this->request->data['Role']['Role']);
  140. $this->request->data['Block']['visibility_paths'] = $this->Block->encodeData(explode("\n", $this->request->data['Block']['visibility_paths']));
  141. if ($this->Block->save($this->request->data)) {
  142. $this->Session->setFlash(__d('croogo', 'The Block has been saved'), 'default', array('class' => 'success'));
  143. $this->Croogo->redirect(array('action' => 'edit', $id));
  144. } else {
  145. $this->Session->setFlash(__d('croogo', 'The Block could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  146. }
  147. }
  148. if (empty($this->request->data)) {
  149. $data = $this->Block->read(null, $id);
  150. $data['Role']['Role'] = $this->Block->decodeData($data['Block']['visibility_roles']);
  151. if ($data['Block']['visibility_paths'] != '') {
  152. $data['Block']['visibility_paths'] = implode("\n", $this->Block->decodeData($data['Block']['visibility_paths']));
  153. }
  154. $this->request->data = $data;
  155. }
  156. $regions = $this->Block->Region->find('list');
  157. $roles = $this->Role->find('list');
  158. $this->set(compact('regions', 'roles'));
  159. }
  160. /**
  161. * Admin delete
  162. *
  163. * @param integer $id
  164. * @return void
  165. * @access public
  166. */
  167. public function admin_delete($id = null) {
  168. if (!$id) {
  169. $this->Session->setFlash(__d('croogo', 'Invalid id for Block'), 'default', array('class' => 'error'));
  170. return $this->redirect(array('action' => 'index'));
  171. }
  172. if ($this->Block->delete($id)) {
  173. $this->Session->setFlash(__d('croogo', 'Block deleted'), 'default', array('class' => 'success'));
  174. return $this->redirect(array('action' => 'index'));
  175. }
  176. }
  177. /**
  178. * Admin moveup
  179. *
  180. * @param integer $id
  181. * @param integer $step
  182. * @return void
  183. * @access public
  184. */
  185. public function admin_moveup($id, $step = 1) {
  186. if ($this->Block->moveUp($id, $step)) {
  187. $this->Session->setFlash(__d('croogo', 'Moved up successfully'), 'default', array('class' => 'success'));
  188. } else {
  189. $this->Session->setFlash(__d('croogo', 'Could not move up'), 'default', array('class' => 'error'));
  190. }
  191. return $this->redirect(array('action' => 'index'));
  192. }
  193. /**
  194. * Admin movedown
  195. *
  196. * @param integer $id
  197. * @param integer $step
  198. * @return void
  199. * @access public
  200. */
  201. public function admin_movedown($id, $step = 1) {
  202. if ($this->Block->moveDown($id, $step)) {
  203. $this->Session->setFlash(__d('croogo', 'Moved down successfully'), 'default', array('class' => 'success'));
  204. } else {
  205. $this->Session->setFlash(__d('croogo', 'Could not move down'), 'default', array('class' => 'error'));
  206. }
  207. return $this->redirect(array('action' => 'index'));
  208. }
  209. /**
  210. * Admin process
  211. *
  212. * @return void
  213. * @access public
  214. */
  215. public function admin_process() {
  216. $Block = $this->{$this->modelClass};
  217. list($action, $ids) = $this->BulkProcess->getRequestVars($Block->alias);
  218. $options = array(
  219. 'messageMap' => array(
  220. 'delete' => __d('croogo', 'Blocks deleted'),
  221. 'publish' => __d('croogo', 'Blocks published'),
  222. 'unpublish' => __d('croogo', 'Blocks unpublished'),
  223. 'copy' => __d('croogo', 'Blocks copied'),
  224. ),
  225. );
  226. return $this->BulkProcess->process($Block, $action, $ids, $options);
  227. }
  228. }