PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/contacts_controller.php

https://github.com/Tamsmiranda/croogo
PHP | 206 lines | 151 code | 23 blank | 32 comment | 27 complexity | ef38c9c3edd8faebf5224d88a2b3816e MD5 | raw file
  1. <?php
  2. /**
  3. * Contacts Controller
  4. *
  5. * PHP version 5
  6. *
  7. * @category Controller
  8. * @package Croogo
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class ContactsController extends AppController {
  15. /**
  16. * Controller name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'Contacts';
  22. /**
  23. * Components
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. public $components = array(
  29. 'Akismet',
  30. 'Email',
  31. 'Recaptcha',
  32. );
  33. /**
  34. * Models used by the Controller
  35. *
  36. * @var array
  37. * @access public
  38. */
  39. public $uses = array('Contact');
  40. public function admin_index() {
  41. $this->set('title_for_layout', __('Contacts', true));
  42. $this->Contact->recursive = 0;
  43. $this->paginate['Contact']['order'] = 'Contact.title ASC';
  44. $this->set('contacts', $this->paginate());
  45. }
  46. public function admin_add() {
  47. $this->set('title_for_layout', __('Add Contact', true));
  48. if (!empty($this->data)) {
  49. $this->Contact->create();
  50. if ($this->Contact->save($this->data)) {
  51. $this->Session->setFlash(__('The Contact has been saved', true), 'default', array('class' => 'success'));
  52. $this->redirect(array('action'=>'index'));
  53. } else {
  54. $this->Session->setFlash(__('The Contact could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
  55. }
  56. }
  57. }
  58. public function admin_edit($id = null) {
  59. $this->set('title_for_layout', __('Edit Contact', true));
  60. if (!$id && empty($this->data)) {
  61. $this->Session->setFlash(__('Invalid Contact', true), 'default', array('class' => 'error'));
  62. $this->redirect(array('action'=>'index'));
  63. }
  64. if (!empty($this->data)) {
  65. if ($this->Contact->save($this->data)) {
  66. $this->Session->setFlash(__('The Contact has been saved', true), 'default', array('class' => 'success'));
  67. $this->redirect(array('action'=>'index'));
  68. } else {
  69. $this->Session->setFlash(__('The Contact could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
  70. }
  71. }
  72. if (empty($this->data)) {
  73. $this->data = $this->Contact->read(null, $id);
  74. }
  75. }
  76. public function admin_delete($id = null) {
  77. if (!$id) {
  78. $this->Session->setFlash(__('Invalid id for Contact', true), 'default', array('class' => 'error'));
  79. $this->redirect(array('action'=>'index'));
  80. }
  81. if (!isset($this->params['named']['token']) || ($this->params['named']['token'] != $this->params['_Token']['key'])) {
  82. $blackHoleCallback = $this->Security->blackHoleCallback;
  83. $this->$blackHoleCallback();
  84. }
  85. if ($this->Contact->delete($id)) {
  86. $this->Session->setFlash(__('Contact deleted', true), 'default', array('class' => 'success'));
  87. $this->redirect(array('action'=>'index'));
  88. }
  89. }
  90. public function view($alias = null) {
  91. if (!$alias) {
  92. $this->redirect('/');
  93. }
  94. $contact = $this->Contact->find('first', array(
  95. 'conditions' => array(
  96. 'Contact.alias' => $alias,
  97. 'Contact.status' => 1,
  98. ),
  99. 'cache' => array(
  100. 'name' => 'contact_'.$alias,
  101. 'config' => 'contacts_view',
  102. ),
  103. ));
  104. if (!isset($contact['Contact']['id'])) {
  105. $this->redirect('/');
  106. }
  107. $this->set('contact', $contact);
  108. $continue = true;
  109. if (!$contact['Contact']['message_status']) {
  110. $continue = false;
  111. }
  112. if (!empty($this->data) && $continue === true) {
  113. $this->data['Message']['contact_id'] = $contact['Contact']['id'];
  114. $this->data['Message']['title'] = htmlspecialchars($this->data['Message']['title']);
  115. $this->data['Message']['name'] = htmlspecialchars($this->data['Message']['name']);
  116. $this->data['Message']['body'] = htmlspecialchars($this->data['Message']['body']);
  117. $continue = $this->__validation($continue, $contact);
  118. $continue = $this->__spam_protection($continue, $contact);
  119. $continue = $this->__captcha($continue, $contact);
  120. $continue = $this->__send_email($continue, $contact);
  121. if ($continue === true) {
  122. //$this->Session->setFlash(__('Your message has been received.', true));
  123. //unset($this->data['Message']);
  124. echo $this->flash(__('Your message has been received...', true), '/');
  125. }
  126. }
  127. $this->set('title_for_layout', $contact['Contact']['title']);
  128. $this->set(compact('continue'));
  129. }
  130. private function __validation($continue, $contact) {
  131. if ($this->Contact->Message->set($this->data) &&
  132. $this->Contact->Message->validates() &&
  133. $continue === true) {
  134. if ($contact['Contact']['message_archive'] &&
  135. !$this->Contact->Message->save($this->data['Message'])) {
  136. $continue = false;
  137. }
  138. } else {
  139. $continue = false;
  140. }
  141. return $continue;
  142. }
  143. private function __spam_protection($continue, $contact) {
  144. if (!empty($this->data) &&
  145. $contact['Contact']['message_spam_protection'] &&
  146. $continue === true) {
  147. $this->Akismet->setCommentAuthor($this->data['Message']['name']);
  148. $this->Akismet->setCommentAuthorEmail($this->data['Message']['email']);
  149. $this->Akismet->setCommentContent($this->data['Message']['body']);
  150. if ($this->Akismet->isCommentSpam()) {
  151. $continue = false;
  152. $this->Session->setFlash(__('Sorry, the message appears to be spam.', true), 'default', array('class' => 'error'));
  153. }
  154. }
  155. return $continue;
  156. }
  157. private function __captcha($continue, $contact) {
  158. if (!empty($this->data) &&
  159. $contact['Contact']['message_captcha'] &&
  160. $continue === true &&
  161. !$this->Recaptcha->valid($this->params['form'])) {
  162. $continue = false;
  163. $this->Session->setFlash(__('Invalid captcha entry', true), 'default', array('class' => 'error'));
  164. }
  165. return $continue;
  166. }
  167. private function __send_email($continue, $contact) {
  168. if ($contact['Contact']['message_notify'] &&
  169. $continue === true) {
  170. $this->Email->from = Configure::read('Site.title') . ' '
  171. . '<croogo@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])).'>';
  172. $this->Email->to = $contact['Contact']['email'];
  173. $this->Email->from = $this->data['Message']['name'] . ' <' . $this->data['Message']['email'] . '>';
  174. $this->Email->subject = '[' . Configure::read('Site.title') . '] ' . $contact['Contact']['title'];
  175. $this->Email->template = 'contact';
  176. $this->set('contact', $contact);
  177. $this->set('message', $this->data);
  178. $this->Email->send();
  179. }
  180. return $continue;
  181. }
  182. }
  183. ?>