PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Contacts/Controller/ContactsController.php

https://github.com/kareypowell/croogo
PHP | 289 lines | 167 code | 28 blank | 94 comment | 28 complexity | a07794e6b25a3f479c05fbd3aa0da0db MD5 | raw file
  1. <?php
  2. App::uses('CakeEmail', 'Network/Email');
  3. App::uses('ContactsAppController', 'Contacts.Controller');
  4. /**
  5. * Contacts Controller
  6. *
  7. * @category Controller
  8. * @package Croogo.Contacts.Controller
  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 ContactsAppController {
  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. 'Croogo.Akismet',
  30. 'Croogo.Recaptcha',
  31. );
  32. /**
  33. * Models used by the Controller
  34. *
  35. * @var array
  36. * @access public
  37. */
  38. public $uses = array('Contacts.Contact', 'Contacts.Message');
  39. /**
  40. * Admin index
  41. *
  42. * @return void
  43. * @access public
  44. */
  45. public function admin_index() {
  46. $this->set('title_for_layout', __d('croogo', 'Contacts'));
  47. $this->Contact->recursive = 0;
  48. $this->paginate['Contact']['order'] = 'Contact.title ASC';
  49. $this->set('contacts', $this->paginate());
  50. $this->set('displayFields', $this->Contact->displayFields());
  51. }
  52. /**
  53. * Admin add
  54. *
  55. * @return void
  56. * @access public
  57. */
  58. public function admin_add() {
  59. $this->set('title_for_layout', __d('croogo', 'Add Contact'));
  60. if (!empty($this->request->data)) {
  61. $this->Contact->create();
  62. if ($this->Contact->save($this->request->data)) {
  63. $this->Session->setFlash(__d('croogo', 'The Contact has been saved'), 'default', array('class' => 'success'));
  64. $this->Croogo->redirect(array('action' => 'edit', $this->Contact->id));
  65. } else {
  66. $this->Session->setFlash(__d('croogo', 'The Contact could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  67. }
  68. }
  69. }
  70. /**
  71. * Admin edit
  72. *
  73. * @param integer $id
  74. * @return void
  75. * @access public
  76. */
  77. public function admin_edit($id = null) {
  78. $this->set('title_for_layout', __d('croogo', 'Edit Contact'));
  79. if (!$id && empty($this->request->data)) {
  80. $this->Session->setFlash(__d('croogo', 'Invalid Contact'), 'default', array('class' => 'error'));
  81. return $this->redirect(array('action' => 'index'));
  82. }
  83. if (!empty($this->request->data)) {
  84. if ($this->Contact->save($this->request->data)) {
  85. $this->Session->setFlash(__d('croogo', 'The Contact has been saved'), 'default', array('class' => 'success'));
  86. $this->Croogo->redirect(array('action' => 'edit', $this->Contact->id));
  87. } else {
  88. $this->Session->setFlash(__d('croogo', 'The Contact could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  89. }
  90. }
  91. if (empty($this->request->data)) {
  92. $this->request->data = $this->Contact->read(null, $id);
  93. }
  94. }
  95. /**
  96. * Admin delete
  97. *
  98. * @param integer $id
  99. * @return void
  100. * @access public
  101. */
  102. public function admin_delete($id = null) {
  103. if (!$id) {
  104. $this->Session->setFlash(__d('croogo', 'Invalid id for Contact'), 'default', array('class' => 'error'));
  105. return $this->redirect(array('action' => 'index'));
  106. }
  107. if ($this->Contact->delete($id)) {
  108. $this->Session->setFlash(__d('croogo', 'Contact deleted'), 'default', array('class' => 'success'));
  109. return $this->redirect(array('action' => 'index'));
  110. }
  111. }
  112. /**
  113. * View
  114. *
  115. * @param string $alias
  116. * @return void
  117. * @access public
  118. * @throws NotFoundException
  119. */
  120. public function view($alias = null) {
  121. if (!$alias) {
  122. $alias = 'contact';
  123. }
  124. $contact = $this->Contact->find('first', array(
  125. 'conditions' => array(
  126. 'Contact.alias' => $alias,
  127. 'Contact.status' => 1,
  128. ),
  129. 'cache' => array(
  130. 'name' => $alias,
  131. 'config' => 'contacts_view',
  132. ),
  133. ));
  134. if (!isset($contact['Contact']['id'])) {
  135. throw new NotFoundException();
  136. }
  137. $this->set('contact', $contact);
  138. $continue = true;
  139. if (!$contact['Contact']['message_status']) {
  140. $continue = false;
  141. }
  142. if (!empty($this->request->data) && $continue === true) {
  143. $this->request->data['Message']['contact_id'] = $contact['Contact']['id'];
  144. $this->request->data['Message']['title'] = htmlspecialchars($this->request->data['Message']['title']);
  145. $this->request->data['Message']['name'] = htmlspecialchars($this->request->data['Message']['name']);
  146. $this->request->data['Message']['body'] = htmlspecialchars($this->request->data['Message']['body']);
  147. $continue = $this->_spam_protection($continue, $contact);
  148. $continue = $this->_captcha($continue, $contact);
  149. $continue = $this->_validation($continue, $contact);
  150. $continue = $this->_send_email($continue, $contact);
  151. $this->set(compact('continue'));
  152. if ($continue === true) {
  153. $this->Session->setFlash(__d('croogo', 'Your message has been received...'), 'default', array('class' => 'success'));
  154. return $this->Croogo->redirect('/');
  155. }
  156. } else {
  157. $this->Croogo->setReferer();
  158. }
  159. $this->Croogo->viewFallback(array(
  160. 'view_' . $contact['Contact']['id'],
  161. 'view_' . $contact['Contact']['alias'],
  162. ));
  163. $this->set('title_for_layout', $contact['Contact']['title']);
  164. }
  165. /**
  166. * Validation
  167. *
  168. * @param boolean $continue
  169. * @param array $contact
  170. * @return boolean
  171. * @access protected
  172. */
  173. protected function _validation($continue, $contact) {
  174. if ($this->Contact->Message->set($this->request->data) &&
  175. $this->Contact->Message->validates() &&
  176. $continue === true) {
  177. if ($contact['Contact']['message_archive'] &&
  178. !$this->Contact->Message->save($this->request->data['Message'])) {
  179. $continue = false;
  180. }
  181. } else {
  182. $continue = false;
  183. }
  184. return $continue;
  185. }
  186. /**
  187. * Spam protection
  188. *
  189. * @param boolean $continue
  190. * @param array $contact
  191. * @return boolean
  192. * @access protected
  193. */
  194. protected function _spam_protection($continue, $contact) {
  195. if (!empty($this->request->data) &&
  196. $contact['Contact']['message_spam_protection'] &&
  197. $continue === true) {
  198. $this->Akismet->setCommentAuthor($this->request->data['Message']['name']);
  199. $this->Akismet->setCommentAuthorEmail($this->request->data['Message']['email']);
  200. $this->Akismet->setCommentContent($this->request->data['Message']['body']);
  201. if ($this->Akismet->isCommentSpam()) {
  202. $continue = false;
  203. $this->Session->setFlash(__d('croogo', 'Sorry, the message appears to be spam.'), 'default', array('class' => 'error'));
  204. }
  205. }
  206. return $continue;
  207. }
  208. /**
  209. * Captcha
  210. *
  211. * @param boolean $continue
  212. * @param array $contact
  213. * @return boolean
  214. * @access protected
  215. */
  216. protected function _captcha($continue, $contact) {
  217. if (!empty($this->request->data) &&
  218. $contact['Contact']['message_captcha'] &&
  219. $continue === true &&
  220. !$this->Recaptcha->valid($this->request)) {
  221. $continue = false;
  222. $this->Session->setFlash(__d('croogo', 'Invalid captcha entry'), 'default', array('class' => 'error'));
  223. }
  224. return $continue;
  225. }
  226. /**
  227. * Send Email
  228. *
  229. * @param boolean $continue
  230. * @param array $contact
  231. * @return boolean
  232. * @access protected
  233. */
  234. protected function _send_email($continue, $contact) {
  235. $email = new CakeEmail();
  236. if (!$contact['Contact']['message_notify'] || $continue !== true) {
  237. return $continue;
  238. }
  239. $siteTitle = Configure::read('Site.title');
  240. try {
  241. $email->from($this->request->data['Message']['email'])
  242. ->to($contact['Contact']['email'])
  243. ->subject(__d('croogo', '[%s] %s', $siteTitle, $contact['Contact']['title']))
  244. ->template('Contacts.contact')
  245. ->viewVars(array(
  246. 'contact' => $contact,
  247. 'message' => $this->request->data,
  248. ));
  249. if ($this->theme) {
  250. $email->theme($this->theme);
  251. }
  252. if (!$email->send()) {
  253. $continue = false;
  254. }
  255. } catch (SocketException $e) {
  256. $this->log(sprintf('Error sending contact notification: %s', $e->getMessage()));
  257. $continue = false;
  258. }
  259. return $continue;
  260. }
  261. }