/**
  * Submits a new report.
  */
 public function submit($id = false, $saved = false)
 {
     $this->template->header->this_page = 'reports_submit';
     $this->template->content = new View('reports_submit');
     // setup and initialize form field names
     $form = array('incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'location_name' => '', 'country_id' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'incident_doc' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'form_id' => '', 'custom_field' => 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;
     if ($saved == 'saved') {
         $form_saved = TRUE;
     } else {
         $form_saved = FALSE;
     }
     // Initialize Default Values
     $form['incident_date'] = date("m/d/Y", time());
     $form['incident_hour'] = "12";
     $form['incident_minute'] = "00";
     $form['incident_ampm'] = "pm";
     // initialize custom field array
     $form['custom_field'] = $this->_get_custom_form_fields($id, '', true);
     //GET custom forms
     $forms = array();
     foreach (ORM::factory('form')->find_all() as $custom_forms) {
         $forms[$custom_forms->id] = $custom_forms->form_title;
     }
     $this->template->content->forms = $forms;
     // 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 = Validation::factory(array_merge($_POST, $_FILES));
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         $post->add_rules('incident_title', 'required', 'length[3,200]');
         $post->add_rules('incident_description', 'required');
         $post->add_rules('incident_date', 'required', 'date_mmddyyyy');
         $post->add_rules('incident_hour', 'required', 'between[1,12]');
         $post->add_rules('incident_minute', 'required', 'between[0,59]');
         if ($_POST['incident_ampm'] != "am" && $_POST['incident_ampm'] != "pm") {
             $post->add_error('incident_ampm', 'values');
         }
         // Validate for maximum and minimum latitude values
         $post->add_rules('latitude', 'required', 'between[-90,90]');
         $post->add_rules('longitude', 'required', 'between[-180,180]');
         $post->add_rules('location_name', 'required', 'length[3,200]');
         //XXX: Hack to validate for no checkboxes checked
         if (!isset($_POST['incident_category'])) {
             $post->incident_category = "";
             $post->add_error('incident_category', 'required');
         } else {
             $post->add_rules('incident_category.*', 'required', 'numeric');
         }
         // Validate only the fields that are filled in
         if (!empty($_POST['incident_news'])) {
             foreach ($_POST['incident_news'] as $key => $url) {
                 if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
                     $post->add_error('incident_news', 'url');
                 }
             }
         }
         // Validate only the fields that are filled in
         if (!empty($_POST['incident_video'])) {
             foreach ($_POST['incident_video'] as $key => $url) {
                 if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
                     $post->add_error('incident_video', 'url');
                 }
             }
         }
         // Validate photo uploads
         $post->add_rules('incident_photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[2M]');
         // Validate doc uploads
         $post->add_rules('incident_doc', 'upload::valid', 'upload::type[doc,pdf,odt,xml]', 'upload::size[2M]');
         // Validate Personal Information
         if (!empty($_POST['person_first'])) {
             $post->add_rules('person_first', 'length[3,100]');
         }
         if (!empty($_POST['person_last'])) {
             $post->add_rules('person_last', 'length[3,100]');
         }
         if (!empty($_POST['person_email'])) {
             $post->add_rules('person_email', 'email', 'length[3,100]');
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // STEP 1: SAVE LOCATION
             $location = new Location_Model();
             $location->location_name = $post->location_name;
             $location->latitude = $post->latitude;
             $location->longitude = $post->longitude;
             $location->location_date = date("Y-m-d H:i:s", time());
             $location->save();
             // STEP 2: SAVE INCIDENT
             $incident = new Incident_Model();
             $incident->location_id = $location->id;
             $incident->form_id = $post->form_id;
             $incident->user_id = 0;
             $incident->incident_title = $post->incident_title;
             $incident->incident_description = $post->incident_description;
             $incident_date = explode("/", $post->incident_date);
             // The $_POST['date'] is a value posted by form in mm/dd/yyyy format
             $incident_date = $incident_date[2] . "-" . $incident_date[0] . "-" . $incident_date[1];
             $incident_time = $post->incident_hour . ":" . $post->incident_minute . ":00 " . $post->incident_ampm;
             $incident->incident_date = $incident_date . " " . $incident_time;
             $incident->incident_dateadd = date("Y-m-d H:i:s", time());
             $incident->save();
             // STEP 3: SAVE CATEGORIES
             foreach ($post->incident_category as $item) {
                 $incident_category = new Incident_Category_Model();
                 $incident_category->incident_id = $incident->id;
                 $incident_category->category_id = $item;
                 $incident_category->save();
             }
             // STEP 4: SAVE MEDIA
             // a. News
             foreach ($post->incident_news as $item) {
                 if (!empty($item)) {
                     $news = new Media_Model();
                     $news->location_id = $location->id;
                     $news->incident_id = $incident->id;
                     $news->media_type = 4;
                     // News
                     $news->media_link = $item;
                     $news->media_date = date("Y-m-d H:i:s", time());
                     $news->save();
                 }
             }
             // b. Video
             foreach ($post->incident_video as $item) {
                 if (!empty($item)) {
                     $video = new Media_Model();
                     $video->location_id = $location->id;
                     $video->incident_id = $incident->id;
                     $video->media_type = 2;
                     // Video
                     $video->media_link = $item;
                     $video->media_date = date("Y-m-d H:i:s", time());
                     $video->save();
                 }
             }
             // c. Photos
             $filenames = upload::save('incident_photo');
             $i = 1;
             foreach ($filenames as $filename) {
                 $new_filename = $incident->id . "_" . $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);
                 // Save to DB
                 $photo = new Media_Model();
                 $photo->location_id = $location->id;
                 $photo->incident_id = $incident->id;
                 $photo->media_type = 1;
                 // Images
                 $photo->media_link = $new_filename . ".jpg";
                 $photo->media_thumb = $new_filename . "_t.jpg";
                 $photo->media_date = date("Y-m-d H:i:s", time());
                 $photo->save();
                 $i++;
             }
             // d. Docs
             $docnames = upload::save('incident_doc', '', Kohana::config('upload.directory', TRUE) . 'docs/');
             $i = 1;
             $extensions = array('.doc' => 1, '.pdf' => 2, '.xml' => 3, '.odt' => 4);
             foreach ($docnames as $docname) {
                 $new_docname = $incident->id . "_" . $i . "_" . time() . strrchr($docname, '.');
                 // 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");*/
                 //upload::save($docname,$new_docname,Kohana::config('upload.directory',TRUE). 'docs/');
                 // Remove the temporary file
                 //rename file
                 rename($docname, Kohana::config('upload.directory', TRUE) . 'docs/' . $new_docname);
                 //unlink($docname);
                 // Save to DB
                 $docs = new Doc_Model();
                 $docs->location_id = $location->id;
                 $docs->incident_id = $incident->id;
                 $docs->doc_type = $extensions[strrchr($docname, '.')];
                 // Images
                 $docs->doc_link = $new_docname;
                 $docs->doc_date = date("Y-m-d H:i:s", time());
                 $docs->save();
                 $i++;
             }
             // STEP 7: SAVE CUSTOM FORM FIELDS
             if (isset($post->custom_field)) {
                 foreach ($post->custom_field as $key => $value) {
                     $form_response = ORM::factory('form_response')->where('form_field_id', $key)->where('incident_id', $incident->id)->find();
                     if ($form_response->loaded == true) {
                         $form_response->form_field_id = $key;
                         $form_response->form_response = $value;
                         $form_response->save();
                     } else {
                         $form_response = new Form_Response_Model();
                         $form_response->form_field_id = $key;
                         $form_response->incident_id = $incident->id;
                         $form_response->form_response = $value;
                         $form_response->save();
                     }
                 }
             }
             // STEP 5: SAVE PERSONAL INFORMATION
             $person = new Incident_Person_Model();
             $person->location_id = $location->id;
             $person->incident_id = $incident->id;
             $person->person_first = $post->person_first;
             $person->person_last = $post->person_last;
             $person->person_email = $post->person_email;
             $person->person_date = date("Y-m-d H:i:s", time());
             $person->save();
             url::redirect('reports/thanks');
         } 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'));
             $form_error = TRUE;
         }
     }
     // Retrieve Country Cities
     $default_country = Kohana::config('settings.default_country');
     $this->template->content->cities = $this->_get_cities($default_country);
     $this->template->content->multi_country = Kohana::config('settings.multi_country');
     $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->categories = $this->_get_categories($form['incident_category']);
     // Retrieve Custom Form Fields Structure
     $disp_custom_fields = $this->_get_custom_form_fields($id, $form['form_id'], false);
     $this->template->content->disp_custom_fields = $disp_custom_fields;
     // Javascript Header
     $this->template->header->map_enabled = TRUE;
     $this->template->header->datepicker_enabled = TRUE;
     $this->template->header->treeview_enabled = TRUE;
     $this->template->header->js = new View('reports_submit_js');
     $this->template->header->js->default_map = Kohana::config('settings.default_map');
     $this->template->header->js->default_zoom = Kohana::config('settings.default_zoom');
     if (!$form['latitude'] || !$form['latitude']) {
         $this->template->header->js->latitude = Kohana::config('settings.default_lat');
         $this->template->header->js->longitude = Kohana::config('settings.default_lon');
     } else {
         $this->template->header->js->latitude = $form['latitude'];
         $this->template->header->js->longitude = $form['longitude'];
     }
     //include footer form js file
     $footerjs = new View('footer_form_js');
     // Pack the javascript using the javascriptpacker helper
     $myPacker = new javascriptpacker($footerjs, 'Normal', false, false);
     $footerjs = $myPacker->pack();
     $this->template->header->js .= $footerjs;
 }
Exemple #2
0
 /**
  * Edit a report
  * @param bool|int $id The id no. of the report
  * @param bool|string $saved
  */
 function edit($id = false, $saved = false)
 {
     $this->template->content = new View('admin/reports_edit');
     $this->template->content->title = 'Create A 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' => '', 'location_name' => '', 'country_id' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'incident_doc' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'custom_field' => array(), 'incident_active' => '', 'incident_verified' => '', 'incident_source' => '', 'incident_information' => '');
     //  copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     if ($saved == 'saved') {
         $form_saved = TRUE;
     } else {
         $form_saved = FALSE;
     }
     // 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'] = "12";
     $form['incident_minute'] = "00";
     $form['incident_ampm'] = "pm";
     // initialize custom field array
     $form['custom_field'] = $this->_get_custom_form_fields($id, '', true);
     // Locale (Language) Array
     $this->template->content->locale_array = Kohana::config('locale.all_languages');
     // Create Categories
     $this->template->content->categories = $this->_get_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();
     // 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;
     //GET custom forms
     $forms = array();
     foreach (ORM::factory('form')->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);
     // Are we creating this report from SMS/Email/Twitter?
     // If so retrieve message
     if (isset($_GET['mid']) && !empty($_GET['mid'])) {
         $message_id = $_GET['mid'];
         $service_id = "";
         $message = ORM::factory('message', $message_id);
         if ($message->loaded == true && $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)) {
                 $incident_description .= "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" . $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;
             // 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;
     }
     // 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 = Validation::factory(array_merge($_POST, $_FILES));
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         // Add some rules, the input field, followed by a list of checks, carried out in order
         // $post->add_rules('locale','required','alpha_dash','length[5]');
         $post->add_rules('location_id', 'numeric');
         $post->add_rules('message_id', 'numeric');
         $post->add_rules('incident_title', 'required', 'length[3,200]');
         $post->add_rules('incident_description', 'required');
         $post->add_rules('incident_date', 'required', 'date_mmddyyyy');
         $post->add_rules('incident_hour', 'required', 'between[1,12]');
         $post->add_rules('incident_minute', 'required', 'between[0,59]');
         if ($_POST['incident_ampm'] != "am" && $_POST['incident_ampm'] != "pm") {
             $post->add_error('incident_ampm', 'values');
         }
         $post->add_rules('latitude', 'required', 'between[-90,90]');
         // Validate for maximum and minimum latitude values
         $post->add_rules('longitude', 'required', 'between[-180,180]');
         // Validate for maximum and minimum longitude values
         $post->add_rules('location_name', 'required', 'length[3,200]');
         //XXX: Hack to validate for no checkboxes checked
         if (!isset($_POST['incident_category'])) {
             $post->incident_category = "";
             $post->add_error('incident_category', 'required');
         } else {
             $post->add_rules('incident_category.*', 'required', 'numeric');
         }
         // Validate only the fields that are filled in
         if (!empty($_POST['incident_news'])) {
             foreach ($_POST['incident_news'] as $key => $url) {
                 if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
                     $post->add_error('incident_news', 'url');
                 }
             }
         }
         // Validate only the fields that are filled in
         if (!empty($_POST['incident_video'])) {
             foreach ($_POST['incident_video'] as $key => $url) {
                 if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
                     $post->add_error('incident_video', 'url');
                 }
             }
         }
         // Validate photo uploads
         $post->add_rules('incident_photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[2M]');
         // Validate doc uploads
         $post->add_rules('incident_doc', 'upload::valid', 'upload::type[doc,pdf,odt,xml]', 'upload::size[2M]');
         // Validate Personal Information
         if (!empty($_POST['person_first'])) {
             $post->add_rules('person_first', 'length[3,100]');
         }
         if (!empty($_POST['person_last'])) {
             $post->add_rules('person_last', 'length[3,100]');
         }
         if (!empty($_POST['person_email'])) {
             $post->add_rules('person_email', 'email', 'length[3,100]');
         }
         // Validate Custom Fields
         if (isset($post->custom_field) && !$this->_validate_custom_form_fields($post->custom_field)) {
             $post->add_error('custom_field', 'values');
         }
         $post->add_rules('incident_active', 'required', 'between[0,1]');
         $post->add_rules('incident_verified', 'required', 'length[0,1]');
         $post->add_rules('incident_source', 'alpha', 'length[1,1]');
         $post->add_rules('incident_information', 'numeric', 'length[1,1]');
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             // Yes! everything is valid
             $location_id = $post->location_id;
             // STEP 1: SAVE LOCATION
             $location = new Location_Model($location_id);
             $location->location_name = $post->location_name;
             $location->latitude = $post->latitude;
             $location->longitude = $post->longitude;
             $location->location_date = date("Y-m-d H:i:s", time());
             $location->save();
             // STEP 2: SAVE INCIDENT
             $incident = new Incident_Model($id);
             $incident->location_id = $location->id;
             //$incident->locale = $post->locale;
             $incident->form_id = $post->form_id;
             $incident->user_id = $_SESSION['auth_user']->id;
             $incident->incident_title = $post->incident_title;
             $incident->incident_description = $post->incident_description;
             $incident_date = explode("/", $post->incident_date);
             // where the $_POST['date'] is a value posted by form in mm/dd/yyyy format
             $incident_date = $incident_date[2] . "-" . $incident_date[0] . "-" . $incident_date[1];
             $incident_time = $post->incident_hour . ":" . $post->incident_minute . ":00 " . $post->incident_ampm;
             $incident->incident_date = $incident_date . " " . $incident_time;
             // Is this new or edit?
             if ($id) {
                 $incident->incident_datemodify = date("Y-m-d H:i:s", time());
             } else {
                 $incident->incident_dateadd = date("Y-m-d H:i:s", time());
             }
             // Is this an Email, SMS, Twitter submitted report?
             //XXX: We may get rid of incident_mode altogether... ???
             //$_POST
             if (!empty($service_id)) {
                 if ($service_id == 1) {
                     // SMS
                     $incident->incident_mode = 2;
                 } elseif ($service_id == 2) {
                     // Email
                     $incident->incident_mode = 3;
                 } elseif ($service_id == 3) {
                     // Twitter
                     $incident->incident_mode = 4;
                 } elseif ($service_id == 4) {
                     // Laconica
                     $incident->incident_mode = 5;
                 }
             }
             // Incident Evaluation Info
             $incident->incident_active = $post->incident_active;
             $incident->incident_verified = $post->incident_verified;
             $incident->incident_source = $post->incident_source;
             $incident->incident_information = $post->incident_information;
             //Save
             $incident->save();
             // Record Approval/Verification Action
             $verify = new Verify_Model();
             $verify->incident_id = $incident->id;
             $verify->user_id = $_SESSION['auth_user']->id;
             // Record 'Verified By' Action
             $verify->verified_date = date("Y-m-d H:i:s", time());
             if ($post->incident_active == 1) {
                 $verify->verified_status = '1';
             } elseif ($post->incident_verified == 1) {
                 $verify->verified_status = '2';
             } elseif ($post->incident_active == 1 && $post->incident_verified == 1) {
                 $verify->verified_status = '3';
             } else {
                 $verify->verified_status = '0';
             }
             $verify->save();
             // STEP 3: SAVE CATEGORIES
             ORM::factory('Incident_Category')->where('incident_id', $incident->id)->delete_all();
             // Delete Previous Entries
             foreach ($post->incident_category as $item) {
                 $incident_category = new Incident_Category_Model();
                 $incident_category->incident_id = $incident->id;
                 $incident_category->category_id = $item;
                 $incident_category->save();
             }
             // STEP 4: SAVE MEDIA
             ORM::factory('Media')->where('incident_id', $incident->id)->where('media_type <> 1')->delete_all();
             // Delete Previous Entries
             // a. News
             foreach ($post->incident_news as $item) {
                 if (!empty($item)) {
                     $news = new Media_Model();
                     $news->location_id = $location->id;
                     $news->incident_id = $incident->id;
                     $news->media_type = 4;
                     // News
                     $news->media_link = $item;
                     $news->media_date = date("Y-m-d H:i:s", time());
                     $news->save();
                 }
             }
             // b. Video
             foreach ($post->incident_video as $item) {
                 if (!empty($item)) {
                     $video = new Media_Model();
                     $video->location_id = $location->id;
                     $video->incident_id = $incident->id;
                     $video->media_type = 2;
                     // Video
                     $video->media_link = $item;
                     $video->media_date = date("Y-m-d H:i:s", time());
                     $video->save();
                 }
             }
             // c. Photos
             $filenames = upload::save('incident_photo');
             $i = 1;
             foreach ($filenames as $filename) {
                 $new_filename = $incident->id . "_" . $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);
                 // Save to DB
                 $photo = new Media_Model();
                 $photo->location_id = $location->id;
                 $photo->incident_id = $incident->id;
                 $photo->media_type = 1;
                 // Images
                 $photo->media_link = $new_filename . ".jpg";
                 $photo->media_thumb = $new_filename . "_t.jpg";
                 $photo->media_date = date("Y-m-d H:i:s", time());
                 $photo->save();
                 $i++;
             }
             // d. Docs
             $docnames = upload::save('incident_doc', '', Kohana::config('upload.directory', TRUE) . 'docs/');
             $i = 1;
             $extensions = array('.doc' => 1, '.pdf' => 2, '.xml' => 3, '.odt' => 4);
             foreach ($docnames as $docname) {
                 $new_docname = $incident->id . "_" . $i . "_" . time() . strrchr($docname, '.');
                 // 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");*/
                 //upload::save($docname,$new_docname,Kohana::config('upload.directory',TRUE). 'docs/');
                 // Remove the temporary file
                 //rename file
                 rename($docname, Kohana::config('upload.directory', TRUE) . 'docs/' . $new_docname);
                 //unlink($docname);
                 // Save to DB
                 $docs = new Doc_Model();
                 $docs->location_id = $location->id;
                 $docs->incident_id = $incident->id;
                 $docs->doc_type = $extensions[strrchr($docname, '.')];
                 // Images
                 $docs->doc_link = $new_docname;
                 $docs->doc_date = date("Y-m-d H:i:s", time());
                 $docs->save();
                 $i++;
             }
             // STEP 5: SAVE PERSONAL INFORMATION
             ORM::factory('Incident_Person')->where('incident_id', $incident->id)->delete_all();
             // Delete Previous Entries
             $person = new Incident_Person_Model();
             $person->location_id = $location->id;
             $person->incident_id = $incident->id;
             $person->person_first = $post->person_first;
             $person->person_last = $post->person_last;
             $person->person_email = $post->person_email;
             $person->person_date = date("Y-m-d H:i:s", time());
             $person->save();
             // STEP 6: SAVE LINK TO REPORTER MESSAGE
             if (isset($message_id) && $message_id != "") {
                 $savemessage = ORM::factory('message', $message_id);
                 if ($savemessage->loaded == true) {
                     $savemessage->incident_id = $incident->id;
                     $savemessage->save();
                 }
             }
             // STEP 7: SAVE CUSTOM FORM FIELDS
             if (isset($post->custom_field)) {
                 foreach ($post->custom_field as $key => $value) {
                     $form_response = ORM::factory('form_response')->where('form_field_id', $key)->where('incident_id', $incident->id)->find();
                     if ($form_response->loaded == true) {
                         $form_response->form_field_id = $key;
                         $form_response->form_response = $value;
                         $form_response->save();
                     } else {
                         $form_response = new Form_Response_Model();
                         $form_response->form_field_id = $key;
                         $form_response->incident_id = $incident->id;
                         $form_response->form_response = $value;
                         $form_response->save();
                     }
                 }
             }
             // SAVE AND CLOSE?
             if ($post->save == 1) {
                 url::redirect('admin/reports/edit/' . $incident->id . '/saved');
             } else {
                 url::redirect('admin/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'));
             $form_error = TRUE;
         }
     } else {
         if ($id) {
             // 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();
                 $incident_doc = array();
                 foreach ($incident->media as $media) {
                     if ($media->media_type == 5) {
                         $incident_doc = $media->media_link;
                     } elseif ($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;
                     }
                 }
                 // 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, 'incident_doc' => $incident_doc, 'person_first' => $incident->incident_person->person_first, 'person_last' => $incident->incident_person->person_last, 'person_email' => $incident->incident_person->person_email, 'custom_field' => $this->_get_custom_form_fields($id, $incident->form_id, true), 'incident_active' => $incident->incident_active, 'incident_verified' => $incident->incident_verified, 'incident_source' => $incident->incident_source, 'incident_information' => $incident->incident_information);
                 // 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
     $disp_custom_fields = $this->_get_custom_form_fields($id, $form['form_id'], false);
     $this->template->content->disp_custom_fields = $disp_custom_fields;
     // Retrieve Previous & Next Records
     $previous = ORM::factory('incident')->where('id < ', $id)->orderby('id', 'desc')->find();
     $previous_url = $previous->loaded ? url::base() . 'admin/reports/edit/' . $previous->id : url::base() . 'admin/reports/';
     $next = ORM::factory('incident')->where('id > ', $id)->orderby('id', 'desc')->find();
     $next_url = $next->loaded ? url::base() . 'admin/reports/edit/' . $next->id : url::base() . 'admin/reports/';
     $this->template->content->previous_url = $previous_url;
     $this->template->content->next_url = $next_url;
     // Javascript Header
     $this->template->map_enabled = TRUE;
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/reports_edit_js');
     $this->template->js->default_map = Kohana::config('settings.default_map');
     $this->template->js->default_zoom = Kohana::config('settings.default_zoom');
     if (!$form['latitude'] || !$form['latitude']) {
         $this->template->js->latitude = Kohana::config('settings.default_lat');
         $this->template->js->longitude = Kohana::config('settings.default_lon');
     } else {
         $this->template->js->latitude = $form['latitude'];
         $this->template->js->longitude = $form['longitude'];
     }
     // 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();
 }