PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Contacts/Controller/MessagesController.php

https://github.com/kareypowell/croogo
PHP | 141 lines | 70 code | 14 blank | 57 comment | 8 complexity | 40757e9848b3122c481c88039186cd9e MD5 | raw file
  1. <?php
  2. App::uses('ContactsAppController', 'Contacts.Controller');
  3. /**
  4. * Messages Controller
  5. *
  6. * @category Contacts.Controller
  7. * @package Croogo.Contacts.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 MessagesController extends ContactsAppController {
  14. /**
  15. * Controller name
  16. *
  17. * @var string
  18. * @access public
  19. */
  20. public $name = 'Messages';
  21. /**
  22. * Models used by the Controller
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $uses = array('Contacts.Message');
  28. /**
  29. * Components
  30. *
  31. * @var array
  32. * @access public
  33. */
  34. public $components = array(
  35. 'Croogo.BulkProcess',
  36. 'Search.Prg' => array(
  37. 'presetForm' => array(
  38. 'paramType' => 'querystring',
  39. ),
  40. 'commonProcess' => array(
  41. 'paramType' => 'querystring',
  42. 'filterEmpty' => true,
  43. ),
  44. ),
  45. );
  46. /**
  47. * Preset Search Variables
  48. */
  49. public $presetVars = true;
  50. /**
  51. * Admin index
  52. *
  53. * @return void
  54. * @access public
  55. */
  56. public function admin_index() {
  57. $this->set('title_for_layout', __d('croogo', 'Messages'));
  58. $this->Prg->commonProcess();
  59. $this->Message->recursive = 0;
  60. $criteria = $this->Message->parseCriteria($this->Prg->parsedParams());
  61. $contacts = $this->Message->Contact->find('list');
  62. $messages = $this->paginate($criteria);
  63. $searchFields = array('contact_id', 'status' => array(
  64. 'label' => __d('croogo', 'Read'),
  65. 'type' => 'hidden',
  66. ));
  67. $this->set(compact('criteria', 'messages', 'contacts', 'searchFields'));
  68. }
  69. /**
  70. * Admin edit
  71. *
  72. * @param integer $id
  73. * @return void
  74. * @access public
  75. */
  76. public function admin_edit($id = null) {
  77. $this->set('title_for_layout', __d('croogo', 'Edit Message'));
  78. if (!$id && empty($this->request->data)) {
  79. $this->Session->setFlash(__d('croogo', 'Invalid Message'));
  80. return $this->redirect(array('action' => 'index'));
  81. }
  82. if (!empty($this->request->data)) {
  83. if ($this->Message->save($this->request->data)) {
  84. $this->Session->setFlash(__d('croogo', 'The Message has been saved'), 'default', array('class' => 'success'));
  85. return $this->redirect(array('action' => 'index'));
  86. } else {
  87. $this->Session->setFlash(__d('croogo', 'The Message could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  88. }
  89. }
  90. if (empty($this->request->data)) {
  91. $this->request->data = $this->Message->read(null, $id);
  92. }
  93. }
  94. /**
  95. * Admin delete
  96. *
  97. * @param integer $id
  98. * @return void
  99. * @access public
  100. */
  101. public function admin_delete($id = null) {
  102. if (!$id) {
  103. $this->Session->setFlash(__d('croogo', 'Invalid id for Message'), 'default', array('class' => 'error'));
  104. return $this->redirect(array('action' => 'index'));
  105. }
  106. if ($this->Message->delete($id)) {
  107. $this->Session->setFlash(__d('croogo', 'Message deleted'), 'default', array('class' => 'success'));
  108. return $this->redirect(array('action' => 'index'));
  109. }
  110. }
  111. /**
  112. * Admin process
  113. *
  114. * @return void
  115. * @access public
  116. */
  117. public function admin_process() {
  118. $Message = $this->{$this->modelClass};
  119. list($action, $ids) = $this->BulkProcess->getRequestVars($Message->alias);
  120. $messageMap = array(
  121. 'delete' => __d('croogo', 'Messages deleted'),
  122. 'read' => __d('croogo', 'Messages marked as read'),
  123. 'unread' => __d('croogo', 'Messages marked as unread'),
  124. );
  125. return $this->BulkProcess->process($Message, $action, $ids, $messageMap);
  126. }
  127. }