/** * Tests the report submit action * @test */ public function testReportSubmit() { // Test if validation succeeds $this->assertEquals(TRUE, reports::validate($this->post), 'Report validation failed'); // Location model $location = new Location_Model(); // STEP 1: Save the location reports::save_location($this->post, $location); // Test the save $this->assertEquals(TRUE, intval($location->id) > 0, 'The location was not saved'); // Incident model object $incident = new Incident_Model(); // STEP 2: Save the incident reports::save_report($this->post, $incident, $location->id); $this->assertEquals($location->id, $incident->location_id, 'Incident not associated with location'); // Test if the incident has been saved $this->assertEquals(TRUE, intval($incident->id) > 0); // STEP 3: Save the category reports::save_category($this->post, $incident); // Test if the category has been saved $category_count = ORM::factory('incident_category')->where('incident_id', $incident->id)->find_all()->count(); $this->assertEquals(TRUE, $category_count > 0, 'No entries in incident_categorgy for incident'); // Save personal information reports::save_personal_info($this->post, $incident); // Test $personal_info = ORM::factory('incident_person')->where('incident_id', $incident->id)->find_all()->count(); $this->assertEquals(TRUE, $personal_info > 0, 'No entries in incident_person for incident'); // @todo Test for saving of incident media // Cleanup ORM::factory('incident_category')->where('incident_id', $incident->id)->delete_all(); ORM::factory('incident_person')->where('incident_id', $incident->id)->delete_all(); $incident->delete(); $location->delete(); }
/** * Submits a new report. */ public function submit($id = FALSE, $saved = FALSE) { $db = new Database(); // 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' => '', 'geometry' => array(), '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; $form_saved = $saved == 'saved'; // 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'] = customforms::get_custom_form_fields($id, '', true); //GET custom forms $forms = array(); foreach (customforms::get_custom_forms() 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 = array_merge($_POST, $_FILES); // Test to see if things passed the rule checks if (reports::validate($post)) { // STEP 1: SAVE LOCATION $location = new Location_Model(); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); reports::save_report($post, $incident, $location->id); // STEP 2b: SAVE INCIDENT GEOMETRIES reports::save_report_geometry($post, $incident); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE CUSTOM FORM FIELDS reports::save_custom_fields($post, $incident); // STEP 6: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // Action::report_add/report_submit - Added a New Report //++ Do we need two events for this? Or will one suffice? Event::run('ushahidi_action.report_add', $incident); Event::run('ushahidi_action.report_submit', $post); 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; $categories = $this->get_categories($form['incident_category']); $this->template->content->categories = $categories; // Pass timezone $this->template->content->site_timezone = Kohana::config('settings.site_timezone'); // Pass the submit report message $this->template->content->site_submit_report_message = Kohana::config('settings.site_submit_report_message'); // Retrieve Custom Form Fields Structure $this->template->content->custom_forms = new View('reports_submit_custom_forms'); $disp_custom_fields = customforms::get_custom_form_fields($id, $form['form_id'], FALSE); $this->template->content->disp_custom_fields = $disp_custom_fields; $this->template->content->stroke_width_array = $this->_stroke_width_array(); $this->template->content->custom_forms->disp_custom_fields = $disp_custom_fields; $this->template->content->custom_forms->form = $form; // Javascript Header $this->themes->map_enabled = TRUE; $this->themes->datepicker_enabled = TRUE; $this->themes->treeview_enabled = TRUE; $this->themes->colorpicker_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']; } $this->themes->js->geometries = $form['geometry']; // Rebuild Header Block $this->template->header->header_block = $this->themes->header_block(); }
/** * Edit a report * @param bool|int $id The id no. of the report * @param bool|string $saved */ public function edit($id = FALSE, $saved = FALSE) { $db = new Database(); $this->template->content = new View('members/reports_edit'); $this->template->content->title = Kohana::lang('ui_admin.create_report'); // Setup and initialize form field names $form = array('location_id' => '', 'form_id' => '1', 'locale' => '', 'incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'geometry' => array(), 'location_name' => '', 'country_id' => '', 'country_name' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'custom_field' => array(), 'incident_zoom' => '', 'incident_source' => '', 'incident_information' => ''); // Copy the form as errors, so the errors will be stored with keys // corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = $saved == 'saved'; // Initialize Default Values $form['locale'] = Kohana::config('locale.language'); $form['latitude'] = Kohana::config('settings.default_lat'); $form['longitude'] = Kohana::config('settings.default_lon'); $form['country_id'] = Kohana::config('settings.default_country'); $form['incident_date'] = date("m/d/Y", time()); $form['incident_hour'] = date('h'); $form['incident_minute'] = date('i'); $form['incident_ampm'] = date('a'); // Initialize custom field array $form_id = $form['form_id']; $form['custom_field'] = customforms::get_custom_form_fields($id, $form_id, TRUE); // Locale (Language) Array $this->template->content->locale_array = Kohana::config('locale.all_languages'); // Time formatting $this->template->content->hour_array = $this->_hour_array(); $this->template->content->minute_array = $this->_minute_array(); $this->template->content->ampm_array = $this->_ampm_array(); $this->template->content->stroke_width_array = $this->_stroke_width_array(); // Get Countries $countries = array(); foreach (ORM::factory('country')->orderby('country')->find_all() as $country) { // Create a list of all categories $this_country = $country->country; if (strlen($this_country) > 35) { $this_country = substr($this_country, 0, 35) . "..."; } $countries[$country->id] = $this_country; } $this->template->content->countries = $countries; // Initialize Default Value for Hidden Field Country Name, just incase Reverse Geo coding yields no result $form['country_name'] = $countries[$form['country_id']]; //GET custom forms $forms = array(); foreach (ORM::factory('form')->where('form_active', 1)->find_all() as $custom_forms) { $forms[$custom_forms->id] = $custom_forms->form_title; } $this->template->content->forms = $forms; // Retrieve thumbnail photos (if edit); //XXX: fix _get_thumbnails $this->template->content->incident = $this->_get_thumbnails($id); // Check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things $post = array_merge($_POST, $_FILES); if (reports::validate($post)) { // STEP 1: SAVE LOCATION $location = new Location_Model(); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident = new Incident_Model($id); reports::save_report($post, $incident, $location->id); // STEP 2b: SAVE INCIDENT GEOMETRIES reports::save_report_geometry($post, $incident); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE CUSTOM FORM FIELDS reports::save_custom_fields($post, $incident); // STEP 6: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // Action::report_add / report_submit_members - Added a New Report Event::run('ushahidi_action.report_submit_members', $post); Event::run('ushahidi_action.report_edit', $incident); // SAVE AND CLOSE? if ($post->save == 1) { // Save but don't close url::redirect('members/reports/edit/' . $incident->id . '/saved'); } else { // Save and close url::redirect('members/reports/'); } } else { // Repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // Populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('report')); $form_error = TRUE; } } else { if ($id) { // Retrieve Current Incident $incident = ORM::factory('incident')->where('user_id', $this->user->id)->find($id); if ($incident->loaded == true) { // Retrieve Categories $incident_category = array(); foreach ($incident->incident_category as $category) { $incident_category[] = $category->category_id; } // Retrieve Media $incident_news = array(); $incident_video = array(); $incident_photo = array(); foreach ($incident->media as $media) { if ($media->media_type == 4) { $incident_news[] = $media->media_link; } elseif ($media->media_type == 2) { $incident_video[] = $media->media_link; } elseif ($media->media_type == 1) { $incident_photo[] = $media->media_link; } } // Get Geometries via SQL query as ORM can't handle Spatial Data $sql = "SELECT AsText(geometry) as geometry, geometry_label, \n\t\t\t\t\t\tgeometry_comment, geometry_color, geometry_strokewidth \n\t\t\t\t\t\tFROM " . Kohana::config('database.default.table_prefix') . "geometry \n\t\t\t\t\t\tWHERE incident_id = ?"; $query = $db->query($sql, $id); foreach ($query as $item) { $geometry = array("geometry" => $item->geometry, "label" => $item->geometry_label, "comment" => $item->geometry_comment, "color" => $item->geometry_color, "strokewidth" => $item->geometry_strokewidth); $form['geometry'][] = json_encode($geometry); } // Combine Everything $incident_arr = array('location_id' => $incident->location->id, 'form_id' => $incident->form_id, 'locale' => $incident->locale, 'incident_title' => $incident->incident_title, 'incident_description' => $incident->incident_description, 'incident_date' => date('m/d/Y', strtotime($incident->incident_date)), 'incident_hour' => date('h', strtotime($incident->incident_date)), 'incident_minute' => date('i', strtotime($incident->incident_date)), 'incident_ampm' => date('a', strtotime($incident->incident_date)), 'latitude' => $incident->location->latitude, 'longitude' => $incident->location->longitude, 'location_name' => $incident->location->location_name, 'country_id' => $incident->location->country_id, 'incident_category' => $incident_category, 'incident_news' => $incident_news, 'incident_video' => $incident_video, 'incident_photo' => $incident_photo, 'person_first' => $incident->incident_person->person_first, 'person_last' => $incident->incident_person->person_last, 'person_email' => $incident->incident_person->person_email, 'incident_source' => '', 'incident_information' => '', 'custom_field' => customforms::get_custom_form_fields($id, $incident->form_id, TRUE), 'incident_zoom' => $incident->incident_zoom); // Merge To Form Array For Display $form = arr::overwrite($form, $incident_arr); } else { // Redirect url::redirect('members/reports/'); } } } $this->template->content->id = $id; $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; // Retrieve Custom Form Fields Structure $this->template->content->custom_forms = new View('reports/submit_custom_forms'); $disp_custom_fields = customforms::get_custom_form_fields($id, $form['form_id'], FALSE, "view"); $custom_field_mismatch = customforms::get_edit_mismatch($form['form_id']); // Quick hack to make sure view-only fields have data set foreach ($custom_field_mismatch as $id => $field) { $form['custom_field'][$id] = $disp_custom_fields[$id]['field_response']; } $this->template->content->custom_forms->disp_custom_fields = $disp_custom_fields; $this->template->content->custom_forms->custom_field_mismatch = $custom_field_mismatch; $this->template->content->custom_forms->form = $form; // Retrieve Previous & Next Records $previous = ORM::factory('incident')->where('id < ', $id)->orderby('id', 'desc')->find(); $previous_url = $previous->loaded ? url::site('members/reports/edit/' . $previous->id) : url::site() . 'members/reports/'; $next = ORM::factory('incident')->where('id > ', $id)->orderby('id', 'desc')->find(); $next_url = $next->loaded ? url::site('members/reports/edit/' . $next->id) : url::site('members/reports/'); $this->template->content->previous_url = $previous_url; $this->template->content->next_url = $next_url; // Javascript Header $this->themes->map_enabled = TRUE; $this->themes->colorpicker_enabled = TRUE; $this->themes->treeview_enabled = TRUE; $this->themes->json2_enabled = TRUE; $this->themes->js = new View('reports/submit_edit_js'); $this->themes->js->edit_mode = FALSE; $this->themes->js->default_map = Kohana::config('settings.default_map'); $this->themes->js->default_zoom = Kohana::config('settings.default_zoom'); if (!$form['latitude'] or !$form['longitude']) { $this->themes->js->latitude = Kohana::config('settings.default_lat'); $this->themes->js->longitude = Kohana::config('settings.default_lon'); } else { $this->themes->js->latitude = $form['latitude']; $this->themes->js->longitude = $form['longitude']; } $this->themes->js->incident_zoom = $form['incident_zoom']; $this->themes->js->geometries = $form['geometry']; // Inline Javascript $this->template->content->date_picker_js = $this->_date_picker_js(); $this->template->content->color_picker_js = $this->_color_picker_js(); // Pack Javascript $myPacker = new javascriptpacker($this->themes->js, 'Normal', FALSE, FALSE); $this->themes->js = $myPacker->pack(); }
/** * Edit a report * @param bool|int $id The id no. of the report * @param bool|string $saved */ public function edit($id = FALSE, $saved = FALSE) { $db = new Database(); // If user doesn't have access, redirect to dashboard if (!$this->auth->has_permission("reports_edit")) { url::redirect('admin/dashboard'); } $this->template->content = new View('admin/reports/edit'); $this->template->content->title = Kohana::lang('ui_admin.create_report'); // Setup and initialize form field names $form = array('location_id' => '', 'form_id' => '', 'locale' => '', 'incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'geometry' => array(), 'location_name' => '', 'country_id' => '', 'country_name' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'custom_field' => array(), 'incident_active' => '', 'incident_verified' => '', 'incident_zoom' => ''); // Copy the form as errors, so the errors will be stored with keys // corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = $saved == 'saved'; // Initialize Default Values $form['locale'] = Kohana::config('locale.language'); //$form['latitude'] = Kohana::config('settings.default_lat'); //$form['longitude'] = Kohana::config('settings.default_lon'); $form['incident_date'] = date("m/d/Y", time()); $form['incident_hour'] = date('h'); $form['incident_minute'] = date('i'); $form['incident_ampm'] = date('a'); $form['country_id'] = Kohana::config('settings.default_country'); // get the form ID if relevant, kind of a hack // to just hit the database like this for one // tiny bit of info then throw away the DB model object, // but seems to be what everyone else does, so // why should I care. Just know that when your Ush system crashes // because you have 1000 concurrent users you'll need to do this // correctly. Etherton. $form['form_id'] = 1; $form_id = $form['form_id']; if ($id and Incident_Model::is_valid_incident($id, FALSE)) { $form_id = ORM::factory('incident', $id)->form_id; } // Initialize custom field array $form['custom_field'] = customforms::get_custom_form_fields($id, $form_id, TRUE); // Locale (Language) Array $this->template->content->locale_array = Kohana::config('locale.all_languages'); // Create Categories $this->template->content->new_categories_form = $this->_new_categories_form_arr(); // Time formatting $this->template->content->hour_array = $this->_hour_array(); $this->template->content->minute_array = $this->_minute_array(); $this->template->content->ampm_array = $this->_ampm_array(); $this->template->content->stroke_width_array = $this->_stroke_width_array(); // Get Countries $countries = array(); foreach (ORM::factory('country')->orderby('country')->find_all() as $country) { // Create a list of all countries $this_country = $country->country; if (strlen($this_country) > 35) { $this_country = substr($this_country, 0, 35) . "..."; } $countries[$country->id] = $this_country; } // Initialize Default Value for Hidden Field Country Name, // just incase Reverse Geo coding yields no result $form['country_name'] = $countries[$form['country_id']]; $this->template->content->countries = $countries; // GET custom forms $forms = array(); foreach (customforms::get_custom_forms(FALSE) as $custom_forms) { $forms[$custom_forms->id] = $custom_forms->form_title; } $this->template->content->forms = $forms; // Get the incident media $incident_media = Incident_Model::is_valid_incident($id, FALSE) ? ORM::factory('incident', $id)->media : FALSE; $this->template->content->incident_media = $incident_media; // Are we creating this report from SMS/Email/Twitter? // If so retrieve message if (isset($_GET['mid']) and intval($_GET['mid']) > 0) { $message_id = intval($_GET['mid']); $service_id = ""; $message = ORM::factory('message', $message_id); if ($message->loaded and $message->message_type == 1) { $service_id = $message->reporter->service_id; // Has a report already been created for this Message? if ($message->incident_id != 0) { // Redirect to report url::redirect('admin/reports/edit/' . $message->incident_id); } $this->template->content->show_messages = TRUE; $incident_description = $message->message; if (!empty($message->message_detail)) { $form['incident_title'] = $message->message; $incident_description = $message->message_detail; } $form['incident_description'] = $incident_description; $form['incident_date'] = date('m/d/Y', strtotime($message->message_date)); $form['incident_hour'] = date('h', strtotime($message->message_date)); $form['incident_minute'] = date('i', strtotime($message->message_date)); $form['incident_ampm'] = date('a', strtotime($message->message_date)); $form['person_first'] = $message->reporter->reporter_first; $form['person_last'] = $message->reporter->reporter_last; // Does the message itself have a location? if ($message->latitude != NULL and $message->longitude != NULL) { $form['latitude'] = $message->latitude; $form['longitude'] = $message->longitude; } elseif ($message->reporter->location->loaded) { $form['location_id'] = $message->reporter->location->id; $form['latitude'] = $message->reporter->location->latitude; $form['longitude'] = $message->reporter->location->longitude; $form['location_name'] = $message->reporter->location->location_name; } // Events to manipulate an already known location Event::run('ushahidi_action.location_from', $message_from = $message->message_from); // Filter location name Event::run('ushahidi_filter.location_name', $form['location_name']); // Filter //location find Event::run('ushahidi_filter.location_find', $form['location_find']); // Retrieve Last 5 Messages From this account $this->template->content->all_messages = ORM::factory('message')->where('reporter_id', $message->reporter_id)->orderby('message_date', 'desc')->limit(5)->find_all(); } else { $message_id = ""; $this->template->content->show_messages = FALSE; } } else { $this->template->content->show_messages = FALSE; } // Are we creating this report from a Newsfeed? if (isset($_GET['fid']) and intval($_GET['fid']) > 0) { $feed_item_id = intval($_GET['fid']); $feed_item = ORM::factory('feed_item', $feed_item_id); if ($feed_item->loaded) { // Has a report already been created for this Feed item? if ($feed_item->incident_id != 0) { // Redirect to report url::redirect('admin/reports/edit/' . $feed_item->incident_id); } $form['incident_title'] = $feed_item->item_title; $form['incident_description'] = $feed_item->item_description; $form['incident_date'] = date('m/d/Y', strtotime($feed_item->item_date)); $form['incident_hour'] = date('h', strtotime($feed_item->item_date)); $form['incident_minute'] = date('i', strtotime($feed_item->item_date)); $form['incident_ampm'] = date('a', strtotime($feed_item->item_date)); // News Link $form['incident_news'][0] = $feed_item->item_link; // Does this newsfeed have a geolocation? if ($feed_item->location_id) { $form['location_id'] = $feed_item->location_id; $form['latitude'] = $feed_item->location->latitude; $form['longitude'] = $feed_item->location->longitude; $form['location_name'] = $feed_item->location->location_name; } // HT: new code $feed_item_categories = ORM::factory('feed_item_category')->where('feed_item_id', $feed_item->id)->select_list('id', 'category_id'); if ($feed_item_categories) { foreach ($feed_item_categories as $feed_item_category) { $form['incident_category'][] = $feed_item_category; } } // HT: end of new code } else { $feed_item_id = ""; } } // Check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite // $_POST fields with our own things $post = array_merge($_POST, $_FILES); // Check if the service id exists if (isset($service_id) and intval($service_id) > 0) { $post = array_merge($post, array('service_id' => $service_id)); } // Check if the incident id is valid an add it to the post data if (Incident_Model::is_valid_incident($id, FALSE)) { $post = array_merge($post, array('incident_id' => $id)); } /** * NOTES - E.Kala July 27, 2011 * * Previously, the $post parameter for this event was a Validation * object. Now it's an array (i.e. the raw data without any validation rules applied to them). * As such, all plugins making use of this event shall have to be updated */ // Action::report_submit_admin - Report Posted Event::run('ushahidi_action.report_submit_admin', $post); // Validate if (reports::validate($post)) { // Yes! everything is valid $location_id = $post->location_id; // STEP 1: SAVE LOCATION $location = new Location_Model($location_id); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident = new Incident_Model($id); reports::save_report($post, $incident, $location->id); // STEP 2b: Record Approval/Verification Action reports::verify_approve($incident); // STEP 2c: SAVE INCIDENT GEOMETRIES reports::save_report_geometry($post, $incident); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // STEP 6a: SAVE LINK TO REPORTER MESSAGE // We're creating a report from a message with this option if (isset($message_id) and intval($message_id) > 0) { $savemessage = ORM::factory('message', $message_id); if ($savemessage->loaded) { $savemessage->incident_id = $incident->id; $savemessage->save(); // Does Message Have Attachments? // Add Attachments $attachments = ORM::factory("media")->where("message_id", $savemessage->id)->find_all(); foreach ($attachments as $attachment) { $attachment->incident_id = $incident->id; $attachment->save(); } } } // STEP 6b: SAVE LINK TO NEWS FEED // We're creating a report from a newsfeed with this option if (isset($feed_item_id) and intval($feed_item_id) > 0) { $savefeed = ORM::factory('feed_item', $feed_item_id); if ($savefeed->loaded) { $savefeed->incident_id = $incident->id; $savefeed->location_id = $location->id; $savefeed->save(); } } // STEP 7: SAVE CUSTOM FORM FIELDS reports::save_custom_fields($post, $incident); // Action::report_edit - Edited a Report Event::run('ushahidi_action.report_edit', $incident); // SAVE AND CLOSE? switch ($post->save) { case 1: case 'dontclose': // Save but don't close url::redirect('admin/reports/edit/' . $incident->id . '/saved'); break; case 'addnew': // Save and add new url::redirect('admin/reports/edit/0/saved'); break; default: // Save and close url::redirect('admin/reports/'); } } else { // Repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // Populate the error fields, if any $errors = arr::merge($errors, $post->errors('report')); $form_error = TRUE; } } else { if (Incident_Model::is_valid_incident($id, FALSE)) { // Retrieve Current Incident $incident = ORM::factory('incident', $id); if ($incident->loaded == TRUE) { // Retrieve Categories $incident_category = array(); foreach ($incident->incident_category as $category) { $incident_category[] = $category->category_id; } // Retrieve Media $incident_news = array(); $incident_video = array(); $incident_photo = array(); foreach ($incident->media as $media) { if ($media->media_type == 4) { $incident_news[] = $media->media_link; } elseif ($media->media_type == 2) { $incident_video[] = $media->media_link; } elseif ($media->media_type == 1) { $incident_photo[] = $media->media_link; } } // Get Geometries via SQL query as ORM can't handle Spatial Data $sql = "SELECT AsText(geometry) as geometry, geometry_label,\n\t\t\t\t\t\tgeometry_comment, geometry_color, geometry_strokewidth\n\t\t\t\t\t\tFROM " . Kohana::config('database.default.table_prefix') . "geometry\n\t\t\t\t\t\tWHERE incident_id = ?"; $query = $db->query($sql, $id); foreach ($query as $item) { $geometry = array("geometry" => $item->geometry, "label" => $item->geometry_label, "comment" => $item->geometry_comment, "color" => $item->geometry_color, "strokewidth" => $item->geometry_strokewidth); $form['geometry'][] = json_encode($geometry); } // Combine Everything $incident_arr = array('location_id' => $incident->location->id, 'form_id' => $incident->form_id, 'locale' => $incident->locale, 'incident_title' => $incident->incident_title, 'incident_description' => $incident->incident_description, 'incident_date' => date('m/d/Y', strtotime($incident->incident_date)), 'incident_hour' => date('h', strtotime($incident->incident_date)), 'incident_minute' => date('i', strtotime($incident->incident_date)), 'incident_ampm' => date('a', strtotime($incident->incident_date)), 'latitude' => $incident->location->latitude, 'longitude' => $incident->location->longitude, 'location_name' => $incident->location->location_name, 'country_id' => $incident->location->country_id, 'incident_category' => $incident_category, 'incident_news' => $incident_news, 'incident_video' => $incident_video, 'incident_photo' => $incident_photo, 'person_first' => $incident->incident_person->person_first, 'person_last' => $incident->incident_person->person_last, 'person_email' => $incident->incident_person->person_email, 'custom_field' => customforms::get_custom_form_fields($id, $incident->form_id, TRUE, 'submit'), 'incident_active' => $incident->incident_active, 'incident_verified' => $incident->incident_verified, 'incident_zoom' => $incident->incident_zoom); // Merge To Form Array For Display $form = arr::overwrite($form, $incident_arr); } else { // Redirect url::redirect('admin/reports/'); } } } $this->template->content->id = $id; $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; // Retrieve Custom Form Fields Structure $this->template->content->custom_forms = new View('reports/submit_custom_forms'); $disp_custom_fields = customforms::get_custom_form_fields($id, $form['form_id'], FALSE, "view"); $custom_field_mismatch = customforms::get_edit_mismatch($form['form_id']); // Quick hack to make sure view-only fields have data set foreach ($custom_field_mismatch as $id => $field) { $form['custom_field'][$id] = $disp_custom_fields[$id]['field_response']; } $this->template->content->custom_forms->disp_custom_fields = $disp_custom_fields; $this->template->content->custom_forms->custom_field_mismatch = $custom_field_mismatch; $this->template->content->custom_forms->form = $form; // Retrieve Previous & Next Records $previous = ORM::factory('incident')->where('id < ', $id)->orderby('id', 'desc')->find(); $previous_url = $previous->loaded ? url::site('admin/reports/edit/' . $previous->id) : url::site('admin/reports/'); $next = ORM::factory('incident')->where('id > ', $id)->orderby('id', 'desc')->find(); $next_url = $next->loaded ? url::site('admin/reports/edit/' . $next->id) : url::site('admin/reports/'); $this->template->content->previous_url = $previous_url; $this->template->content->next_url = $next_url; // Javascript Header $this->themes->map_enabled = TRUE; $this->themes->colorpicker_enabled = TRUE; $this->themes->treeview_enabled = TRUE; $this->themes->json2_enabled = TRUE; $this->themes->js = new View('reports/submit_edit_js'); $this->themes->js->edit_mode = TRUE; $this->themes->js->default_map = Kohana::config('settings.default_map'); $this->themes->js->default_zoom = Kohana::config('settings.default_zoom'); if (!$form['latitude'] or !$form['latitude']) { $this->themes->js->latitude = Kohana::config('settings.default_lat'); $this->themes->js->longitude = Kohana::config('settings.default_lon'); } else { $this->themes->js->latitude = $form['latitude']; $this->themes->js->longitude = $form['longitude']; } $this->themes->js->incident_zoom = $form['incident_zoom']; $this->themes->js->geometries = $form['geometry']; // Inline Javascript $this->template->content->date_picker_js = $this->_date_picker_js(); $this->template->content->color_picker_js = $this->_color_picker_js(); $this->template->content->new_category_toggle_js = $this->_new_category_toggle_js(); // Pack Javascript $myPacker = new javascriptpacker($this->themes->js, 'Normal', FALSE, FALSE); $this->themes->js = $myPacker->pack(); }
/** * 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('members/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_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'custom_field' => array(), 'incident_source' => '', 'incident_information' => '', '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; 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('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); // Locale (Language) Array $this->template->content->locale_array = Kohana::config('locale.all_languages'); // Create Categories $this->template->content->categories = $this->_get_categories(); // 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 a Checkin? if (isset($_GET['cid']) && !empty($_GET['cid'])) { $checkin_id = (int) $_GET['cid']; $checkin = ORM::factory('checkin', $checkin_id); if ($checkin->loaded) { // Has a report already been created for this Checkin? if ((int) $checkin->incident_id > 0) { // Redirect to report url::redirect('members/reports/edit/' . $checkin->incident_id); } $incident_description = $checkin->checkin_description; $incident_title = text::limit_chars(strip_tags($incident_description), 100, "...", true); $form['incident_title'] = $incident_title; $form['incident_description'] = $incident_description; $form['incident_date'] = date('m/d/Y', strtotime($checkin->checkin_date)); $form['incident_hour'] = date('h', strtotime($checkin->checkin_date)); $form['incident_minute'] = date('i', strtotime($checkin->checkin_date)); $form['incident_ampm'] = date('a', strtotime($checkin->checkin_date)); // Does the sender of this message have a location? if ($checkin->location->loaded) { $form['location_id'] = $checkin->location_id; $form['latitude'] = $checkin->location->latitude; $form['longitude'] = $checkin->location->longitude; $form['location_name'] = $checkin->location->location_name; } } } // 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_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()) { // STEP 1: SAVE LOCATION $location = new Location_Model(); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); reports::save_report($post, $incident, $location->id); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE CUSTOM FORM FIELDS reports::save_custom_fields($post, $incident); // STEP 6: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // If creating a report from a checkin if (isset($checkin_id) and $checkin_id != "") { $checkin = ORM::factory('checkin', $checkin_id); if ($checkin->loaded) { $checkin->incident_id = $incident->id; $checkin->save(); // Attach all the media items in this checkin to the report foreach ($checkin->media as $media) { $media->incident_id = $incident->id; $media->save(); } } } // Action::report_add / report_submit_members - Added a New Report //++ Do we need two events for this? Or will one suffice? Event::run('ushahidi_action.report_add', $incident); Event::run('ushahidi_action.report_submit_members', $post); // SAVE AND CLOSE? if ($post->save == 1) { url::redirect('members/reports/edit/' . $incident->id . '/saved'); } else { url::redirect('members/reports/'); } } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('report')); $form_error = TRUE; } } else { if ($id) { // Retrieve Current Incident $incident = ORM::factory('incident')->where('user_id', $this->user->id)->find($id); if ($incident->loaded == true) { // Retrieve Categories $incident_category = array(); foreach ($incident->incident_category as $category) { $incident_category[] = $category->category_id; } // Retrieve Media $incident_news = array(); $incident_video = array(); $incident_photo = array(); foreach ($incident->media as $media) { if ($media->media_type == 4) { $incident_news[] = $media->media_link; } elseif ($media->media_type == 2) { $incident_video[] = $media->media_link; } elseif ($media->media_type == 1) { $incident_photo[] = $media->media_link; } } // Get Geometries via SQL query as ORM can't handle Spatial Data $sql = "SELECT AsText(geometry) as geometry, geometry_label, \n\t\t\t\t\t\tgeometry_comment, geometry_color, geometry_strokewidth \n\t\t\t\t\t\tFROM " . Kohana::config('database.default.table_prefix') . "geometry \n\t\t\t\t\t\tWHERE incident_id=" . $id; $query = $db->query($sql); foreach ($query as $item) { $geometry = array("geometry" => $item->geometry, "label" => $item->geometry_label, "comment" => $item->geometry_comment, "color" => $item->geometry_color, "strokewidth" => $item->geometry_strokewidth); $form['geometry'][] = json_encode($geometry); } // Combine Everything $incident_arr = array('location_id' => $incident->location->id, 'form_id' => $incident->form_id, 'locale' => $incident->locale, 'incident_title' => $incident->incident_title, 'incident_description' => $incident->incident_description, 'incident_date' => date('m/d/Y', strtotime($incident->incident_date)), 'incident_hour' => date('h', strtotime($incident->incident_date)), 'incident_minute' => date('i', strtotime($incident->incident_date)), 'incident_ampm' => date('a', strtotime($incident->incident_date)), 'latitude' => $incident->location->latitude, 'longitude' => $incident->location->longitude, 'location_name' => $incident->location->location_name, 'country_id' => $incident->location->country_id, 'incident_category' => $incident_category, 'incident_news' => $incident_news, 'incident_video' => $incident_video, 'incident_photo' => $incident_photo, 'person_first' => $incident->incident_person->person_first, 'person_last' => $incident->incident_person->person_last, 'person_email' => $incident->incident_person->person_email, 'custom_field' => $this->_get_custom_form_fields($id, $incident->form_id, true), 'incident_source' => $incident->incident_source, 'incident_information' => $incident->incident_information, 'incident_zoom' => $incident->incident_zoom); // Merge To Form Array For Display $form = arr::overwrite($form, $incident_arr); } else { // Redirect url::redirect('members/reports/'); } } } $this->template->content->id = $id; $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; // Retrieve Custom Form Fields Structure $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() . 'members/reports/edit/' . $previous->id : url::base() . 'members/reports/'; $next = ORM::factory('incident')->where('id > ', $id)->orderby('id', 'desc')->find(); $next_url = $next->loaded ? url::base() . 'members/reports/edit/' . $next->id : url::base() . 'members/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->json2_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']; } $this->template->js->incident_zoom = $form['incident_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(); // Pack Javascript $myPacker = new javascriptpacker($this->template->js, 'Normal', false, false); $this->template->js = $myPacker->pack(); }
/** * The actual reporting - * * @return int */ private function _submit_report() { // setup and initialize form field names $form = array('location_id' => '', 'incident_id' => '', 'incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'location_name' => '', 'country_id' => '', 'incident_category' => '', 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'incident_active ' => '', 'incident_verified' => ''); $errors = $form; // check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite // $_POST fields with our own things $post = array_merge($_POST, $_FILES); $post['incident_category'] = explode(',', $post['incident_category']); // Action::report_submit_admin - Report Posted Event::run('ushahidi_action.report_submit_admin', $post); // Test to see if things passed the rule checks if (reports::validate($post, TRUE)) { // Yes! everything is valid $location_id = $post->location_id; // STEP 1: SAVE LOCATION $location = new Location_Model($location_id); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident_id = $post->incident_id; $incident = new Incident_Model($incident_id); reports::save_report($post, $incident, $location->id); // STEP 2b: Record Approval/Verification Action $verify = new Verify_Model(); reports::verify_approve($post, $verify, $incident); // STEP 2c: SAVE INCIDENT GEOMETRIES reports::save_report_geometry($post, $incident); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // Action::report_edit - Edited a Report Event::run('ushahidi_action.report_edit', $incident); // Success return $this->response(0); } else { // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('report')); foreach ($errors as $error_item => $error_description) { if (!is_array($error_description)) { $this->error_messages .= $error_description; if ($error_description != end($errors)) { $this->error_messages .= " - "; } } } //FAILED!!! //validation error return $this->response(1, $this->error_messages); } } else { // Not sent by post method. return $this->response(3); } }
/** * Submits a new report. */ public function submit($id = FALSE, $saved = FALSE) { $db = new Database(); // 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'); $this->template->header->page_title .= Kohana::lang('ui_main.reports_submit_new') . Kohana::config('settings.title_delimiter'); //Retrieve API URL $this->template->api_url = Kohana::config('settings.api_url'); // Setup and initialize form field names // JP: added additional form data for advanced settings $form = array('incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'geometry' => array(), 'location_name' => '', 'country_id' => '', 'country_name' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'incident_zoom' => '', 'person_first' => '', 'person_last' => '', 'person_email' => '', 'form_id' => '', 'custom_field' => array(), 'form_data' => array()); // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = $saved == 'saved'; // Initialize Default Values $form['incident_date'] = date("m/d/Y", time()); $form['incident_hour'] = date('h'); $form['incident_minute'] = date('i'); $form['incident_ampm'] = date('a'); $form['country_id'] = Kohana::config('settings.default_country'); // Initialize Default Value for Hidden Field Country Name, just incase Reverse Geo coding yields no result $country_name = ORM::factory('country', $form['country_id']); $form['country_name'] = $country_name->country; // Initialize custom field array $form['form_id'] = 1; // JP: Removed the $form_id variable since it was being mistakenly used later as the ID of the posted form, resulting in bugs. Changed instances of $form_id to $form['form_id'] (like below) and $post['form_id'], if posted. $form['custom_field'] = customforms::get_custom_form_fields($id, $form['form_id'], true); // JP: Grab additional form information for advanced settings. $form['form_data'] = customforms::get_custom_form($form['form_id']); // GET custom forms $forms = array(); foreach (customforms::get_custom_forms() 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 = array_merge($_POST, $_FILES); // JP: Ensure that the advanced settings are correct. $form['form_data'] = customforms::get_custom_form($post['form_id']); // JP: Add the description_active boolean to our post data so the appropriate validation rules can be added. $post['description_active'] = $form['form_data']->description_active; // Adding event for endtime plugin to hook into Event::run('ushahidi_action.report_posted_frontend', $post); // Test to see if things passed the rule checks if (reports::validate($post)) { // STEP 1: SAVE LOCATION $location = new Location_Model(); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); reports::save_report($post, $incident, $location->id); // STEP 2b: SAVE INCIDENT GEOMETRIES reports::save_report_geometry($post, $incident); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE CUSTOM FORM FIELDS reports::save_custom_fields($post, $incident); // STEP 6: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // Run events Event::run('ushahidi_action.report_submit', $post); Event::run('ushahidi_action.report_add', $incident); url::redirect('reports/thanks'); } else { // Repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // Populate the error fields, if any $errors = arr::merge($errors, $post->errors('report')); // JP: Replace default Report Title and Description names with custom names in the error listing. if ($errors['incident_title'] and !empty($form['form_data']->report_title_name)) { $errors['incident_title'] = str_replace(Kohana::lang('ui_main.reports_title'), $form['form_data']->report_title_name, $errors['incident_title']); } if ($errors['incident_description'] and !empty($form['form_data']->description_name)) { $errors['incident_description'] = str_replace(Kohana::lang('ui_main.reports_description'), $form['form_data']->description_name, $errors['incident_description']); } $form_error = TRUE; } } // 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; // Populate this for backwards compat $this->template->content->categories = array(); // Pass timezone $this->template->content->site_timezone = Kohana::config('settings.site_timezone'); // Pass the submit report message $this->template->content->site_submit_report_message = Kohana::config('settings.site_submit_report_message'); // Retrieve Custom Form Fields Structure $this->template->content->custom_forms = new View('reports/submit_custom_forms'); // JP: This needs to be passed $form['form_id'] rather than $form_id so that we use the correct custom fields. $disp_custom_fields = customforms::get_custom_form_fields($id, $form['form_id'], FALSE); $this->template->content->disp_custom_fields = $disp_custom_fields; $this->template->content->stroke_width_array = $this->_stroke_width_array(); $this->template->content->custom_forms->disp_custom_fields = $disp_custom_fields; $this->template->content->custom_forms->form = $form; // Javascript Header $this->themes->map_enabled = TRUE; $this->themes->treeview_enabled = TRUE; $this->themes->colorpicker_enabled = TRUE; $this->themes->js = new View('reports/submit_edit_js'); $this->themes->js->edit_mode = FALSE; $this->themes->js->incident_zoom = FALSE; $this->themes->js->default_map = Kohana::config('settings.default_map'); $this->themes->js->default_zoom = Kohana::config('settings.default_zoom'); if (!$form['latitude'] or !$form['latitude']) { $this->themes->js->latitude = Kohana::config('settings.default_lat'); $this->themes->js->longitude = Kohana::config('settings.default_lon'); } else { $this->themes->js->latitude = $form['latitude']; $this->themes->js->longitude = $form['longitude']; } $this->themes->js->geometries = $form['geometry']; }
/** * This function updates the categories associated * with this well * @param $incident_id the id of the report for this well * @param $ivr_data the data we just got from the IVR * @return none */ private function update_categories($incident_id, $ivr_data) { //find the two categories that apply to wells and IVR data $functioning_category = ORM::factory('category')->where('category_title', api_ivr::$wellstatus['functioning'])->find(); if (!$functioning_category->loaded) { $this->response['status'] = 'Error'; $this->response['message'][] = "Could not find well functioning category: " . api_ivr::$wellstatus['functioning']; $this->errors_found = TRUE; return; } $malfunctioning_category = ORM::factory('category')->where('category_title', api_ivr::$wellstatus['malfunctioning'])->find(); if (!$malfunctioning_category->loaded) { $this->response['status'] = 'Error'; $this->response['message'][] = "Could not find well malfunctioning category: " . api_ivr::$wellstatus['malfunctioning']; $this->errors_found = TRUE; return; } //so there are easier ways to do this, but we've done things in this way so we can be compatible with the //Version Categories plugin. We try to simulate a report being updated via a Post command //now add the correct category $chosen_cat_id = $malfunctioning_category->id; $remove_cat_id = $functioning_category->id; if ($ivr_data->well_working) { $chosen_cat_id = $functioning_category->id; $remove_cat_id = $malfunctioning_category->id; } //get the current cats for this incident $new_cats = array(); $current_cats = ORM::factory('incident_category')->where('incident_id', $incident_id)->find_all(); foreach ($current_cats as $c) { if ($c->category_id != $remove_cat_id) { $new_cats[$c->category_id] = $c->category_id; } } $new_cats[$chosen_cat_id] = $chosen_cat_id; $post = new dummy_post($new_cats); $incident = ORM::factory('incident')->where('id', $incident_id)->find(); reports::save_category($post, $incident); /* // Event so the category change plugin can record the date and time // that categories are changed and what they were changed to $event_data = array('id'=>$incident_id, 'new_categories'=>$new_cats); Event::run('ushahidi_action.report_categories_changing', $event_data); /* //now remove any current category associates between these two categories and our well ORM::factory('incident_category') ->where(array('category_id'=> $remove_cat_id, 'incident_id'=>$incident_id)) ->delete_all(); //now add in the right category $cat = ORM::factory('incident_category'); $cat->incident_id = $incident_id; $cat->category_id = $chosen_cat_id; $cat->save(); */ }
/** * The actual reporting - * * @return int */ private function _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' => '', 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => ''); $this->messages = $form; // Check for HTTP POST, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite // $_POST fields with our own things $post = array_merge($_POST, $_FILES); $post['incident_category'] = explode(',', $post['incident_category']); // // EK <*****@*****.**> - 17/05/2012 // Commenting out this event ('ushahidi_action.report_submit_api') because // of the following: // The 'ushahidi_action.report_submit' and 'ushahidi_action.report_add' // events should suffice for all plugins that wish to run extra // operations once a report has been submitted and saved - avoid // superfluous events // // In case there's a plugin that would like to know about // this new incident, I mean report // Event::run('ushahidi_action.report_submit_api', $post); if (reports::validate($post)) { // STEP 1: SAVE LOCATION $location = new Location_Model(); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); reports::save_report($post, $incident, $location->id); // STEP 2b: SAVE INCIDENT GEOMETRIES reports::save_report_geometry($post, $incident); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE CUSTOM FORM FIELDS reports::save_custom_fields($post, $incident); // STEP 6: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // Run events Event::run('ushahidi_action.report_submit', $post); Event::run('ushahidi_action.report_add', $incident); // Action::report_edit_api - Edited a Report Event::run('ushahidi_action.report_edit_api', $incident); // Success return 0; } else { // Populate the error fields, if any $this->messages = arr::overwrite($this->messages, $post->errors('report')); foreach ($this->messages as $error_item => $error_description) { if (!is_array($error_description)) { $this->error_string .= $error_description; if ($error_description != end($this->messages)) { $this->error_string .= " - "; } } } //FAILED!!! return 1; //validation error } } else { return 3; // Not sent by post method. } }
/** * The actual reporting * * @return int */ private function save_incident($data) { // Convert dates to DateTime objects $incident_date = isset($data->incident_date) ? date_create($data->incident_date, new DateTimeZone('UTC')) : new DateTime(); $incident_date = $incident_date instanceof DateTime ? $incident_date : new DateTime(); $updated_at = isset($data->updated_at) ? date_create($data->updated_at, new DateTimeZone('UTC')) : new DateTime(); $updated_at = $updated_at instanceof DateTime ? $updated_at : new DateTime(); // Change to site timezone $incident_date->setTimezone(new DateTimeZone(date_default_timezone_get())); $updated_at->setTimezone(new DateTimeZone(date_default_timezone_get())); // Mash data into format expected by reports helper $post = array('location_id' => isset($data->location_id) ? $data->location_id : null, 'incident_id' => isset($data->id) ? $data->id : (isset($data->sid) ? $data->sid : null), 'incident_title' => isset($data->incident_title) ? $data->incident_title : null, 'incident_description' => isset($data->incident_description) ? $data->incident_description : null, 'incident_date' => $incident_date->format('m/d/Y'), 'incident_hour' => $incident_date->format('h'), 'incident_minute' => $incident_date->format('i'), 'incident_ampm' => $incident_date->format('a'), 'latitude' => isset($data->location->latitude) ? $data->location->latitude : null, 'longitude' => isset($data->location->longitude) ? $data->location->longitude : null, 'location_name' => isset($data->location->location_name) ? $data->location->location_name : null, 'country_id' => isset($data->location->country_id) ? $data->location->country_id : null, 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => isset($data->incident_person->person_first) ? $data->incident_person->person_first : '', 'person_last' => isset($data->incident_person->person_last) ? $data->incident_person->person_last : '', 'person_email' => isset($data->incident_person->person_email) ? $data->incident_person->person_email : '', 'person_phone' => isset($data->incident_person->person_phone) ? $data->incident_person->person_phone : '', 'incident_active' => isset($data->incident_active) ? $data->incident_active : null, 'incident_verified' => isset($data->incident_verified) ? $data->incident_verified : null, 'incident_zoom' => isset($data->incident_zoom) ? $data->incident_zoom : null, 'message_id' => isset($data->message_id) ? $data->message_id : null); if (isset($data->category)) { foreach ($data->category as $cat) { isset($cat->id) ? $post['incident_category'][] = $cat->id : ''; } } if (isset($data->media)) { foreach ($data->media as $media) { if ($media->media_type == 2) { isset($media->media_link) ? $post['incident_video'][] = $media->media_link : ""; } elseif ($media->media_type == 4) { isset($media->media_link) ? $post['incident_news'][] = $media->media_link : ""; } } } // Action::report_submit_admin - Report Posted Event::run('ushahidi_action.report_submit_admin', $post); // Test to see if things passed the rule checks if (reports::validate($post, TRUE)) { // Yes! everything is valid $location_id = $post->location_id; // STEP 1: SAVE LOCATION $location = new Location_Model($location_id); reports::save_location($post, $location); // STEP 2: SAVE INCIDENT $incident_id = $post->incident_id; $incident = new Incident_Model($incident_id); reports::save_report($post, $incident, $location->id, FALSE); // Overwrite datemodify/dateadd based on posted data if ($incident_id != null) { // Edit $incident->incident_datemodify = $updated_at->format("Y-m-d H:i:s"); } else { // New $incident->incident_dateadd = $updated_at->format("Y-m-d H:i:s"); } $incident->save(); // STEP 2b: Record Approval/Verification Action reports::verify_approve($incident); // STEP 2c: SAVE INCIDENT GEOMETRIES reports::save_report_geometry($post, $incident); // STEP 3: SAVE CATEGORIES reports::save_category($post, $incident); // STEP 4: SAVE MEDIA reports::save_media($post, $incident); // STEP 5: SAVE PERSONAL INFORMATION reports::save_personal_info($post, $incident); // STEP 6a: SAVE LINK TO REPORTER MESSAGE // We're creating a report from a message with this option // @todo move to reports helper if (isset($post->message_id) and intval(isset($post->message_id))) { $savemessage = ORM::factory('message', $post->message_id); if ($savemessage->loaded) { $savemessage->incident_id = $incident->id; $savemessage->save(); // Does Message Have Attachments? // Add Attachments $attachments = ORM::factory("media")->where("message_id", $savemessage->id)->find_all(); foreach ($attachments as $attachment) { $attachment->incident_id = $incident->id; $attachment->save(); } } } // Action::report_edit - Edited a Report Event::run('ushahidi_action.report_edit', $incident); // Success return $this->get_incidents_array($incident->id); } else { // populate the error fields, if any $errors = $post->errors('report'); $this->rest_error(400, $errors); } }