/** * Submits a new report. */ public function submit($id = false, $saved = false) { // First, are we allowed to submit new reports? if ( ! Kohana::config('settings.allow_reports')) { url::redirect(url::site().'main'); } $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(), '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'] = date('g'); $form['incident_minute'] = date('i'); $form['incident_ampm'] = date('a'); // 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" AND $_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 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 = date( "Y-m-d H:i:s", strtotime($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(); $file_type = strrev(substr(strrev($filename),0,4)); // IMAGE SIZES: 800X600, 400X300, 89X59 // Large size Image::factory($filename)->resize(800,600,Image::AUTO) ->save(Kohana::config('upload.directory', TRUE).$new_filename.$file_type); // Medium size Image::factory($filename)->resize(400,300,Image::HEIGHT) ->save(Kohana::config('upload.directory', TRUE).$new_filename."_m".$file_type); // Thumbnail Image::factory($filename)->resize(89,59,Image::HEIGHT) ->save(Kohana::config('upload.directory', TRUE).$new_filename."_t".$file_type); // 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.$file_type; $photo->media_medium = $new_filename."_m".$file_type; $photo->media_thumb = $new_filename."_t".$file_type; $photo->media_date = date("Y-m-d H:i:s",time()); $photo->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(); // Action::report_add - Added a New Report Event::run('ushahidi_action.report_add', $incident); url::redirect('reports/thanks'); } // No! We have validation errors, we need to show the form again, with the errors 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; $categories = $this->get_categories($form['incident_category']); $this->template->content->categories = $categories; // Pass timezone $this->template->content->site_timezone = Kohana::config('settings.site_timezone'); // 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->themes->map_enabled = TRUE; $this->themes->datepicker_enabled = TRUE; $this->themes->treeview_enabled = TRUE; $this->themes->js = new View('reports_submit_js'); $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']; } // Rebuild Header Block $this->template->header->header_block = $this->themes->header_block(); }
/** * Function to save custom field values * * @param mixed $incident_model * @param mixed $post * */ public static function save_custom_fields($post, $incident) { 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(); } } } }
/** * 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 = 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' => '', 'location_name' => '', 'country_id' => '', '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_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'] = date('g'); $form['incident_minute'] = date('i'); $form['incident_ampm'] = date('a'); // 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; } // Are we creating this report from a Newsfeed? if (isset($_GET['fid']) && !empty($_GET['fid'])) { $feed_item_id = $_GET['fid']; $feed_item = ORM::factory('feed_item', $feed_item_id); if ($feed_item->loaded == true) { // 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; } } 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 = 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 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', 'numeric', '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 = date("Y-m-d H:i:s", strtotime($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++; } // 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 6a: SAVE LINK TO REPORTER MESSAGE // We're creating a report from a message with this option if (isset($message_id) && $message_id != "") { $savemessage = ORM::factory('message', $message_id); if ($savemessage->loaded == true) { $savemessage->incident_id = $incident->id; $savemessage->save(); } } // STEP 6b: SAVE LINK TO NEWS FEED // We're creating a report from a newsfeed with this option if (isset($feed_item_id) && $feed_item_id != "") { $savefeed = ORM::factory('feed_item', $feed_item_id); if ($savefeed->loaded == true) { $savefeed->incident_id = $incident->id; $savefeed->location_id = $location->id; $savefeed->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(); 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; } } // 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' => $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->treeview_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(); }
/** * Edit a report * @param bool|int $id The id no. of the report * @param bool|string $saved */ function edit($id = false, $saved = false) { $db = new Database(); $this->template->content = new View('admin/reports_edit'); $this->template->content = View::factory('simplegroups/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' => '', 'incident_category' => array(), 'incident_group_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'incident_status' => array(), 'phone_number' => '', '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; // 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'); // initialize custom field array $form['custom_field'] = $this->_get_custom_form_fields($id, '', true); $number_of_message_sender = null; // 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->group_categories = $this->_get_group_categories(); $this->template->content->new_categories_form = $this->_new_categories_form_arr(); $this->template->content->group_name = $this->group->name; // 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; //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); // 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); //figure out the group number that sent the message $number_items = ORM::factory("simplegroups_groups_number")->join("simplegroups_groups_message", "simplegroups_groups_message.number_id", "simplegroups_groups_numbers.id")->where("simplegroups_groups_message.message_id", $message_id)->find_all(); foreach ($number_items as $number_item) { $number_of_message_sender = $number_item; } 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/simplegroups/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; // Does the sender of this message have a location? if ($message->reporter->location->loaded) { $form['latitude'] = $message->reporter->location->latitude; $form['longitude'] = $message->reporter->location->longitude; $form['location_name'] = $message->reporter->location->location_name; } // 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']) && !empty($_GET['fid'])) { $feed_item_id = $_GET['fid']; $feed_item = ORM::factory('feed_item', $feed_item_id); if ($feed_item->loaded == true) { // Has a report already been created for this Feed item? if ($feed_item->incident_id != 0) { // Redirect to report url::redirect('admin/simplegroups/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; } } 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 = 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_status', '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 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','numeric', 'length[1,1]'); //$post->add_rules('incident_information','numeric', 'length[1,1]'); // Action::report_submit_admin - Report Posted Event::run('ushahidi_action.report_submit_admin', $post); // 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->incident_status = $post->incident_status; $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 = date("Y-m-d H:i:s", strtotime($incident_date . " " . $incident_time)); $is_new = false; // 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_new = true; } // 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; //$incident->incident_zoom = (int) $post->incident_zoom; //Save $incident->save(); // Tag this as a report that needs to be sent out as an alert if ($incident->incident_active == '1' and $incident->incident_alert_status != '2') { // 2 = report that has had an alert sent $incident->incident_alert_status = '1'; $incident->save(); } // Remove alert if report is unactivated and alert hasn't yet been sent if ($incident->incident_active == '0' and $incident->incident_alert_status == '1') { $incident->incident_alert_status = '0'; $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 2.5: SAVE THE GROUP ASSOCIATION if ($is_new) { $group_incident = ORM::factory("simplegroups_groups_incident"); $group_incident->incident_id = $incident->id; $group_incident->simplegroups_groups_id = $this->group->id; if ($number_of_message_sender) { $group_incident->number_id = $number_of_message_sender->id; } $group_incident->save(); } // STEP 2b: SAVE INCIDENT GEOMETRIES ORM::factory('geometry')->where('incident_id', $incident->id)->delete_all(); if (isset($post->geometry)) { foreach ($post->geometry as $item) { if (!empty($item)) { //Decode JSON $item = json_decode($item); //++ TODO - validate geometry $geometry = isset($item->geometry) ? mysql_escape_string($item->geometry) : ""; $label = isset($item->label) ? mysql_escape_string(substr($item->label, 0, 150)) : ""; $comment = isset($item->comment) ? mysql_escape_string(substr($item->comment, 0, 255)) : ""; $color = isset($item->color) ? mysql_escape_string(substr($item->color, 0, 6)) : ""; $strokewidth = (isset($item->strokewidth) and (double) $item->strokewidth) ? (double) $item->strokewidth : "2.5"; if ($geometry) { //++ Can't Use ORM for this $sql = "INSERT INTO " . Kohana::config('database.default.table_prefix') . "geometry (\n\t\t\t\t\t\t\tincident_id, geometry, geometry_label, geometry_comment, geometry_color, geometry_strokewidth ) \n\t\t\t\t\t\t\tVALUES( " . $incident->id . ",\n\t\t\t\t\t\t\tGeomFromText( '" . $geometry . "' ),'" . $label . "','" . $comment . "','" . $color . "','" . $strokewidth . "')"; $db->query($sql); } } } } // 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 3.1: SAVE GROUP CATEGORIES ORM::factory('simplegroups_incident_category')->where('incident_id', $incident->id)->delete_all(); // Delete Previous Entries if (isset($post->incident_group_category)) { foreach ($post->incident_group_category as $item) { $incident_group_category = ORM::factory('simplegroups_incident_category'); $incident_group_category->incident_id = $incident->id; $incident_group_category->simplegroups_category_id = $item; $incident_group_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)->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++; } // 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(); if ($is_new) { groups::forward_incident_to_own_instance($incident->id, $this->group->id); } // STEP 6a: SAVE LINK TO REPORTER MESSAGE // We're creating a report from a message with this option if (isset($message_id) && $message_id != "") { $savemessage = ORM::factory('message', $message_id); if ($savemessage->loaded == true) { $savemessage->incident_id = $incident->id; $savemessage->save(); } } // STEP 6b: SAVE LINK TO NEWS FEED // We're creating a report from a newsfeed with this option if (isset($feed_item_id) && $feed_item_id != "") { $savefeed = ORM::factory('feed_item', $feed_item_id); if ($savefeed->loaded == true) { $savefeed->incident_id = $incident->id; $savefeed->location_id = $location->id; $savefeed->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(); } } } // Action::report_edit - Edited a Report Event::run('ushahidi_action.report_edit', $incident); // SAVE AND CLOSE? if ($post->save == 1) { url::redirect('admin/simplegroups/reports/edit/' . $incident->id . '/saved'); } else { url::redirect('admin/simplegroups/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) { //make sure the group user is allowed to see this report $count = ORM::factory("simplegroups_groups_incident")->where(array("incident_id" => $id, "simplegroups_groups_id" => $this->group->id))->count_all(); if ($count == 0) { url::redirect(url::site() . 'admin/simplegroups/reports'); } // 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 Group Categories $incident_group_category = array(); $incident_group_categories = ORM::factory("simplegroups_category")->join("simplegroups_incident_category", "simplegroups_category.id", "simplegroups_incident_category.simplegroups_category_id")->where("simplegroups_incident_category.incident_id", $id)->find_all(); foreach ($incident_group_categories as $category) { $incident_group_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\tgeometry_comment, geometry_color, geometry_strokewidth \n\t\t\t\tFROM " . Kohana::config('database.default.table_prefix') . "geometry \n\t\t\t\tWHERE incident_id=" . $id; $query = $db->query($sql); foreach ($query as $item) { $form['geometry'][] = $item; } // 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_group_category' => $incident_group_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' => $this->_get_custom_form_fields($id, $incident->form_id, true), 'incident_active' => $incident->incident_active, 'incident_verified' => $incident->incident_verified, 'incident_status' => $incident->incident_status); // Merge To Form Array For Display $form = arr::overwrite($form, $incident_arr); } else { // Redirect url::redirect('admin/simplegroups/reports/'); } } else { //this is a new report with no id //check to see if we need to add some group categories that default on //first find out what's out there. //check and see if we need to tag this with a catgory //find all the categories for this group with tag by default turned on $categories = ORM::factory("simplegroups_category")->where("simplegroups_groups_id", $this->group->id)->where("selected_by_default", "1")->where("applies_to_report", "1")->find_all(); $default_categories = array(); foreach ($categories as $category) { $default_categories[$category->id] = $category->id; } if (isset($message) && $message->loaded) { //if a messge was used in the creation of this report we're gonna copy the appropriate categories over //figure out what categories this has $message_cats = ORM::factory("simplegroups_category")->join("simplegroups_message_category", "simplegroups_message_category.simplegroups_category_id", "simplegroups_category.id")->where('simplegroups_message_category.message_id', $message->id)->where("simplegroups_category.applies_to_report", "1")->find_all(); foreach ($message_cats as $message_cat) { $default_categories[$message_cat->id] = $message_cat->id; } } $form['incident_group_category'] = $default_categories; } } $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 $incident_date = date("c"); if (isset($incident)) { $incident_date = $incident->incident_date; } $previous = ORM::factory('incident')->join("simplegroups_groups_incident", "incident.id", "simplegroups_groups_incident.incident_id")->where('incident.incident_date < ', $incident_date)->where("simplegroups_groups_incident.simplegroups_groups_id", $this->group->id)->orderby('incident.incident_date', 'desc')->find(); $previous_url = $previous->loaded ? url::base() . 'admin/simplegroups/reports/edit/' . $previous->id : url::base() . 'admin/simplegroups/reports/'; $next = ORM::factory('incident')->join("simplegroups_groups_incident", "incident.id", "simplegroups_groups_incident.incident_id")->where("simplegroups_groups_incident.simplegroups_groups_id", $this->group->id)->where('incident.incident_date > ', $incident_date)->orderby('incident.incident_date', 'asc')->find(); $next_url = $next->loaded ? url::base() . 'admin/simplegroups/reports/edit/' . $next->id : url::base() . 'admin/simplegroups/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->treeview_enabled = TRUE; $this->template->editor_enabled = TRUE; $this->template->js = new View('reports_submit_edit_js'); $this->template->js->edit_mode = TRUE; $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']; } $this->template->js->incident_zoom = Kohana::config('settings.default_zoom'); $this->template->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(); }
/** * 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]'); $post->add_rules('person_first', 'required', 'length[3,100]'); $post->add_rules('person_last', 'required', 'length[3,100]'); $post->add_rules('person_email', 'required', 'email', 'length[3,100]'); 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(); } // 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++; } // 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->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; }
/** * Import Reports via XML * @param DOMNodeList Object $report * @return bool */ public function import_reports($reports) { /* Import individual reports */ foreach ($reports->getElementsByTagName('report') as $report) { $this->totalreports++; // Get Report id $report_id = $report->getAttribute('id'); // Check if this incident already exists in the db if (isset($report_id) and isset($this->incident_ids[$report_id])) { $this->notices[] = Kohana::lang('import.incident_exists') . $report_id; } else { /* Step 1: Location information */ $locations = $report->getElementsByTagName('location'); // If location information has been provided if ($locations->length > 0) { $report_location = $locations->item(0); // Location Name $location_name = xml::get_node_text($report_location, 'name'); // Longitude $longitude = xml::get_node_text($report_location, 'longitude'); // Latitude $latitude = xml::get_node_text($report_location, 'latitude'); if ($location_name) { // For geocoding purposes $location_geocoded = map::geocode($location_name); // Save the location $new_location = new Location_Model(); $new_location->location_name = $location_name ? $location_name : NULL; $new_location->location_date = $this->time; // If longitude/latitude values are present if ($latitude and $longitude) { $new_location->latitude = $latitude ? $latitude : 0; $new_location->longitude = $longitude ? $longitude : 0; } else { // Get geocoded lat/lon values $new_location->latitude = $location_geocoded ? $location_geocoded['latitude'] : $latitude; $new_location->longitude = $location_geocoded ? $location_geocoded['longitude'] : $longitude; } $new_location->country_id = $location_geocoded ? $location_geocoded['country_id'] : 0; $new_location->save(); // Add this location to array of imported locations $this->locations_added[] = $new_location->id; } } /* Step 2: Save Report */ // Report Title $report_title = xml::get_node_text($report, 'title'); // Report Date $report_date = xml::get_node_text($report, 'date'); // Missing report title or report date? if (!$report_title or !$report_date) { $this->errors[] = Kohana::lang('import.xml.incident_title_date') . $this->totalreports; } // If report date is not in the required format if (!strtotime($report_date)) { $this->errors[] = Kohana::lang('import.incident_date') . $this->totalreports . ': ' . html::escape($report_date); } else { // Approval status? $approved = $report->getAttribute('approved'); $report_approved = (isset($approved) and in_array($approved, $this->allowable)) ? $approved : 0; // Verified Status? $verified = $report->getAttribute('verified'); $report_verified = (isset($verified) and in_array($verified, $this->allowable)) ? $verified : 0; // Report mode? $allowed_modes = array(1, 2, 3, 4); $mode = $report->getAttribute('mode'); $report_mode = (isset($mode) and in_array($mode, $allowed_modes)) ? $mode : 1; // Report Form $report_form = xml::get_node_text($report, 'form_name', FALSE); if ($report_form) { if (!isset($this->existing_forms[utf8::strtoupper($report_form)])) { $this->notices[] = Kohana::lang('import.xml.no_form_exists') . $this->totalreports . ': "' . $report_form . '"'; } $form_id = isset($this->existing_forms[utf8::strtoupper($report_form)]) ? $this->existing_forms[utf8::strtoupper($report_form)] : 1; } // Report Date added $dateadd = xml::get_node_text($report, 'dateadd'); // Report Description $report_description = xml::get_node_text($report, 'description'); $new_report = new Incident_Model(); $new_report->location_id = isset($new_location) ? $new_location->id : 0; $new_report->user_id = 0; $new_report->incident_title = $report_title; $new_report->incident_description = $report_description ? $report_description : ''; $new_report->incident_date = date("Y-m-d H:i:s", strtotime($report_date)); $new_report->incident_dateadd = ($dateadd and strtotime($dateadd)) ? $dateadd : $this->time; $new_report->incident_active = $report_approved; $new_report->incident_verified = $report_verified; $new_report->incident_mode = $report_mode; $new_report->form_id = isset($form_id) ? $form_id : 1; $new_report->save(); // Increment imported rows counter $this->importedreports++; // Add this report to array of reports added during import $this->incidents_added[] = $new_report->id; /* Step 3: Save Report Categories*/ // Report Categories exist? $reportcategories = $report->getElementsByTagName('report_categories'); if ($reportcategories->length > 0) { $report_categories = $reportcategories->item(0); foreach ($report_categories->getElementsByTagName('category') as $r_category) { $category = trim($r_category->nodeValue); $report_category = (isset($category) and $category != '') ? $category : ''; if ($report_category != '' and isset($this->existing_categories[utf8::strtoupper($report_category)])) { // Save the incident category $new_incident_category = new Incident_Category_Model(); $new_incident_category->incident_id = $new_report->id; $new_incident_category->category_id = $this->existing_categories[utf8::strtoupper($report_category)]; $new_incident_category->save(); // Add this to array of incident categories added $this->incident_categories_added[] = $new_incident_category->id; } if ($report_category != '' and !isset($this->existing_categories[utf8::strtoupper($report_category)])) { $this->notices[] = Kohana::lang('import.xml.no_category_exists') . $this->totalreports . ': "' . $report_category . '"'; } } } /* Step 4: Save Custom form field responses for this report */ // Report Custom Fields $this_form = $new_report->form_id; $reportfields = $report->getElementsByTagName('custom_fields'); if ($reportfields->length > 0) { $report_fields = $reportfields->item(0); $custom_fields = $report_fields->getElementsByTagName('field'); if ($custom_fields->length > 0) { foreach ($custom_fields as $field) { // Field Name $field_name = $field->hasAttribute('name') ? xml::get_node_text($field, 'name', FALSE) : FALSE; if ($field_name) { // If this field exists in the form listed for this report if (isset($this->existing_fields[utf8::strtoupper($field_name)][$this_form])) { // Get field type and default values $match_field_id = $this->existing_fields[utf8::strtoupper($field_name)][$this_form]; // Grab form field object $match_fields = ORM::Factory('form_field', $match_field_id); $match_field_type = $match_fields->field_type; $match_field_defaults = $match_fields->field_default; // Grab form responses $field_response = trim($field->nodeValue); if ($field_response != '') { // Initialize form response model $new_form_response = new Form_Response_Model(); $new_form_response->incident_id = $new_report->id; $new_form_response->form_field_id = $match_field_id; // For radio buttons, checkbox fields and drop downs, make sure form responses are // within bounds of allowable options for that field // Split field defaults into individual values $field_defaults = explode(',', $match_field_defaults); /* Radio buttons and Drop down fields which take single responses */ if ($match_field_type == 5 or $match_field_type == 7) { foreach ($field_defaults as $match_field_default) { // Carry out a case insensitive string comparison $new_form_response->form_response = strcasecmp($match_field_default, $field_response) == 0 ? $match_field_default : NULL; } } // Checkboxes which if ($match_field_type == 6) { // Split user responses into individual value $responses = explode(',', $field_response); $values = array(); foreach ($match_field_defaults as $match_field_default) { foreach ($responses as $response) { $values[] = strcasecmp($match_field_default, $response) == 0 ? $match_field_default : NULL; } } // Concatenate checkbox values into a string, separated by a comma $new_form_response->form_response = implode(",", $values); } else { $new_form_response->form_response = $field_response; } // Only save if form response is not empty if ($new_form_response->form_response != NULL) { $new_form_response->save(); } // Add this to array of form responses added $this->incident_responses_added[] = $new_form_response->id; } } else { $this->notices[] = Kohana::lang('import.xml.form_field_no_match') . $this->totalreports . ': "' . $field_name . '" on form "' . $new_report->form->form_title . '"'; } } } } } /* Step 5: Save incident persons for this report */ // Report Personal Information $personal_info = $report->getElementsByTagName('personal_info'); // If personal info exists if ($personal_info->length > 0) { $report_info = $personal_info->item(0); // First Name $firstname = xml::get_node_text($report_info, 'first_name'); // Last Name $lastname = xml::get_node_text($report_info, 'last_name'); // Email $r_email = xml::get_node_text($report_info, 'email'); $email = ($r_email and valid::email($r_email)) ? $r_email : NULL; $new_incident_person = new Incident_Person_Model(); $new_incident_person->incident_id = $new_report->id; $new_incident_person->person_date = $new_report->incident_dateadd; // Make sure that at least one of the personal info field entries is provided if ($firstname or $lastname or $email != NULL) { $new_incident_person->person_first = $firstname ? $firstname : NULL; $new_incident_person->person_last = $lastname ? $firstname : NULL; $new_incident_person->person_email = $email; $new_incident_person->save(); // Add this to array of incident persons added during import $this->incident_persons_added[] = $new_incident_person->id; } } /* Step 6: Save media links for this report */ // Report Media $media = $report->getElementsByTagName('media'); if ($media->length > 0) { $media = $media->item(0); foreach ($media->getElementsByTagName('item') as $media_element) { $media_link = trim($media_element->nodeValue); $media_date = $media_element->getAttribute('date'); if (!empty($media_link)) { $media_item = new Media_Model(); $media_item->location_id = isset($new_location) ? $new_location->id : 0; $media_item->incident_id = $new_report->id; $media_item->media_type = $media_element->getAttribute('type'); $media_item->media_link = $media_link; $media_item->media_date = !empty($media_date) ? $media_date : $new_report->incident_date; $media_item->save(); } } } } } } // end individual report import // If we have errors, return FALSE, else TRUE return count($this->errors) === 0; }
/** * Submits a new report. */ public function submit($id = false, $saved = false) { // First, are we allowed to submit new reports? if (!Kohana::config('settings.allow_reports')) { url::redirect(url::site() . 'main'); } $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(), '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" and $_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 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_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 = date("Y-m-d H:i:s", strtotime($incident_date . " " . $incident_time)); $incident->incident_dateadd = date("Y-m-d H:i:s", time()); //fetch categories for title $db = new Database(); $i = 0; foreach ($post->incident_category as $item) { $query = 'SELECT category_title FROM ' . $this->table_prefix . 'category WHERE id=' . $item . ' OR parent_id=' . $item . ';'; $query = $db->query($query); if ($i > 0) { $incident->incident_title = $incident->incident_title . ", "; } $titles = array(); foreach ($query as $items) { $titles[] = $items->category_title; } $incident->incident_title = $incident->incident_title . $titles[0]; $i++; } // Auto approve reports using workaround discussed at.. . // http://forums.ushahidi.com/forums/topic/automatic-report-approval/ // note this should be possible using an 'Action' but doesn't work // with the current version (as of 20120902) for details, see.. . // https://github.com/ushahidi/Ushahidi_Web/issues/817#issuecomment-8209766 $incident->incident_active = 1; $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++; } // 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(); // Action::report_add - Added a New Report Event::run('ushahidi_action.report_add', $incident); // The $_POST['date'] is a value posted by form in dd/mm/yyyy format $incident_date2 = explode("/", $post->incident_date); $incident_date2 = $incident_date2[1] . "-" . $incident_date2[0] . "-" . $incident_date2[2]; //Send e-mail notification to the moderator //Hardcoded mail-adress here. This is simple addition, no gui for it. $to = '*****@*****.**'; $subject = 'New report: ' . $incident->incident_title; $message = 'The following report was submitted and requires moderation:' . "\r\n" . 'Title: ' . $incident->incident_title . "\r\n" . 'Description: ' . $post->incident_description . "\r\n" . 'Date: ' . $incident_date2 . "\r\n"; $headers = 'From: boskoi.mail@gmail.com' . "\r\n" . 'Reply-To: boskoi.mail@gmail.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); 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->themes->map_enabled = TRUE; $this->themes->datepicker_enabled = TRUE; $this->themes->treeview_enabled = TRUE; $this->themes->js = new View('reports_submit_js'); $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']; } // Rebuild Header Block $this->template->header->header_block = $this->themes->header_block(); }
/** * Function to import a report form a row in the CSV file * @param array $row * @return bool */ function import_report($row) { // If the date is not in proper date format if (!strtotime($row['INCIDENT DATE'])) { $this->errors[] = Kohana::lang('import.incident_date') . ($this->rownumber + 1) . ': ' . $row['INCIDENT DATE']; } // If a value of Yes or No is NOT set for approval status for the imported row if (isset($row["APPROVED"]) and !in_array(utf8::strtoupper($row["APPROVED"]), array('NO', 'YES'))) { $this->errors[] = Kohana::lang('import.csv.approved') . ($this->rownumber + 1); } // If a value of Yes or No is NOT set for verified status for the imported row if (isset($row["VERIFIED"]) and !in_array(utf8::strtoupper($row["VERIFIED"]), array('NO', 'YES'))) { $this->errors[] = Kohana::lang('import.csv.verified') . ($this->rownumber + 1); } if (count($this->errors)) { return false; } // STEP 1: SAVE LOCATION if (isset($row['LOCATION'])) { $location = new Location_Model(); $location->location_name = isset($row['LOCATION']) ? $row['LOCATION'] : ''; // For Geocoding purposes $location_geocoded = map::geocode($location->location_name); // If we have LATITUDE and LONGITUDE use those if (isset($row['LATITUDE']) and isset($row['LONGITUDE'])) { $location->latitude = isset($row['LATITUDE']) ? $row['LATITUDE'] : 0; $location->longitude = isset($row['LONGITUDE']) ? $row['LONGITUDE'] : 0; } else { $location->latitude = $location_geocoded ? $location_geocoded['latitude'] : 0; $location->longitude = $location_geocoded ? $location_geocoded['longitude'] : 0; } $location->country_id = $location_geocoded ? $location_geocoded['country_id'] : 0; $location->location_date = $this->time; $location->save(); $this->locations_added[] = $location->id; } // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); $incident->location_id = isset($row['LOCATION']) ? $location->id : 0; $incident->user_id = 0; $incident->form_id = (isset($row['FORM #']) and Form_Model::is_valid_form($row['FORM #'])) ? $row['FORM #'] : 1; $incident->incident_title = $row['INCIDENT TITLE']; $incident->incident_description = isset($row['DESCRIPTION']) ? $row['DESCRIPTION'] : ''; $incident->incident_date = date("Y-m-d H:i:s", strtotime($row['INCIDENT DATE'])); $incident->incident_dateadd = $this->time; $incident->incident_active = (isset($row['APPROVED']) and utf8::strtoupper($row['APPROVED']) == 'YES') ? 1 : 0; $incident->incident_verified = (isset($row['VERIFIED']) and utf8::strtoupper($row['VERIFIED']) == 'YES') ? 1 : 0; $incident->save(); $this->incidents_added[] = $incident->id; // STEP 3: Save Personal Information if (isset($row['FIRST NAME']) or isset($row['LAST NAME']) or isset($row['EMAIL'])) { $person = new Incident_Person_Model(); $person->incident_id = $incident->id; $person->person_first = isset($row['FIRST NAME']) ? $row['FIRST NAME'] : ''; $person->person_last = isset($row['LAST NAME']) ? $row['LAST NAME'] : ''; $person->person_email = (isset($row['EMAIL']) and valid::email($row['EMAIL'])) ? $row['EMAIL'] : ''; $person->person_date = date("Y-m-d H:i:s", time()); // Make sure that you're not importing an empty record i.e at least one field has been recorded // If all fields are empty i.e you have an empty record, don't save if (!empty($person->person_first) or !empty($person->person_last) or !empty($person->person_email)) { $person->save(); // Add to array of incident persons added $this->incident_persons_added[] = $person->id; } } // STEP 4: SAVE CATEGORIES // If CATEGORY column exists if (isset($row['CATEGORY'])) { $categorynames = explode(',', trim($row['CATEGORY'])); // Trim whitespace from array values $categorynames = array_map('trim', $categorynames); // Get rid of duplicate category entries in a row $categories = array_unique(array_map('strtolower', $categorynames)); // Add categories to incident foreach ($categories as $categoryname) { // Convert the first string character of the category name to Uppercase $categoryname = utf8::ucfirst($categoryname); // For purposes of adding an entry into the incident_category table $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; // If category name exists, add entry in incident_category table if ($categoryname != '') { // Check if the category exists (made sure to convert to uppercase for comparison) if (!isset($this->existing_categories[utf8::strtoupper($categoryname)])) { $this->notices[] = Kohana::lang('import.new_category') . $categoryname; $category = new Category_Model(); $category->category_title = $categoryname; // We'll just use black for now. Maybe something random? $category->category_color = '000000'; // because all current categories are of type '5' $category->category_visible = 1; $category->category_description = $categoryname; $category->category_position = count($this->existing_categories); $category->save(); $this->categories_added[] = $category->id; // Now category_id is known: This time, and for the rest of the import. $this->existing_categories[utf8::strtoupper($categoryname)] = $category->id; } $incident_category->category_id = $this->existing_categories[utf8::strtoupper($categoryname)]; $incident_category->save(); $this->incident_categories_added[] = $incident_category->id; } } } // STEP 5: Save Custom form fields responses // Check for form_id $form_id = (isset($row['FORM #']) and Form_Model::is_valid_form($row['FORM #'])) ? $row['FORM #'] : 1; // Get custom form fields for this particular form $custom_titles = customforms::get_custom_form_fields('', $form_id, false); // Do custom form fields exist on this deployment? if (!empty($custom_titles)) { foreach ($custom_titles as $field_name) { // Check if the column exists in the CSV $rowname = utf8::strtoupper($field_name['field_name']); if (isset($row[$rowname . '-' . $form_id])) { $response = $row[$rowname . '-' . $form_id]; // Grab field_id and field_type $field_id = $field_name['field_id']; $field_type = $field_name['field_type']; // Initialize form response model $form_response = new Form_Response_Model(); $form_response->incident_id = $incident->id; $form_response->form_field_id = $field_id; // If form response exists if ($response != '') { /* Handling case sensitivity issues with custom form field upload */ // Check if the field is a radio button, checkbox OR dropdown field if ($field_type == '5' or $field_type == '6' or $field_type == '7') { // Get field option values $field_values = $field_name['field_default']; // Split field options into individual values $options = explode(",", $field_values); // Since radio button and dropdown fields take single responses if ($field_type == '5' or $field_type == '7') { foreach ($options as $option) { // Carry out a case insensitive comparison between individual field options and csv response // If there's a match, store field option value from the db if (strcasecmp($option, $response) == 0) { $form_response->form_response = $option; } } } // For checkboxes, which accomodate multiple responses if ($field_type == '6') { // Split user responses into single values $csvresponses = explode(",", $response); $values = array(); foreach ($options as $option) { foreach ($csvresponses as $csvresponse) { // Carry out a case insensitive comparison between individual field options and csv response // If there's a match if (strcasecmp($option, $csvresponse) == 0) { // Store field option value from the db $values[] = $option; } } } // Concatenate checkbox values into a string, separated by a comma $form_response->form_response = implode(",", $values); } } else { $form_response->form_response = $response; } // If form_response is provided based on conditions set above, Save the form response if ($form_response->form_response != '') { $form_response->save(); // Add to array of field responses added $this->incident_responses_added[] = $form_response->id; } } } } } return true; }