Beispiel #1
0
 public function index()
 {
     $db = new Database();
     $incidents = $db->query("SELECT incident.id, incident_title, \n\t\t\t\t\t\t\t\t incident_description, incident_verified, \n\t\t\t\t\t\t\t\t location.latitude, location.longitude, alert_sent.incident_id\n\t\t\t\t\t\t\t\t FROM incident INNER JOIN location ON incident.location_id = location.id\n\t\t\t\t\t\t\t\t LEFT OUTER JOIN alert_sent ON incident.id = alert_sent.incident_id");
     $config = kohana::config('alerts');
     $sms_from = NULL;
     $settings = ORM::factory('settings', 1);
     if ($settings->loaded == true) {
         // Get SMS Numbers
         if (!empty($settings->sms_no3)) {
             $sms_from = $settings->sms_no3;
         } elseif (!empty($settings->sms_no2)) {
             $sms_from = $settings->sms_no2;
         } elseif (!empty($settings->sms_no1)) {
             $sms_from = $settings->sms_no1;
         } else {
             $sms_from = "000";
         }
         // User needs to set up an SMS number
     }
     foreach ($incidents as $incident) {
         if ($incident->incident_id != NULL) {
             continue;
         }
         $verified = (int) $incident->incident_verified;
         if ($verified) {
             $latitude = (double) $incident->latitude;
             $longitude = (double) $incident->longitude;
             $proximity = new Proximity($latitude, $longitude);
             $alertees = $this->_get_alertees($proximity);
             foreach ($alertees as $alertee) {
                 $alert_type = (int) $alertee->alert_type;
                 if ($alert_type == 1) {
                     $sms = new Eflyer();
                     $sms->user = $settings->eflyer_username;
                     $sms->password = $settings->eflyer_password;
                     $sms->use_ssl = false;
                     $sms->sms();
                     $message = $incident->incident_description;
                     if ($sms->send($alertee->alert_recipient, $message) == "OK") {
                         $db->insert('alert_sent', array('alert_id' => $alertee->id, 'incident_id' => $incident->id, 'alert_date' => date("Y-m-d H:i:s")));
                         $db->clear_cache(true);
                     }
                 } elseif ($alert_type == 2) {
                     $to = $alertee->alert_recipient;
                     $from = $config['alerts_email'];
                     $subject = $incident->incident_title;
                     $message = $incident->incident_description;
                     if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                         $db->insert('alert_sent', array('alert_id' => $alertee->id, 'incident_id' => $incident->id, 'alert_date' => date("Y-m-d H:i:s")));
                         $db->clear_cache(true);
                     }
                 }
             }
         }
     }
 }
Beispiel #2
0
 public function index()
 {
     // Create new session
     $this->session->create();
     $this->template->header->this_page = 'alerts';
     $this->template->content = new View('alerts');
     // Display news feeds?
     $this->template->content->allow_feed = Kohana::config('settings.allow_feed');
     // Retrieve default country, latitude, longitude
     $default_country = Kohana::config('settings.default_country');
     // Retrieve Country Cities
     $this->template->content->cities = $this->_get_cities($default_country);
     // setup and initialize form field names
     $form = array('alert_mobile' => '', 'alert_mobile_yes' => '', 'alert_email' => '', 'alert_email_yes' => '', 'alert_lat' => '', 'alert_lon' => '');
     // copy the form as errors, so the errors will be stored with keys
     // corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $post = new Validation($_POST);
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         if (!empty($_POST['alert_mobile']) || isset($_POST['alert_mobile_yes'])) {
             $post->add_rules('alert_mobile', 'required', 'numeric', 'length[6,20]');
         }
         if (!empty($_POST['alert_email']) || isset($_POST['alert_email_yes'])) {
             $post->add_rules('alert_email', 'required', 'email', 'length[3,64]');
         }
         if (empty($_POST['alert_email']) && empty($_POST['alert_mobile'])) {
             $post->add_error('alert_mobile', 'one_required');
             $post->add_error('alert_email', 'one_required');
         }
         $post->add_rules('alert_lat', 'required', 'between[-90,90]');
         // Validate for maximum and minimum latitude values
         $post->add_rules('alert_lon', 'required', 'between[-180,180]');
         // Validate for maximum and minimum longitude values
         // Add a callback, to validate the mobile phone/email (See the methods below)
         $post->add_callbacks('alert_mobile', array($this, 'mobile_check'));
         $post->add_callbacks('alert_email', array($this, 'email_check'));
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // Yes! everything is valid
             // Save alert and send out confirmation code
             $email_confirmation_saved = FALSE;
             $sms_confirmation_saved = FALSE;
             if (!empty($post->alert_mobile)) {
                 $alert_code = $this->_mk_code();
                 $settings = ORM::factory('settings', 1);
                 if ($settings->loaded == true) {
                     // Get SMS Numbers
                     if (!empty($settings->sms_no3)) {
                         $sms_from = $settings->sms_no3;
                     } elseif (!empty($settings->sms_no2)) {
                         $sms_from = $settings->sms_no2;
                     } elseif (!empty($settings->sms_no1)) {
                         $sms_from = $settings->sms_no1;
                     } else {
                         $sms_from = "000";
                         // User needs to set up an SMS number
                     }
                     $sms = new Eflyer();
                     $sms->user = $settings->eflyer_username;
                     $sms->password = $settings->eflyer_password;
                     $sms->use_ssl = false;
                     $sms->sms();
                     $message = "Tú código de alerta es: " . $alert_code . " www.cuidemoselvoto.org";
                     if ($sms->send($post->alert_mobile, $message) == "OK") {
                         $alert = ORM::factory('alert');
                         $alert->alert_type = self::MOBILE_ALERT;
                         $alert->alert_recipient = $post->alert_mobile;
                         $alert->alert_code = $alert_code;
                         $alert->alert_lon = $post->alert_lon;
                         $alert->alert_lat = $post->alert_lat;
                         $alert->save();
                         if ($alert->saved == TRUE) {
                             $sms_confirmation_saved = TRUE;
                         }
                     }
                 }
             }
             if (!empty($post->alert_email)) {
                 $alert_code = $this->_mk_code();
                 //Send verification email
                 $config = kohana::config('alerts');
                 $settings = kohana::config('settings');
                 $to = $post->alert_email;
                 $from = $config['alerts_email'];
                 $subject = $settings['site_name'] . ' alertas - verificación';
                 $message = "Por favor da click en http://www.cuidemoselvoto.org/alerts/verify/" . $alert_code . " para confirmar tu e-mail y recibir alertas.<br/><br/>Atte<br/>El equipo de Cuidemoselvoto.org";
                 if (email::send($to, $from, $subject, $message, TRUE) == 1) {
                     $alert = ORM::factory('alert');
                     $alert->alert_type = self::EMAIL_ALERT;
                     $alert->alert_recipient = $post->alert_email;
                     $alert->alert_code = $alert_code;
                     $alert->alert_lon = $post->alert_lon;
                     $alert->alert_lat = $post->alert_lat;
                     $alert->save();
                     if ($alert->saved == TRUE) {
                         $email_confirmation_saved = TRUE;
                     }
                 }
             }
             $this->session->set('alert_mobile', $post->alert_mobile);
             $this->session->set('alert_email', $post->alert_email);
             $this->session->set('sms_confirmation_saved', $sms_confirmation_saved);
             $this->session->set('email_confirmation_saved', $email_confirmation_saved);
             url::redirect('alerts/confirm');
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('alerts'));
             $form_error = TRUE;
         }
     } else {
         $form['alert_lat'] = Kohana::config('settings.default_lat');
         $form['alert_lon'] = Kohana::config('settings.default_lon');
     }
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     // Javascript Header
     $this->template->header->map_enabled = TRUE;
     $this->template->header->js = new View('alerts_js');
     $this->template->header->js->default_map = Kohana::config('settings.default_map');
     $this->template->header->js->default_zoom = Kohana::config('settings.default_zoom');
     $this->template->header->js->latitude = $form['alert_lat'];
     $this->template->header->js->longitude = $form['alert_lon'];
 }