/Controller/ProblemsController.php

https://github.com/CakeDC/problems · PHP · 347 lines · 210 code · 31 blank · 106 comment · 17 complexity · cb4c544f10660f30a1228236b732bcf2 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. /**
  12. * Problems Controller
  13. *
  14. * @package problems
  15. * @subpackage problems.controllers
  16. */
  17. class ProblemsController extends AppController {
  18. /**
  19. * Controller name
  20. *
  21. * @var string
  22. */
  23. public $name = 'Problems';
  24. /**
  25. * Components
  26. *
  27. * @var array
  28. */
  29. public $components = array('Utils.Referer');
  30. /**
  31. * Helpers
  32. *
  33. * @var array
  34. */
  35. public $helpers = array('Html', 'Form', 'Time', 'Text');
  36. public $flashTypes = array(
  37. 'error' => array(),
  38. 'success' => array(),
  39. );
  40. /**
  41. * Before filter callback
  42. * Restricts the add and edit actions to logged in users only
  43. *
  44. * @return void
  45. */
  46. public function beforeFilter() {
  47. parent::beforeFilter();
  48. $this->Auth->deny('add', 'edit');
  49. $appTypes = Configure::read('Problems.flashTypes');
  50. if (!empty($appTypes) && is_array($appTypes)) {
  51. $this->flashTypes = $appTypes + $this->flashTypes;
  52. }
  53. }
  54. /**
  55. * Add for problem.
  56. *
  57. * @param string $foreignKey, Book id
  58. */
  59. public function add($objectType, $foreignKey) {
  60. try {
  61. $model = Inflector::classify($objectType);
  62. if (!ClassRegistry::isKeySet($model)) {
  63. $this->{$model} = ClassRegistry::init(Configure::read('Problems.Models.' . $model));
  64. } else {
  65. $this->{$model} = ClassRegistry::getObject($model);
  66. }
  67. if (get_class($this->{$model}) === 'AppModel') {
  68. throw new Exception(__d('problems', 'Could not save the Problem of unallowed type.'));
  69. }
  70. $result = $this->{$model}->report($foreignKey, $this->Auth->user('id'), $this->request->data);
  71. if ($result === true) {
  72. $this->_setFlash(__d('problems', 'The problem has been saved'), 'success');
  73. $this->Referer->redirect('/');
  74. }
  75. } catch (OutOfBoundsException $e) {
  76. $this->_setFlash($e->getMessage(), 'error');
  77. } catch (LogicException $e) {
  78. $this->_setFlash($e->getMessage(), 'error');
  79. $this->Referer->redirect('/');
  80. } catch (Exception $e) {
  81. $this->_setFlash($e->getMessage(), 'error');
  82. $this->redirect('/');
  83. }
  84. $types = $this->{$model}->Problem->types;
  85. $this->set(compact('foreignKey', 'types', 'objectType'));
  86. }
  87. /**
  88. * Edit for problem.
  89. *
  90. * @param string $id, problem id
  91. */
  92. public function edit($id = null) {
  93. try {
  94. $problem = $this->Problem->view($id);
  95. $model = Inflector::classify($problem['Problem']['model']);
  96. $this->{$model} = ClassRegistry::init(Configure::read('Problems.Models.' . $model));
  97. $result = $this->{$model}->Problem->edit($id, $this->Auth->user('id'), $this->request->data);
  98. if ($result === true) {
  99. $foreignKey = $this->Problem->data['Problem']['foreign_key'];
  100. $this->_setFlash(__d('problems', 'Problem saved'), 'success');
  101. $this->Referer->redirect('/');
  102. } else {
  103. $this->request->data = $this->Problem->data;
  104. $foreignKey = $this->request->data['Problem']['foreign_key'];
  105. $model = $this->request->data['Problem']['model'];
  106. }
  107. } catch (OutOfBoundsException $e) {
  108. $this->_setFlash($e->getMessage(), 'error');
  109. $this->redirect('/');
  110. }
  111. $this->set('type', strtolower($model));
  112. $this->set(compact('foreignKey'));
  113. $this->set('types', $this->{$model}->Problem->types);
  114. }
  115. /**
  116. * Admin index for problem.
  117. *
  118. * @param string $objectType
  119. */
  120. public function admin_index($objectType = null) {
  121. $this->paginate = array('totals', 'order' => 'Problem.model DESC');
  122. if (!empty($objectType)) {
  123. $this->paginate['conditions']['Problem.model'] = Inflector::classify($objectType);
  124. }
  125. $this->set('problems', $this->paginate());
  126. $this->set('objectType', $objectType);
  127. $this->set('reportTypes', $this->Problem->types);
  128. }
  129. /**
  130. * Admin view for problem.
  131. *
  132. * @param string $id, problem id
  133. */
  134. public function admin_view($id = null) {
  135. try {
  136. $problem = $this->Problem->view($id);
  137. $foreignKey = $problem['Problem']['foreign_key'];
  138. } catch (OutOfBoundsException $e) {
  139. $this->_setFlash($e->getMessage(), 'error');
  140. $this->redirect('/');
  141. }
  142. $this->set(compact('problem', 'foreignKey'));
  143. }
  144. /**
  145. * Admin edit for problem.
  146. *
  147. * @param string $id, problem id
  148. */
  149. public function admin_edit($id = null) {
  150. try {
  151. $problem = $this->Problem->view($id);
  152. $model = Inflector::classify($problem['Problem']['model']);
  153. $this->{$model} = ClassRegistry::init(Configure::read('Problems.Models.' . $model));
  154. if ($this->{$model}->Problem->edit($id, $this->Auth->user('id'), $this->request->data)) {
  155. $this->_setFlash(__d('problems', 'Problem saved'), 'success');
  156. }
  157. } catch (OutOfBoundsException $e) {
  158. $this->_setFlash($e->getMessage(), 'error');
  159. $this->redirect('/');
  160. }
  161. $this->request->data = $this->Problem->data;
  162. $this->set('problem', $this->Problem->data);
  163. $this->set('type', strtolower($model));
  164. $this->set('types', $this->{$model}->Problem->types);
  165. }
  166. /**
  167. * Admin delete for problem.
  168. *
  169. * @param string $id, problem id
  170. */
  171. public function admin_delete($id = null) {
  172. try {
  173. $problem = $this->Problem->view($id);
  174. $foreignKey = $problem['Problem']['foreign_key'];
  175. $model = Inflector::underscore($problem['Problem']['model']);
  176. $this->set(compact('foreignKey'));
  177. $result = $this->Problem->validateAndDelete($id, $this->Auth->user('id'), $this->request->data);
  178. if ($result === true) {
  179. $this->_setFlash(__d('problems', 'Problem deleted'), 'success');
  180. $this->redirect(array('action' => 'index', $model));
  181. }
  182. } catch (Exception $e) {
  183. $this->_setFlash($e->getMessage(), 'error');
  184. $this->redirect('/');
  185. }
  186. if (!empty($this->Problem->data['problem'])) {
  187. $this->set('problem', $this->Problem->data['problem']);
  188. }
  189. }
  190. /**
  191. * Lists all problems for a object type record
  192. *
  193. * @param string $objectType the name of the object with problems associated
  194. * @param string $foreignKey
  195. */
  196. public function admin_review($objectType, $foreignKey) {
  197. $objectType = Inflector::camelize($objectType);
  198. if (!$model = Configure::read('Problems.Models.' . $objectType)) {
  199. $this->redirect($this->referer('/'));
  200. }
  201. $this->{$model} = ClassRegistry::init($model);
  202. $this->paginate['conditions'] = array(
  203. 'Problem.model' => $objectType,
  204. 'Problem.foreign_key' => $foreignKey
  205. );
  206. $this->set('problems', $this->paginate());
  207. $this->set('reportTypes', $this->Problem->types);
  208. }
  209. /**
  210. * Accepts a problem report as valid
  211. *
  212. * @param string $id the Problem identifier
  213. */
  214. public function admin_accept($id) {
  215. try {
  216. $model = $this->Problem->field('model', array('Problem.id' => $id));
  217. if (!$model = Configure::read('Problems.Models.' . $model)) {
  218. $this->redirect($this->referer('/'));
  219. }
  220. $result = ClassRegistry::init($model)->acceptReport($id);
  221. } catch (OutOfBoundsException $e) {
  222. $this->_setFlash($e->getMessage(), 'error');
  223. $this->redirect('/');
  224. } catch (Exception $e) {
  225. $this->_setFlash($e->getMessage(), 'error');
  226. $this->redirect($this->referer('/'));
  227. }
  228. $this->_setFlash(__d('problems', 'Problem report was accepted'), 'success');
  229. $this->redirect($this->referer('/'));
  230. }
  231. /**
  232. * Marks a problem report as not valid
  233. *
  234. * @param string $id the Problem identifier
  235. */
  236. public function admin_unaccept($id) {
  237. try {
  238. $result = $this->Problem->accept($id, false);
  239. } catch (OutOfBoundsException $e) {
  240. $this->_setFlash($e->getMessage(), 'error');
  241. $this->redirect('/');
  242. } catch (Exception $e) {
  243. $this->_setFlash($e->getMessage(), 'error');
  244. $this->redirect($this->referer('/'));
  245. }
  246. $this->_setFlash(__d('problems', 'Problem report was unaccepted'), 'success');
  247. $this->redirect($this->referer('/'));
  248. }
  249. /**
  250. * Accepts all problem reports for a object type and foreignKey as valid
  251. *
  252. * @param string $objectType the name of the object with problems associated
  253. * @param string $foreignKey
  254. */
  255. public function admin_accept_all($objectType, $foreignKey) {
  256. try {
  257. $result = $this->Problem->acceptAll($objectType, $foreignKey, true);
  258. } catch (OutOfBoundsException $e) {
  259. $this->_setFlash($e->getMessage(), 'error');
  260. $this->redirect('/');
  261. } catch (Exception $e) {
  262. $this->_setFlash($e->getMessage(), 'error');
  263. $this->redirect($this->referer('/'));
  264. }
  265. $this->_setFlash(__d('problems', 'Problem reports were accepted'), 'success');
  266. $this->redirect($this->referer('/'));
  267. }
  268. /**
  269. * Marks all problem reports for a object type and foreignKey as not valid
  270. *
  271. * @param string $objectType the name of the object with problems associated
  272. * @param string $foreignKey
  273. */
  274. public function admin_unaccept_all($objectType, $foreignKey) {
  275. try {
  276. $result = $this->Problem->acceptAll($objectType, $foreignKey, false);
  277. } catch (OutOfBoundsException $e) {
  278. $this->_setFlash($e->getMessage(), 'error');
  279. $this->redirect('/');
  280. } catch (Exception $e) {
  281. $this->_setFlash($e->getMessage(), 'error');
  282. $this->redirect($this->referer('/'));
  283. }
  284. $this->_setFlash(__d('problems', 'Problem reports were unaccepted'), 'success');
  285. $this->redirect($this->referer('/'));
  286. }
  287. /**
  288. * Redirects to the page permitting to view the reported object
  289. *
  290. * @param string $id, problem id
  291. */
  292. public function admin_view_object($objectType, $foreignKey) {
  293. $objectType = Inflector::camelize($objectType);
  294. if (!$model = Configure::read('Problems.Models.' . $objectType)) {
  295. $this->redirect($this->referer('/'));
  296. }
  297. $this->redirect(ClassRegistry::init($model)->reportedObjectUrl($foreignKey));
  298. }
  299. /**
  300. * Used to set a session variable that can be used to output messages in the view.
  301. * Use the flashTypes paramsto set additionals parameters to Session::setFlash
  302. *
  303. * @param string $message Message to be flashed
  304. * @param string $type The type of flash message (success or error)
  305. */
  306. protected function _setFlash($message, $type = 'success') {
  307. if (!empty($this->flashTypes[$type])) {
  308. call_user_func_array(
  309. array($this->Session, 'setFlash'),
  310. array_merge(array($message), $this->flashTypes[$type])
  311. );
  312. } else {
  313. $this->Session->setFlash($message);
  314. }
  315. }
  316. }