diff -wbruN -x '*~' ./application/controllers/alerts.php /var/www/ushahidi/application/controllers/alerts.php --- ./application/controllers/alerts.php 2010-12-04 21:38:05.000000000 -0800 +++ /var/www/ushahidi/application/controllers/alerts.php 2010-12-04 20:59:40.000000000 -0800 @@ -40,6 +40,9 @@ // Retrieve Country Cities $this->template->content->cities = $this->_get_cities($default_country); + // Get all active top level categories + $this->template->content->categories = $this->get_categories('foo'); + // Setup and initialize form field names $form = array ( 'alert_mobile' => '', @@ -60,25 +63,19 @@ // If there is a post and $_POST is not empty if ($post = $this->input->post()) { - // Create a new alert - $alert = ORM::factory('alert'); - - // Test to see if things passed the rule checks - if ($alert->validate($post)) + if ($this->_valid($post)) { // Yes! everything is valid // Save alert and send out confirmation code if ( ! empty($post->alert_mobile)) { - $this->_send_mobile_alert($post->alert_mobile, $post->alert_lon, - $post->alert_lat, $post->alert_radius); + $this->_send_mobile_alert($post); } if ( ! empty($post->alert_email)) { - $this->_send_email_alert($post->alert_email, $post->alert_lon, - $post->alert_lat, $post->alert_radius); + $this->_send_email_alert($post); } $this->session->set('alert_mobile', $post->alert_mobile); @@ -102,6 +99,7 @@ $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 = $form; @@ -112,6 +110,7 @@ // Javascript Header $this->themes->map_enabled = TRUE; $this->themes->js = new View('alerts_js'); + $this->themes->treeview_enabled = TRUE; $this->themes->js->default_map = Kohana::config('settings.default_map'); $this->themes->js->default_zoom = Kohana::config('settings.default_zoom'); $this->themes->js->latitude = $form['alert_lat']; @@ -240,6 +239,8 @@ // XXX Might need to validate $code as well if ($code != NULL) { + // XXX The alert_category table has to cleaned by a + // Trigger!!! $alert_code = ORM::factory('alert') ->where('alert_code', $code) ->delete_all(); @@ -266,9 +267,13 @@ } - private function _send_mobile_alert($alert_mobile, $alert_lon, $alert_lat, $alert_radius) - { + private function _send_mobile_alert($post) { // For Mobile Alerts, Confirmation Code + $alert_mobile = $post->alert_mobile; + $alert_lon = $post->alert_lon; + $alert_lat = $post->alert_lat; + $alert_radius = $post->alert_radius; + // Should be 6 distinct characters $alert_code = text::random('distinct', 8); @@ -309,15 +314,22 @@ $alert->alert_radius = $alert_radius; $alert->save(); + $this->_add_categories($alert, $post); + return TRUE; } return FALSE; } - private function _send_email_alert($alert_email, $alert_lon, $alert_lat, $alert_radius) + private function _send_email_alert($post) { // Email Alerts, Confirmation Code + $alert_email = $post->alert_email; + $alert_lon = $post->alert_lon; + $alert_lat = $post->alert_radius; + $alert_radius = $post->alert_radius; + $alert_code = text::random('alnum', 20); $settings = kohana::config('settings'); @@ -341,9 +353,55 @@ $alert->alert_radius = $alert_radius; $alert->save(); + $this->_add_categories($alert, $post); + return TRUE; } return FALSE; } + + private function _add_categories(Alert_Model $alert, $post) { + if (isset($post->alert_category)) { + foreach($post->alert_category as $item) { + $category = ORM::factory('category') + ->find($item); + + if($category->loaded) { + $alert_category = new Alert_Category_Model(); + $alert_category->alert_id = $alert->id; + $alert_category->category_id = $category->id; + $alert_category->save(); + } + } + } + } + + private function _valid(array & $post) { + // Create a new alert + $alert = ORM::factory('alert'); + + // Test to see if things passed the rule checks + if (!$alert->validate($post)) { + return false; + } + + // Instantiate Validation + $valid = Validation::factory($_POST); + + // Add some filters + $valid->pre_filter('trim', TRUE); + $valid->add_rules('alert_category.*', 'numeric'); + + if (!isset($_POST['alert_category'])) { + // That's OK. + $valid->alert_category = ""; + } + + if ($valid->validate()) { + return true; + } + + return false; + } } diff -wbruN -x '*~' ./application/controllers/main.php /var/www/ushahidi/application/controllers/main.php --- ./application/controllers/main.php 2010-12-04 21:38:05.000000000 -0800 +++ /var/www/ushahidi/application/controllers/main.php 2010-12-04 21:49:52.000000000 -0800 @@ -91,6 +91,21 @@ } } + /** + * Retrieves Categories + */ + protected function get_categories($selected_categories) + { + $categories = ORM::factory('category') + ->where('category_visible', '1') + ->where('parent_id', '0') + ->where('category_trusted != 1') + ->orderby('category_title', 'ASC') + ->find_all(); + + return $categories; + } + public function index() { $this->template->header->this_page = 'home'; diff -wbruN -x '*~' ./application/controllers/reports.php /var/www/ushahidi/application/controllers/reports.php --- ./application/controllers/reports.php 2010-12-04 21:38:05.000000000 -0800 +++ /var/www/ushahidi/application/controllers/reports.php 2010-12-04 21:43:20.000000000 -0800 @@ -563,7 +563,7 @@ $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; - $categories = $this->_get_categories($form['incident_category']); + $categories = $this->get_categories($form['incident_category']); $this->template->content->categories = $categories; // Retrieve Custom Form Fields Structure @@ -1054,21 +1054,6 @@ } /** - * Retrieves Categories - */ - private function _get_categories($selected_categories) - { - $categories = ORM::factory('category') - ->where('category_visible', '1') - ->where('parent_id', '0') - ->where('category_trusted != 1') - ->orderby('category_title', 'ASC') - ->find_all(); - - return $categories; - } - - /** * Retrieves Total Rating For Specific Post * Also Updates The Incident & Comment Tables (Ratings Column) */ diff -wbruN -x '*~' ./application/controllers/scheduler/s_alerts.php /var/www/ushahidi/application/controllers/scheduler/s_alerts.php --- ./application/controllers/scheduler/s_alerts.php 2010-12-04 21:38:05.000000000 -0800 +++ /var/www/ushahidi/application/controllers/scheduler/s_alerts.php 2010-12-04 21:44:42.000000000 -0800 @@ -90,6 +90,9 @@ $latitude = (double) $incident->latitude; $longitude = (double) $incident->longitude; + // Find all the catecories including parents + $category_ids = $this->_find_categories($incident->id); + // Get all alertees $alertees = ORM::factory('alert') ->where('alert_confirmed','1') @@ -101,6 +104,11 @@ if ($alertee->id == $incident->alert_id) continue; + // Check the categories + if (!$this->_check_categories($alertee, $category_ids)) { + continue; + } + $alert_radius = (int) $alertee->alert_radius; $alert_type = (int) $alertee->alert_type; $latitude2 = (double) $alertee->alert_lat; @@ -175,4 +183,61 @@ } } } + + private function _find_categories($incident_id) { + $ret = array(); + $incident_categories = ORM::factory('incident_category') + ->where('incident_id', $incident_id) + ->find_all(); + + foreach ($incident_categories as $ic) { + $category = ORM::factory('category') + ->where('id', $ic->category_id) + ->find(); + $this->_add_category($ret, $category); + } + + return $ret; + } + + private function _add_category(array & $ids, Category_Model $category) { + if ($category == null) { + return; + } + + $id = (string)$category->id; + + if (!array_key_exists($id, $ids)) { + $ids[$id] = 1; + } + + if ($category->parent_id != 0) { + $parent = ORM::factory('category') + ->where('id', $category->parent_id) + ->find(); + + $this->_add_category($ids, $parent); + } + } + + private function _check_categories(Alert_Model $alertee, array $category_ids) { + $ret = false; + + $alert_categories = ORM::factory('alert_category') + ->where('alert_id', $alertee->id) + ->find_all(); + + if (count($alert_categories) == 0) { + $ret = true; + } + else { + foreach ($alert_categories as $ac) { + if (array_key_exists((string)$ac->category_id, $category_ids)) { + $ret = true; + } + } + } + + return $ret; + } } diff -wbruN -x '*~' ./application/helpers/category.php /var/www/ushahidi/application/helpers/category.php --- ./application/helpers/category.php 2010-12-04 21:37:45.000000000 -0800 +++ /var/www/ushahidi/application/helpers/category.php 2010-12-04 21:21:55.000000000 -0800 @@ -12,7 +12,7 @@ /** * Displays a single category checkbox. */ - public static function display_category_checkbox($category, $selected_categories, $form_field) + public static function display_category_checkbox($category, $selected_categories, $form_field, $enable_parents = false) { $html = ''; @@ -36,7 +36,7 @@ $category_checked = in_array($cid, $selected_categories); $disabled = ""; - if ($category->children->count() > 0) + if (!$enable_parents && $category->children->count() > 0) { $disabled = " disabled=\"disabled\""; } @@ -51,7 +51,7 @@ /** * Display category tree with input checkboxes. */ - public static function tree($categories, array $selected_categories, $form_field, $columns = 1) + public static function tree($categories, array $selected_categories, $form_field, $columns = 1, $enable_parents = false) { $html = ''; @@ -79,7 +79,7 @@ // Display parent category. $html .= '