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

/application/controllers/alerts.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 320 lines | 208 code | 53 blank | 59 comment | 18 complexity | f89f4c448519fcc6145f9cc8548e9cab MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * This controller handles requests for SMS/ Email alerts
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @subpackage Controllers
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class Alerts_Controller extends Main_Controller {
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. public function index()
  21. {
  22. // First, are we allowed to subscribe for alerts via web?
  23. if ( ! Kohana::config('settings.allow_alerts'))
  24. {
  25. url::redirect(url::site().'main');
  26. }
  27. $this->template->header->this_page = $this->themes->this_page = 'alerts';
  28. $this->template->content = new View('alerts/main');
  29. // Load the alert radius map view
  30. $alert_radius_view = new View('alerts/radius');
  31. $alert_radius_view->show_usage_info = TRUE;
  32. $alert_radius_view->enable_find_location = TRUE;
  33. $this->template->content->alert_radius_view = $alert_radius_view;
  34. // Display Mobile Option?
  35. $this->template->content->show_mobile = TRUE;
  36. if ( ! Kohana::config("settings.sms_provider"))
  37. {
  38. // Hide Mobile
  39. $this->template->content->show_mobile = FALSE;
  40. }
  41. // Retrieve default country, latitude, longitude
  42. $default_country = Kohana::config('settings.default_country');
  43. // Retrieve Country Cities
  44. $this->template->content->cities = $this->_get_cities($default_country);
  45. // Populate this for backwards compat
  46. $this->template->content->categories = array();
  47. // Setup and initialize form field names
  48. $form = array (
  49. 'alert_mobile' => '',
  50. 'alert_mobile_yes' => '',
  51. 'alert_email' => '',
  52. 'alert_email_yes' => '',
  53. 'alert_lat' => '',
  54. 'alert_lon' => '',
  55. 'alert_radius' => '',
  56. 'alert_country' => '',
  57. 'alert_confirmed' => ''
  58. );
  59. if ($this->user)
  60. {
  61. $form['alert_email'] = $this->user->email;
  62. }
  63. // Get Countries
  64. $countries = array();
  65. foreach (ORM::factory('country')->orderby('country')->find_all() as $country)
  66. {
  67. // Create a list of all countries
  68. $this_country = $country->country;
  69. if (strlen($this_country) > 35)
  70. {
  71. $this_country = substr($this_country, 0, 35) . "...";
  72. }
  73. $countries[$country->id] = $this_country;
  74. }
  75. //Initialize default value for Alert confirmed hidden value
  76. $this->template->content->countries = $countries;
  77. // Copy the form as errors, so the errors will be stored with keys
  78. // corresponding to the form field names
  79. $errors = $form;
  80. $form_error = FALSE;
  81. $form_saved = FALSE;
  82. // If there is a post and $_POST is not empty
  83. if ($post = $this->input->post())
  84. {
  85. $alert_orm = new Alert_Model();
  86. // HT: created new model and post for mobile alert
  87. $alert_orm1 = new Alert_Model();
  88. $post1 = $this->input->post();
  89. if ($alert_orm->validate($post))
  90. {
  91. // Yes! everything is valid
  92. // Save alert and send out confirmation code
  93. if ( ! empty($post->alert_mobile))
  94. {
  95. // HT: setting value of post1 to alert_orm1
  96. $alert_orm1->validate($post1);
  97. alert::_send_mobile_alert($post1, $alert_orm1);
  98. $this->session->set('alert_mobile', $post->alert_mobile);
  99. }
  100. if ( ! empty($post->alert_email))
  101. {
  102. alert::_send_email_alert($post, $alert_orm);
  103. $this->session->set('alert_email', $post->alert_email);
  104. }
  105. url::redirect('alerts/confirm');
  106. }
  107. // No! We have validation errors, we need to show the form again, with the errors
  108. else
  109. {
  110. // repopulate the form fields
  111. $form = arr::overwrite($form, $post->as_array());
  112. // populate the error fields, if any
  113. $errors = arr::overwrite($errors, $post->errors('alerts'));
  114. if (array_key_exists('alert_recipient', $post->errors('alerts')))
  115. {
  116. $errors = array_merge($errors, $post->errors('alerts'));
  117. }
  118. $form_error = TRUE;
  119. }
  120. }
  121. else
  122. {
  123. $form['alert_lat'] = Kohana::config('settings.default_lat');
  124. $form['alert_lon'] = Kohana::config('settings.default_lon');
  125. $form['alert_radius'] = 20;
  126. $form['alert_category'] = array();
  127. }
  128. $this->template->content->form_error = $form_error;
  129. // Initialize Default Value for Hidden Field Country Name, just incase Reverse Geo coding yields no result
  130. $form['alert_country'] = $countries[$default_country];
  131. $this->template->content->form = $form;
  132. $this->template->content->errors = $errors;
  133. $this->template->content->form_saved = $form_saved;
  134. // Javascript Header
  135. $this->themes->map_enabled = TRUE;
  136. $this->themes->js = new View('alerts/alerts_js');
  137. $this->themes->treeview_enabled = TRUE;
  138. $this->themes->slider_enabled = TRUE;
  139. $this->themes->js->latitude = $form['alert_lat'];
  140. $this->themes->js->longitude = $form['alert_lon'];
  141. }
  142. /**
  143. * Alerts Confirmation Page
  144. */
  145. public function confirm()
  146. {
  147. $this->template->header->this_page = 'alerts';
  148. $this->template->content = new View('alerts/confirm');
  149. $this->template->content->alert_mobile = (isset($_SESSION['alert_mobile']) AND ! empty($_SESSION['alert_mobile']))
  150. ? $_SESSION['alert_mobile']
  151. : "";
  152. $this->template->content->alert_email = (isset($_SESSION['alert_email']) AND ! empty($_SESSION['alert_email']))
  153. ? $_SESSION['alert_email']
  154. : "";
  155. // Display Mobile Option?
  156. $this->template->content->show_mobile = TRUE;
  157. if (empty($_SESSION['alert_mobile']))
  158. {
  159. // Hide Mobile
  160. $this->template->content->show_mobile = FALSE;
  161. }
  162. }
  163. /**
  164. * Verifies a previously sent alert confirmation code
  165. */
  166. public function verify()
  167. {
  168. // Define error codes for this view.
  169. define("ER_CODE_VERIFIED", 0);
  170. define("ER_CODE_NOT_FOUND", 1);
  171. define("ER_CODE_ALREADY_VERIFIED", 3);
  172. $code = (isset($_GET['c']) AND !empty($_GET['c'])) ? $_GET['c'] : "";
  173. $email = (isset($_GET['e']) AND !empty($_GET['e'])) ? $_GET['e'] : "";
  174. // HT: Mobile verification by url
  175. $mobile = (isset($_GET['m']) AND !empty($_GET['m'])) ? $_GET['m'] : "";
  176. // INITIALIZE the content's section of the view
  177. $this->template->content = new View('alerts/verify');
  178. $this->template->header->this_page = 'alerts';
  179. $filter = " ";
  180. $missing_info = FALSE;
  181. if ($_POST AND isset($_POST['alert_code']) AND ! empty($_POST['alert_code']))
  182. {
  183. if (isset($_POST['alert_mobile']) AND ! empty($_POST['alert_mobile']))
  184. {
  185. $filter = "alert.alert_type=1 AND alert_code='".Database::instance()->escape_str(utf8::strtoupper($_POST['alert_code']))."' AND alert_recipient='".Database::instance()->escape_str($_POST['alert_mobile'])."' ";
  186. }
  187. elseif (isset($_POST['alert_email']) AND ! empty($_POST['alert_email']))
  188. {
  189. $filter = "alert.alert_type=2 AND alert_code='".Database::instance()->escape_str($_POST['alert_code'])."' AND alert_recipient='".Database::instance()->escape_str($_POST['alert_email'])."' ";
  190. }
  191. else
  192. {
  193. $missing_info = TRUE;
  194. }
  195. }
  196. else
  197. {
  198. //if (empty($code) OR empty($email))
  199. if (empty($code) OR (empty($email) AND empty($mobile)))
  200. {
  201. $missing_info = TRUE;
  202. }
  203. else
  204. {
  205. if(! empty($email)) // HT: condition to check email alert
  206. $filter = "alert.alert_type=2 AND alert_code='".Database::instance()->escape_str($code)."' AND alert_recipient='".Database::instance()->escape_str($email)."' ";
  207. elseif(! empty($mobile)) // HT: condition to check mobile alert
  208. $filter = "alert.alert_type=1 AND alert_code='".Database::instance()->escape_str(utf8::strtoupper($code))."' AND alert_recipient='".Database::instance()->escape_str($mobile)."' ";
  209. }
  210. }
  211. if ( ! $missing_info)
  212. {
  213. $alert_check = ORM::factory('alert')
  214. ->where($filter)
  215. ->find();
  216. // IF there was no result
  217. if ( ! $alert_check->loaded)
  218. {
  219. $this->template->content->errno = ER_CODE_NOT_FOUND;
  220. }
  221. elseif ($alert_check->alert_confirmed)
  222. {
  223. $this->template->content->errno = ER_CODE_ALREADY_VERIFIED;
  224. }
  225. else
  226. {
  227. // SET the alert as confirmed, and save it
  228. $alert_check->set('alert_confirmed', 1)->save();
  229. $this->template->content->errno = ER_CODE_VERIFIED;
  230. }
  231. }
  232. else
  233. {
  234. $this->template->content->errno = ER_CODE_NOT_FOUND;
  235. }
  236. } // END function verify
  237. /**
  238. * Unsubscribes alertee using alertee's confirmation code
  239. *
  240. * @param string $code
  241. */
  242. public function unsubscribe($code = NULL)
  243. {
  244. $this->template->content = new View('alerts/unsubscribe');
  245. $this->template->header->this_page = 'alerts';
  246. $this->template->content->unsubscribed = FALSE;
  247. // XXX Might need to validate $code as well
  248. if ($code != NULL)
  249. {
  250. Alert_Model::unsubscribe($code);
  251. $this->template->content->unsubscribed = TRUE;
  252. }
  253. }
  254. /**
  255. * Retrieves Previously Cached Geonames Cities
  256. */
  257. private function _get_cities()
  258. {
  259. $cities = ORM::factory('city')->orderby('city', 'asc')->find_all();
  260. $city_select = array('' => Kohana::lang('ui_main.alerts_select_city'));
  261. foreach ($cities as $city)
  262. {
  263. $city_select[$city->city_lon.",".$city->city_lat] = $city->city;
  264. }
  265. return $city_select;
  266. }
  267. }