PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Settings/Controller/SettingsController.php

https://github.com/kareypowell/croogo
PHP | 244 lines | 121 code | 26 blank | 97 comment | 21 complexity | f74955165ec6f9564653887e8b827f59 MD5 | raw file
  1. <?php
  2. App::uses('SettingsAppController', 'Settings.Controller');
  3. /**
  4. * Settings Controller
  5. *
  6. * @category Settings.Controller
  7. * @package Croogo.Settings
  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 SettingsController extends SettingsAppController {
  14. /**
  15. * Controller name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Settings';
  21. /**
  22. * Models used by the Controller
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $uses = array('Settings.Setting');
  28. /**
  29. * Components used by the Controller
  30. */
  31. public $components = array(
  32. 'Search.Prg' => array(
  33. 'presetForm' => array(
  34. 'paramType' => 'querystring',
  35. ),
  36. 'commonProcess' => array(
  37. 'paramType' => 'querystring',
  38. 'filterEmpty' => true,
  39. ),
  40. ),
  41. );
  42. /**
  43. * Helpers used by the Controller
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. public $helpers = array('Html', 'Form');
  49. /**
  50. * Preset Variables Search
  51. */
  52. public $presetVars = true;
  53. /**
  54. * Admin dashboard
  55. *
  56. * @return void
  57. * @access public
  58. * @deprecated This method will be moved to Extensions plugin
  59. */
  60. public function admin_dashboard() {
  61. $this->set('title_for_layout', __d('croogo', 'Dashboard'));
  62. }
  63. /**
  64. * Admin index
  65. *
  66. * @return void
  67. * @access public
  68. */
  69. public function admin_index() {
  70. $this->set('title_for_layout', __d('croogo', 'Settings'));
  71. $this->Prg->commonProcess();
  72. $this->Setting->recursive = 0;
  73. $this->paginate['Setting']['order'] = "Setting.weight ASC";
  74. $criteria = $this->Setting->parseCriteria($this->Prg->parsedParams());
  75. $this->set('settings', $this->paginate($criteria));
  76. }
  77. /**
  78. * Admin view
  79. *
  80. * @param view $id
  81. * @return void
  82. * @access public
  83. */
  84. public function admin_view($id = null) {
  85. if (!$id) {
  86. $this->Session->setFlash(__d('croogo', 'Invalid Setting.'), 'default', array('class' => 'error'));
  87. return $this->redirect(array('action' => 'index'));
  88. }
  89. $this->set('setting', $this->Setting->read(null, $id));
  90. }
  91. /**
  92. * Admin add
  93. *
  94. * @return void
  95. * @access public
  96. */
  97. public function admin_add() {
  98. $this->set('title_for_layout', __d('croogo', 'Add Setting'));
  99. if (!empty($this->request->data)) {
  100. $this->Setting->create();
  101. if ($this->Setting->save($this->request->data)) {
  102. $this->Session->setFlash(__d('croogo', 'The Setting has been saved'), 'default', array('class' => 'success'));
  103. return $this->redirect(array('action' => 'index'));
  104. } else {
  105. $this->Session->setFlash(__d('croogo', 'The Setting could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  106. }
  107. }
  108. }
  109. /**
  110. * Admin edit
  111. *
  112. * @param integer $id
  113. * @return void
  114. * @access public
  115. */
  116. public function admin_edit($id = null) {
  117. $this->set('title_for_layout', __d('croogo', 'Edit Setting'));
  118. if (!$id && empty($this->request->data)) {
  119. $this->Session->setFlash(__d('croogo', 'Invalid Setting'), 'default', array('class' => 'error'));
  120. return $this->redirect(array('action' => 'index'));
  121. }
  122. if (!empty($this->request->data)) {
  123. if ($this->Setting->save($this->request->data)) {
  124. $this->Session->setFlash(__d('croogo', 'The Setting has been saved'), 'default', array('class' => 'success'));
  125. return $this->redirect(array('action' => 'index'));
  126. } else {
  127. $this->Session->setFlash(__d('croogo', 'The Setting could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  128. }
  129. }
  130. if (empty($this->request->data)) {
  131. $this->request->data = $this->Setting->read(null, $id);
  132. }
  133. }
  134. /**
  135. * Admin delete
  136. *
  137. * @param integer $id
  138. * @return void
  139. * @access public
  140. */
  141. public function admin_delete($id = null) {
  142. if (!$id) {
  143. $this->Session->setFlash(__d('croogo', 'Invalid id for Setting'), 'default', array('class' => 'error'));
  144. return $this->redirect(array('action' => 'index'));
  145. }
  146. if ($this->Setting->delete($id)) {
  147. $this->Session->setFlash(__d('croogo', 'Setting deleted'), 'default', array('class' => 'success'));
  148. return $this->redirect(array('action' => 'index'));
  149. }
  150. }
  151. /**
  152. * Admin prefix
  153. *
  154. * @param string $prefix
  155. * @return void
  156. * @access public
  157. */
  158. public function admin_prefix($prefix = null) {
  159. $this->set('title_for_layout', __d('croogo', 'Settings: %s', $prefix));
  160. $this->Setting->Behaviors->attach('Croogo.Params');
  161. if (!empty($this->request->data) && $this->Setting->saveAll($this->request->data['Setting'])) {
  162. $this->Session->setFlash(__d('croogo', "Settings updated successfully"), 'default', array('class' => 'success'));
  163. return $this->redirect(array('action' => 'prefix', $prefix));
  164. }
  165. $settings = $this->Setting->find('all', array(
  166. 'order' => 'Setting.weight ASC',
  167. 'conditions' => array(
  168. 'Setting.key LIKE' => $prefix . '.%',
  169. 'Setting.editable' => 1,
  170. ),
  171. ));
  172. $this->set(compact('settings'));
  173. if (count($settings) == 0) {
  174. $this->Session->setFlash(__d('croogo', "Invalid Setting key"), 'default', array('class' => 'error'));
  175. }
  176. $this->set("prefix", $prefix);
  177. }
  178. /**
  179. * Admin moveup
  180. *
  181. * @param integer $id
  182. * @param integer $step
  183. * @return void
  184. * @access public
  185. */
  186. public function admin_moveup($id, $step = 1) {
  187. if ($this->Setting->moveUp($id, $step)) {
  188. $this->Session->setFlash(__d('croogo', 'Moved up successfully'), 'default', array('class' => 'success'));
  189. } else {
  190. $this->Session->setFlash(__d('croogo', 'Could not move up'), 'default', array('class' => 'error'));
  191. }
  192. if (!$redirect = $this->referer()) {
  193. $redirect = array(
  194. 'admin' => true,
  195. 'plugin' => 'settings',
  196. 'controller' => 'settings',
  197. 'action' => 'index'
  198. );
  199. }
  200. return $this->redirect($redirect);
  201. }
  202. /**
  203. * Admin moveup
  204. *
  205. * @param integer $id
  206. * @param integer $step
  207. * @return void
  208. * @access public
  209. */
  210. public function admin_movedown($id, $step = 1) {
  211. if ($this->Setting->moveDown($id, $step)) {
  212. $this->Session->setFlash(__d('croogo', 'Moved down successfully'), 'default', array('class' => 'success'));
  213. } else {
  214. $this->Session->setFlash(__d('croogo', 'Could not move down'), 'default', array('class' => 'error'));
  215. }
  216. return $this->redirect(array('admin' => true, 'controller' => 'settings', 'action' => 'index'));
  217. }
  218. }