/**
  * Tests Incident_Model::is_valid_incident
  * @test
  */
 public function testIsValidIncident()
 {
     // Get any incident
     $random_incident = testutils::get_random_id('incident');
     $inactive_incident = testutils::get_random_id('incident', 'WHERE incident_active = 0');
     $active_incident = testutils::get_random_id('incident', 'WHERE incident_active = 1');
     //Test to see if there are data in the incident table to test with.
     if (empty($random_incident)) {
         $this->markTestSkipped('The incident table is empty.');
     } elseif (empty($inactive_incident)) {
         $this->markTestSkipped('No inactive incidents in incident table.');
     } elseif (empty($active_incident)) {
         $this->markTestSkipped('No active incidents in incident table.');
     } else {
         $this->assertEquals(TRUE, Incident_Model::is_valid_incident($random_incident, FALSE));
         // Get inactive incident
         $inactive_incident = testutils::get_random_id('incident', 'WHERE incident_active = 0');
         // Check fails with default args and explicitly limit to active only
         $this->assertEquals(FALSE, Incident_Model::is_valid_incident($inactive_incident));
         $this->assertEquals(FALSE, Incident_Model::is_valid_incident($inactive_incident, TRUE));
         // Check success when including inactive incidents
         $this->assertEquals(TRUE, Incident_Model::is_valid_incident($inactive_incident, FALSE));
         // Get active incident
         $active_incident = testutils::get_random_id('incident', 'WHERE incident_active = 1');
         // Check success with default args and explicitly limit to active only
         $this->assertEquals(TRUE, Incident_Model::is_valid_incident($active_incident));
         $this->assertEquals(TRUE, Incident_Model::is_valid_incident($active_incident, TRUE));
         // Null incident value
         $this->assertEquals(FALSE, Incident_Model::is_valid_incident(NULL));
         // Non numeric incident value
         $this->assertEquals(FALSE, Incident_Model::is_valid_incident('0.999'));
     }
 }
 /**
  * Tests Incident_Model::is_valid_incident
  * @test
  */
 public function testIsValidIncident()
 {
     // Get any incident
     $random_incident = testutils::get_random_id('incident');
     //Test to see if there are data in the incident table to test with.
     if (empty($random_incident)) {
         $this->markTestSkipped('The incident table is empty.');
     } else {
         $this->assertEquals(TRUE, Incident_Model::is_valid_incident($random_incident));
         // Get inactive incident
         $inactive_incident = testutils::get_random_id('incident', 'WHERE incident_active = 0');
         $this->assertEquals(FALSE, Incident_Model::is_valid_incident($inactive_incident, TRUE));
         // Get active incident
         $active_incident = testutils::get_random_id('incident', 'WHERE incident_active = 1');
         $this->assertEquals(TRUE, Incident_Model::is_valid_incident($active_incident, TRUE));
         // Null incident value
         $this->assertEquals(FALSE, Incident_Model::is_valid_incident(NULL));
         // Non numeric incident value
         $this->assertEquals(FALSE, Incident_Model::is_valid_incident('0.999'));
     }
 }
Exemple #3
0
 /**
  * Retrieve Custom Form Fields
  * @param bool|int $incident_id The unique incident_id of the original report
  * @param int $form_id The unique form_id. Uses default form (1), if none selected
  * @param bool $field_names_only Whether or not to include just fields names, or field names + data
  * @param bool $data_only Whether or not to include just data
  * @param string $action If this is being used to grab fields for submit or view of data
  */
 public static function get_custom_form_fields($incident_id = FALSE, $form_id = 1, $data_only = FALSE, $action = "submit")
 {
     $fields_array = array();
     if (!$form_id) {
         $form_id = 1;
     }
     // Validation
     if (!Form_Model::is_valid_form($form_id)) {
         return $fields_array;
     }
     // Database table prefix
     $table_prefix = Kohana::config('database.default.table_prefix');
     //NOTE will probably need to add a user_level variable for non-web based requests
     $user_level = self::get_user_max_auth();
     // Get the predicates for the public state
     $public_state = $action == "view" ? '<=' . $user_level : ' <= ' . $user_level;
     // Query to fetch the form fields and their responses
     $sql = "SELECT ff.*, '' AS form_response FROM " . $table_prefix . "form_field ff WHERE 1=1 ";
     // Check if the provided incident exists
     if (Incident_Model::is_valid_incident($incident_id)) {
         // Overwrite the previous query
         $sql = "SELECT ff.*, fr.form_response " . "FROM " . $table_prefix . "form_field ff " . "RIGHT JOIN " . $table_prefix . "form_response fr ON (fr.form_field_id = ff.id) " . "WHERE fr.incident_id = " . $incident_id . " ";
     }
     $sql .= "AND ff.form_id = " . $form_id . " " . "AND ff.field_ispublic_visible " . $public_state . " " . "ORDER BY ff.field_position ASC";
     // Execute the SQL to fetch the custom form fields
     $form_fields = Database::instance()->query($sql);
     foreach ($form_fields as $custom_formfield) {
         if ($data_only) {
             // Return Data Only
             $fields_array[$custom_formfield->id] = $custom_formfield->form_response;
         } else {
             // Return Field Structure
             $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'field_name' => $custom_formfield->field_name, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => $custom_formfield->form_response);
         }
     }
     // Garbage collection
     unset($form_fields);
     // Return
     return $fields_array;
 }
Exemple #4
0
 /**
  * Retrieve Custom Form Fields
  * @param bool|int $incident_id The unique incident_id of the original report
  * @param int $form_id The unique form_id. If none selected, retrieve custom form fields from ALL custom forms
  * @param bool $data_only Whether or not to include just data
  * @param string $action If this is being used to grab fields for submit or view of data
  */
 public static function get_custom_form_fields($incident_id = FALSE, $form_id = NULL, $data_only = FALSE, $action = "submit")
 {
     $fields_array = array();
     // If we have a form id but its invalid, return empty
     if (!empty($form_id) and !Form_Model::is_valid_form($form_id)) {
         return $fields_array;
     }
     // Database table prefix
     $table_prefix = Kohana::config('database.default.table_prefix');
     // Get field we'll check permissions against
     $ispublic_field = $action == "view" ? 'field_ispublic_visible' : 'field_ispublic_submit';
     // NOTE will probably need to add a user_level variable for non-web based requests
     $user_level = self::get_user_max_auth();
     // Check if incident is valid
     // Have to do this early since we can't build 2 ORM queries at once.
     $valid_incident = Incident_Model::is_valid_incident($incident_id, FALSE);
     // Check if the provided incident exists, then fill in the data
     if ($valid_incident) {
         $sql = "SELECT ff.*, fr.form_response\n\t\t\tFROM `{$table_prefix}form_field` ff\n\t\t\tLEFT JOIN `{$table_prefix}roles` r ON (r.id = {$ispublic_field})\n\t\t\tLEFT JOIN\n\t\t\t\t`{$table_prefix}form_response` fr ON (\n\t\t\t\t\tfr.form_field_id = ff.id AND\n\t\t\t\t\tfr.incident_id = :incident_id\n\t\t\t\t)\n\t\t\tWHERE (access_level <= :user_level OR access_level IS NULL) " . (!empty($form_id) ? "AND form_id = :form_id " : '') . "ORDER BY field_position ASC";
     } else {
         $sql = "SELECT ff.*\n\t\t\tFROM `{$table_prefix}form_field` ff\n\t\t\tLEFT JOIN `{$table_prefix}roles` r ON (r.id = {$ispublic_field})\n\t\t\tWHERE (access_level <= :user_level OR access_level IS NULL) " . (!empty($form_id) ? "AND form_id = :form_id " : '') . "ORDER BY field_position ASC";
     }
     $form_fields = Database::instance()->query($sql, array(':form_id' => $form_id, ':user_level' => $user_level, ':incident_id' => $incident_id));
     foreach ($form_fields as $custom_formfield) {
         if ($data_only) {
             // Return Data Only
             $fields_array[$custom_formfield->id] = isset($custom_formfield->form_response) ? $custom_formfield->form_response : '';
         } else {
             // Return Field Structure
             // JP: added field description
             $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'form_id' => $custom_formfield->form_id, 'field_name' => $custom_formfield->field_name, 'field_description' => $custom_formfield->field_description, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => isset($custom_formfield->form_response) ? $custom_formfield->form_response : '');
         }
     }
     // Garbage collection
     unset($form_fields);
     // Return
     return $fields_array;
 }
Exemple #5
0
 /**
  * Displays a report.
  * @param boolean $id If id is supplied, a report with that id will be
  * retrieved.
  */
 public function view($id = FALSE)
 {
     $this->template->header->this_page = 'reports';
     $this->template->content = new View('reports_view');
     // Load Akismet API Key (Spam Blocker)
     $api_akismet = Kohana::config('settings.api_akismet');
     if (!Incident_Model::is_valid_incident($id, TRUE)) {
         url::redirect('main');
     } else {
         $incident = ORM::factory('incident')->where('id', $id)->where('incident_active', 1)->find();
         if ($incident->id == 0) {
             url::redirect('reports/view/');
         }
         // Comment Post?
         // Setup and initialize form field names
         $form = array('comment_author' => '', 'comment_description' => '', 'comment_email' => '', 'comment_ip' => '', 'captcha' => '');
         $captcha = Captcha::factory();
         $errors = $form;
         $form_error = FALSE;
         // Check, has the form been submitted, if so, setup validation
         if ($_POST and Kohana::config('settings.allow_comments')) {
             // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
             $post = Validation::factory($_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 (!$this->user) {
                 $post->add_rules('comment_author', 'required', 'length[3,100]');
                 $post->add_rules('comment_email', 'required', 'email', 'length[4,100]');
             }
             $post->add_rules('comment_description', 'required');
             $post->add_rules('captcha', 'required', 'Captcha::valid');
             // Test to see if things passed the rule checks
             if ($post->validate()) {
                 // Yes! everything is valid
                 if ($api_akismet != "") {
                     // Run Akismet Spam Checker
                     $akismet = new Akismet();
                     // Comment data
                     $comment = array('website' => "", 'body' => $post->comment_description, 'user_ip' => $_SERVER['REMOTE_ADDR']);
                     if ($this->user) {
                         $comment['author'] = $this->user->name;
                         $comment['email'] = $this->user->email;
                     } else {
                         $comment['author'] = $post->comment_author;
                         $comment['email'] = $post->comment_email;
                     }
                     $config = array('blog_url' => url::site(), 'api_key' => $api_akismet, 'comment' => $comment);
                     $akismet->init($config);
                     if ($akismet->errors_exist()) {
                         if ($akismet->is_error('AKISMET_INVALID_KEY')) {
                             // throw new Kohana_Exception('akismet.api_key');
                         } elseif ($akismet->is_error('AKISMET_RESPONSE_FAILED')) {
                             // throw new Kohana_Exception('akismet.server_failed');
                         } elseif ($akismet->is_error('AKISMET_SERVER_NOT_FOUND')) {
                             // throw new Kohana_Exception('akismet.server_not_found');
                         }
                         // If the server is down, we have to post
                         // the comment :(
                         // $this->_post_comment($comment);
                         $comment_spam = 0;
                     } else {
                         $comment_spam = $akismet->is_spam() ? 1 : 0;
                     }
                 } else {
                     // No API Key!!
                     $comment_spam = 0;
                 }
                 $comment = new Comment_Model();
                 $comment->incident_id = $id;
                 if ($this->user) {
                     $comment->user_id = $this->user->id;
                     $comment->comment_author = $this->user->name;
                     $comment->comment_email = $this->user->email;
                 } else {
                     $comment->comment_author = strip_tags($post->comment_author);
                     $comment->comment_email = strip_tags($post->comment_email);
                 }
                 $comment->comment_description = strip_tags($post->comment_description);
                 $comment->comment_ip = $_SERVER['REMOTE_ADDR'];
                 $comment->comment_date = date("Y-m-d H:i:s", time());
                 // Activate comment for now
                 if ($comment_spam == 1) {
                     $comment->comment_spam = 1;
                     $comment->comment_active = 0;
                 } else {
                     $comment->comment_spam = 0;
                     $comment->comment_active = Kohana::config('settings.allow_comments') == 1 ? 1 : 0;
                 }
                 $comment->save();
                 // Event::comment_add - Added a New Comment
                 Event::run('ushahidi_action.comment_add', $comment);
                 // Notify Admin Of New Comment
                 $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_comment.subject'), Kohana::lang('notifications.admin_new_comment.message') . "\n\n'" . strtoupper($incident->incident_title) . "'" . "\n" . url::base() . 'reports/view/' . $id);
                 // Redirect
                 url::redirect('reports/view/' . $id);
             } else {
                 // No! We have validation errors, we need to show the form again, with the errors
                 // Repopulate the form fields
                 $form = arr::overwrite($form, $post->as_array());
                 // Populate the error fields, if any
                 $errors = arr::overwrite($errors, $post->errors('comments'));
                 $form_error = TRUE;
             }
         }
         // Filters
         $incident_title = $incident->incident_title;
         $incident_description = nl2br($incident->incident_description);
         Event::run('ushahidi_filter.report_title', $incident_title);
         Event::run('ushahidi_filter.report_description', $incident_description);
         // Add Features
         $this->template->content->features_count = $incident->geometry->count();
         $this->template->content->features = $incident->geometry;
         $this->template->content->incident_id = $incident->id;
         $this->template->content->incident_title = $incident_title;
         $this->template->content->incident_description = $incident_description;
         $this->template->content->incident_location = $incident->location->location_name;
         $this->template->content->incident_latitude = $incident->location->latitude;
         $this->template->content->incident_longitude = $incident->location->longitude;
         $this->template->content->incident_date = date('M j Y', strtotime($incident->incident_date));
         $this->template->content->incident_time = date('H:i', strtotime($incident->incident_date));
         $this->template->content->incident_category = $incident->incident_category;
         // Incident rating
         $this->template->content->incident_rating = $incident->incident_rating == '' ? 0 : $incident->incident_rating;
         // Retrieve Media
         $incident_news = array();
         $incident_video = array();
         $incident_photo = array();
         foreach ($incident->media as $media) {
             if ($media->media_type == 4) {
                 $incident_news[] = $media->media_link;
             } elseif ($media->media_type == 2) {
                 $incident_video[] = $media->media_link;
             } elseif ($media->media_type == 1) {
                 $incident_photo[] = $media->media_link;
             }
         }
         $this->template->content->incident_verified = $incident->incident_verified;
         // Retrieve Comments (Additional Information)
         $this->template->content->comments = "";
         if (Kohana::config('settings.allow_comments')) {
             $this->template->content->comments = new View('reports_comments');
             $incident_comments = array();
             if ($id) {
                 $incident_comments = Incident_Model::get_comments($id);
             }
             $this->template->content->comments->incident_comments = $incident_comments;
         }
     }
     // Add Neighbors
     $this->template->content->incident_neighbors = Incident_Model::get_neighbouring_incidents($id, TRUE, 0, 5);
     // News Source links
     $this->template->content->incident_news = $incident_news;
     // Video links
     $this->template->content->incident_videos = $incident_video;
     // Images
     $this->template->content->incident_photos = $incident_photo;
     // Create object of the video embed class
     $video_embed = new VideoEmbed();
     $this->template->content->videos_embed = $video_embed;
     // Javascript Header
     $this->themes->map_enabled = TRUE;
     $this->themes->photoslider_enabled = TRUE;
     $this->themes->videoslider_enabled = TRUE;
     $this->themes->js = new View('reports_view_js');
     $this->themes->js->incident_id = $incident->id;
     $this->themes->js->default_map = Kohana::config('settings.default_map');
     $this->themes->js->default_zoom = Kohana::config('settings.default_zoom');
     $this->themes->js->latitude = $incident->location->latitude;
     $this->themes->js->longitude = $incident->location->longitude;
     $this->themes->js->incident_zoom = $incident->incident_zoom;
     $this->themes->js->incident_photos = $incident_photo;
     // Initialize custom field array
     $this->template->content->custom_forms = new View('reports_view_custom_forms');
     $form_field_names = customforms::get_custom_form_fields($id, $incident->form_id, FALSE, "view");
     $this->template->content->custom_forms->form_field_names = $form_field_names;
     // Are we allowed to submit comments?
     $this->template->content->comments_form = "";
     if (Kohana::config('settings.allow_comments')) {
         $this->template->content->comments_form = new View('reports_comments_form');
         $this->template->content->comments_form->user = $this->user;
         $this->template->content->comments_form->form = $form;
         $this->template->content->comments_form->form_field_names = $form_field_names;
         $this->template->content->comments_form->captcha = $captcha;
         $this->template->content->comments_form->errors = $errors;
         $this->template->content->comments_form->form_error = $form_error;
     }
     // If the Admin is Logged in - Allow for an edit link
     $this->template->content->logged_in = $this->logged_in;
     // Rebuild Header Block
     $this->template->header->header_block = $this->themes->header_block();
 }
Exemple #6
0
 /**
  * Edit a report
  * @param bool|int $id The id no. of the report
  * @param bool|string $saved
  */
 public function edit($id = FALSE, $saved = FALSE)
 {
     $db = new Database();
     // If user doesn't have access, redirect to dashboard
     if (!$this->auth->has_permission("reports_edit")) {
         url::redirect('admin/dashboard');
     }
     $this->template->content = new View('admin/reports/edit');
     $this->template->content->title = Kohana::lang('ui_admin.create_report');
     // Setup and initialize form field names
     $form = array('location_id' => '', 'form_id' => '', 'locale' => '', 'incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'geometry' => array(), 'location_name' => '', 'country_id' => '', 'country_name' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'custom_field' => array(), 'incident_active' => '', 'incident_verified' => '', 'incident_zoom' => '');
     // 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 = $saved == 'saved';
     // Initialize Default Values
     $form['locale'] = Kohana::config('locale.language');
     //$form['latitude'] = Kohana::config('settings.default_lat');
     //$form['longitude'] = Kohana::config('settings.default_lon');
     $form['incident_date'] = date("m/d/Y", time());
     $form['incident_hour'] = date('h');
     $form['incident_minute'] = date('i');
     $form['incident_ampm'] = date('a');
     $form['country_id'] = Kohana::config('settings.default_country');
     // get the form ID if relevant, kind of a hack
     // to just hit the database like this for one
     // tiny bit of info then throw away the DB model object,
     // but seems to be what everyone else does, so
     // why should I care. Just know that when your Ush system crashes
     // because you have 1000 concurrent users you'll need to do this
     // correctly. Etherton.
     $form['form_id'] = 1;
     $form_id = $form['form_id'];
     if ($id and Incident_Model::is_valid_incident($id, FALSE)) {
         $form_id = ORM::factory('incident', $id)->form_id;
     }
     // Initialize custom field array
     $form['custom_field'] = customforms::get_custom_form_fields($id, $form_id, TRUE);
     // Locale (Language) Array
     $this->template->content->locale_array = Kohana::config('locale.all_languages');
     // Create Categories
     $this->template->content->new_categories_form = $this->_new_categories_form_arr();
     // Time formatting
     $this->template->content->hour_array = $this->_hour_array();
     $this->template->content->minute_array = $this->_minute_array();
     $this->template->content->ampm_array = $this->_ampm_array();
     $this->template->content->stroke_width_array = $this->_stroke_width_array();
     // 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 Hidden Field Country Name,
     // just incase Reverse Geo coding yields no result
     $form['country_name'] = $countries[$form['country_id']];
     $this->template->content->countries = $countries;
     // GET custom forms
     $forms = array();
     foreach (customforms::get_custom_forms(FALSE) as $custom_forms) {
         $forms[$custom_forms->id] = $custom_forms->form_title;
     }
     $this->template->content->forms = $forms;
     // Get the incident media
     $incident_media = Incident_Model::is_valid_incident($id, FALSE) ? ORM::factory('incident', $id)->media : FALSE;
     $this->template->content->incident_media = $incident_media;
     // Are we creating this report from SMS/Email/Twitter?
     // If so retrieve message
     if (isset($_GET['mid']) and intval($_GET['mid']) > 0) {
         $message_id = intval($_GET['mid']);
         $service_id = "";
         $message = ORM::factory('message', $message_id);
         if ($message->loaded and $message->message_type == 1) {
             $service_id = $message->reporter->service_id;
             // Has a report already been created for this Message?
             if ($message->incident_id != 0) {
                 // Redirect to report
                 url::redirect('admin/reports/edit/' . $message->incident_id);
             }
             $this->template->content->show_messages = TRUE;
             $incident_description = $message->message;
             if (!empty($message->message_detail)) {
                 $form['incident_title'] = $message->message;
                 $incident_description = $message->message_detail;
             }
             $form['incident_description'] = $incident_description;
             $form['incident_date'] = date('m/d/Y', strtotime($message->message_date));
             $form['incident_hour'] = date('h', strtotime($message->message_date));
             $form['incident_minute'] = date('i', strtotime($message->message_date));
             $form['incident_ampm'] = date('a', strtotime($message->message_date));
             $form['person_first'] = $message->reporter->reporter_first;
             $form['person_last'] = $message->reporter->reporter_last;
             // Does the message itself have a location?
             if ($message->latitude != NULL and $message->longitude != NULL) {
                 $form['latitude'] = $message->latitude;
                 $form['longitude'] = $message->longitude;
             } elseif ($message->reporter->location->loaded) {
                 $form['location_id'] = $message->reporter->location->id;
                 $form['latitude'] = $message->reporter->location->latitude;
                 $form['longitude'] = $message->reporter->location->longitude;
                 $form['location_name'] = $message->reporter->location->location_name;
             }
             // Events to manipulate an already known location
             Event::run('ushahidi_action.location_from', $message_from = $message->message_from);
             // Filter location name
             Event::run('ushahidi_filter.location_name', $form['location_name']);
             // Filter //location find
             Event::run('ushahidi_filter.location_find', $form['location_find']);
             // Retrieve Last 5 Messages From this account
             $this->template->content->all_messages = ORM::factory('message')->where('reporter_id', $message->reporter_id)->orderby('message_date', 'desc')->limit(5)->find_all();
         } else {
             $message_id = "";
             $this->template->content->show_messages = FALSE;
         }
     } else {
         $this->template->content->show_messages = FALSE;
     }
     // Are we creating this report from a Newsfeed?
     if (isset($_GET['fid']) and intval($_GET['fid']) > 0) {
         $feed_item_id = intval($_GET['fid']);
         $feed_item = ORM::factory('feed_item', $feed_item_id);
         if ($feed_item->loaded) {
             // Has a report already been created for this Feed item?
             if ($feed_item->incident_id != 0) {
                 // Redirect to report
                 url::redirect('admin/reports/edit/' . $feed_item->incident_id);
             }
             $form['incident_title'] = $feed_item->item_title;
             $form['incident_description'] = $feed_item->item_description;
             $form['incident_date'] = date('m/d/Y', strtotime($feed_item->item_date));
             $form['incident_hour'] = date('h', strtotime($feed_item->item_date));
             $form['incident_minute'] = date('i', strtotime($feed_item->item_date));
             $form['incident_ampm'] = date('a', strtotime($feed_item->item_date));
             // News Link
             $form['incident_news'][0] = $feed_item->item_link;
             // Does this newsfeed have a geolocation?
             if ($feed_item->location_id) {
                 $form['location_id'] = $feed_item->location_id;
                 $form['latitude'] = $feed_item->location->latitude;
                 $form['longitude'] = $feed_item->location->longitude;
                 $form['location_name'] = $feed_item->location->location_name;
             }
             // HT: new code
             $feed_item_categories = ORM::factory('feed_item_category')->where('feed_item_id', $feed_item->id)->select_list('id', 'category_id');
             if ($feed_item_categories) {
                 foreach ($feed_item_categories as $feed_item_category) {
                     $form['incident_category'][] = $feed_item_category;
                 }
             }
             // HT: end of new code
         } else {
             $feed_item_id = "";
         }
     }
     // 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 = array_merge($_POST, $_FILES);
         // Check if the service id exists
         if (isset($service_id) and intval($service_id) > 0) {
             $post = array_merge($post, array('service_id' => $service_id));
         }
         // Check if the incident id is valid an add it to the post data
         if (Incident_Model::is_valid_incident($id, FALSE)) {
             $post = array_merge($post, array('incident_id' => $id));
         }
         /**
          * NOTES - E.Kala July 27, 2011
          *
          * Previously, the $post parameter for this event was a Validation
          * object. Now it's an array (i.e. the raw data without any validation rules applied to them).
          * As such, all plugins making use of this event shall have to be updated
          */
         // Action::report_submit_admin - Report Posted
         Event::run('ushahidi_action.report_submit_admin', $post);
         // Validate
         if (reports::validate($post)) {
             // Yes! everything is valid
             $location_id = $post->location_id;
             // STEP 1: SAVE LOCATION
             $location = new Location_Model($location_id);
             reports::save_location($post, $location);
             // STEP 2: SAVE INCIDENT
             $incident = new Incident_Model($id);
             reports::save_report($post, $incident, $location->id);
             // STEP 2b: Record Approval/Verification Action
             reports::verify_approve($incident);
             // STEP 2c: SAVE INCIDENT GEOMETRIES
             reports::save_report_geometry($post, $incident);
             // STEP 3: SAVE CATEGORIES
             reports::save_category($post, $incident);
             // STEP 4: SAVE MEDIA
             reports::save_media($post, $incident);
             // STEP 5: SAVE PERSONAL INFORMATION
             reports::save_personal_info($post, $incident);
             // STEP 6a: SAVE LINK TO REPORTER MESSAGE
             // We're creating a report from a message with this option
             if (isset($message_id) and intval($message_id) > 0) {
                 $savemessage = ORM::factory('message', $message_id);
                 if ($savemessage->loaded) {
                     $savemessage->incident_id = $incident->id;
                     $savemessage->save();
                     // Does Message Have Attachments?
                     // Add Attachments
                     $attachments = ORM::factory("media")->where("message_id", $savemessage->id)->find_all();
                     foreach ($attachments as $attachment) {
                         $attachment->incident_id = $incident->id;
                         $attachment->save();
                     }
                 }
             }
             // STEP 6b: SAVE LINK TO NEWS FEED
             // We're creating a report from a newsfeed with this option
             if (isset($feed_item_id) and intval($feed_item_id) > 0) {
                 $savefeed = ORM::factory('feed_item', $feed_item_id);
                 if ($savefeed->loaded) {
                     $savefeed->incident_id = $incident->id;
                     $savefeed->location_id = $location->id;
                     $savefeed->save();
                 }
             }
             // STEP 7: SAVE CUSTOM FORM FIELDS
             reports::save_custom_fields($post, $incident);
             // Action::report_edit - Edited a Report
             Event::run('ushahidi_action.report_edit', $incident);
             // SAVE AND CLOSE?
             switch ($post->save) {
                 case 1:
                 case 'dontclose':
                     // Save but don't close
                     url::redirect('admin/reports/edit/' . $incident->id . '/saved');
                     break;
                 case 'addnew':
                     // Save and add new
                     url::redirect('admin/reports/edit/0/saved');
                     break;
                 default:
                     // Save and close
                     url::redirect('admin/reports/');
             }
         } else {
             // Repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // Populate the error fields, if any
             $errors = arr::merge($errors, $post->errors('report'));
             $form_error = TRUE;
         }
     } else {
         if (Incident_Model::is_valid_incident($id, FALSE)) {
             // Retrieve Current Incident
             $incident = ORM::factory('incident', $id);
             if ($incident->loaded == TRUE) {
                 // Retrieve Categories
                 $incident_category = array();
                 foreach ($incident->incident_category as $category) {
                     $incident_category[] = $category->category_id;
                 }
                 // Retrieve Media
                 $incident_news = array();
                 $incident_video = array();
                 $incident_photo = array();
                 foreach ($incident->media as $media) {
                     if ($media->media_type == 4) {
                         $incident_news[] = $media->media_link;
                     } elseif ($media->media_type == 2) {
                         $incident_video[] = $media->media_link;
                     } elseif ($media->media_type == 1) {
                         $incident_photo[] = $media->media_link;
                     }
                 }
                 // Get Geometries via SQL query as ORM can't handle Spatial Data
                 $sql = "SELECT AsText(geometry) as geometry, geometry_label,\n\t\t\t\t\t\tgeometry_comment, geometry_color, geometry_strokewidth\n\t\t\t\t\t\tFROM " . Kohana::config('database.default.table_prefix') . "geometry\n\t\t\t\t\t\tWHERE incident_id = ?";
                 $query = $db->query($sql, $id);
                 foreach ($query as $item) {
                     $geometry = array("geometry" => $item->geometry, "label" => $item->geometry_label, "comment" => $item->geometry_comment, "color" => $item->geometry_color, "strokewidth" => $item->geometry_strokewidth);
                     $form['geometry'][] = json_encode($geometry);
                 }
                 // Combine Everything
                 $incident_arr = array('location_id' => $incident->location->id, 'form_id' => $incident->form_id, 'locale' => $incident->locale, 'incident_title' => $incident->incident_title, 'incident_description' => $incident->incident_description, 'incident_date' => date('m/d/Y', strtotime($incident->incident_date)), 'incident_hour' => date('h', strtotime($incident->incident_date)), 'incident_minute' => date('i', strtotime($incident->incident_date)), 'incident_ampm' => date('a', strtotime($incident->incident_date)), 'latitude' => $incident->location->latitude, 'longitude' => $incident->location->longitude, 'location_name' => $incident->location->location_name, 'country_id' => $incident->location->country_id, 'incident_category' => $incident_category, 'incident_news' => $incident_news, 'incident_video' => $incident_video, 'incident_photo' => $incident_photo, 'person_first' => $incident->incident_person->person_first, 'person_last' => $incident->incident_person->person_last, 'person_email' => $incident->incident_person->person_email, 'custom_field' => customforms::get_custom_form_fields($id, $incident->form_id, TRUE, 'submit'), 'incident_active' => $incident->incident_active, 'incident_verified' => $incident->incident_verified, 'incident_zoom' => $incident->incident_zoom);
                 // Merge To Form Array For Display
                 $form = arr::overwrite($form, $incident_arr);
             } else {
                 // Redirect
                 url::redirect('admin/reports/');
             }
         }
     }
     $this->template->content->id = $id;
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     // Retrieve Custom Form Fields Structure
     $this->template->content->custom_forms = new View('reports/submit_custom_forms');
     $disp_custom_fields = customforms::get_custom_form_fields($id, $form['form_id'], FALSE, "view");
     $custom_field_mismatch = customforms::get_edit_mismatch($form['form_id']);
     // Quick hack to make sure view-only fields have data set
     foreach ($custom_field_mismatch as $id => $field) {
         $form['custom_field'][$id] = $disp_custom_fields[$id]['field_response'];
     }
     $this->template->content->custom_forms->disp_custom_fields = $disp_custom_fields;
     $this->template->content->custom_forms->custom_field_mismatch = $custom_field_mismatch;
     $this->template->content->custom_forms->form = $form;
     // Retrieve Previous & Next Records
     $previous = ORM::factory('incident')->where('id < ', $id)->orderby('id', 'desc')->find();
     $previous_url = $previous->loaded ? url::site('admin/reports/edit/' . $previous->id) : url::site('admin/reports/');
     $next = ORM::factory('incident')->where('id > ', $id)->orderby('id', 'desc')->find();
     $next_url = $next->loaded ? url::site('admin/reports/edit/' . $next->id) : url::site('admin/reports/');
     $this->template->content->previous_url = $previous_url;
     $this->template->content->next_url = $next_url;
     // Javascript Header
     $this->themes->map_enabled = TRUE;
     $this->themes->colorpicker_enabled = TRUE;
     $this->themes->treeview_enabled = TRUE;
     $this->themes->json2_enabled = TRUE;
     $this->themes->js = new View('reports/submit_edit_js');
     $this->themes->js->edit_mode = TRUE;
     $this->themes->js->default_map = Kohana::config('settings.default_map');
     $this->themes->js->default_zoom = Kohana::config('settings.default_zoom');
     if (!$form['latitude'] or !$form['latitude']) {
         $this->themes->js->latitude = Kohana::config('settings.default_lat');
         $this->themes->js->longitude = Kohana::config('settings.default_lon');
     } else {
         $this->themes->js->latitude = $form['latitude'];
         $this->themes->js->longitude = $form['longitude'];
     }
     $this->themes->js->incident_zoom = $form['incident_zoom'];
     $this->themes->js->geometries = $form['geometry'];
     // Inline Javascript
     $this->template->content->date_picker_js = $this->_date_picker_js();
     $this->template->content->color_picker_js = $this->_color_picker_js();
     $this->template->content->new_category_toggle_js = $this->_new_category_toggle_js();
     // Pack Javascript
     $myPacker = new javascriptpacker($this->themes->js, 'Normal', FALSE, FALSE);
     $this->themes->js = $myPacker->pack();
 }
Exemple #7
0
 /**
  * Retrieve Single Marker (and its neighbours)
  * 
  * @param int $incident_id
  */
 public function single($incident_id = 0)
 {
     $json_features = array();
     $incident_id = intval($incident_id);
     // Check if incident valid/approved
     if (!Incident_Model::is_valid_incident($incident_id, TRUE)) {
         throw new Kohana_404_Exception();
     }
     // Load the incident
     // @todo avoid the double load here
     $marker = ORM::factory('incident')->where('incident.incident_active', 1)->with('location')->find($incident_id);
     if (!$marker->loaded) {
         throw new Kohana_404_Exception();
     }
     // Get geojson features for main incident (including geometry)
     $json_features = $this->markers_geojson(array($marker), 0, null, null, TRUE);
     // Get the neigbouring incidents & their json (without geometries)
     $neighbours = Incident_Model::get_neighbouring_incidents($incident_id, FALSE, 20, 100);
     if ($neighbours) {
         $json_features = array_merge($json_features, $this->markers_geojson($neighbours, 0, null, null, FALSE));
     }
     Event::run('ushahidi_filter.json_single_features', $json_features);
     $this->render_geojson($json_features);
 }
Exemple #8
0
 /**
  * Retrieve Custom Form Fields
  * @param bool|int $incident_id The unique incident_id of the original report
  * @param int $form_id The unique form_id. If none selected, retrieve custom form fields from ALL custom forms
  * @param bool $data_only Whether or not to include just data
  * @param string $action If this is being used to grab fields for submit or view of data
  */
 public static function get_custom_form_fields($incident_id = FALSE, $form_id = NULL, $data_only = FALSE, $action = "submit")
 {
     $fields_array = array();
     if ($form_id != null and $form_id != '') {
         // Validation
         if (!Form_Model::is_valid_form($form_id)) {
             return $fields_array;
         }
     }
     // Database table prefix
     $table_prefix = Kohana::config('database.default.table_prefix');
     // Get field we'll check permissions against
     $ispublic_field = $action == "view" ? 'field_ispublic_visible' : 'field_ispublic_submit';
     // Query to fetch the form fields associated with the given form id
     $sql = "SELECT ff.*, '' AS form_response FROM " . $table_prefix . "form_field ff LEFT JOIN roles r ON (r.id = {$ispublic_field}) WHERE 1=1 ";
     if ($form_id != null and $form_id != '') {
         $sql .= "AND ff.form_id = " . $form_id . " ";
     }
     // NOTE will probably need to add a user_level variable for non-web based requests
     $user_level = self::get_user_max_auth();
     // Check access_level
     $sql .= 'AND (r.access_level <= ' . $user_level . ' OR r.access_level IS NULL)';
     $sql .= " ORDER BY ff.field_position ASC";
     // Execute the SQL to fetch the custom form fields
     $form_fields = Database::instance()->query($sql);
     foreach ($form_fields as $custom_formfield) {
         if ($data_only) {
             // Return Data Only
             $fields_array[$custom_formfield->id] = $custom_formfield->form_response;
         } else {
             // Return Field Structure
             $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'field_name' => $custom_formfield->field_name, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => $custom_formfield->form_response);
         }
     }
     // Garbage collection
     unset($form_fields);
     // Check if the provided incident exists, then fill in the data
     if (Incident_Model::is_valid_incident($incident_id)) {
         // Overwrite the previous query
         $sql = "SELECT ff.*, fr.form_response " . "FROM " . $table_prefix . "form_field ff " . "RIGHT JOIN " . $table_prefix . "form_response fr ON (fr.form_field_id = ff.id) " . "LEFT JOIN roles r ON (r.id = {$ispublic_field})" . "WHERE fr.incident_id = " . $incident_id . " ";
         if ($form_id != null and $form_id != '') {
             $sql .= "AND ff.form_id = " . $form_id . " ";
         }
         $sql .= 'AND (r.access_level <= ' . $user_level . ' OR r.access_level IS NULL)';
         $sql .= " ORDER BY ff.field_position ASC";
         // Execute the SQL to fetch the custom form fields
         $form_fields = Database::instance()->query($sql);
         foreach ($form_fields as $custom_formfield) {
             if ($data_only) {
                 // Return Data Only
                 $fields_array[$custom_formfield->id] = $custom_formfield->form_response;
             } else {
                 // Return Field Structure
                 $fields_array[$custom_formfield->id] = array('field_id' => $custom_formfield->id, 'field_name' => $custom_formfield->field_name, 'field_type' => $custom_formfield->field_type, 'field_default' => $custom_formfield->field_default, 'field_required' => $custom_formfield->field_required, 'field_maxlength' => $custom_formfield->field_maxlength, 'field_height' => $custom_formfield->field_height, 'field_width' => $custom_formfield->field_width, 'field_isdate' => $custom_formfield->field_isdate, 'field_ispublic_visible' => $custom_formfield->field_ispublic_visible, 'field_ispublic_submit' => $custom_formfield->field_ispublic_submit, 'field_response' => $custom_formfield->form_response);
             }
         }
     }
     // Garbage collection
     unset($form_fields);
     // Return
     return $fields_array;
 }
Exemple #9
0
 /**
  * Retrieve Single Marker
  */
 public function single($incident_id = 0)
 {
     $json = "";
     $json_item = "";
     $json_features = array();
     $incident_id = intval($incident_id);
     // Check if incident valid/approved
     if (!Incident_Model::is_valid_incident($incident_id, TRUE)) {
         throw new Kohana_404_Exception();
     }
     // Get the neigbouring incidents
     $neighbours = Incident_Model::get_neighbouring_incidents($incident_id, FALSE, 20, 100);
     if ($neighbours) {
         // Load the incident
         // @todo Get this fixed
         $marker = ORM::factory('incident')->where('incident.incident_active', 1)->find($incident_id);
         if (!$marker->loaded) {
             throw new Kohana_404_Exception();
         }
         // Get the incident/report date
         $incident_date = date('Y-m', strtotime($marker->incident_date));
         foreach ($neighbours as $row) {
             $link = url::base() . "reports/view/" . $row->id;
             $item_name = $this->get_title($row->incident_title, $link);
             $json_item = array();
             $json_item['type'] = 'Feature';
             $json_item['properties'] = array('id' => $row->id, 'name' => $item_name, 'link' => $link, 'category' => array(0), 'timestamp' => strtotime($row->incident_date));
             $json_item['geometry'] = array('type' => 'Point', 'coordinates' => array($row->longitude, $row->latitude));
             array_push($json_features, $json_item);
         }
         // Get Incident Geometries
         $geometry = $this->_get_geometry($marker->id, $marker->incident_title, $marker->incident_date);
         // If there are no geometries, use Single Incident Marker
         if (!count($geometry)) {
             // Single Main Incident
             $link = url::base() . "reports/view/" . $marker->id;
             $item_name = $this->get_title($marker->incident_title, $link);
             $json_item = array();
             $json_item['type'] = 'Feature';
             $json_item['properties'] = array('id' => $marker->id, 'name' => $item_name, 'link' => $link, 'category' => array(0), 'timestamp' => strtotime($marker->incident_date));
             $json_item['geometry'] = array('type' => 'Point', 'coordinates' => array($marker->location->longitude, $marker->location->latitude));
             array_push($json_features, $json_item);
         } else {
             foreach ($geometry as $g) {
                 array_push($json_features, $g);
             }
         }
     }
     Event::run('ushahidi_filter.json_single_features', $json_features);
     $json = json_encode(array("type" => "FeatureCollection", "features" => $json_features));
     header('Content-type: application/json; charset=utf-8');
     echo $json;
 }
Exemple #10
0
 /**
  * Edit a report
  * @param bool|int $id The id no. of the report
  * @param bool|string $saved
  */
 public function edit($id = FALSE, $saved = FALSE)
 {
     $db = new Database();
     $this->template->content = new View('members/reports_edit');
     $this->template->content->title = Kohana::lang('ui_admin.create_report');
     // Setup and initialize form field names
     // JP: added additional form data for advanced settings
     $form = array('location_id' => '', 'form_id' => '', 'locale' => '', 'incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'geometry' => array(), 'location_name' => '', 'country_id' => '', 'country_name' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'custom_field' => array(), 'incident_zoom' => '', 'incident_source' => '', 'incident_information' => '', 'form_data' => array());
     // 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 = $saved == 'saved';
     // Initialize Default Values
     $form['locale'] = Kohana::config('locale.language');
     //$form['latitude'] = Kohana::config('settings.default_lat');
     //$form['longitude'] = Kohana::config('settings.default_lon');
     $form['country_id'] = Kohana::config('settings.default_country');
     $form['incident_date'] = date("m/d/Y", time());
     $form['incident_hour'] = date('h');
     $form['incident_minute'] = date('i');
     $form['incident_ampm'] = date('a');
     // JP: If we are editing an existing report, given by $id,
     // we need to make sure we are using the correct form id.
     // Otherwise, we use the id of the default report (1).
     if ($id and Incident_Model::is_valid_incident($id, FALSE)) {
         $form['form_id'] = ORM::factory('incident', $id)->form_id;
     } else {
         $form['form_id'] = 1;
     }
     // Initialize custom field array
     $form_id = $form['form_id'];
     $form['custom_field'] = customforms::get_custom_form_fields($id, $form_id, TRUE);
     // JP: Grab additional form information for advanced settings.
     $form['form_data'] = customforms::get_custom_form($form_id);
     // Locale (Language) Array
     $this->template->content->locale_array = Kohana::config('locale.all_languages');
     // Time formatting
     $this->template->content->hour_array = $this->_hour_array();
     $this->template->content->minute_array = $this->_minute_array();
     $this->template->content->ampm_array = $this->_ampm_array();
     $this->template->content->stroke_width_array = $this->_stroke_width_array();
     // Get Countries
     $countries = array();
     foreach (ORM::factory('country')->orderby('country')->find_all() as $country) {
         // Create a list of all categories
         $this_country = $country->country;
         if (strlen($this_country) > 35) {
             $this_country = substr($this_country, 0, 35) . "...";
         }
         $countries[$country->id] = $this_country;
     }
     $this->template->content->countries = $countries;
     // Initialize Default Value for Hidden Field Country Name, just incase Reverse Geo coding yields no result
     $form['country_name'] = $countries[$form['country_id']];
     //GET custom forms
     $forms = array();
     foreach (ORM::factory('form')->where('form_active', 1)->find_all() as $custom_forms) {
         $forms[$custom_forms->id] = $custom_forms->form_title;
     }
     $this->template->content->forms = $forms;
     // Retrieve thumbnail photos (if edit);
     //XXX: fix _get_thumbnails
     $this->template->content->incident = $this->_get_thumbnails($id);
     // 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 = array_merge($_POST, $_FILES);
         // JP: Make sure we are using the correct form ID so that the page does not revert to the default form if it is reloaded.
         $form_id = $post['form_id'];
         // JP: Ensure that the advanced settings are correct.
         $form['form_data'] = customforms::get_custom_form($form_id);
         // JP: Add the description_active boolean to our post data so the appropriate validation rules can be added
         $post['description_active'] = $form['form_data']->description_active;
         if (reports::validate($post)) {
             // STEP 1: SAVE LOCATION
             $location = new Location_Model();
             reports::save_location($post, $location);
             // STEP 2: SAVE INCIDENT
             $incident = new Incident_Model($id);
             reports::save_report($post, $incident, $location->id);
             // STEP 2b: SAVE INCIDENT GEOMETRIES
             reports::save_report_geometry($post, $incident);
             // STEP 3: SAVE CATEGORIES
             reports::save_category($post, $incident);
             // STEP 4: SAVE MEDIA
             reports::save_media($post, $incident);
             // STEP 5: SAVE CUSTOM FORM FIELDS
             reports::save_custom_fields($post, $incident);
             // STEP 6: SAVE PERSONAL INFORMATION
             reports::save_personal_info($post, $incident);
             // Action::report_add / report_submit_members - Added a New Report
             Event::run('ushahidi_action.report_submit_members', $post);
             Event::run('ushahidi_action.report_edit', $incident);
             // SAVE AND CLOSE?
             if ($post->save == 1) {
                 // Save but don't close
                 url::redirect('members/reports/edit/' . $incident->id . '/saved');
             } else {
                 // Save and close
                 url::redirect('members/reports/');
             }
         } else {
             // Repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // Populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('report'));
             // JP: replace default Report Title and Description names with custom names in the error listing.
             if ($errors['incident_title'] and !empty($form['form_data']->report_title_name)) {
                 $errors['incident_title'] = str_replace(Kohana::lang('ui_main.reports_title'), $form['form_data']->report_title_name, $errors['incident_title']);
             }
             if ($errors['incident_description'] and !empty($form['form_data']->description_name)) {
                 $errors['incident_description'] = str_replace(Kohana::lang('ui_main.reports_description'), $form['form_data']->description_name, $errors['incident_description']);
             }
             $form_error = TRUE;
         }
     } else {
         if ($id) {
             // Retrieve Current Incident
             $incident = ORM::factory('incident')->where('user_id', $this->user->id)->find($id);
             if ($incident->loaded == true) {
                 // Retrieve Categories
                 $incident_category = array();
                 foreach ($incident->incident_category as $category) {
                     $incident_category[] = $category->category_id;
                 }
                 // Retrieve Media
                 $incident_news = array();
                 $incident_video = array();
                 $incident_photo = array();
                 foreach ($incident->media as $media) {
                     if ($media->media_type == 4) {
                         $incident_news[] = $media->media_link;
                     } elseif ($media->media_type == 2) {
                         $incident_video[] = $media->media_link;
                     } elseif ($media->media_type == 1) {
                         $incident_photo[] = $media->media_link;
                     }
                 }
                 // Get Geometries via SQL query as ORM can't handle Spatial Data
                 $sql = "SELECT AsText(geometry) as geometry, geometry_label, \n\t\t\t\t\t\tgeometry_comment, geometry_color, geometry_strokewidth \n\t\t\t\t\t\tFROM " . Kohana::config('database.default.table_prefix') . "geometry \n\t\t\t\t\t\tWHERE incident_id = ?";
                 $query = $db->query($sql, $id);
                 foreach ($query as $item) {
                     $geometry = array("geometry" => $item->geometry, "label" => $item->geometry_label, "comment" => $item->geometry_comment, "color" => $item->geometry_color, "strokewidth" => $item->geometry_strokewidth);
                     $form['geometry'][] = json_encode($geometry);
                 }
                 // Combine Everything
                 $incident_arr = array('location_id' => $incident->location->id, 'form_id' => $incident->form_id, 'locale' => $incident->locale, 'incident_title' => $incident->incident_title, 'incident_description' => $incident->incident_description, 'incident_date' => date('m/d/Y', strtotime($incident->incident_date)), 'incident_hour' => date('h', strtotime($incident->incident_date)), 'incident_minute' => date('i', strtotime($incident->incident_date)), 'incident_ampm' => date('a', strtotime($incident->incident_date)), 'latitude' => $incident->location->latitude, 'longitude' => $incident->location->longitude, 'location_name' => $incident->location->location_name, 'country_id' => $incident->location->country_id, 'incident_category' => $incident_category, 'incident_news' => $incident_news, 'incident_video' => $incident_video, 'incident_photo' => $incident_photo, 'person_first' => $incident->incident_person->person_first, 'person_last' => $incident->incident_person->person_last, 'person_email' => $incident->incident_person->person_email, 'incident_source' => '', 'incident_information' => '', 'custom_field' => customforms::get_custom_form_fields($id, $incident->form_id, TRUE), 'incident_zoom' => $incident->incident_zoom);
                 // Merge To Form Array For Display
                 $form = arr::overwrite($form, $incident_arr);
             } else {
                 // Redirect
                 url::redirect('members/reports/');
             }
         }
     }
     $this->template->content->id = $id;
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     // Retrieve Custom Form Fields Structure
     $this->template->content->custom_forms = new View('reports/submit_custom_forms');
     $disp_custom_fields = customforms::get_custom_form_fields($id, $form['form_id'], FALSE, "view");
     $custom_field_mismatch = customforms::get_edit_mismatch($form['form_id']);
     // Quick hack to make sure view-only fields have data set
     foreach ($custom_field_mismatch as $id => $field) {
         $form['custom_field'][$id] = $disp_custom_fields[$id]['field_response'];
     }
     $this->template->content->custom_forms->disp_custom_fields = $disp_custom_fields;
     $this->template->content->custom_forms->custom_field_mismatch = $custom_field_mismatch;
     $this->template->content->custom_forms->form = $form;
     // Retrieve Previous & Next Records
     $previous = ORM::factory('incident')->where('id < ', $id)->orderby('id', 'desc')->find();
     $previous_url = $previous->loaded ? url::site('members/reports/edit/' . $previous->id) : url::site() . 'members/reports/';
     $next = ORM::factory('incident')->where('id > ', $id)->orderby('id', 'desc')->find();
     $next_url = $next->loaded ? url::site('members/reports/edit/' . $next->id) : url::site('members/reports/');
     $this->template->content->previous_url = $previous_url;
     $this->template->content->next_url = $next_url;
     // Javascript Header
     $this->themes->map_enabled = TRUE;
     $this->themes->colorpicker_enabled = TRUE;
     $this->themes->treeview_enabled = TRUE;
     $this->themes->json2_enabled = TRUE;
     $this->themes->js = new View('reports/submit_edit_js');
     $this->themes->js->edit_mode = FALSE;
     $this->themes->js->default_map = Kohana::config('settings.default_map');
     $this->themes->js->default_zoom = Kohana::config('settings.default_zoom');
     if (!$form['latitude'] or !$form['latitude']) {
         $this->themes->js->latitude = Kohana::config('settings.default_lat');
         $this->themes->js->longitude = Kohana::config('settings.default_lon');
     } else {
         $this->themes->js->latitude = $form['latitude'];
         $this->themes->js->longitude = $form['longitude'];
     }
     $this->themes->js->incident_zoom = $form['incident_zoom'];
     $this->themes->js->geometries = $form['geometry'];
     // Inline Javascript
     $this->template->content->date_picker_js = $this->_date_picker_js();
     $this->template->content->color_picker_js = $this->_color_picker_js();
     // Pack Javascript
     $myPacker = new javascriptpacker($this->themes->js, 'Normal', FALSE, FALSE);
     $this->themes->js = $myPacker->pack();
 }
 /**
  * Tag a news item to an incident.
  * 
  * @param int incidentid - The incident id.
  * @param string mediatype - The media type,video, picture,etc
  *
  * @return Array
  */
 private function _tag_media($incidentid, $mediatype)
 {
     if ($_POST) {
         // Check if incident ID exist
         $incidentid_exist = Incident_Model::is_valid_incident($incidentid);
         if (!$incidentid_exist) {
             return $this->set_error_message(array("error" => $this->api_service->get_error_msg(012)));
         }
         // Get the locationid for the incidentid
         $locationid = 0;
         $items = ORM::factory('incident')->select(array('location_id'))->where(array('incident.id' => $incidentid))->find();
         if ($items->count_all() > 0) {
             $locationid = $items->location_id;
         }
         $media = new Media_Model();
         //create media model object
         $url = '';
         $post = Validation::factory(array_merge($_POST, $_FILES));
         if ($mediatype == 2 or $mediatype == 4) {
             //require a url
             if (!$this->api_service->verify_array_index($this->request, 'url')) {
                 return $this->set_error_message(array("error" => $this->api_service->get_error_msg(01, 'url')));
             } else {
                 $url = $this->request['url'];
                 $media->media_link = $url;
             }
         } else {
             if (!$this->api_service->verify_array_index($this->request, 'photo')) {
                 $this->set_error_message(array("error" => $this->api_service->get_error_msg(01), 'photo'));
             }
             $post->add_rules('photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[1M]');
             if ($post->validate(FALSE)) {
                 //assuming this is a photo
                 $filename = upload::save('photo');
                 $new_filename = $incidentid . "_" . $i . "_" . time();
                 // Resize original file... make sure its max 408px wide
                 Image::factory($filename)->resize(408, 248, Image::AUTO)->save(Kohana::config('upload.directory', TRUE) . $new_filename . ".jpg");
                 // Create thumbnail
                 Image::factory($filename)->resize(70, 41, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . "_t.jpg");
                 // Remove the temporary file
                 unlink($filename);
                 $media->media_link = $new_filename . ".jpg";
                 $media->media_thumb = $new_filename . "_t.jpg";
             }
         }
         // Optional title & description
         $title = '';
         if ($this->api_service->verify_array_index($_POST, 'title')) {
             $title = $_POST['title'];
         }
         $description = '';
         if ($this->api_service->verify_array_index($_POST, 'description')) {
             $description = $_POST['description'];
         }
         $media->location_id = $locationid;
         $media->incident_id = $incidentid;
         $media->media_type = $mediatype;
         $media->media_title = $title;
         $media->media_description = $description;
         $media->media_date = date("Y-m-d H:i:s", time());
         $media->save();
         //save the thing
         // SUCESS!!!
         $ret = array("payload" => array("domain" => $this->domain, "success" => "true"), "error" => $this->api_service->get_error_msg(0));
         return $this->set_error_message($ret);
     } else {
         return $this->set_error_message(array("error" => $this->api_service->get_error_msg(03)));
     }
 }