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 .= '
  • '; - $html .= category::display_category_checkbox($category, $selected_categories, $form_field); + $html .= category::display_category_checkbox($category, $selected_categories, $form_field, $enable_parents); // Display child categories. if ($category->children->count() > 0) @@ -88,7 +88,7 @@ foreach ($category->children as $child) { $html .= '
  • '; - $html .= category::display_category_checkbox($child, $selected_categories, $form_field); + $html .= category::display_category_checkbox($child, $selected_categories, $form_field, $enable_parents); } $html .= ''; } diff -wbruN -x '*~' ./application/i18n/en_US/ui_main.php /var/www/ushahidi/application/i18n/en_US/ui_main.php --- ./application/i18n/en_US/ui_main.php 2010-12-04 21:38:05.000000000 -0800 +++ /var/www/ushahidi/application/i18n/en_US/ui_main.php 2010-12-04 21:31:50.000000000 -0800 @@ -31,6 +31,7 @@ 'alerts_select_city' => 'Select a city', 'alerts_step1_select_city' => 'Step 1: Select your city or location:', 'alerts_step2_send_alerts' => 'Step 2: Send alerts to my:', + 'alerts_step3_select_catgories' => 'Step 3 (Optional): Select Categories', 'alert_confirm_previous' => 'Confirm A Previous Alert Request', 'alert_saved' => 'Your Alert Has Been Saved!', 'all' => 'All', diff -wbruN -x '*~' ./application/models/alert_category.php /var/www/ushahidi/application/models/alert_category.php --- ./application/models/alert_category.php 1969-12-31 16:00:00.000000000 -0800 +++ /var/www/ushahidi/application/models/alert_category.php 2010-12-04 14:08:21.000000000 -0800 @@ -0,0 +1,23 @@ + + * @package Ushahidi - http://source.ushahididev.com + * @module Incident Category Model + * @copyright Ushahidi - http://www.ushahidi.com + * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL) + */ + +class Alert_Category_Model extends ORM +{ + protected $belongs_to = array('alert', 'category'); + + // Database table name + protected $table_name = 'alert_category'; +} diff -wbruN -x '*~' ./application/models/alert.php /var/www/ushahidi/application/models/alert.php --- ./application/models/alert.php 2010-12-04 21:37:45.000000000 -0800 +++ /var/www/ushahidi/application/models/alert.php 2010-12-04 14:07:02.000000000 -0800 @@ -16,7 +16,7 @@ class Alert_Model extends ORM { - protected $has_many = array('incident' => 'alert_sent'); + protected $has_many = array('incident' => 'alert_sent', 'category' => 'alert_category'); // Database table name protected $table_name = 'alert'; diff -wbruN -x '*~' ./application/views/alerts_js.php /var/www/ushahidi/application/views/alerts_js.php --- ./application/views/alerts_js.php 2010-12-04 21:37:45.000000000 -0800 +++ /var/www/ushahidi/application/views/alerts_js.php 2010-12-04 13:48:32.000000000 -0800 @@ -194,3 +194,14 @@ } }); }); \ No newline at end of file + + $(document).ready(function() { + // Category treeview + $("#category-column-1,#category-column-2").treeview({ + persist: "location", + collapsed: true, + unique: false + }); + }); + + diff -wbruN -x '*~' ./sql/ushahidi.sql /var/www/ushahidi/sql/ushahidi.sql --- ./sql/ushahidi.sql 2010-12-04 21:38:05.000000000 -0800 +++ /var/www/ushahidi/sql/ushahidi.sql 2010-12-04 21:51:45.000000000 -0800 @@ -1505,6 +1505,32 @@ (7, 'Updated account information'); +/** +* Table structure for table 'alert_category' +* +*/ +CREATE TABLE `alert_category` ( + `id` int(11) NOT NULL auto_increment, + `alert_id` int(11), + `category_id` int(11), + PRIMARY KEY (`id`), + KEY `alert_id` (`alert_id`), + KEY `category_id` (`category_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; + +/* +* Add a trigger to clean up when the alert is deleted +*/ + +delimiter | + +CREATE TRIGGER ac_cleanup BEFORE DELETE ON `alert` + FOR EACH ROW BEGIN + DELETE FROM `alert_category` WHERE `alert_id` = OLD.id; + END; +| + +delimiter ; /** * Constraints for dumped tables diff -wbruN -x '*~' ./themes/default/views/alerts.php /var/www/ushahidi/themes/default/views/alerts.php --- ./themes/default/views/alerts.php 2010-12-04 21:38:05.000000000 -0800 +++ /var/www/ushahidi/themes/default/views/alerts.php 2010-12-04 21:34:53.000000000 -0800 @@ -86,6 +86,22 @@ +
    +

    +
    +
    +
    + +
    +
    +
    +


    ">