/** * Tag a news item to an incident. * * @param int incidentid - The incident id. * @param string mediatype - The media type,video, picture,etc * * @return Array */ private function _tag_media($incidentid, $mediatype) { if ($_POST) { // Check if incident ID exist $incidentid_exist = Incident_Model::is_valid_incident($incidentid); if (!$incidentid_exist) { return $this->set_error_message(array("error" => $this->api_service->get_error_msg(012))); } // Get the locationid for the incidentid $locationid = 0; $items = ORM::factory('incident')->select(array('location_id'))->where(array('incident.id' => $incidentid))->find(); if ($items->count_all() > 0) { $locationid = $items->location_id; } $media = new Media_Model(); //create media model object $url = ''; $post = Validation::factory(array_merge($_POST, $_FILES)); if ($mediatype == 2 or $mediatype == 4) { //require a url if (!$this->api_service->verify_array_index($this->request, 'url')) { return $this->set_error_message(array("error" => $this->api_service->get_error_msg(01, 'url'))); } else { $url = $this->request['url']; $media->media_link = $url; } } else { if (!$this->api_service->verify_array_index($this->request, 'photo')) { $this->set_error_message(array("error" => $this->api_service->get_error_msg(01), 'photo')); } $post->add_rules('photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[1M]'); if ($post->validate(FALSE)) { //assuming this is a photo $filename = upload::save('photo'); $new_filename = $incidentid . "_" . $i . "_" . time(); // Resize original file... make sure its max 408px wide Image::factory($filename)->resize(408, 248, Image::AUTO)->save(Kohana::config('upload.directory', TRUE) . $new_filename . ".jpg"); // Create thumbnail Image::factory($filename)->resize(70, 41, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . "_t.jpg"); // Remove the temporary file unlink($filename); $media->media_link = $new_filename . ".jpg"; $media->media_thumb = $new_filename . "_t.jpg"; } } // Optional title & description $title = ''; if ($this->api_service->verify_array_index($_POST, 'title')) { $title = $_POST['title']; } $description = ''; if ($this->api_service->verify_array_index($_POST, 'description')) { $description = $_POST['description']; } $media->location_id = $locationid; $media->incident_id = $incidentid; $media->media_type = $mediatype; $media->media_title = $title; $media->media_description = $description; $media->media_date = date("Y-m-d H:i:s", time()); $media->save(); //save the thing // SUCESS!!! $ret = array("payload" => array("domain" => $this->domain, "success" => "true"), "error" => $this->api_service->get_error_msg(0)); return $this->set_error_message($ret); } else { return $this->set_error_message(array("error" => $this->api_service->get_error_msg(03))); } }
/** * Tag a news item to an incident. * * @param int incidentid - The incident id. * @param string mediatype - The media type,video, picture,etc * * @return Array */ private function _tag_media($incidentid, $mediatype) { if ($_POST) { //get the locationid for the incidentid $locationid = 0; $this->query = "SELECT location_id FROM " . $this->table_prefix . "incident WHERE id={$incidentid}"; $items = $this->db->query($this->query); if (count($items) > 0) { $locationid = $items[0]->location_id; } $media = new Media_Model(); //create media model object $url = ''; $post = Validation::factory(array_merge($_POST, $_FILES)); if ($mediatype == 2 or $mediatype == 4) { //require a url if (!$this->api_service->verify_array_index($this->request, 'url')) { if ($this->response_type == 'json') { json_encode(array("error" => $this->api_service->get_error_msg(01, 'url'))); } else { $err = array("error" => $this->api_service->get_error_msg(01, 'url')); return $this->array_as_xml($err, array()); } } else { $url = $this->request['url']; $media->media_link = $url; } } else { if (!$this->api_service->verify_array_index($this->request, 'photo')) { if ($this->response_type == 'photo') { json_encode(array("error" => $this->api_service->get_error_msg(01, 'photo'))); } else { $err = array("error" => $this->api_service->get_error_msg(01, 'photo')); return $this->api_service->array_as_xml($err, array()); } } $post->add_rules('photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[1M]'); if ($post->validate()) { //assuming this is a photo $filename = upload::save('photo'); $new_filename = $incidentid . "_" . $i . "_" . time(); // Resize original file... make sure its max 408px wide Image::factory($filename)->resize(408, 248, Image::AUTO)->save(Kohana::config('upload.directory', TRUE) . $new_filename . ".jpg"); // Create thumbnail Image::factory($filename)->resize(70, 41, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . "_t.jpg"); // Remove the temporary file unlink($filename); $media->media_link = $new_filename . ".jpg"; $media->media_thumb = $new_filename . "_t.jpg"; } } // Optional title & description $title = ''; if ($this->api_service->verify_array_index($_POST, 'title')) { $title = $_POST['title']; } $description = ''; if ($this->api_service->verify_array_index($_POST, 'description')) { $description = $_POST['description']; } $media->location_id = $locationid; $media->incident_id = $incidentid; $media->media_type = $mediatype; $media->media_title = $title; $media->media_description = $description; $media->media_date = date("Y-m-d H:i:s", time()); $media->save(); //save the thing // SUCESS!!! $ret = array("payload" => array("domain" => $this->domain, "success" => "true"), "error" => $this->api_service->get_error_msg(0)); if ($this->response_type == 'json') { return json_encode($ret); } else { return $this->array_as_xml($ret, array()); } } else { if ($this->response_type == 'json') { return json_encode(array("error" => $this->api_service->get_error_msg(03))); } else { $err = array("error" => $this->api_service->get_error_msg(03)); return $this->array_as_xml($err, array()); } } }
/** * Submits a new report. */ public function submit() { $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' => ''); //copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; // Initialize Default Values $form['incident_date'] = date("m/d/Y", time()); $form['incident_hour'] = "12"; $form['incident_minute'] = "00"; $form['incident_ampm'] = "pm"; // check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things $post = Validation::factory(array_merge($_POST, $_FILES)); // Add some filters $post->pre_filter('trim', TRUE); // Add some rules, the input field, followed by a list of checks, carried out in order $post->add_rules('incident_title', 'required', 'length[3,200]'); $post->add_rules('incident_description', 'required'); $post->add_rules('incident_date', 'required', 'date_mmddyyyy'); $post->add_rules('incident_hour', 'required', 'between[1,12]'); $post->add_rules('incident_minute', 'required', 'between[0,59]'); if ($_POST['incident_ampm'] != "am" && $_POST['incident_ampm'] != "pm") { $post->add_error('incident_ampm', 'values'); } // Validate for maximum and minimum latitude values $post->add_rules('latitude', 'required', 'between[-90,90]'); $post->add_rules('longitude', 'required', 'between[-180,180]'); $post->add_rules('location_name', 'required', 'length[3,200]'); //XXX: Hack to validate for no checkboxes checked if (!isset($_POST['incident_category'])) { $post->incident_category = ""; $post->add_error('incident_category', 'required'); } else { $post->add_rules('incident_category.*', 'required', 'numeric'); } // Validate only the fields that are filled in if (!empty($_POST['incident_news'])) { foreach ($_POST['incident_news'] as $key => $url) { if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { $post->add_error('incident_news', 'url'); } } } // Validate only the fields that are filled in if (!empty($_POST['incident_video'])) { foreach ($_POST['incident_video'] as $key => $url) { if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { $post->add_error('incident_video', 'url'); } } } // Validate photo uploads $post->add_rules('incident_photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[2M]'); // Validate 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->user_id = 0; $incident->incident_title = $post->incident_title; $incident->incident_description = $post->incident_description; $incident_date = explode("/", $post->incident_date); // The $_POST['date'] is a value posted by form in mm/dd/yyyy format $incident_date = $incident_date[2] . "-" . $incident_date[0] . "-" . $incident_date[1]; $incident_time = $post->incident_hour . ":" . $post->incident_minute . ":00 " . $post->incident_ampm; $incident->incident_date = $incident_date . " " . $incident_time; $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->save(); // STEP 3: SAVE CATEGORIES foreach ($post->incident_category as $item) { $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $item; $incident_category->save(); } // STEP 4: SAVE MEDIA // a. News foreach ($post->incident_news as $item) { if (!empty($item)) { $news = new Media_Model(); $news->location_id = $location->id; $news->incident_id = $incident->id; $news->media_type = 4; // News $news->media_link = $item; $news->media_date = date("Y-m-d H:i:s", time()); $news->save(); } } // b. Video foreach ($post->incident_video as $item) { if (!empty($item)) { $video = new Media_Model(); $video->location_id = $location->id; $video->incident_id = $incident->id; $video->media_type = 2; // Video $video->media_link = $item; $video->media_date = date("Y-m-d H:i:s", time()); $video->save(); } } // c. Photos $filenames = upload::save('incident_photo'); $i = 1; foreach ($filenames as $filename) { $new_filename = $incident->id . "_" . $i . "_" . time(); // Resize original file... make sure its max 408px wide Image::factory($filename)->resize(408, 248, Image::AUTO)->save(Kohana::config('upload.directory', TRUE) . $new_filename . ".jpg"); // Create thumbnail Image::factory($filename)->resize(70, 41, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . "_t.jpg"); // Remove the temporary file unlink($filename); // Save to DB $photo = new Media_Model(); $photo->location_id = $location->id; $photo->incident_id = $incident->id; $photo->media_type = 1; // Images $photo->media_link = $new_filename . ".jpg"; $photo->media_thumb = $new_filename . "_t.jpg"; $photo->media_date = date("Y-m-d H:i:s", time()); $photo->save(); $i++; } // 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(); // Notify Admin Of New Report $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_report.subject'), Kohana::lang('notifications.admin_new_report.message') . "\n\n'" . strtoupper($incident->incident_title) . "'" . "\n" . $incident->incident_description); 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->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->categories = $this->_get_categories($form['incident_category']); // 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']; } }
/** * Adds email to the database and saves the sender as a new * Reporter if they don't already exist * @param string $messages */ private function add_email($messages) { $service = ORM::factory('service')->where('service_name', 'Email')->find(); if (!$service->loaded) { return; } if (empty($messages) or !is_array($messages)) { return; } foreach ($messages as $message) { $reporter = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message['email'])->find(); if (!$reporter->loaded == true) { // Add new reporter $names = explode(' ', $message['from'], 2); $last_name = ''; if (count($names) == 2) { $last_name = $names[1]; } // get default reporter level (Untrusted) $level = ORM::factory('level')->where('level_weight', 0)->find(); $reporter->service_id = $service->id; $reporter->level_id = $level->id; $reporter->service_account = $message['email']; $reporter->reporter_first = $names[0]; $reporter->reporter_last = $last_name; $reporter->reporter_email = $message['email']; $reporter->reporter_phone = null; $reporter->reporter_ip = null; $reporter->reporter_date = date('Y-m-d'); $reporter->save(); } if ($reporter->level_id > 1 && count(ORM::factory('message')->where('service_messageid', $message['message_id'])->find_all()) == 0) { // Save Email as Message $email = new Message_Model(); $email->parent_id = 0; $email->incident_id = 0; $email->user_id = 0; $email->reporter_id = $reporter->id; $email->message_from = $message['from']; $email->message_to = null; $email->message = $message['subject']; $email->message_detail = $message['body']; $email->message_type = 1; // Inbox $email->message_date = $message['date']; $email->service_messageid = $message['message_id']; $email->save(); // Attachments? foreach ($message['attachments'] as $attachments) { foreach ($attachments as $attachment) { $media = new Media_Model(); $media->location_id = 0; $media->incident_id = 0; $media->message_id = $email->id; $media->media_type = 1; // Images $media->media_link = $attachment[0]; $media->media_medium = $attachment[1]; $media->media_thumb = $attachment[2]; $media->media_date = date("Y-m-d H:i:s", time()); $media->save(); } } // Auto-Create A Report if Reporter is Trusted $reporter_weight = $reporter->level->level_weight; $reporter_location = $reporter->location; if ($reporter_weight > 0 and $reporter_location) { // Create Incident $incident = new Incident_Model(); $incident->location_id = $reporter_location->id; $incident->incident_title = $message['subject']; $incident->incident_description = $message['body']; $incident->incident_date = $message['date']; $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->incident_active = 1; if ($reporter_weight == 2) { $incident->incident_verified = 1; } $incident->save(); // Update Message with Incident ID $email->incident_id = $incident->id; $email->save(); // Save Incident Category $trusted_categories = ORM::factory("category")->where("category_trusted", 1)->find(); if ($trusted_categories->loaded) { $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $trusted_categories->id; $incident_category->save(); } // Add Attachments $attachments = ORM::factory("media")->where("message_id", $email->id)->find_all(); foreach ($attachments as $attachment) { $attachment->incident_id = $incident->id; $attachment->save(); } } // Notify Admin Of New Email Message $send = notifications::notify_admins("[" . Kohana::config('settings.site_name') . "] " . Kohana::lang('notifications.admin_new_email.subject'), Kohana::lang('notifications.admin_new_email.message')); // Action::message_email_add - Email Received! Event::run('ushahidi_action.message_email_add', $email); } } }
break; // Delete // Delete case 2: $model = new Layout_Model(); if (!empty($_POST)) { if ($model->deleteGallery($_POST['pictureId'])) { echo 1; } } break; case 3: $model = new Layout_Model(); $allowedExtensions = array("jpg", "JPG", "jpeg", "png"); $sizeLimit = 20 * 1024 * 1024; $uploader = new Media_Model($allowedExtensions, $sizeLimit); $savePath = $root . '/images/media/original/'; $medium = $root . '/images/media/thumb/'; $pre = 'Villa-Aqua-' . Tools::slugify($_POST['sectionName']); $mediumWidth = 550; if ($result = $uploader->handleUpload($savePath, $pre)) { $uploader->getThumb($result['fileName'], $savePath, $medium, $mediumWidth, 'width', ''); $newData = getimagesize($medium . $result['fileName']); $wp = $newData[0]; $hp = $newData[1]; $lastId = 0; if ($newData) { $lastId = $model->addGallery($_POST['sectionName'], $result['fileName']); } $data = array('success' => true, 'fileName' => $result['fileName'], 'wp' => $wp, 'hp' => $hp, 'lastId' => $lastId); echo htmlspecialchars(json_encode($data), ENT_NOQUOTES);
/** * Function to save news, photos and videos * * @param mixed $location_model * @param mixed $post * */ public static function save_media($post, $incident) { // Delete Previous Entries ORM::factory('media')->where('incident_id', $incident->id)->where('media_type <> 1')->delete_all(); // a. News foreach ($post->incident_news as $item) { if (!empty($item)) { $news = new Media_Model(); $news->location_id = $incident->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 = $incident->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 = $incident->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++; } }
/** * This function performs the actual checkin and will register a new user * if the user doesn't exist. Also, if the name and email is passed with * the checkin, the user will be updated. * * mobileid, lat and lon are the only required fields. * * Handles the API task parameters */ public function register_checkin($mobileid, $lat, $lon, $message = FALSE, $firstname = FALSE, $lastname = FALSE, $email = FALSE, $color = FALSE) { // Check if this device has been registered yet if (!User_Devices_Model::device_registered($mobileid)) { // Device has not been registered yet. Register it! // TODO: Formalize the user creation process. For now we are creating // a new user for every new device but eventually, we need // to be able to have multiple devices for each user if ($firstname and $lastname) { $user_name = $firstname . ' ' . $lastname; } else { $user_name = ''; } if ($email) { $user_email = $email; } else { $user_email = $this->getRandomString(); } if ($color) { $user_color = $color; } else { $user_color = $this->random_color(); } // Check if email exists $query = 'SELECT id FROM ' . $this->table_prefix . 'users WHERE `email` = \'' . $user_email . '\' LIMIT 1;'; $usercheck = $this->db->query($query); if (isset($usercheck[0]->id)) { $user_id = $usercheck[0]->id; } else { // Create a new user $user = ORM::factory('user'); $user->name = $user_name; $user->email = $user_email; $user->username = $this->getRandomString(); $user->password = '******'; $user->color = $user_color; $user->add(ORM::factory('role', 'login')); $user_id = $user->save(); } // TODO: When we have user registration down, we need to pass a user id here // so we can assign it to a specific user User_Devices_Model::register_device($mobileid, $user_id); } // Now we have a fully registered device so lets update our user if we need to if ($firstname and $lastname and $email) { $user_id = User_Devices_Model::device_owner($mobileid); $user_name = $firstname . ' ' . $lastname; $user_email = $email; $user = ORM::factory('user', $user_id); $user->name = $user_name; $user->email = $user_email; if ($color) { $user->color = $color; } $user_id = $user->save(); $user_id = $user_id->id; } // Get our user id if it hasn't already been set by one of the processes above if (!isset($user_id)) { $user_id = User_Devices_Model::device_owner($mobileid); } // Whew, now that all that is out of the way, do the flippin checkin! // FIRST, save the location $location = new Location_Model(); $location->location_name = $lat . ',' . $lon; $location->latitude = $lat; $location->longitude = $lon; $location->location_date = date("Y-m-d H:i:s", time()); $location_id = $location->save(); // SECOND, save the checkin if (!$message) { $message = ''; } $checkin = ORM::factory('checkin'); $checkin->user_id = $user_id; $checkin->location_id = $location_id; $checkin->checkin_description = $message; $checkin->checkin_date = date("Y-m-d H:i:s", time()); $checkin_id = $checkin->save(); // THIRD, save the photo, if there is a photo if (isset($_FILES['photo'])) { $filename = upload::save('photo'); $new_filename = 'ci_' . $user_id . '_' . time() . '_' . $this->getRandomString(4); $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 $media_photo = new Media_Model(); $media_photo->location_id = $location_id; $media_photo->checkin_id = $checkin_id; $media_photo->media_type = 1; // Images $media_photo->media_link = $new_filename . $file_type; $media_photo->media_medium = $new_filename . "_m" . $file_type; $media_photo->media_thumb = $new_filename . "_t" . $file_type; $media_photo->media_date = date("Y-m-d H:i:s", time()); $media_photo->save(); } $return = array("checkin_id" => $checkin_id->id, "user_id" => $user_id); // Hook on successful checkin Event::run('ushahidi_action.checkin_recorded', $checkin); return $return; }
if ($newData) { if ($_POST['lastIdLogo'] > 0) { if ($model->updateCompanyLogo($_POST['lastIdLogo'], $result['fileName'])) { $lastId = $_POST['lastIdLogo']; } } else { $lastId = $model->addCompanyLogo($_POST['companyId'], $result['fileName']); } } $data = array('success' => true, 'fileName' => $result['fileName'], 'wp' => $wp, 'hp' => $hp, 'lastId' => $lastId); echo htmlspecialchars(json_encode($data), ENT_NOQUOTES); } break; // Crop // Crop case 2: $model = new Media_Model(); $data = $backend->loadBackend(); if (!empty($_POST)) { $dstWidth = 300; $dstImageHeight = 150; $source = $root . '/img-up/companies_pictures/original/' . $_POST['imgId']; $destination = $root . '/img-up/companies_pictures/logo/' . $_POST['imgId']; if ($model->cropImage($_POST, $dstWidth, $dstImageHeight, $source, $destination)) { echo '1'; } else { echo '0'; } } break; }
/** * Map Settings */ function index($saved = false) { // Display all maps $this->template->api_url = Kohana::config('settings.api_url_all'); // Current Default Country $current_country = Kohana::config('settings.default_country'); $this->template->content = new View('admin/settings'); $this->template->content->title = Kohana::lang('ui_admin.settings'); // setup and initialize form field names $form = array('default_map' => '', 'api_google' => '', 'api_live' => '', 'default_country' => '', 'multi_country' => '', 'default_lat' => '', 'default_lon' => '', 'default_zoom' => '', 'default_map_all' => '', 'allow_clustering' => '', 'default_map_all_icon' => '', 'delete_default_map_all_icon' => ''); // 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'; // 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($_POST)->pre_filter('trim', TRUE)->add_rules('default_country', 'required', 'numeric', 'length[1,4]')->add_rules('multi_country', 'numeric', 'length[1,1]')->add_rules('default_map', 'required', 'length[0,100]')->add_rules('default_zoom', 'required', 'between[0,21]')->add_rules('default_lat', 'required', 'between[-85,85]')->add_rules('default_lon', 'required', 'between[-180,180]')->add_rules('allow_clustering', 'required', 'between[0,1]')->add_rules('default_map_all', 'required', 'alpha_numeric', 'length[6,6]')->add_rules('api_google', 'length[0,200]')->add_rules('api_live', 'length[0,200]'); // Add rules for file upload $files = Validation::factory($_FILES); $files->add_rules('default_map_all_icon', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[250K]'); // Test to see if things passed the rule checks if ($post->validate() and $files->validate(FALSE)) { // Yes! everything is valid $settings = new Settings_Model(1); $settings->default_country = $post->default_country; $settings->multi_country = $post->multi_country; $settings->default_map = $post->default_map; $settings->api_google = $post->api_google; // E.Kala 20th April 2012 // Gangsta workaround prevent resetting og Bing Maps API Key // Soon to be addressed conclusively if (isset($post['api_live']) and !empty($post['api_live'])) { $settings->api_live = $post->api_live; } $settings->default_zoom = $post->default_zoom; $settings->default_lat = $post->default_lat; $settings->default_lon = $post->default_lon; $settings->allow_clustering = $post->allow_clustering; $settings->default_map_all = $post->default_map_all; $settings->date_modify = date("Y-m-d H:i:s", time()); $settings->save(); // Deal with default category icon now // Check if deleting or updating a new image (or doing nothing) if (isset($post->delete_default_map_all_icon) and $post->delete_default_map_all_icon == 1) { // Delete old badge image ORM::factory('media')->delete($settings->default_map_all_icon_id); // Remove from DB table $settings = new Settings_Model(1); $settings->default_map_all_icon_id = NULL; $settings->save(); } else { // We aren't deleting, so try to upload if we are uploading an image $filename = upload::save('default_map_all_icon'); if ($filename) { $new_filename = "default_map_all_" . time(); $file_type = strrev(substr(strrev($filename), 0, 4)); // Large size $l_name = $new_filename . $file_type; Image::factory($filename)->save(Kohana::config('upload.directory', TRUE) . $l_name); // Medium size $m_name = $new_filename . "_m" . $file_type; Image::factory($filename)->resize(32, 32, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $m_name); // Thumbnail $t_name = $new_filename . "_t" . $file_type; Image::factory($filename)->resize(16, 16, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $t_name); // Name the files for the DB $media_link = $l_name; $media_medium = $m_name; $media_thumb = $t_name; // Okay, now we have these three different files on the server, now check to see // if we should be dropping them on the CDN if (Kohana::config("cdn.cdn_store_dynamic_content")) { $media_link = cdn::upload($media_link); $media_medium = cdn::upload($media_medium); $media_thumb = cdn::upload($media_thumb); // We no longer need the files we created on the server. Remove them. $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/') . '/'; unlink($local_directory . $l_name); unlink($local_directory . $m_name); unlink($local_directory . $t_name); } // Remove the temporary file unlink($filename); // Save image in the media table $media = new Media_Model(); $media->media_type = 1; // Image $media->media_link = $media_link; $media->media_medium = $media_medium; $media->media_thumb = $media_thumb; $media->media_date = date("Y-m-d H:i:s", time()); $media->save(); // Save new image in settings $settings = new Settings_Model(1); $settings->default_map_all_icon_id = $media->id; $settings->save(); } } // Delete Settings Cache $this->cache->delete('settings'); $this->cache->delete_tag('settings'); // Everything is A-Okay! $form_saved = TRUE; // Action::map_settings_modified - Map settings have changed Event::run('ushahidi_action.map_settings_modified'); // Redirect to reload everything over again url::redirect('admin/settings/index/saved'); } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('settings')); $form_error = TRUE; } } else { // Retrieve Current Settings $settings = ORM::factory('settings', 1); $form = array('default_map' => $settings->default_map, 'api_google' => $settings->api_google, 'api_live' => $settings->api_live, 'default_country' => $settings->default_country, 'multi_country' => $settings->multi_country, 'default_lat' => $settings->default_lat, 'default_lon' => $settings->default_lon, 'default_zoom' => $settings->default_zoom, 'allow_clustering' => $settings->allow_clustering, 'default_map_all' => $settings->default_map_all, 'default_map_all_icon_id' => $settings->default_map_all_icon_id); } // Get default category image $settings = ORM::factory('settings', 1); if ($settings->default_map_all_icon_id != NULL) { $icon = ORM::factory('media')->find($settings->default_map_all_icon_id); $this->template->content->default_map_all_icon = url::convert_uploaded_to_abs($icon->media_link); $this->template->content->default_map_all_icon_m = url::convert_uploaded_to_abs($icon->media_medium); $this->template->content->default_map_all_icon_t = url::convert_uploaded_to_abs($icon->media_thumb); } else { $this->template->content->default_map_all_icon = NULL; $this->template->content->default_map_all_icon_m = NULL; $this->template->content->default_map_all_icon_t = NULL; } $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; // 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, 30) . "..."; } $countries[$country->id] = $this_country; } $this->template->content->countries = $countries; // Zoom Array for Slider $default_zoom_array = array(); for ($i = Kohana::config('map.minZoomLevel'); $i < Kohana::config('map.minZoomLevel') + Kohana::config('map.numZoomLevels'); $i++) { $default_zoom_array[$i] = $i; } $this->template->content->default_zoom_array = $default_zoom_array; // Get Map API Providers $layers = map::base(); $map_array = array(); foreach ($layers as $layer) { $map_array[$layer->name] = $layer->title; } $this->template->content->map_array = $map_array; $this->template->content->yesno_array = array('1' => strtoupper(Kohana::lang('ui_main.yes')), '0' => strtoupper(Kohana::lang('ui_main.no'))); // Javascript Header $this->template->map_enabled = TRUE; $this->template->colorpicker_enabled = TRUE; $this->template->js = new View('admin/settings_js'); $this->template->js->default_map = $form['default_map']; $this->template->js->default_zoom = $form['default_zoom']; $this->template->js->default_lat = $form['default_lat']; $this->template->js->default_lon = $form['default_lon']; $this->template->js->all_maps_json = $this->_generate_settings_map_js(); }
/** * Edit a report * @param bool|int $id The id no. of the report * @param bool|string $saved */ function edit($id = false, $saved = false) { $this->template->content = new View('admin/reports_edit'); $this->template->content->title = 'Create A Report'; // setup and initialize form field names $form = array('location_id' => '', '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' => ''); // 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; } // 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; // Retrieve thumbnail photos (if edit); //XXX: fix _get_thumbnails $this->template->content->incident = $this->_get_thumbnails($id); // Are we creating this report from an SMS or Twitter Message? // If so retrieve message if (isset($_GET['mid']) && !empty($_GET['mid']) || isset($_GET['tid']) && !empty($_GET['tid'])) { // Check what kind of message this is if (isset($_GET['mid'])) { //Then it's an SMS message $messageType = 'sms'; $mobile_id = $_GET['mid']; $dbtable = 'message'; $col_prefix = 'message'; $incident_title = 'Mobile Report'; } elseif (isset($_GET['tid'])) { //Then it's a Twitter message $messageType = 'twitter'; $mobile_id = $_GET['tid']; $dbtable = 'twitter'; $col_prefix = 'tweet'; $incident_title = 'Twitter Report'; } $message = ORM::factory($dbtable, $mobile_id)->where($col_prefix . '_type', '1'); if ($message->loaded == true) { // Has a report already been created for this SMS? if ($message->incident_id != 0) { // Redirect to report url::redirect('admin/reports/edit/' . $message->incident_id); } if ($messageType == 'sms') { $this->template->content->message = $message->message; $this->template->content->message_from = $message->message_from; $this->template->content->show_messages = true; $form['incident_title'] = $incident_title; $form['incident_description'] = $message->message; $from_search = $this->template->content->message_from; } elseif ($messageType == 'twitter') { $this->template->content->message = $message->tweet; $this->template->content->message_from = $message->tweet_from; $this->template->content->show_messages = true; $form['incident_title'] = $incident_title; $form['incident_description'] = $message->tweet; $from_search = $this->template->content->tweet_from; } // Retrieve Last 5 Messages From this Number $this->template->content->allmessages = ORM::factory($dbtable)->where($col_prefix . '_from', $from_search)->where($col_prefix . '_type', '1')->orderby($col_prefix . '_date', 'desc')->limit(5)->find_all(); } else { $mobile_id = ""; $this->template->content->show_messages = false; } } else { $this->template->content->show_messages = false; } // check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things $post = Validation::factory(array_merge($_POST, $_FILES)); // Add some filters $post->pre_filter('trim', TRUE); // Add some rules, the input field, followed by a list of checks, carried out in order $post->add_rules('locale', 'required', 'alpha_dash', 'length[5]'); $post->add_rules('location_id', 'numeric'); $post->add_rules('mobile_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]'); } // 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->country_id = $post->country_id; $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->user_id = $_SESSION['auth_user']->id; $incident->incident_title = $post->incident_title; $incident->incident_description = $post->incident_description; $incident_date = split("/", $post->incident_date); // where the $_POST['date'] is a value posted by form in mm/dd/yyyy format $incident_date = $incident_date[2] . "-" . $incident_date[0] . "-" . $incident_date[1]; $incident_time = $post->incident_hour . ":" . $post->incident_minute . ":00 " . $post->incident_ampm; $incident->incident_date = $incident_date . " " . $incident_time; // Is this new or edit? if ($id) { $incident->incident_datemodify = date("Y-m-d H:i:s", time()); } else { $incident->incident_dateadd = date("Y-m-d H:i:s", time()); } // Is this an SMS or Twitter submitted report? //XXX: It is possible that 'mobile_id' may not be available through //$_POST if (isset($messageType) && $messageType != "") { if ($messageType == 'sms') { $incident->incident_mode = 2; // SMS - 2 } elseif ($messageType == 'twitter') { $incident->incident_mode = 4; // Twitter - 4 } elseif (isset($mobile_id) && $mobile_id != "") { $incident->incident_mode = 2; //Set the default as SMS - 2 } } $incident->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 6: SAVE LINK TO SMS MESSAGE if (isset($mobile_id) && $mobile_id != "") { $savemessage = ORM::factory($dbtable, $mobile_id); if ($savemessage->loaded == true) { $savemessage->incident_id = $incident->id; $savemessage->save(); } } // STEP 7: 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 != "0") { // 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, '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); // Merge To Form Array For Display $form = arr::overwrite($form, $incident_arr); } else { // Redirect url::redirect('admin/reports/'); } } else { $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'); } } $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; // Javascript Header $this->template->map_enabled = TRUE; $this->template->colorpicker_enabled = TRUE; $this->template->js = new View('admin/reports_edit_js'); $this->template->js->default_map = Kohana::config('settings.default_map'); $this->template->js->default_zoom = Kohana::config('settings.default_zoom'); $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(); }
public function index() { $apiurl = "http://tasukeai.heroku.com/all.xml"; #$apiurl = "http://localhost/message.xml"; $messages = simplexml_load_file($apiurl); foreach ($messages as $message) { $title = ""; $lat = ""; $active = 1; $long = ""; $matches = array(); if (strcmp($message->title["nil"], "true") != 0) { $title = (string) $message->title; } else { if (preg_match("/\\s*\\[ボランティア名称\\]\\s*\n([^\n]+)\n/", $message->body, $matches)) { $title = $matches[1]; } else { if (preg_match("/\\s*\\[主催\\]\\s*([^\n]+)\n/", $message->body, $matches)) { $title = $matches[1]; } else { if (preg_match("/\\s*\\[タイトル\\]\\s*([^\n]+)\n/", $message->body, $matches)) { $title = $matches[1]; } else { $title = "無題"; $active = 0; } } } } if (strcmp($message->latitude["nil"], "true") != 0 && strcmp($message->longitude["nil"], "true") != 0) { $lat = (double) $message->latitude; $long = (double) $message->longitude; } else { if (preg_match("/\\s*\\[緯度経度\\]\\s*\n([^,]+),([^\n]+)/", $message->body, $matches)) { $lat = $matches[1]; $long = $matches[2]; } } $link = $this->input->xss_clean($message->link); $where_string = "media_link = '" . $link . "'"; $db = new Database(); $count = $db->count_records('media', $where_string); if ($count > 0) { if (strcmp($message->{"valid-f"}, "false") == 0) { $search_query = "SELECT incident_id FROM media" . " WHERE (" . $where_string . ")"; $query = $db->query($search_query); ORM::factory('Incident')->where('id', $query[0]->incident_id)->delete_all(); ORM::factory('Media')->where('incident_id', $query[0]->incident_id)->delete_all(); } continue; } if (strcmp($message->{"valid-f"}, "true") != 0) { continue; } $incident = new Incident_Model(); // STEP 1: SAVE LOCATION if (isset($lat) && isset($long)) { $location = new Location_Model(""); $location->location_name = (string) $message->address; $location->latitude = $lat; $location->longitude = $long; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); $incident->location_id = $location->id; } $incident->incident_title = $title; $incident->incident_description = (string) $message->body; $incident->incident_date = date("Y-m-d H:i:s", strtotime($message->{"created-at"})); $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->incident_mode = 1; $incident->incident_active = $active; $incident->incident_verified = 1; $incident->incident_source = 3; $incident->incident_information = 1; //Save $incident->save(); $news = new Media_Model(); $news->incident_id = $incident->id; if (isset($location)) { $news->location_id = $location->id; } $news->media_type = 4; // News $news->media_link = $link; $news->media_date = date("Y-m-d H:i:s", strtotime($message->{"created-at"})); $news->save(); $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; if (strcmp($message->target, "2") == 0) { $incident_category->category_id = 9; //救援物資 } else { $incident_category->category_id = 13; //求む } $incident_category->save(); } $this->template->content = new View('tasukeaiimport/main'); }
/** * 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 index() { $source = 'http://legacy.ushahidi.com/export_data.asp'; ORM::factory('Location')->delete_all(); ORM::factory('Incident')->delete_all(); ORM::factory('Media')->delete_all(); ORM::factory('Incident_Person')->delete_all(); ORM::factory('Incident_Category')->delete_all(); ORM::factory('Comment')->delete_all(); ORM::factory('Rating')->delete_all(); // load as string $xmlstr = file_get_contents($source); $incidents = new SimpleXMLElement($xmlstr); foreach ($incidents as $post) { // STEP 1: SAVE LOCATION $location = new Location_Model(); $location->location_name = (string) $post->location_name; $location->latitude = (string) $post->latitude; $location->longitude = (string) $post->longitude; $location->country_id = 115; $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->user_id = 0; $incident->incident_title = (string) $post->incident_title; $incident->incident_description = (string) $post->incident_description; $incident->incident_date = (string) $post->incident_date; $incident->incident_active = (string) $post->active; $incident->incident_verified = (string) $post->verified; $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->save(); // STEP 3: SAVE CATEGORIES $incident_category = split(",", (string) $post->incident_category); foreach ($incident_category as $item) { if ($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 $news = new Media_Model(); $news->location_id = $location->id; $news->incident_id = $incident->id; $news->media_type = 4; // News $news->media_link = (string) $post->news; $news->media_date = date("Y-m-d H:i:s", time()); $news->save(); // b. Video $video = new Media_Model(); $video->location_id = $location->id; $video->incident_id = $incident->id; $video->media_type = 2; // Video $video->media_link = (string) $post->video; $video->media_date = date("Y-m-d H:i:s", time()); $video->save(); // STEP 5: SAVE PERSONAL INFORMATION $person = new Incident_Person_Model(); $person->location_id = $location->id; $person->incident_id = $incident->id; $person->person_first = (string) $post->person_first; $person->person_phone = (string) $post->person_phone; $person->person_email = (string) $post->person_email; $person->person_ip = (string) $post->person_ip; $person->person_date = date("Y-m-d H:i:s", time()); $person->save(); } echo "******************************************<BR>"; echo "******************************************<BR>"; echo "**** IMPORT COMPLETE!!!<BR>"; echo "******************************************<BR>"; echo "******************************************<BR>"; }
/** * 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(); }
/** * Delete Photo * @param int $id The unique id of the photo to be deleted */ public function deletePhoto($id) { $this->auto_render = FALSE; $this->template = ""; if ($id) { Media_Model::delete_photo($id); } }
function index() { $this->template->content = new View('admin/badges'); $this->template->content->title = Kohana::lang('ui_main.badges'); // setup and initialize form field names $form = array('id' => '', 'name' => '', 'description' => ''); // 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 = FALSE; $form_action = ""; if ($_POST) { $post = Validation::factory($_POST); // Add some filters $post->pre_filter('trim', TRUE); // Add some rules, the input field, followed by a list of checks, carried out in order $post->add_rules('action', 'required', 'alpha', 'length[1,1]'); $post->add_rules('name', 'standard_text', 'length[1,250]'); $post->add_rules('description', 'standard_text'); $post->add_rules('image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[100K]'); if ($post->validate()) { // ADD if ($post->action == 'a') { // Step 1. Save badge name and description $badge = new Badge_Model(); $badge->name = $post->name; $badge->description = $post->description; $badge->save(); // Step 2. Save badge image $filename = upload::save('image'); if ($filename) { $new_filename = "badge_" . $badge->id . "_" . time(); $file_type = strrev(substr(strrev($filename), 0, 4)); // Large size $l_name = $new_filename . $file_type; Image::factory($filename)->save(Kohana::config('upload.directory', TRUE) . $l_name); // Medium size $m_name = $new_filename . '_m' . $file_type; Image::factory($filename)->resize(80, 80, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $m_name); // Thumbnail $t_name = $new_filename . '_t' . $file_type; Image::factory($filename)->resize(60, 60, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $t_name); // Name the files for the DB $media_link = $l_name; $media_medium = $m_name; $media_thumb = $t_name; // Okay, now we have these three different files on the server, now check to see // if we should be dropping them on the CDN if (Kohana::config("cdn.cdn_store_dynamic_content")) { $cdn = new cdn(); $media_link = $cdn->upload($media_link); $media_medium = $cdn->upload($media_medium); $media_thumb = $cdn->upload($media_thumb); // We no longer need the files we created on the server. Remove them. $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/') . '/'; unlink($local_directory . $new_filename . $file_type); unlink($local_directory . $new_filename . '_m' . $file_type); unlink($local_directory . $new_filename . '_t' . $file_type); } // Remove the temporary file unlink($filename); // Delete old badge image ORM::factory('media')->where(array('badge_id' => $badge->id))->delete_all(); // Save new badge image $media = new Media_Model(); $media->badge_id = $badge->id; $media->media_type = 1; // Image $media->media_link = $media_link; $media->media_medium = $media_medium; $media->media_thumb = $media_thumb; $media->media_date = date("Y-m-d H:i:s", time()); $media->save(); } } // ASSIGN USER if ($post->action == 'b') { $badge_user = new Badge_User_Model(); $badge_user->badge_id = $post->badge_id; $badge_user->user_id = $post->assign_user; $badge_user->save(); } // REVOKE USER if ($post->action == 'r') { ORM::factory('badge_user')->where(array('badge_id' => (int) $post->badge_id, 'user_id' => (int) $post->revoke_user))->delete_all(); } elseif ($post->action == 'd') { // Remove from badge table ORM::factory('badge')->delete((int) $post->badge_id); // Remove from media ORM::factory('media')->where(array('badge_id' => (int) $post->badge_id))->delete_all(); // Remove from assignment ORM::factory('badge_user')->where(array('badge_id' => (int) $post->badge_id))->delete_all(); } } else { $errors = arr::overwrite($errors, $post->errors('badges')); $form_error = TRUE; } } $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->content->form_action = $form_action; // Get badges $this->template->content->badges = Badge_Model::badges(); $this->template->content->total_items = count($this->template->content->badges); // Get all users for dropdowns $users_result = ORM::factory('user')->orderby('name', 'asc')->find_all(); $users = array(); foreach ($users_result as $user) { $users[$user->id] = $user->username; } $this->template->content->users = $users; // Javascript Header $this->template->js = new View('admin/badges_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; }
/** * Function to save news, photos and videos * * @param mixed $location_model * @param mixed $post * */ public static function save_media($post, $incident) { // Delete Previous Entries ORM::factory('media')->where('incident_id', $incident->id)->where('media_type <> 1')->delete_all(); // a. News foreach ($post->incident_news as $item) { if (!empty($item)) { $news = new Media_Model(); $news->location_id = $incident->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 = $incident->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'); if (empty($filenames)) { $filenames = array(); } $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); // Name the files for the DB $media_link = $new_filename . $file_type; $media_medium = $new_filename . '_m' . $file_type; $media_thumb = $new_filename . '_t' . $file_type; // Okay, now we have these three different files on the server, now check to see // if we should be dropping them on the CDN if (Kohana::config("cdn.cdn_store_dynamic_content")) { $media_link = cdn::upload($media_link); $media_medium = cdn::upload($media_medium); $media_thumb = cdn::upload($media_thumb); // We no longer need the files we created on the server. Remove them. $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/') . '/'; unlink($local_directory . $new_filename . $file_type); unlink($local_directory . $new_filename . '_m' . $file_type); unlink($local_directory . $new_filename . '_t' . $file_type); } // Remove the temporary file unlink($filename); // Save to DB $photo = new Media_Model(); $photo->location_id = $incident->location_id; $photo->incident_id = $incident->id; $photo->media_type = 1; // Images $photo->media_link = $media_link; $photo->media_medium = $media_medium; $photo->media_thumb = $media_thumb; $photo->media_date = date("Y-m-d H:i:s", time()); $photo->save(); $i++; } }
$postXml->appendChild($node); try { $message = $post['message']; } catch (Exception $e) { $message = ""; } $node = $xml->createElement('message', $message); $postXml->appendChild($node); $node = $xml->createElement('id', $post['id']); $postXml->appendChild($node); $node = $xml->createElement('time', Date::easy((int) $post['time'])); $postXml->appendChild($node); $MainNode->appendChild($postXml); // Si on demande un post en particulier,on va chercher en envoyer les medias attachés if (isset($_GET['id']) && $_GET['id'] != "-1") { $Medias = new Media_Model(); $MediasTableau = $Medias->getPhotos(); $attachments = DB::select(' SELECT post_id, id, name, ext FROM attachments WHERE post_id IN (' . $post['id'] . ') ORDER BY ext, id ASC '); $nb = 0; foreach ($attachments as $media) { $mediaXml = $xml->createElement('media'); $media['thumb'] = getAttachedFileURL((int) $media['id'], 'jpg', 'thumb'); $media['url'] = getAttachedFileURL((int) $media['id'], $media['ext']); $node = $xml->createElement('path', $media['url']); $mediaXml->appendChild($node); $node = $xml->createElement('type', "image");
/** * Function used by the photo delete button * in /admin/reports/edit/N * @param $id is the DB id of the image to delete **/ public function deletePhoto($id = 0) { $this->auto_render = false; $this->template = null; if ($id) { Media_Model::delete_photo($id); } }
/** * Create a report and assign it to one or more categories and set verification */ public function __response_create_report($vars) { $categories = array(); if (isset($vars['add_category'])) { $categories = $vars['add_category']; } $verify = 0; if (isset($vars['verify'])) { $verify = (int) $vars['verify']; } $approve = 0; if (isset($vars['approve'])) { $approve = (int) $vars['approve']; } // Grab the location_id or create one if we can $location_id = 0; if (isset($this->data->location_id)) { $location_id = $this->data->location_id; } elseif (isset($this->data->latitude) and isset($this->data->longitude)) { $location_name = map::reverse_geocode($this->data->latitude, $this->data->longitude); // In case our location name is too long, chop off the end $location_name = substr_replace($location_name, '', 250); $location_data = (object) array('location_name' => $location_name, 'latitude' => $this->data->latitude, 'longitude' => $this->data->longitude); $location = new Location_Model(); reports::save_location($location_data, $location); $location_id = $location->id; } // We can only create reports if we have location. if ($location_id == FALSE or $location_id == 0) { return false; } // Build title // Build title & description // If this is a message if (isset($this->data->message)) { $incident_title = $this->data->message; $incident_description = $this->data->message; $incident_date = $this->data->message_date; // If we're got more message detail, make that the description if (!empty($message->message_detail)) { $incident_description = $this->data->message_detail; } } elseif (isset($this->data->item_title)) { $incident_title = html::strip_tags(html_entity_decode(html_entity_decode($this->data->item_title, ENT_QUOTES))); $incident_description = html::clean(html_entity_decode($this->data->item_description, ENT_QUOTES)); $incident_date = $this->data->item_date; } // Override title from action options if (!empty($vars['report_title'])) { $incident_title = $vars['report_title']; } // Save Incident $incident = new Incident_Model(); $incident->location_id = $location_id; $incident->incident_title = $incident_title; $incident->incident_description = $incident_description; $incident->incident_date = $incident_date; $incident->incident_active = $approve; $incident->incident_verified = $verify; $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->save(); // Conflicted.. do I run report add here? Potential to create a mess with action triggers? //Event::run('ushahidi_action.report_add', $incident); // Save media if (isset($this->data->item_title)) { $news = new Media_Model(); $news->location_id = $incident->location_id; $news->incident_id = $incident->id; $news->media_type = 4; // News $news->media_link = $this->data->item_link; $news->media_date = $this->data->item_date; $news->save(); } $incident_id = $incident->id; foreach ($categories as $category_id) { // Assign Category Incident_Category_Model::assign_category_to_incident($incident_id, $category_id); } // Link message with incident? if (isset($this->data->message) and isset($this->data->id)) { $message = new Message_Model($this->data->id); $message->incident_id = $incident_id; $message->save(); } elseif (isset($this->data->item_title) and isset($this->data->id)) { $item = new Feed_Item_Model($this->data->id); $item->incident_id = $incident_id; $item->save(); } return TRUE; }
/** * 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(); }
/** * Function to save news, photos and videos * * @param mixed $location_model * @param mixed $post * */ public static function save_media($post, $incident) { $upload_dir = Kohana::config('upload.directory', TRUE); // Delete Previous Entries ORM::factory('media')->where('incident_id', $incident->id)->where('media_type <> 1')->delete_all(); // a. News if (isset($post->incident_news)) { foreach ($post->incident_news as $item) { if (!empty($item)) { $news = new Media_Model(); $news->location_id = $incident->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 if (isset($post->incident_video)) { $videoembed = new VideoEmbed(); foreach ($post->incident_video as $k => $video_link) { if (!empty($video_link)) { $video_thumb = $videoembed->thumbnail($video_link); $new_filename = $incident->id . '_v' . $k . '_' . time(); $file_type = substr($video_thumb, strrpos($video_thumb, '.')); $media_thumb = NULL; $media_medium = NULL; // Make sure file has an image extension if ($video_thumb and in_array($file_type, array('.gif', '.jpg', '.png', '.jpeg'))) { // Name the files for the DB $media_link = $new_filename . $file_type; $media_medium = $new_filename . '_m' . $file_type; $media_thumb = $new_filename . '_t' . $file_type; try { if ($data = file_get_contents($video_thumb)) { file_put_contents($upload_dir . $media_link, $data); } } catch (Exception $e) { } // IMAGE SIZES: 800X600, 400X300, 89X59 // Catch any errors from corrupt image files try { $image = Image::factory($upload_dir . $media_link); // Medium size if ($image->height > 300) { Image::factory($upload_dir . $media_link)->resize(400, 300, Image::HEIGHT)->save($upload_dir . $media_medium); } else { // Cannot reuse the original image as it is deleted a bit further down $image->save($upload_dir . $media_medium); } // Thumbnail if ($image->height > 59) { Image::factory($upload_dir . $media_link)->resize(89, 59, Image::HEIGHT)->save($upload_dir . $media_thumb); } else { // Reuse the medium image when it is small enough $media_thumb = $media_medium; } } catch (Exception $e) { // Do nothing. Too late to throw errors // Set links to NULL $media_medium = NULL; $media_thumb = NULL; } // Okay, now we have these three different files on the server, now check to see // if we should be dropping them on the CDN $local_directory = rtrim($upload_dir, '/') . '/'; if ($media_medium and $media_thumb and Kohana::config("cdn.cdn_store_dynamic_content")) { $cdn_media_medium = cdn::upload($media_medium); $cdn_media_thumb = cdn::upload($media_thumb); // We no longer need the files we created on the server. Remove them. if (file_exists($local_directory . $media_medium)) { unlink($local_directory . $media_medium); } if (file_exists($local_directory . $media_thumb)) { unlink($local_directory . $media_thumb); } $media_medium = $cdn_media_medium; $media_thumb = $cdn_media_thumb; } if (file_exists($local_directory . $media_link)) { // Remove original image unlink($upload_dir . $media_link); } } $video = new Media_Model(); $video->location_id = $incident->location_id; $video->incident_id = $incident->id; $video->media_type = 2; // Video $video->media_link = $video_link; $video->media_thumb = $media_thumb; $video->media_medium = $media_medium; $video->media_date = date("Y-m-d H:i:s", time()); $video->save(); } } } // c. Photos if (!empty($post->incident_photo)) { $filenames = upload::save('incident_photo'); $i = 1; foreach ($filenames as $filename) { $new_filename = $incident->id . '_' . $i . '_' . time(); //$file_type = substr($filename,-4); $file_type = "." . substr(strrchr($filename, '.'), 1); // replaces the commented line above to take care of images with .jpeg extension. // Name the files for the DB $media_link = $new_filename . $file_type; $media_medium = $new_filename . '_m' . $file_type; $media_thumb = $new_filename . '_t' . $file_type; // IMAGE SIZES: 800X600, 400X300, 89X59 // Catch any errors from corrupt image files try { $image = Image::factory($filename); // Large size if ($image->width > 800 || $image->height > 600) { Image::factory($filename)->resize(800, 600, Image::AUTO)->save($upload_dir . $media_link); } else { $image->save($upload_dir . $media_link); } // Medium size if ($image->height > 300) { Image::factory($filename)->resize(400, 300, Image::HEIGHT)->save($upload_dir . $media_medium); } else { // Reuse the large image when it is small enough $media_medium = $media_link; } // Thumbnail if ($image->height > 59) { Image::factory($filename)->resize(89, 59, Image::HEIGHT)->save($upload_dir . $media_thumb); } else { // Reuse the medium image when it is small enough $media_thumb = $media_medium; } } catch (Kohana_Exception $e) { // Do nothing. Too late to throw errors $media_link = NULL; $media_medium = NULL; $media_thumb = NULL; } // Okay, now we have these three different files on the server, now check to see // if we should be dropping them on the CDN if (Kohana::config("cdn.cdn_store_dynamic_content")) { $cdn_media_link = cdn::upload($media_link); $cdn_media_medium = cdn::upload($media_medium); $cdn_media_thumb = cdn::upload($media_thumb); // We no longer need the files we created on the server. Remove them. $local_directory = rtrim($upload_dir, '/') . '/'; if (file_exists($local_directory . $media_link)) { unlink($local_directory . $media_link); } if (file_exists($local_directory . $media_medium)) { unlink($local_directory . $media_medium); } if (file_exists($local_directory . $media_thumb)) { unlink($local_directory . $media_thumb); } $media_link = $cdn_media_link; $media_medium = $cdn_media_medium; $media_thumb = $cdn_media_thumb; } // Remove the temporary file unlink($filename); // Save to DB $photo = new Media_Model(); $photo->location_id = $incident->location_id; $photo->incident_id = $incident->id; $photo->media_type = 1; // Images $photo->media_link = $media_link; $photo->media_medium = $media_medium; $photo->media_thumb = $media_thumb; $photo->media_date = date("Y-m-d H:i:s", time()); $photo->save(); $i++; } } }
/** * Overrides the default delete method for the ORM. * Deletes all other content related to the incident - performs * an SQL destroy */ public function delete() { // Delete Location ORM::factory('location')->where('id', $this->location_id)->delete_all(); // Delete Categories ORM::factory('incident_category')->where('incident_id', $this->id)->delete_all(); // Delete Translations ORM::factory('incident_lang')->where('incident_id', $this->id)->delete_all(); // Delete Photos From Directory $photos = ORM::factory('media')->where('incident_id', $this->id)->where('media_type', 1)->find_all(); foreach ($photos as $photo) { Media_Model::delete_photo($photo->id); } // Delete Media ORM::factory('media')->where('incident_id', $this->id)->delete_all(); // Delete Sender ORM::factory('incident_person')->where('incident_id', $this->id)->delete_all(); // Delete relationship to SMS message $updatemessage = ORM::factory('message')->where('incident_id', $this->id)->find(); if ($updatemessage->loaded) { $updatemessage->incident_id = 0; $updatemessage->save(); } // Delete Comments ORM::factory('comment')->where('incident_id', $this->id)->delete_all(); // Delete ratings ORM::factory('rating')->where('incident_id', $this->id)->delete_all(); $incident_id = $this->id; // Action::report_delete - Deleted a Report Event::run('ushahidi_action.report_delete', $incident_id); parent::delete(); }
/** * 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, 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)); //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); // 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[0,23]'); //$post->add_rules('incident_minute','required','between[0,59]'); if ($this->api_service->verify_array_index($_POST, 'incident_ampm')) { if ($_POST['incident_ampm'] != "am" and $_POST['incident_ampm'] != "pm") { $post->add_error('incident_ampm', '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]'); $post->add_rules('incident_category', 'required', 'length[1,100]'); // 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()) { // SAVE LOCATION (***IF IT DOES NOT EXIST***) $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(); // SAVE INCIDENT $incident = new Incident_Model(); $incident->location_id = $location->id; $incident->user_id = 0; $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)); $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->save(); // SAVE CATEGORIES // Check if data is csv or a single value. $pos = strpos($post->incident_category, ","); if ($pos === false) { //for backward compactibility. will drop support for it in the future. if (@unserialize($post->incident_category)) { $categories = unserialize($post->incident_category); } else { $categories = array($post->incident_category); } } else { $categories = explode(",", $post->incident_category); } if (!empty($categories) and is_array($categories)) { foreach ($categories 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 if (!empty($post->incident_news) and is_array($post->incident_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 if (!empty($post->incident_video) and is_array($post->incident_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 if (!empty($post->incident_photo)) { $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++; } } // SAVE PERSONAL INFORMATION IF ITS FILLED UP if (!empty($post->person_first) || !empty($post->person_last) || !empty($post->person_email)) { $person = new Incident_Person_Model(); $person->location_id = $location->id; $person->incident_id = $incident->id; if (!empty($post->person_first)) { $person->person_first = $post->person_first; } if (!empty($post->person_last)) { $person->person_last = $post->person_last; } if (!empty($post->person_email)) { $person->person_email = $post->person_email; } $person->person_date = date("Y-m-d H:i:s", time()); $person->save(); } // Action::report_edit_api - Edited a Report Event::run('ushahidi_action.report_edit_api', $incident); return 0; //success } 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. } }
/** * 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(); }
/** * Site Settings */ function site() { $this->template->content = new View('admin/site'); $this->template->content->title = Kohana::lang('ui_admin.settings'); $this->template->js = new View('admin/site_js'); // setup and initialize form field names $form = array('site_name' => '', 'site_tagline' => '', 'banner_image' => '', 'delete_banner_image' => '', 'site_email' => '', 'alerts_email' => '', 'site_language' => '', 'site_timezone' => '', 'site_message' => '', 'site_copyright_statement' => '', 'site_submit_report_message' => '', 'site_contact_page' => '', 'items_per_page' => '', 'items_per_page_admin' => '', 'blocks_per_row' => '', 'allow_reports' => '', 'allow_comments' => '', 'allow_feed' => '', 'allow_stat_sharing' => '', 'allow_clustering' => '', 'cache_pages' => '', 'cache_pages_lifetime' => '', 'private_deployment' => '', 'checkins' => '', 'default_map_all' => '', 'google_analytics' => '', 'twitter_hashtags' => '', 'api_akismet' => ''); // 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 = FALSE; // check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite $_POST // fields with our own things $post = new Validation($_POST); // Add some filters $post->pre_filter('trim', TRUE); // Add some rules, the input field, followed by a list of checks, carried out in order $post->add_rules('site_name', 'required', 'length[3,50]'); $post->add_rules('site_tagline', 'length[3,100]'); $post->add_rules('site_email', 'email', 'length[4,100]'); $post->add_rules('alerts_email', 'email', 'length[4,100]'); //$post->add_rules('site_message', 'standard_text'); $post->add_rules('site_copyright_statement', 'length[4,600]'); $post->add_rules('site_language', 'required', 'length[5, 5]'); //$post->add_rules('site_timezone','required', 'between[10,50]'); $post->add_rules('site_contact_page', 'required', 'between[0,1]'); $post->add_rules('items_per_page', 'required', 'between[10,50]'); $post->add_rules('items_per_page_admin', 'required', 'between[10,50]'); $post->add_rules('blocks_per_row', 'required', 'numeric'); $post->add_rules('allow_reports', 'required', 'between[0,1]'); $post->add_rules('allow_comments', 'required', 'between[0,2]'); $post->add_rules('allow_feed', 'required', 'between[0,1]'); $post->add_rules('allow_stat_sharing', 'required', 'between[0,1]'); $post->add_rules('allow_clustering', 'required', 'between[0,1]'); $post->add_rules('cache_pages', 'required', 'between[0,1]'); $post->add_rules('cache_pages_lifetime', 'required', 'in_array[300,600,900,1800]'); $post->add_rules('private_deployment', 'required', 'between[0,1]'); $post->add_rules('checkins', 'required', 'between[0,1]'); $post->add_rules('default_map_all', 'required', 'alpha_numeric', 'length[6,6]'); $post->add_rules('google_analytics', 'length[0,20]'); $post->add_rules('twitter_hashtags', 'length[0,500]'); $post->add_rules('api_akismet', 'length[0,100]', 'alpha_numeric'); // Add rules for file upload $files = Validation::factory($_FILES); $files->add_rules('banner_image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[250K]'); // Test to see if things passed the rule checks if ($post->validate() and $files->validate()) { // Yes! everything is valid $settings = new Settings_Model(1); $settings->site_name = $post->site_name; $settings->site_tagline = $post->site_tagline; $settings->site_email = $post->site_email; $settings->alerts_email = $post->alerts_email; $settings->site_message = $post->site_message; $settings->site_copyright_statement = $post->site_copyright_statement; $settings->site_submit_report_message = $post->site_submit_report_message; $settings->site_language = $post->site_language; $settings->site_timezone = $post->site_timezone; if ($settings->site_timezone == "0") { // "0" is the "Server Timezone" setting and it needs to be null in the db $settings->site_timezone = NULL; } $settings->site_contact_page = $post->site_contact_page; $settings->items_per_page = $post->items_per_page; $settings->items_per_page_admin = $post->items_per_page_admin; $settings->blocks_per_row = $post->blocks_per_row; $settings->allow_reports = $post->allow_reports; $settings->allow_comments = $post->allow_comments; $settings->allow_feed = $post->allow_feed; $settings->allow_stat_sharing = $post->allow_stat_sharing; $settings->allow_clustering = $post->allow_clustering; $settings->cache_pages = $post->cache_pages; $settings->cache_pages_lifetime = $post->cache_pages_lifetime; $settings->private_deployment = $post->private_deployment; $settings->checkins = $post->checkins; $settings->default_map_all = $post->default_map_all; $settings->google_analytics = $post->google_analytics; $settings->twitter_hashtags = $post->twitter_hashtags; $settings->api_akismet = $post->api_akismet; $settings->date_modify = date("Y-m-d H:i:s", time()); $settings->save(); // Deal with banner image now // Check if deleting or updating a new image (or doing nothing) if (isset($post->delete_banner_image) and $post->delete_banner_image == 1) { // Delete old badge image ORM::factory('media')->delete($settings->site_banner_id); // Remove from DB table $settings = new Settings_Model(1); $settings->site_banner_id = NULL; $settings->save(); } else { // We aren't deleting, so try to upload if we are uploading an image $filename = upload::save('banner_image'); if ($filename) { $new_filename = "banner"; $file_type = strrev(substr(strrev($filename), 0, 4)); // Large size $l_name = $new_filename . $file_type; Image::factory($filename)->save(Kohana::config('upload.directory', TRUE) . $l_name); // Medium size $m_name = $new_filename . "_m" . $file_type; Image::factory($filename)->resize(80, 80, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $m_name); // Thumbnail $t_name = $new_filename . "_t" . $file_type; Image::factory($filename)->resize(60, 60, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $t_name); // Remove the temporary file unlink($filename); // Save banner image in the media table $media = new Media_Model(); $media->media_type = 1; // Image $media->media_link = $l_name; $media->media_medium = $m_name; $media->media_thumb = $t_name; $media->media_date = date("Y-m-d H:i:s", time()); $media->save(); // Save new banner image in settings $settings = new Settings_Model(1); $settings->site_banner_id = $media->id; $settings->save(); } } // Delete Settings Cache $this->cache->delete('settings'); $this->cache->delete_tag('settings'); // Everything is A-Okay! $form_saved = TRUE; // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any if (is_array($files->errors()) and count($files->errors()) > 0) { // Error with file upload $errors = arr::overwrite($errors, $files->errors('settings')); } else { // Error with other form filed $errors = arr::overwrite($errors, $post->errors('settings')); } $form_error = TRUE; } } else { // Retrieve Current Settings $settings = ORM::factory('settings', 1); $form = array('site_name' => $settings->site_name, 'site_tagline' => $settings->site_tagline, 'site_banner_id' => $settings->site_banner_id, 'site_email' => $settings->site_email, 'alerts_email' => $settings->alerts_email, 'site_message' => $settings->site_message, 'site_copyright_statement' => $settings->site_copyright_statement, 'site_submit_report_message' => $settings->site_submit_report_message, 'site_language' => $settings->site_language, 'site_timezone' => $settings->site_timezone, 'site_contact_page' => $settings->site_contact_page, 'items_per_page' => $settings->items_per_page, 'items_per_page_admin' => $settings->items_per_page_admin, 'blocks_per_row' => $settings->blocks_per_row, 'allow_reports' => $settings->allow_reports, 'allow_comments' => $settings->allow_comments, 'allow_feed' => $settings->allow_feed, 'allow_stat_sharing' => $settings->allow_stat_sharing, 'allow_clustering' => $settings->allow_clustering, 'cache_pages' => $settings->cache_pages, 'cache_pages_lifetime' => $settings->cache_pages_lifetime, 'private_deployment' => $settings->private_deployment, 'checkins' => $settings->checkins, 'default_map_all' => $settings->default_map_all, 'google_analytics' => $settings->google_analytics, 'twitter_hashtags' => $settings->twitter_hashtags, 'api_akismet' => $settings->api_akismet); } // Get banner image if ($settings->site_banner_id != NULL) { $banner = ORM::factory('media')->find($settings->site_banner_id); $this->template->content->banner = $banner->media_link; $this->template->content->banner_m = $banner->media_medium; $this->template->content->banner_t = $banner->media_thumb; } else { $this->template->content->banner = NULL; $this->template->content->banner_m = NULL; $this->template->content->banner_t = NULL; } $this->template->colorpicker_enabled = TRUE; $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->content->items_per_page_array = array('10' => '10 Items', '20' => '20 Items', '30' => '30 Items', '50' => '50 Items'); $blocks_per_row_array = array(); for ($i = 1; $i <= 21; $i++) { $blocks_per_row_array[$i] = $i; } $this->template->content->blocks_per_row_array = $blocks_per_row_array; $this->template->content->yesno_array = array('1' => strtoupper(Kohana::lang('ui_main.yes')), '0' => strtoupper(Kohana::lang('ui_main.no'))); $this->template->content->comments_array = array('1' => strtoupper(Kohana::lang('ui_main.yes') . " - " . Kohana::lang('ui_admin.approve_auto')), '2' => strtoupper(Kohana::lang('ui_main.yes') . " - " . Kohana::lang('ui_admin.approve_manual')), '0' => strtoupper(Kohana::lang('ui_main.no'))); $this->template->content->cache_pages_lifetime_array = array('300' => '5 ' . Kohana::lang('ui_admin.minutes'), '600' => '10 ' . Kohana::lang('ui_admin.minutes'), '900' => '15 ' . Kohana::lang('ui_admin.minutes'), '1800' => '30 ' . Kohana::lang('ui_admin.minutes')); //Generate all timezones $site_timezone_array = array(); $site_timezone_array[0] = Kohana::lang('ui_admin.server_time'); foreach (timezone_identifiers_list() as $timezone) { $site_timezone_array[$timezone] = $timezone; } $this->template->content->site_timezone_array = $site_timezone_array; // Generate Available Locales $locales = locale::get_i18n(); $this->template->content->locales_array = $locales; $this->cache->set('locales', $locales, array('locales'), 604800); }
/** * 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' => '', 'incident_source' => '', 'incident_information' => ''); $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 = 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('location_id', 'numeric'); $post->add_rules('incident_id', 'required', '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[0,23]'); if ($this->api_service->verify_array_index($_POST, 'incident_ampm')) { if ($_POST['incident_ampm'] != "am" && $_POST['incident_ampm'] != "pm") { $post->add_error('incident_ampm', '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]'); $post->add_rules('incident_category', 'required', 'length[1,100]'); // 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]'); } $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()) { $incident_id = $post->incident_id; $location_id = $post->location_id; // SAVE INCIDENT // SAVE LOCATION (***IF IT DOES NOT EXIST***) $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(); $incident = new Incident_Model($incident_id); $incident->location_id = $location->id; $incident->user_id = 0; $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)); $incident->incident_datemodify = date("Y-m-d H:i:s", time()); // 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->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(); // SAVE CATEGORIES //check if data is csv or a single value. $pos = strpos($post->incident_category, ","); if ($pos === false) { //for backward compactibility. will drop support for it in the future. if (@unserialize($post->incident_category)) { $categories = unserialize($post->incident_category); } else { $categories = array($post->incident_category); } } else { $categories = explode(",", $post->incident_category); } if (!empty($categories) and is_array($categories)) { // STEP 3: SAVE CATEGORIES ORM::factory('Incident_Category')->where('incident_id', $incident->id)->delete_all(); // Delete Previous Entries foreach ($categories 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 if (!empty($post->incident_news) && is_array($post->incident_news)) { ORM::factory('Media')->where('incident_id', $incident->id)->where('media_type <> 1')->delete_all(); // Delete Previous Entries 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 if (!empty($post->incident_video) && is_array($post->incident_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 if (!empty($post->incident_photo)) { $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++; } } // SAVE PERSONAL INFORMATION IF ITS FILLED UP if (!empty($post->person_first) or !empty($post->person_last)) { ORM::factory('Incident_Person')->where('incident_id', $incident->id)->delete_all(); $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(); } return $this->response(0); //success } 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); } }
/** * parse feed and send feed items to database */ public function index() { // Max number of feeds to keep $max_items = 100; // Today's Date $today = strtotime('now'); // Get All Feeds From DB $feeds = ORM::factory('feed')->like('feed_name', 'TED')->find_all(); foreach ($feeds as $feed) { // Get Feed Items with location but no incident yet $feed_items = ORM::factory('feed_item')->where(array('feed_id' => $feed->id, 'location_id !=' => 0, 'incident_id' => 0))->find_all(); foreach ($feed_items as $feed_item) { //echo $feed_item->item_title; $incident = new Incident_Model(); $incident->incident_title = $feed_item->item_title; $incident->incident_description = $feed_item->item_description; $incident->incident_date = $feed_item->item_date; $incident->location_id = $feed_item->location_id; $incident->incident_active = true; $incident->incident_verified = true; if (strpos($feed_item->item_link, 'youtube') !== FALSE) { $id = str_replace(array('http://www.youtube.com/watch?v=', '&feature=youtube_gdata'), '', $feed_item->item_link); // Get extra details from youtube api $json = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$id}?v=2&alt=jsonc"); if ($json !== FALSE) { $json = json_decode($json); $thumb = $json->data->thumbnail->hqDefault; if (!$incident->incident_description) { $incident->incident_description = $json->data->description; } foreach ($json->data->tags as $tag) { if (stripos($tag, 'tedx') !== FALSE && strtolower($tag) != 'tedx') { $cat = $tag; break; } } } $incident->save(); $feed_item->incident_id = $incident->id; $feed_item->save(); // Add video $video = new Media_Model(); $video->location_id = $incident->location_id; $video->incident_id = $incident->id; $video->media_type = 2; // Video $video->media_link = $feed_item->item_link; $video->media_thumb = isset($thumb) ? $thumb : ''; $video->media_medium = isset($thumb) ? $thumb : ''; $video->media_date = $feed_item->item_date; $video->save(); // News Link $news = new Media_Model(); $news->location_id = $incident->location_id; $news->incident_id = $incident->id; $news->media_type = 4; // News $news->media_link = $feed_item->item_link; $news->media_date = $feed_item->item_date; $news->save(); // Category if (!empty($cat)) { $db = Database::instance(); $result = $db->query("SELECT `category`.`id` FROM `category` WHERE lower(`category_title`) = ? ORDER BY `category`.`category_position` ASC LIMIT 0, 1", strtolower($cat)); if ($row = $result->current()) { $category_id = $row->id; } else { $category = new Category_Model(); $category->category_title = $cat; // We'll just use blue since its tedx $category->category_color = '002bff'; // because all current categories are of type '5' $category->category_type = 5; $category->category_visible = 1; $category->category_description = $cat; $category->parent_id = 156; // TEDX $category->save(); $category_id = $category->id; } $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $category_id; $incident_category->save(); } } } } }