/** * Adds reports in JSON format to the database * @param string $data - CF JSON results */ private function add_reports($data) { $reports = json_decode($data, false); foreach ($reports as $report) { //Save the Report location $location = new Location_Model(); $location->longitude = $report->{'longitude'}; $location->latitude = $report->{'latitude'}; $location->location_name = $report->{'location_city'}; $location->save(); // Save CF result as Report $incident = new Incident_Model(); $incident->location_id = $location->id; // $incident->id = $report->{'id'}; $incident->incident_title = date("Y-m-d H:i:s", time()); $incident->incident_description = $report->{'sms_text'}; $incident->incident_date = date("Y-m-d H:i:s", time()); $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->incident_active = 1; $incident->incident_verified = 1; $incident->save(); // Save Incident Category $categories = explode(",", $report->{'categories'}); foreach ($categories as $category) { $report_category_id = ORM::factory("category")->where("category_title", $category)->find(); if ($report_category_id->loaded) { $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $report_category_id->id; $incident_category->save(); } } } }
function __construct() { $this->conf = array('model' => 'Shift_model', 'path' => 'admin/shifts', 'entity' => 'shift', 'after_save' => 'shift'); parent::__construct(User_model::LEVEL_MANAGER); /* check how many locations do we have */ $lm = new Location_Model(); $location_count = $lm->count(); $this->data['location_count'] = $location_count; }
public function view_text($skip = array()) { $return = parent::view_text($skip); unset($return['start']); unset($return['end']); unset($return['status']); $return['date'][1] = $this->date_view($skip); /* optimize it sometime later */ $lm = new Location_Model(); $location_count = $lm->count(); if ($location_count < 2) { unset($return['location']); } return $return; }
/** * Get a list of locations * * @param array $where Key->value array of the set of filters to apply * @return string JSON/XML string with the location data */ private function _get_locations($where = array()) { // Fetch the location items $items = Location_Model::get_locations($where, $this->list_limit); //No record found. if ($items->count() == 0) { return $this->response(4); } // Counter $i = 0; // To hold the json data $json_locations = array(); foreach ($items as $item) { // Needs different treatment depending on the output if ($this->response_type == 'json') { $json_locations[] = array("location" => $item); } else { $json_locations['location' . $i] = array("location" => $item); $this->replar[] = 'location' . $i; } $i++; } // Array to be converted to either JSON or xml $data = array("payload" => array("domain" => $this->domain, "locations" => $json_locations), "error" => $this->api_service->get_error_msg(0)); return $this->response_type == 'json' ? $this->array_as_json($data) : $this->array_as_xml($data, $this->replar); }
/** * 单例模式 * @return Location_Model */ public static function &instance() { if (!isset(self::$instance)) { // Create a new instance self::$instance = new Location_Model(); } return self::$instance; }
function shifts() { if ($this->staff_id) { $um = new User_Model(); $um->get_by_id($this->staff_id); $sm = $um->shift; } elseif ($this->location_id) { $lm = new Location_Model(); $lm->get_by_id($this->location_id); $sm = $lm->shift; } else { $sm = new Shift_Model(); } if ($this->start) { $sm->where('date >=', $this->start); } if ($this->end) { $sm->where('date <=', $this->end); } $sm->order_by('date', 'ASC')->order_by('start', 'ASC')->include_related('user', 'id'); return $sm->get(); }
/** * 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']; } }
/** * 过滤输入、创建群联系人对象 * @param array $data 联系人信息 * @return Group_Contact $contact */ public function array_to_Group_contact($data) { $contact = new Group_Contact(); $location_model = Location_Model::instance(); $bjx_arr = Kohana::config_load('bjx'); foreach ($data as $type => $value) { switch ($type) { case 'tels': if (!empty($value)) { $values = $tmp = array(); foreach ($value as $val) { if (!in_array(trim($val['value']), $tmp)) { $tmp[] = trim($val['value']); $values[] = array('value' => trim($val['value']), 'type' => $val['type'], 'city' => $location_model->get_tel_location(trim($val['value'])), 'pref' => !empty($val['pref']) ? (int) $val['pref'] : 0); } } call_user_func(array($contact, 'set_' . $type), $values); } break; case 'ims': if (!empty($value)) { $values = $tmp = $protocols = array(); foreach ($value as $val) { $val['protocol'] = strtolower($val['protocol']); $keys = array_keys($tmp, trim($val['value'])); $key = isset($keys[0]) ? $keys[0] : -1; if ($key < 0 or $protocols[$key] != $val['protocol']) { $tmp[] = trim($val['value']); $protocols[] = $val['protocol']; $values[] = array('value' => trim($val['value']), 'protocol' => $val['protocol'], 'type' => $val['type']); } } call_user_func(array($contact, 'set_' . $type), $values); } break; case 'addresses': if (!empty($value)) { $values = $tmp = array(); $t = ''; foreach ($value as $val) { $t = trim($val['country']) . '|' . trim($val['region']) . '|' . trim($val['city']) . '|' . trim($val['street']) . '|' . trim($val['postal']); if (!in_array($t, $tmp)) { $values[] = array('country' => trim($val['country']), 'region' => trim($val['region']), 'city' => trim($val['city']), 'street' => trim($val['street']), 'postal' => trim($val['postal']), 'type' => $val['type']); $tmp[] = $t; } } call_user_func(array($contact, 'set_' . $type), $values); } break; case 'emails': case 'urls': case 'events': case 'relations': if (!empty($value)) { $values = $tmp = array(); foreach ($value as $val) { if (!in_array(trim($val['value']), $tmp)) { $tmp[] = trim($val['value']); $values[] = array('value' => trim($val['value']), 'type' => $val['type']); } } call_user_func(array($contact, 'set_' . $type), $values); } break; case 'birthday': $contactModel = Contact_Model::instance(); call_user_func(array($contact, 'set_' . $type), !empty($value) ? $contactModel->_filter_birthday($value) : ''); break; case 'id': break; default: call_user_func(array($contact, 'set_' . $type), !empty($value) ? $value : ''); break; } } $formatted_name = $this->name_to_formatted_name($data['family_name'], $data['given_name']); //拼接后的全名为空,并且输入的全名不是空的,把全名拆分设置 if (empty($formatted_name) and !empty($data['formatted_name'])) { $name = $this->formatted_name_to_name($data['formatted_name']); $contact->set_given_name($name['given_name']); $contact->set_family_name($name['family_name']); } else { $fn = $formatted_name; } if (!empty($fn)) { require_once Kohana::find_file('vendor', 'pinyin/c2p'); $phonetic = getPinYin($fn, false, ' '); $tmp = explode(' ', $phonetic); $sort = ''; if (is_array($tmp)) { foreach ($tmp as $t) { $sort .= isset($t[0]) ? $t[0] : ''; } } $t = ord($sort[0]); if (empty($sort) or $t < 97 or $t > 122) { $sort = '#'; } $sort = substr($sort, 0, 20); $contact->set_formatted_name($fn); $contact->set_phonetic(implode('', $tmp)); $contact->set_sort($sort); } else { $contact->set_formatted_name(''); $contact->set_phonetic(''); $contact->set_sort('#'); } return $contact; }
/** * parse feed and send feed items to database */ private function _parse_feed() { // Max number of feeds to keep $max_feeds = 100; // Today's Date $today = strtotime('now'); // Get All Feeds From DB $feeds = ORM::factory('feed')->find_all(); foreach ($feeds as $feed) { $last_update = $feed->feed_update; // Has it been more than 24 hours since the last update? // Since its a manual refresh, we don't need to set a time if ((int) $today - (int) $last_update > 0) { // Parse Feed URL using Feed Helper $feed_data = feed::simplepie($feed->feed_url); foreach ($feed_data->get_items(0, 50) as $feed_data_item) { $title = $feed_data_item->get_title(); $link = $feed_data_item->get_link(); $description = $feed_data_item->get_description(); $date = $feed_data_item->get_date(); $latitude = $feed_data_item->get_latitude(); $longitude = $feed_data_item->get_longitude(); // Make Sure Title is Set (Atleast) if (isset($title) && !empty($title)) { // We need to check for duplicates!!! // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( ) $dupe_count = ORM::factory('feed_item')->where('item_title', $title)->where('item_date', date("Y-m-d H:i:s", strtotime($date)))->count_all(); if ($dupe_count == 0) { // Does this feed have a location?? $location_id = 0; // STEP 1: SAVE LOCATION if ($latitude && $longitude) { $location = new Location_Model(); $location->location_name = Kohana::lang('ui_admin.unknown'); $location->latitude = $latitude; $location->longitude = $longitude; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); $location_id = $location->id; } $newitem = new Feed_Item_Model(); $newitem->feed_id = $feed->id; $newitem->location_id = $location_id; $newitem->item_title = $title; if (isset($description) && !empty($description)) { $newitem->item_description = $description; } if (isset($link) && !empty($link)) { $newitem->item_link = $link; } if (isset($date) && !empty($date)) { $newitem->item_date = date("Y-m-d H:i:s", strtotime($date)); } else { $newitem->item_date = date("Y-m-d H:i:s", time()); } if (isset($feed_type) && !empty($feed_type)) { $newitem->feed_type = $feed_type; } $newitem->save(); } } } // Get Feed Item Count $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all(); if ($feed_count > $max_feeds) { // Excess Feeds $feed_excess = $feed_count - $max_feeds; // Delete Excess Feeds foreach (ORM::factory('feed_item')->where('feed_id', $feed->id)->orderby('id', 'ASC')->limit($feed_excess)->find_all() as $del_feed) { $del_feed->delete($del_feed->id); } } // Set feed update date $feed->feed_update = strtotime('now'); $feed->save(); } } }
/** * Edit a report * @param bool|int $id The id no. of the report * @param bool|string $saved */ function edit($id = false, $saved = false) { $this->template->content = new View('admin/reports_edit'); $this->template->content->title = Kohana::lang('ui_admin.create_report'); // setup and initialize form field names $form = array('location_id' => '', 'form_id' => '', 'locale' => '', 'incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'location_name' => '', 'country_id' => '', 'incident_category' => array(), 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => '', 'custom_field' => array(), 'incident_active' => '', 'incident_verified' => '', 'incident_source' => '', 'incident_information' => ''); // copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; if ($saved == 'saved') { $form_saved = TRUE; } else { $form_saved = FALSE; } // Initialize Default Values $form['locale'] = Kohana::config('locale.language'); //$form['latitude'] = Kohana::config('settings.default_lat'); //$form['longitude'] = Kohana::config('settings.default_lon'); $form['country_id'] = Kohana::config('settings.default_country'); $form['incident_date'] = date("m/d/Y", time()); $form['incident_hour'] = date('g'); $form['incident_minute'] = date('i'); $form['incident_ampm'] = date('a'); // initialize custom field array $form['custom_field'] = $this->_get_custom_form_fields($id, '', true); // Locale (Language) Array $this->template->content->locale_array = Kohana::config('locale.all_languages'); // Create Categories $this->template->content->categories = $this->_get_categories(); $this->template->content->new_categories_form = $this->_new_categories_form_arr(); // Time formatting $this->template->content->hour_array = $this->_hour_array(); $this->template->content->minute_array = $this->_minute_array(); $this->template->content->ampm_array = $this->_ampm_array(); // Get Countries $countries = array(); foreach (ORM::factory('country')->orderby('country')->find_all() as $country) { // Create a list of all categories $this_country = $country->country; if (strlen($this_country) > 35) { $this_country = substr($this_country, 0, 35) . "..."; } $countries[$country->id] = $this_country; } $this->template->content->countries = $countries; //GET custom forms $forms = array(); foreach (ORM::factory('form')->find_all() as $custom_forms) { $forms[$custom_forms->id] = $custom_forms->form_title; } $this->template->content->forms = $forms; // Retrieve thumbnail photos (if edit); //XXX: fix _get_thumbnails $this->template->content->incident = $this->_get_thumbnails($id); // Are we creating this report from SMS/Email/Twitter? // If so retrieve message if (isset($_GET['mid']) && !empty($_GET['mid'])) { $message_id = $_GET['mid']; $service_id = ""; $message = ORM::factory('message', $message_id); if ($message->loaded == true && $message->message_type == 1) { $service_id = $message->reporter->service_id; // Has a report already been created for this Message? if ($message->incident_id != 0) { // Redirect to report url::redirect('admin/reports/edit/' . $message->incident_id); } $this->template->content->show_messages = true; $incident_description = $message->message; if (!empty($message->message_detail)) { $incident_description .= "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" . $message->message_detail; } $form['incident_description'] = $incident_description; $form['incident_date'] = date('m/d/Y', strtotime($message->message_date)); $form['incident_hour'] = date('h', strtotime($message->message_date)); $form['incident_minute'] = date('i', strtotime($message->message_date)); $form['incident_ampm'] = date('a', strtotime($message->message_date)); $form['person_first'] = $message->reporter->reporter_first; $form['person_last'] = $message->reporter->reporter_last; // Retrieve Last 5 Messages From this account $this->template->content->all_messages = ORM::factory('message')->where('reporter_id', $message->reporter_id)->orderby('message_date', 'desc')->limit(5)->find_all(); } else { $message_id = ""; $this->template->content->show_messages = false; } } else { $this->template->content->show_messages = false; } // Are we creating this report from a Newsfeed? if (isset($_GET['fid']) && !empty($_GET['fid'])) { $feed_item_id = $_GET['fid']; $feed_item = ORM::factory('feed_item', $feed_item_id); if ($feed_item->loaded == true) { // Has a report already been created for this Feed item? if ($feed_item->incident_id != 0) { // Redirect to report url::redirect('admin/reports/edit/' . $feed_item->incident_id); } $form['incident_title'] = $feed_item->item_title; $form['incident_description'] = $feed_item->item_description; $form['incident_date'] = date('m/d/Y', strtotime($feed_item->item_date)); $form['incident_hour'] = date('h', strtotime($feed_item->item_date)); $form['incident_minute'] = date('i', strtotime($feed_item->item_date)); $form['incident_ampm'] = date('a', strtotime($feed_item->item_date)); // News Link $form['incident_news'][0] = $feed_item->item_link; // Does this newsfeed have a geolocation? if ($feed_item->location_id) { $form['location_id'] = $feed_item->location_id; $form['latitude'] = $feed_item->location->latitude; $form['longitude'] = $feed_item->location->longitude; $form['location_name'] = $feed_item->location->location_name; } } else { $feed_item_id = ""; } } // check, has the form been submitted, if so, setup validation if ($_POST) { // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things $post = Validation::factory(array_merge($_POST, $_FILES)); // Add some filters $post->pre_filter('trim', TRUE); // Add some rules, the input field, followed by a list of checks, carried out in order // $post->add_rules('locale','required','alpha_dash','length[5]'); $post->add_rules('location_id', 'numeric'); $post->add_rules('message_id', 'numeric'); $post->add_rules('incident_title', 'required', 'length[3,200]'); $post->add_rules('incident_description', 'required'); $post->add_rules('incident_date', 'required', 'date_mmddyyyy'); $post->add_rules('incident_hour', 'required', 'between[1,12]'); $post->add_rules('incident_minute', 'required', 'between[0,59]'); if ($_POST['incident_ampm'] != "am" && $_POST['incident_ampm'] != "pm") { $post->add_error('incident_ampm', 'values'); } $post->add_rules('latitude', 'required', 'between[-90,90]'); // Validate for maximum and minimum latitude values $post->add_rules('longitude', 'required', 'between[-180,180]'); // Validate for maximum and minimum longitude values $post->add_rules('location_name', 'required', 'length[3,200]'); //XXX: Hack to validate for no checkboxes checked if (!isset($_POST['incident_category'])) { $post->incident_category = ""; $post->add_error('incident_category', 'required'); } else { $post->add_rules('incident_category.*', 'required', 'numeric'); } // Validate only the fields that are filled in if (!empty($_POST['incident_news'])) { foreach ($_POST['incident_news'] as $key => $url) { if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { $post->add_error('incident_news', 'url'); } } } // Validate only the fields that are filled in if (!empty($_POST['incident_video'])) { foreach ($_POST['incident_video'] as $key => $url) { if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { $post->add_error('incident_video', 'url'); } } } // Validate photo uploads $post->add_rules('incident_photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[2M]'); // Validate Personal Information if (!empty($_POST['person_first'])) { $post->add_rules('person_first', 'length[3,100]'); } if (!empty($_POST['person_last'])) { $post->add_rules('person_last', 'length[3,100]'); } if (!empty($_POST['person_email'])) { $post->add_rules('person_email', 'email', 'length[3,100]'); } // Validate Custom Fields if (isset($post->custom_field) && !$this->_validate_custom_form_fields($post->custom_field)) { $post->add_error('custom_field', 'values'); } $post->add_rules('incident_active', 'required', 'between[0,1]'); $post->add_rules('incident_verified', 'required', 'length[0,1]'); $post->add_rules('incident_source', 'numeric', 'length[1,1]'); $post->add_rules('incident_information', 'numeric', 'length[1,1]'); // Test to see if things passed the rule checks if ($post->validate()) { // Yes! everything is valid $location_id = $post->location_id; // STEP 1: SAVE LOCATION $location = new Location_Model($location_id); $location->location_name = $post->location_name; $location->latitude = $post->latitude; $location->longitude = $post->longitude; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); // STEP 2: SAVE INCIDENT $incident = new Incident_Model($id); $incident->location_id = $location->id; //$incident->locale = $post->locale; $incident->form_id = $post->form_id; $incident->user_id = $_SESSION['auth_user']->id; $incident->incident_title = $post->incident_title; $incident->incident_description = $post->incident_description; $incident_date = explode("/", $post->incident_date); // where the $_POST['date'] is a value posted by form in mm/dd/yyyy format $incident_date = $incident_date[2] . "-" . $incident_date[0] . "-" . $incident_date[1]; $incident_time = $post->incident_hour . ":" . $post->incident_minute . ":00 " . $post->incident_ampm; $incident->incident_date = date("Y-m-d H:i:s", strtotime($incident_date . " " . $incident_time)); // Is this new or edit? if ($id) { $incident->incident_datemodify = date("Y-m-d H:i:s", time()); } else { $incident->incident_dateadd = date("Y-m-d H:i:s", time()); } // Is this an Email, SMS, Twitter submitted report? //XXX: We may get rid of incident_mode altogether... ??? //$_POST if (!empty($service_id)) { if ($service_id == 1) { // SMS $incident->incident_mode = 2; } elseif ($service_id == 2) { // Email $incident->incident_mode = 3; } elseif ($service_id == 3) { // Twitter $incident->incident_mode = 4; } elseif ($service_id == 4) { // Laconica $incident->incident_mode = 5; } } // Incident Evaluation Info $incident->incident_active = $post->incident_active; $incident->incident_verified = $post->incident_verified; $incident->incident_source = $post->incident_source; $incident->incident_information = $post->incident_information; //Save $incident->save(); // Record Approval/Verification Action $verify = new Verify_Model(); $verify->incident_id = $incident->id; $verify->user_id = $_SESSION['auth_user']->id; // Record 'Verified By' Action $verify->verified_date = date("Y-m-d H:i:s", time()); if ($post->incident_active == 1) { $verify->verified_status = '1'; } elseif ($post->incident_verified == 1) { $verify->verified_status = '2'; } elseif ($post->incident_active == 1 && $post->incident_verified == 1) { $verify->verified_status = '3'; } else { $verify->verified_status = '0'; } $verify->save(); // STEP 3: SAVE CATEGORIES ORM::factory('Incident_Category')->where('incident_id', $incident->id)->delete_all(); // Delete Previous Entries foreach ($post->incident_category as $item) { $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $item; $incident_category->save(); } // STEP 4: SAVE MEDIA ORM::factory('Media')->where('incident_id', $incident->id)->where('media_type <> 1')->delete_all(); // Delete Previous Entries // a. News foreach ($post->incident_news as $item) { if (!empty($item)) { $news = new Media_Model(); $news->location_id = $location->id; $news->incident_id = $incident->id; $news->media_type = 4; // News $news->media_link = $item; $news->media_date = date("Y-m-d H:i:s", time()); $news->save(); } } // b. Video foreach ($post->incident_video as $item) { if (!empty($item)) { $video = new Media_Model(); $video->location_id = $location->id; $video->incident_id = $incident->id; $video->media_type = 2; // Video $video->media_link = $item; $video->media_date = date("Y-m-d H:i:s", time()); $video->save(); } } // c. Photos $filenames = upload::save('incident_photo'); $i = 1; foreach ($filenames as $filename) { $new_filename = $incident->id . "_" . $i . "_" . time(); // Resize original file... make sure its max 408px wide Image::factory($filename)->resize(408, 248, Image::AUTO)->save(Kohana::config('upload.directory', TRUE) . $new_filename . ".jpg"); // Create thumbnail Image::factory($filename)->resize(70, 41, Image::HEIGHT)->save(Kohana::config('upload.directory', TRUE) . $new_filename . "_t.jpg"); // Remove the temporary file unlink($filename); // Save to DB $photo = new Media_Model(); $photo->location_id = $location->id; $photo->incident_id = $incident->id; $photo->media_type = 1; // Images $photo->media_link = $new_filename . ".jpg"; $photo->media_thumb = $new_filename . "_t.jpg"; $photo->media_date = date("Y-m-d H:i:s", time()); $photo->save(); $i++; } // STEP 5: SAVE PERSONAL INFORMATION ORM::factory('Incident_Person')->where('incident_id', $incident->id)->delete_all(); // Delete Previous Entries $person = new Incident_Person_Model(); $person->location_id = $location->id; $person->incident_id = $incident->id; $person->person_first = $post->person_first; $person->person_last = $post->person_last; $person->person_email = $post->person_email; $person->person_date = date("Y-m-d H:i:s", time()); $person->save(); // STEP 6a: SAVE LINK TO REPORTER MESSAGE // We're creating a report from a message with this option if (isset($message_id) && $message_id != "") { $savemessage = ORM::factory('message', $message_id); if ($savemessage->loaded == true) { $savemessage->incident_id = $incident->id; $savemessage->save(); } } // STEP 6b: SAVE LINK TO NEWS FEED // We're creating a report from a newsfeed with this option if (isset($feed_item_id) && $feed_item_id != "") { $savefeed = ORM::factory('feed_item', $feed_item_id); if ($savefeed->loaded == true) { $savefeed->incident_id = $incident->id; $savefeed->location_id = $location->id; $savefeed->save(); } } // STEP 7: SAVE CUSTOM FORM FIELDS if (isset($post->custom_field)) { foreach ($post->custom_field as $key => $value) { $form_response = ORM::factory('form_response')->where('form_field_id', $key)->where('incident_id', $incident->id)->find(); if ($form_response->loaded == true) { $form_response->form_field_id = $key; $form_response->form_response = $value; $form_response->save(); } else { $form_response = new Form_Response_Model(); $form_response->form_field_id = $key; $form_response->incident_id = $incident->id; $form_response->form_response = $value; $form_response->save(); } } } // SAVE AND CLOSE? if ($post->save == 1) { url::redirect('admin/reports/edit/' . $incident->id . '/saved'); } else { url::redirect('admin/reports/'); } } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('report')); $form_error = TRUE; } } else { if ($id) { // Retrieve Current Incident $incident = ORM::factory('incident', $id); if ($incident->loaded == true) { // Retrieve Categories $incident_category = array(); foreach ($incident->incident_category as $category) { $incident_category[] = $category->category_id; } // Retrieve Media $incident_news = array(); $incident_video = array(); $incident_photo = array(); foreach ($incident->media as $media) { if ($media->media_type == 4) { $incident_news[] = $media->media_link; } elseif ($media->media_type == 2) { $incident_video[] = $media->media_link; } elseif ($media->media_type == 1) { $incident_photo[] = $media->media_link; } } // Combine Everything $incident_arr = array('location_id' => $incident->location->id, 'form_id' => $incident->form_id, 'locale' => $incident->locale, 'incident_title' => $incident->incident_title, 'incident_description' => $incident->incident_description, 'incident_date' => date('m/d/Y', strtotime($incident->incident_date)), 'incident_hour' => date('h', strtotime($incident->incident_date)), 'incident_minute' => date('i', strtotime($incident->incident_date)), 'incident_ampm' => date('A', strtotime($incident->incident_date)), 'latitude' => $incident->location->latitude, 'longitude' => $incident->location->longitude, 'location_name' => $incident->location->location_name, 'country_id' => $incident->location->country_id, 'incident_category' => $incident_category, 'incident_news' => $incident_news, 'incident_video' => $incident_video, 'incident_photo' => $incident_photo, 'person_first' => $incident->incident_person->person_first, 'person_last' => $incident->incident_person->person_last, 'person_email' => $incident->incident_person->person_email, 'custom_field' => $this->_get_custom_form_fields($id, $incident->form_id, true), 'incident_active' => $incident->incident_active, 'incident_verified' => $incident->incident_verified, 'incident_source' => $incident->incident_source, 'incident_information' => $incident->incident_information); // Merge To Form Array For Display $form = arr::overwrite($form, $incident_arr); } else { // Redirect url::redirect('admin/reports/'); } } } $this->template->content->id = $id; $this->template->content->form = $form; $this->template->content->errors = $errors; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; // Retrieve Custom Form Fields Structure $disp_custom_fields = $this->_get_custom_form_fields($id, $form['form_id'], false); $this->template->content->disp_custom_fields = $disp_custom_fields; // Retrieve Previous & Next Records $previous = ORM::factory('incident')->where('id < ', $id)->orderby('id', 'desc')->find(); $previous_url = $previous->loaded ? url::base() . 'admin/reports/edit/' . $previous->id : url::base() . 'admin/reports/'; $next = ORM::factory('incident')->where('id > ', $id)->orderby('id', 'desc')->find(); $next_url = $next->loaded ? url::base() . 'admin/reports/edit/' . $next->id : url::base() . 'admin/reports/'; $this->template->content->previous_url = $previous_url; $this->template->content->next_url = $next_url; // Javascript Header $this->template->map_enabled = TRUE; $this->template->colorpicker_enabled = TRUE; $this->template->treeview_enabled = TRUE; $this->template->js = new View('admin/reports_edit_js'); $this->template->js->default_map = Kohana::config('settings.default_map'); $this->template->js->default_zoom = Kohana::config('settings.default_zoom'); if (!$form['latitude'] || !$form['latitude']) { $this->template->js->latitude = Kohana::config('settings.default_lat'); $this->template->js->longitude = Kohana::config('settings.default_lon'); } else { $this->template->js->latitude = $form['latitude']; $this->template->js->longitude = $form['longitude']; } // Inline Javascript $this->template->content->date_picker_js = $this->_date_picker_js(); $this->template->content->color_picker_js = $this->_color_picker_js(); $this->template->content->new_category_toggle_js = $this->_new_category_toggle_js(); }
/** * the actual reporting - ***must find a cleaner way to do this than duplicating code verbatim - modify report*** */ 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' => 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 $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)); // 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 ($this->_verifyArrayIndex($_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]'); // 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]'); $post->add_rules('incident_category', 'required', 'length[3,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 = 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; $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->save(); // SAVE CATEGORIES //check if data is array or a serialized data. if (is_array($post->incident_category)) { $categories = $post->incident_category; } else { $categories = unserialize($post->incident_category); } if (!empty($categories) && 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) && 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) && 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)) { $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 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_messages .= $error_description; if ($error_description != end($this->messages)) { $this->error_messages .= " - "; } } } //FAILED!!! return 1; //validation error } } else { return 2; // Not sent by post method. } }
function index() { if (isset($_GET['key'])) { $frontlinesms_key = $_GET['key']; } if (isset($_GET['s'])) { $message_from = $_GET['s']; // Remove non-numeric characters from string $message_from = ereg_replace("[^0-9]", "", $message_from); } if (isset($_GET['m'])) { $message_description = $_GET['m']; } if (!empty($frontlinesms_key) && !empty($message_from) && !empty($message_description)) { // Is this a valid FrontlineSMS Key? $keycheck = ORM::factory('settings', 1)->where('frontlinesms_key', $frontlinesms_key)->find(); if ($keycheck->loaded == true) { $services = new Service_Model(); $service = $services->where('service_name', 'SMS')->find(); if (!$service) { return; } $reporter_check = ORM::factory('reporter')->where('service_id', $service->id)->where('service_account', $message_from)->find(); if ($reporter_check->loaded == true) { $reporter_id = $reporter_check->id; } else { // get default reporter level (Untrusted) $levels = new Level_Model(); $default_level = $levels->where('level_weight', 0)->find(); $reporter = new Reporter_Model(); $reporter->service_id = $service->id; $reporter->service_userid = null; $reporter->service_account = $message_from; $reporter->reporter_level = $default_level; $reporter->reporter_first = null; $reporter->reporter_last = null; $reporter->reporter_email = null; $reporter->reporter_phone = null; $reporter->reporter_ip = null; $reporter->reporter_date = date('Y-m-d'); $reporter->save(); $reporter_id = $reporter->id; } $rest = $this->getsms($message_description); if ($rest['incidencia'] != '') { if ($coord = $this->gettextaddress($rest['localidad'] . ', mexico')) { // STEP 1: SAVE LOCATION $location = new Location_Model(''); $location->location_name = $coord['textaddress']; $location->country_id = 157; //Mexico $location->latitude = $coord['latitude']; $location->longitude = $coord['longitude']; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); $locationid = $location->id; } else { $locationid = 0; } // STEP 2: SAVE INCIDENT $incident = new Incident_Model(''); $incident->location_id = $locationid; //$incident->locale = $post->locale; $incident->form_id = 1; $incident->user_id = 0; $incident->incident_title = $rest['mensaje']; $incident->incident_description = $rest['mensaje']; $incident->incident_date = date("Y-m-d H:i:s", time()); $incident->incident_dateadd = date("Y-m-d H:i:s", time()); $incident->incident_mode = 2; // Incident Evaluation Info $incident->incident_active = 0; $incident->incident_verified = 0; $incident->incident_source = null; $incident->incident_information = null; //Save $incident->save(); $incidentid = $incident->id; // STEP 3: SAVE CATEGORIES $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $rest['incidencia']; $incident_category->save(); } else { $incidentid = 0; } // Save Message $message = new Message_Model(); $message->parent_id = 0; $message->incident_id = $incidentid; $message->user_id = 0; $message->reporter_id = $reporter_id; $message->message_from = $message_from; $message->message_to = null; $message->message = $message_description; $message->message_type = 1; // Inbox $message->message_date = date("Y-m-d H:i:s", time()); $message->service_messageid = null; $message->save(); } } }
/** * Function to import a report form a row in the CSV file * @param array $row * @return bool */ function importreport($row) { // If the date is not in proper date format if (!strtotime($row['INCIDENT DATE'])) { $this->errors[] = 'Could not parse incident date "' . htmlspecialchars($row['INCIDENT DATE']) . '" on line ' . ($this->rownumber + 1); } // If a value of Yes or No is NOT set for approval status for the imported row if (isset($row["APPROVED"]) and !in_array($row["APPROVED"], array('NO', 'YES'))) { $this->errors[] = 'APPROVED must be either YES or NO on line ' . ($this->rownumber + 1); } // If a value of Yes or No is NOT set for verified status for the imported row if (isset($row["VERIFIED"]) and !in_array($row["VERIFIED"], array('NO', 'YES'))) { $this->errors[] = 'VERIFIED must be either YES or NO on line ' . ($this->rownumber + 1); } if (count($this->errors)) { return false; } // STEP 1: SAVE LOCATION if (isset($row['LOCATION'])) { $location = new Location_Model(); $location->location_name = isset($row['LOCATION']) ? $row['LOCATION'] : ''; $location->latitude = isset($row['LATITUDE']) ? $row['LATITUDE'] : ''; $location->longitude = isset($row['LONGITUDE']) ? $row['LONGITUDE'] : ''; $location->location_date = $this->time; $location->save(); $this->locations_added[] = $location->id; } // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); $incident->location_id = isset($row['LOCATION']) ? $location->id : 0; $incident->user_id = 0; $incident->incident_title = $row['INCIDENT TITLE']; $incident->incident_description = isset($row['DESCRIPTION']) ? $row['DESCRIPTION'] : ''; $incident->incident_date = date("Y-m-d H:i:s", strtotime($row['INCIDENT DATE'])); $incident->incident_dateadd = $this->time; $incident->incident_active = (isset($row['APPROVED']) and $row['APPROVED'] == 'YES') ? 1 : 0; $incident->incident_verified = (isset($row['VERIFIED']) and $row['VERIFIED'] == 'YES') ? 1 : 0; $incident->save(); $this->incidents_added[] = $incident->id; // STEP 3: SAVE CATEGORIES // If CATEGORIES column exists if (isset($row['CATEGORY'])) { $categorynames = explode(',', trim($row['CATEGORY'])); // Add categories to incident foreach ($categorynames as $categoryname) { // There seems to be an uppercase convention for categories... Don't know why $categoryname = strtoupper(trim($categoryname)); // Empty categoryname not allowed if ($categoryname != '') { if (!isset($this->category_ids[$categoryname])) { $this->notices[] = 'There exists no category "' . htmlspecialchars($categoryname) . '" in database yet.' . ' Added to database.'; $category = new Category_Model(); $category->category_title = $categoryname; // We'll just use black for now. Maybe something random? $category->category_color = '000000'; // because all current categories are of type '5' $category->category_type = 5; $category->category_visible = 1; $category->category_description = $categoryname; $category->save(); $this->categories_added[] = $category->id; // Now category_id is known: This time, and for the rest of the import. $this->category_ids[$categoryname] = $category->id; } $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $this->category_ids[$categoryname]; $incident_category->save(); $this->incident_categories_added[] = $incident_category->id; } } } return true; }
/** * the actual reporting - ***must find a cleaner way to do this than duplicating code verbatim - modify report*** */ 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' => ''); //copy the form as errors, so the errors will be stored with keys corresponding to the form field names $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)); // 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->_verifyArrayIndex($_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]'); // 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]'); $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 = $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) && 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) && 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) && 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)) { $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(); } // 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: ' . $post->incident_title; $message = 'The following report was submitted and requires moderation:' . "\r\n" . 'Title: ' . $post->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); return 0; //success } else { // No! We have validation errors, we need to show the form again, with the errors // 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_messages .= $error_description; if ($error_description != end($this->messages)) { $this->error_messages .= " - "; } } } //FAILED!!! return 1; //validation error } } else { return 2; // Not sent by post method. } }
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'); }
/** * parse feed and send feed items to database */ public function index() { // Max number of feeds to keep $max_feeds = 100; // Today's Date $today = strtotime('now'); // Get All Feeds From DB $feeds = ORM::factory('feed')->find_all(); foreach ($feeds as $feed) { $last_update = $feed->feed_update; // Parse Feed URL using Feed Helper $feed_data = feed::simplepie($feed->feed_url); foreach ($feed_data->get_items(0, 50) as $feed_data_item) { $title = $feed_data_item->get_title(); $link = $feed_data_item->get_link(); $description = $feed_data_item->get_description(); $date = $feed_data_item->get_date(); $latitude = $feed_data_item->get_latitude(); $longitude = $feed_data_item->get_longitude(); // Make Sure Title is Set (Atleast) if (isset($title) && !empty($title)) { // We need to check for duplicates!!! // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( ) $dupe_count = ORM::factory('feed_item')->where('item_title', $title)->where('item_date', date("Y-m-d H:i:s", strtotime($date)))->count_all(); if ($dupe_count == 0) { // Does this feed have a location?? $location_id = 0; // STEP 1: SAVE LOCATION if ($latitude && $longitude) { $location = new Location_Model(); $location->location_name = "Unknown"; $location->latitude = $latitude; $location->longitude = $longitude; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); $location_id = $location->id; } $newitem = new Feed_Item_Model(); $newitem->feed_id = $feed->id; $newitem->location_id = $location_id; $newitem->item_title = $title; if (isset($description) && !empty($description)) { $newitem->item_description = $description; } if (isset($link) && !empty($link)) { $newitem->item_link = $link; } if (isset($date) && !empty($date)) { $newitem->item_date = date("Y-m-d H:i:s", strtotime($date)); } else { $newitem->item_date = date("Y-m-d H:i:s", time()); } $newitem->save(); } } } // Get Feed Item Count $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all(); if ($feed_count > $max_feeds) { // Excess Feeds $feed_excess = $feed_count - $max_feeds; // Delete Excess Feeds /**** DISABLED FOR NOW ****/ // foreach (ORM::factory('feed_item') // ->where('feed_id', $feed->id) // ->orderby('id', 'ASC') // ->limit($feed_excess) // ->find_all() as $del_feed) // { // $del_feed->delete($del_feed->id); // } } // Set feed update date $feed->feed_update = strtotime('now'); $feed->save(); } }
public function submit($saved = false) { // Cacheable Controller $this->is_cachable = FALSE; $this->template->header->show_map = TRUE; $this->template->content = new View('keitai/reports_submit'); // First, are we allowed to submit new reports? if ( ! Kohana::config('settings.allow_reports')) { url::redirect(url::site().'main'); } // setup and initialize form field names $form = array ( 'incident_title' => '', 'incident_description' => '', 'incident_month' => '', 'incident_day' => '', 'incident_year' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'location_name' => '', 'country_id' => '', 'incident_category' => 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_month'] = date('m'); $form['incident_day'] = date('d'); $form['incident_year'] = date('Y'); $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); //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_month', 'required', 'numeric', 'between[1,12]'); $post->add_rules('incident_day', 'required', 'numeric', 'between[1,31]'); $post->add_rules('incident_year', 'required', 'numeric', 'length[4,4]'); if ( ! checkdate($_POST['incident_month'], $_POST['incident_day'], $_POST['incident_year']) ) { $post->add_error('incident_date','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', 'between[-90,90]'); $post->add_rules('longitude', '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'); } // Geocode Location if ( empty($_POST['latitude']) AND empty($_POST['longitude']) AND ! empty($_POST['location_name']) ) { $default_country = Kohana::config('settings.default_country'); $country_name = ""; if ($default_country) { $country = ORM::factory('country', $default_country); if ($country->loaded) { $country_name = $country->country; } } $geocode = keitai_geocoder::geocode($_POST['location_name'].", ".$country_name); if ($geocode) { $post->latitude = $geocode['lat']; $post->longitude = $geocode['lon']; } else { $post->add_error('location_name', 'geocode'); } } // Test to see if things passed the rule checks if ($post->validate()) { if ($post->latitude AND $post->longitude) { // 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(); if (isset($location) AND $location->loaded) { $incident->location_id = $location->id; } $incident->user_id = 0; $incident->incident_title = $post->incident_title; $incident->incident_description = $post->incident_description; $incident_date = $post->incident_year."-".$post->incident_month."-".$post->incident_day; $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(); } url::redirect('keitai/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; } } else { $form['latitude'] = (isset($_GET['latitude'])) ? $_GET['latitude'] : ""; $form['longitude'] = (isset($_GET['longitude'])) ? $_GET['longitude'] : ""; } $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']); $this->template->content->cities = $this->_get_cities(); $this->template->header->js = new View('keitai/reports_submit_js'); 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']; } $this->template->content->device = $this->checkdevice($_SERVER['HTTP_USER_AGENT']); }
/** * 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 index() { $args = $this->parse_args(func_get_args()); $args = array_merge($this->default_params, $args); $display = 'all'; $location_id = array(); $supplied_location_id = isset($args['location']) ? $args['location'] : ''; if (strlen($supplied_location_id)) { if (strpos($supplied_location_id, ',') !== FALSE) { $location_id = explode(',', $supplied_location_id); array_walk($location_id, 'intval'); } else { if ($location_id) { $location_id = array($supplied_location_id); } else { $location_id = $supplied_location_id; } // 0 for all } } $staff_id = array(); $supplied_staff_id = isset($args['staff']) ? $args['staff'] : ''; if ($supplied_staff_id) { if (strpos($supplied_staff_id, ',') !== FALSE) { $staff_id = explode(',', $supplied_staff_id); array_walk($staff_id, 'intval'); } elseif ($supplied_staff_id == '_current_user_id_') { if ($this->auth && $this->auth->user()) { $staff_id = array($this->auth->user()->id); } } else { $staff_id = array($supplied_staff_id); } } $within = FALSE; /* whithin now */ if (isset($args['within'])) { $within = $args['within']; if (strlen($within)) { $within = urldecode($within); } $this->hc_time->setNow(); $start_date = $this->hc_time->formatDate_Db(); $within_from = $this->hc_time->getTimestamp(); if ($within) { $this->hc_time->modify($within); } $end_date = $this->hc_time->formatDate_Db(); $within_to = $this->hc_time->getTimestamp(); $this->data['within_from'] = $within_from; $this->data['within_to'] = $within_to; } else { if (isset($args['start'])) { $start_date = $args['start']; } else { $start_date = $this->hc_time->setNow()->formatDate_Db(); } if (isset($args['end'])) { $end_date = $args['end']; } else { $end_date = ''; } $this->hc_time->setDateDb($start_date); $range = isset($args['range']) ? $args['range'] : ''; if ($range) { switch ($range) { case 'week': $this->hc_time->setStartWeek(); $start_date = $this->hc_time->formatDate_Db(); $this->hc_time->setEndWeek(); $end_date = $this->hc_time->formatDate_Db(); break; case 'month': $this->hc_time->setStartMonth(); $start_date = $this->hc_time->formatDate_Db(); $this->hc_time->setEndMonth(); $end_date = $this->hc_time->formatDate_Db(); break; default: $this->hc_time->modify('+' . $range); $this->hc_time->modify('-1 day'); $end_date = $this->hc_time->formatDate_Db(); break; } } } /* find dates that we have shifts */ $shift_model = new Shift_Model(); $shift_model->select('date'); $shift_model->where('date >=', $start_date); if ($end_date) { $shift_model->where('date <=', $end_date); } $shift_model->group_start(); $shift_model->where('status', SHIFT_MODEL::STATUS_ACTIVE); if ($this->auth->check() && $this->app_conf->get('staff_pick_shifts')) { // $shift_model->or_where('user_id IS ', 'NULL', FALSE); } else { $shift_model->where('user_id IS NOT ', 'NULL', FALSE); } $shift_model->group_end(); if ($location_id) { $shift_model->where_related('location', 'id', $location_id); } if ($staff_id) { $shift_model->where_related('user', 'id', $staff_id); } $shift_model->distinct(); // $shift_model->limit( 3 ); $shift_model->order_by('date', 'ASC'); $shift_model->order_by('start', 'ASC'); $shift_model->get(); // $shift_model->check_last_query(); // exit; $dates = array(); foreach ($shift_model as $s) { $dates[] = $s->date; } $this->data['dates'] = $dates; /* preload staff information */ $um = new User_Model(); $staffs = $um->get_staff(); $this->data['staffs'] = array(); foreach ($staffs as $sta) { $this->data['staffs'][$sta->id] = $sta; } /* preload location information */ $lm = new Location_Model(); $locations = $lm->get()->all; $this->data['locations'] = array(); foreach ($locations as $loc) { $this->data['locations'][$loc->id] = $loc; } $this->data['location_id'] = $location_id; $this->data['display'] = $display; $this->data['within'] = $within; /* load shifts so that they can be reused in module displays to save queries */ $this->_load_shifts($dates, $staff_id, $location_id, $within); $view = 'index'; $this->set_include($view); $this->load->view($this->template, $this->data); return; }
<?php $whitelabel_file = FCPATH . '/whitelabel.php'; if (file_exists($whitelabel_file)) { require $whitelabel_file; } $brand_title = isset($whitelabel['title']) ? $whitelabel['title'] : $this->config->item('nts_app_title'); $brand_url = isset($whitelabel['url']) ? $whitelabel['url'] : 'http://www.' . $this->config->item('nts_app') . '.com'; $promo = $this->config->item('nts_app_promo'); $this->load->model('Location_model'); $lm = new Location_Model(); $lm->condition_not_yet(); $not_yet_count = $lm->count_all(); $lm->condition_failed(); $failed_count = $lm->count_all(); $warning_view = ''; if ($not_yet_count > 0) { $warning_view .= '<span class="badge badge-warning">' . $not_yet_count . '</span>'; } if ($failed_count > 0) { $warning_view .= '<span class="badge badge-important">' . $failed_count . '</span>'; } if ($this->auth->logged_in() && $this->auth->is_admin()) { ?> <?php if (!isset($GLOBALS['NTS_IS_PLUGIN'])) { ?> <p> <h3> <a class="brand" target="_blank" href="<?php echo $brand_url;
/** * parse feed and send feed items to database */ public function index() { // Max number of feeds to keep $max_feeds = 100; // Today's Date $today = strtotime('now'); // Get All Feeds From DB $feeds = ORM::factory('feed')->find_all(); foreach ($feeds as $feed) { $last_update = $feed->feed_update; // Parse Feed URL using Feed Helper $feed_data = feed::simplepie($feed->feed_url); foreach ($feed_data->get_items(0, 50) as $feed_data_item) { $title = $feed_data_item->get_title(); $link = $feed_data_item->get_link(); $description = $feed_data_item->get_description(); $date = $feed_data_item->get_date(); $latitude = $feed_data_item->get_latitude(); $longitude = $feed_data_item->get_longitude(); $categories = $feed_data_item->get_categories(); // HT: new code $category_ids = new stdClass(); // HT: new code // Make Sure Title is Set (Atleast) if (isset($title) && !empty($title)) { // We need to check for duplicates!!! // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( ) $dupe_count = ORM::factory('feed_item')->where('item_title', $title)->where('item_date', date("Y-m-d H:i:s", strtotime($date)))->count_all(); if ($dupe_count == 0) { // Does this feed have a location?? $location_id = 0; // STEP 1: SAVE LOCATION if ($latitude and $longitude) { $location = new Location_Model(); $location->location_name = "Unknown"; $location->latitude = $latitude; $location->longitude = $longitude; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); $location_id = $location->id; } $newitem = new Feed_Item_Model(); $newitem->feed_id = $feed->id; $newitem->location_id = $location_id; $newitem->item_title = $title; if (isset($description) and !empty($description)) { $newitem->item_description = $description; } if (isset($link) and !empty($link)) { $newitem->item_link = $link; } if (isset($date) and !empty($date)) { $newitem->item_date = date("Y-m-d H:i:s", strtotime($date)); } else { $newitem->item_date = date("Y-m-d H:i:s", time()); } // HT: new code if (!empty($categories)) { foreach ($categories as $category) { $categoryData = ORM::factory('category')->where('category_title', $category->term)->find(); if ($categoryData->loaded == TRUE) { $category_ids->feed_item_category[$categoryData->id] = $categoryData->id; } elseif (Kohana::config('settings.allow_feed_category')) { $newcategory = new Category_Model(); $newcategory->category_title = $category->term; $newcategory->parent_id = 0; $newcategory->category_description = $category->term; $newcategory->category_color = '000000'; $newcategory->category_visible = 0; $newcategory->save(); $category_ids->feed_item_category[$newcategory->id] = $newcategory->id; } } } // HT: End of new code $newitem->save(); // HT: New code if (!empty($category_ids->feed_item_category)) { feed::save_category($category_ids, $newitem); } // HT: End of New code // Action::feed_item_add - Feed Item Received! Event::run('ushahidi_action.feed_item_add', $newitem); } } } // Get Feed Item Count $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all(); if ($feed_count > $max_feeds) { // Excess Feeds $feed_excess = $feed_count - $max_feeds; } // Set feed update date $feed->feed_update = strtotime('now'); $feed->save(); } }
/** * The actual reporting - * * @return int */ private function _submit() { // Setup and initialize form field names $form = array('incident_title' => '', 'incident_description' => '', 'incident_date' => '', 'incident_hour' => '', 'incident_minute' => '', 'incident_ampm' => '', 'latitude' => '', 'longitude' => '', 'location_name' => '', 'country_id' => '', 'incident_category' => '', 'incident_news' => array(), 'incident_video' => array(), 'incident_photo' => array(), 'person_first' => '', 'person_last' => '', 'person_email' => ''); $this->messages = $form; // check, 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. } }
function importreport($row, $group) { if (!strtotime($row['INCIDENT DATE'])) { $this->errors[] = 'Could not parse incident date "' . htmlspecialchars($row['INCIDENT DATE']) . '" on line ' . ($this->rownumber + 1); } if (isset($row["APPROVED"]) and !in_array($row["APPROVED"], array('NO', 'YES'))) { $this->errors[] = 'APPROVED must be either YES or NO on line ' . ($this->rownumber + 1); } if (isset($row["VERIFIED"]) and !in_array($row["VERIFIED"], array('NO', 'YES'))) { $this->errors[] = 'VERIFIED must be either YES or NO on line ' . ($this->rownumber + 1); } if (count($this->errors)) { return false; } // STEP 1: SAVE LOCATION if (isset($row['LOCATION'])) { $location = new Location_Model(); $location->location_name = isset($row['LOCATION']) ? $row['LOCATION'] : ''; $location->latitude = isset($row['LATITUDE']) ? $row['LATITUDE'] : ''; $location->longitude = isset($row['LONGITUDE']) ? $row['LONGITUDE'] : ''; $location->location_date = $this->time; $location->save(); $this->locations_added[] = $location->id; } // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); $incident->location_id = isset($row['LOCATION']) ? $location->id : 0; $incident->user_id = 0; $incident->incident_title = $row['INCIDENT TITLE']; $incident->incident_description = isset($row['DESCRIPTION']) ? $row['DESCRIPTION'] : ''; $incident->incident_date = date("Y-m-d H:i:s", strtotime($row['INCIDENT DATE'])); $incident->incident_dateadd = $this->time; $incident->incident_active = (isset($row['APPROVED']) and $row['APPROVED'] == 'YES') ? 1 : 0; $incident->incident_verified = (isset($row['VERIFIED']) and $row['VERIFIED'] == 'YES') ? 1 : 0; $incident->save(); $this->incidents_added[] = $incident->id; //STEP 2.5: SAVE THE GROUP ASSOCIATION $group_incident = ORM::factory("simplegroups_groups_incident"); $group_incident->incident_id = $incident->id; $group_incident->simplegroups_groups_id = $group->id; $group_incident->save(); // STEP 3: SAVE CATEGORIES if (isset($row['CATEGORY'])) { $categorynames = explode(',', trim($row['CATEGORY'])); foreach ($categorynames as $categoryname) { $categoryname = strtoupper(trim($categoryname)); // There seems to be an uppercase convention for categories... Don't know why. if ($categoryname != '') { if (!isset($this->category_ids[$categoryname])) { $this->notices[] = 'There exists no category "' . htmlspecialchars($categoryname) . '" in database yet. This category was skipped.'; continue; /* $this->notices[] = 'There exists no category "'.htmlspecialchars($categoryname).'" in database yet. Added to database.'; $category = new Category_Model; $category->category_title = $categoryname; $category->category_color = '000000'; // We'll just use black for now. Maybe something random? $category->category_type = 5; // because all current categories are of type '5' $category->category_visible = 1; $category->category_description = $categoryname; $category->save(); $this->categories_added[] = $category->id; $this->category_ids[$categoryname] = $category->id; // Now category_id is known: This time, and for the rest of the import. */ } $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; $incident_category->category_id = $this->category_ids[$categoryname]; $incident_category->save(); $this->incident_categories_added[] = $incident_category->id; } // empty categoryname not allowed } // add categories to incident } // if CATEGORIES column exists // STEP 4: SAVE GROUP CATEGORIES if (isset($row['GROUP CATEGORY'])) { $categorynames = explode(',', trim($row['GROUP CATEGORY'])); foreach ($categorynames as $categoryname) { $categoryname = strtoupper(trim($categoryname)); // There seems to be an uppercase convention for categories... Don't know why. if ($categoryname != '') { if (!isset($this->group_category_ids[$categoryname])) { $this->notices[] = 'There exists no category "' . htmlspecialchars($categoryname) . '" in the group categories yet. Added to database.'; $category = ORM::factory("simplegroups_category"); $category->category_title = $categoryname; $category->category_color = '000000'; // We'll just use black for now. Maybe something random? $category->category_type = 5; // because all current categories are of type '5' $category->category_visible = 1; $category->category_description = $categoryname; $category->simplegroups_groups_id = $group->id; $category->applies_to_report = 1; $category->save(); $this->categories_added[] = $category->id; $this->group_category_ids[$categoryname] = $category->id; // Now category_id is known: This time, and for the rest of the import. } $incident_category = ORM::factory("simplegroups_incident_category"); $incident_category->incident_id = $incident->id; $incident_category->simplegroups_category_id = $this->group_category_ids[$categoryname]; $incident_category->save(); $this->incident_categories_added[] = $incident_category->id; } // empty categoryname not allowed } // add categories to incident } // if CATEGORIES column exists 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) { $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(); }
public function index($service_id = 1) { $this->template->content = new View('admin/reporters'); $this->template->content->title = Kohana::lang('ui_admin.reporters'); $filter = "1=1"; $search_type = ""; $keyword = ""; // Get Search Type (If Any) if ($service_id) { $search_type = $service_id; $filter .= " AND (service_id='" . $service_id . "')"; } else { $search_type = "0"; } // Get Search Keywords (If Any) if (isset($_GET['k']) and !empty($_GET['k'])) { $keyword = $_GET['k']; $filter .= " AND (service_account LIKE'%" . $_GET['k'] . "%')"; } // setup and initialize form field names $form = array('reporter_id' => '', 'level_id' => '', 'service_name' => '', 'service_account' => '', 'location_id' => '', 'location_name' => '', 'latitude' => '', 'longitude' => ''); // 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 = ""; // 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); // 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('reporter_id.*', 'required', 'numeric'); if ($post->action == 'l') { $post->add_rules('level_id', 'required', 'numeric'); } elseif ($post->action == 'a') { $post->add_rules('level_id', 'required', 'numeric'); // If any location data is provided, require all location parameters if ($post->latitude or $post->longitude or $post->location_name) { $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]'); } } // Test to see if things passed the rule checks if ($post->validate()) { if ($post->action == 'd') { foreach ($post->reporter_id as $item) { // Delete Reporters Messages ORM::factory('message')->where('reporter_id', $item)->delete_all(); // Delete Reporter $reporter = ORM::factory('reporter')->find($item); $reporter->delete($item); } $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.deleted')); } elseif ($post->action == 'l') { foreach ($post->reporter_id as $item) { // Update Reporter Level $reporter = ORM::factory('reporter')->find($item); if ($reporter->loaded) { $reporter->level_id = $post->level_id; $reporter->save(); } } $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } else { if ($post->action == 'a') { foreach ($post->reporter_id as $item) { $reporter = ORM::factory('reporter')->find($item); // SAVE Reporter only if loaded if ($reporter->loaded) { $reporter->level_id = $post->level_id; // SAVE Location if available if ($post->latitude and $post->longitude) { $location = new Location_Model($post->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(); $reporter->location_id = $location->id; } $reporter->save(); $form_saved = TRUE; $form_action = strtoupper(Kohana::lang('ui_admin.modified')); } } } } } else { // repopulate the form fields $form = arr::overwrite($form, $post->as_array()); // populate the error fields, if any $errors = arr::overwrite($errors, $post->errors('reporters')); $form_error = TRUE; } } // Pagination $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('reporter')->where($filter)->count_all())); $reporters = ORM::factory('reporter')->where($filter)->orderby('service_account', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset); $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; $this->template->content->pagination = $pagination; $this->template->content->total_items = $pagination->total_items; $this->template->content->reporters = $reporters; $this->template->content->service_id = $service_id; $this->template->content->search_type = $search_type; $search_type_array = Service_Model::get_array(); $search_type_array[0] = "All"; asort($search_type_array); $this->template->content->search_type_array = $search_type_array; $this->template->content->keyword = $keyword; $levels = ORM::factory('level')->orderby('level_weight')->find_all(); $this->template->content->levels = $levels; // Level and Service Arrays $this->template->content->level_array = Level_Model::get_array(); $this->template->content->service_array = Service_Model::get_array(); // Javascript Header $this->template->map_enabled = TRUE; $this->template->js = new View('admin/reporters_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 = Kohana::config('settings.default_lat'); $this->template->js->longitude = Kohana::config('settings.default_lon'); $this->template->js->form_error = $form_error; }
public function index() { $birth = 1263427200; // 2010-01-14 - We'll move in 3 hour increments from here $cache = Cache::instance(); $last_message_date = $cache->get('georss_parser'); if ($last_message_date == NULL) { $last_message_date = $birth; $cache->set('georss_parser', $birth, array('georss'), 0); } //echo $last_message_date; $settings = ORM::factory('settings', 1); $sms_rss = $settings->georss_feed . "&only_phone=1&limit=50," . $this->items; //."&uptots=".$last_message_date; $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, $sms_rss); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); // Timeout curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Set curl to store data in variable instead of print $buffer = curl_exec($curl_handle); curl_close($curl_handle); // Parse Feed URL using SimplePIE $feed_data = $this->_simplepie($buffer); if (count($feed_data->get_items(0, $this->items)) == 0) { $cache->set('georss_parser', $last_message_date + 3600, array('georss'), 0); //exit; } // Cycle through feed data $i = 0; foreach ($feed_data->get_items(0, $this->items) as $feed_data_item) { $service_messageid = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'id'); $service_messageid = str_replace("http://4636.ushahidi.com/person.php?id=", "", trim($service_messageid[0]['data'])); $date = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'updated'); $date = date("Y-m-d H:i:s", strtotime(trim($date[0]['data']))); $phone = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'phone'); $phone = intval($phone[0]['data']); $category = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'categorization'); $category = trim($category[0]['data']); $message_sms = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'sms'); $message_sms = trim($message_sms[0]['data']); $message_notes = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'notes'); $message_notes = trim($message_notes[0]['data']); $message_detail = $message_notes . "\n~~~~~~~~~~~~~~~~~\n"; $message_detail .= "Category: " . $category; $latitude = $feed_data_item->get_latitude(); $longitude = $feed_data_item->get_longitude(); $location_name = $feed_data_item->get_item_tags('http://www.w3.org/2005/Atom', 'city'); $location_name = trim($location_name[0]['data']); // Okay now we have everything we need // Step 1. Does this message have a phone number? if ($phone) { // Step 2. Has this particular message been saved before?? $exists = ORM::factory('message')->where('service_messageid', $service_messageid)->where('message_from', $phone)->find(); if (!$exists->loaded) { $parent_id = 0; // Step 3. Make sure this phone number is not in our database $reporter = ORM::factory('reporter')->where('service_id', 1)->where('service_account', $phone)->find(); if (!$reporter->loaded) { $reporter->service_id = 1; // 1 - SMS (See Service Table) $reporter->level_id = 3; // 3 - Untrusted (See Level Table) $reporter->service_account = $phone; $reporter->reporter_date = $date; $reporter->save(); } else { // Find previous message and use it as parent $parent = ORM::factory('message')->where('reporter_id', $reporter->id)->where('message_type', '1')->where('parent_id', '0')->where('message_trash', '0')->orderby('message_date', 'desc')->find(); if ($parent->loaded) { $parent_id = $parent->id; $parent->message_reply = 1; $parent->save($parent->id); } } // Step 4. If this message has a location, save it! $location_id = 0; if ($latitude && $longitude) { $location = ORM::factory('location')->where('latitude', $latitude)->where('longitude', $longitude)->find(); if (!$location->loaded) { $location = new Location_Model(); if ($location_name) { $location->location_name = $location_name; } else { $location->location_name = "Unknown"; } $location->latitude = $latitude; $location->longitude = $longitude; $location->location_date = date("Y-m-d H:i:s", time()); $location->save(); $location_id = $location->id; } } // Save Message $message = new Message_Model(); $message->parent_id = $parent_id; $message->incident_id = 0; $message->location_id = $location_id; $message->user_id = 0; $message->reporter_id = $reporter->id; $message->message_from = $phone; $message->message_to = null; $message->message = $message_sms; $message->message_detail = $message_detail; $message->message_type = 1; // Inbox $message->message_date = $date; $message->service_messageid = $service_messageid; $message->save(); $i++; } } } if ($i == 0) { $cache->set('georss_parser', $last_message_date + 3600, array('georss'), 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'] = 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(); }
/** * Saves an incident * * @param Validation $post Validation object with the data to be saved * @param Incident_Model $incident Incident_Model instance to be modified * @param Location_Model $location_model Location to be attached to the incident * @param int $id ID no. of the report * */ public static function save_report($post, $incident, $location_id) { // Exception handling if (!$post instanceof Validation_Core and !$incident instanceof Incident_Model) { // Throw exception throw new Kohana_Exception('Invalid parameter types'); } // Verify that the location id exists if (!Location_Model::is_valid_location($location_id)) { throw new Kohana_Exception(sprintf('Invalid location id specified: ', $location_id)); } // Is this new or edit? if ($incident->loaded) { // Edit $incident->incident_datemodify = date("Y-m-d H:i:s", time()); } else { // New $incident->incident_dateadd = date("Y-m-d H:i:s", time()); } $incident->location_id = $location_id; //$incident->locale = $post->locale; if (isset($post->form_id)) { $incident->form_id = $post->form_id; } // Check if the user id has been specified if (isset($_SESSION['auth_user'])) { $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 an Email, SMS, Twitter submitted report? if (!empty($post->service_id)) { // SMS if ($post->service_id == 1) { $incident->incident_mode = 2; } elseif ($post->service_id == 2) { $incident->incident_mode = 3; } elseif ($post->service_id == 3) { $incident->incident_mode = 4; } else { // Default to Web Form $incident->incident_mode = 1; } } // Approval Status if (isset($post->incident_active)) { $incident->incident_active = $post->incident_active; } // Verification status if (isset($post->incident_verified)) { $incident->incident_verified = $post->incident_verified; } // Incident zoom if (!empty($post->incident_zoom)) { $incident->incident_zoom = intval($post->incident_zoom); } // 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'; } // 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'; } // Save the incident $incident->save(); }
/** * 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); } }
/** * 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; }