Exemplo n.º 1
0
 public function index()
 {
     // First, are we allowed to subscribe for alerts via web?
     if (!Kohana::config('settings.allow_alerts')) {
         url::redirect(url::site() . 'main');
     }
     $this->template->header->this_page = $this->themes->this_page = 'alerts';
     $this->template->content = new View('alerts/main');
     // Load the alert radius map view
     $alert_radius_view = new View('alerts/radius');
     $alert_radius_view->show_usage_info = TRUE;
     $alert_radius_view->enable_find_location = TRUE;
     $this->template->content->alert_radius_view = $alert_radius_view;
     // Display Mobile Option?
     $this->template->content->show_mobile = TRUE;
     if (!Kohana::config("settings.sms_provider")) {
         // Hide Mobile
         $this->template->content->show_mobile = FALSE;
     }
     // Retrieve default country, latitude, longitude
     $default_country = Kohana::config('settings.default_country');
     // Retrieve Country Cities
     $this->template->content->cities = $this->_get_cities($default_country);
     // Populate this for backwards compat
     $this->template->content->categories = array();
     // Setup and initialize form field names
     $form = array('alert_mobile' => '', 'alert_mobile_yes' => '', 'alert_email' => '', 'alert_email_yes' => '', 'alert_lat' => '', 'alert_lon' => '', 'alert_radius' => '', 'alert_country' => '', 'alert_confirmed' => '');
     if ($this->user) {
         $form['alert_email'] = $this->user->email;
     }
     // Get Countries
     $countries = array();
     foreach (ORM::factory('country')->orderby('country')->find_all() as $country) {
         // Create a list of all countries
         $this_country = $country->country;
         if (strlen($this_country) > 35) {
             $this_country = substr($this_country, 0, 35) . "...";
         }
         $countries[$country->id] = $this_country;
     }
     //Initialize default value for Alert confirmed hidden value
     $this->template->content->countries = $countries;
     // 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;
     // If there is a post and $_POST is not empty
     if ($post = $this->input->post()) {
         $alert_orm = new Alert_Model();
         // HT: created new model and post for mobile alert
         $alert_orm1 = new Alert_Model();
         $post1 = $this->input->post();
         if ($alert_orm->validate($post)) {
             // Yes! everything is valid
             // Save alert and send out confirmation code
             if (!empty($post->alert_mobile)) {
                 // HT: setting value of post1 to alert_orm1
                 $alert_orm1->validate($post1);
                 alert::_send_mobile_alert($post1, $alert_orm1);
                 $this->session->set('alert_mobile', $post->alert_mobile);
             }
             if (!empty($post->alert_email)) {
                 alert::_send_email_alert($post, $alert_orm);
                 $this->session->set('alert_email', $post->alert_email);
             }
             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'));
             if (array_key_exists('alert_recipient', $post->errors('alerts'))) {
                 $errors = array_merge($errors, $post->errors('alerts'));
             }
             $form_error = TRUE;
         }
     } else {
         $form['alert_lat'] = Kohana::config('settings.default_lat');
         $form['alert_lon'] = Kohana::config('settings.default_lon');
         $form['alert_radius'] = 20;
         $form['alert_category'] = array();
     }
     $this->template->content->form_error = $form_error;
     // Initialize Default Value for Hidden Field Country Name, just incase Reverse Geo coding yields no result
     $form['alert_country'] = $countries[$default_country];
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_saved = $form_saved;
     // Javascript Header
     $this->themes->map_enabled = TRUE;
     $this->themes->js = new View('alerts/alerts_js');
     $this->themes->treeview_enabled = TRUE;
     $this->themes->slider_enabled = TRUE;
     $this->themes->js->latitude = $form['alert_lat'];
     $this->themes->js->longitude = $form['alert_lon'];
 }