/** * Google Location GeoCoding * * Reuses map::geocode() rather than reimplementing. * Only really keeping this for backwards compat * * @param string location / address * @return array (longitude, latitude) */ function geocode_location($address = NULL) { $result = map::geocode($address); if ($result) { return array($result['longitude'], $result['latitude'], $result['country_id']); } else { return false; } }
public function geocode() { $this->template = ""; $this->auto_render = FALSE; if (isset($_POST['address']) AND ! empty($_POST['address'])) { $geocode = map::geocode($_POST['address']); if ($geocode) { echo json_encode(array("status"=>"success", "message"=>array($geocode['lat'], $geocode['lon']))); } else { echo json_encode(array("status"=>"error", "message"=>"ERROR!")); } } else { echo json_encode(array("status"=>"error", "message"=>"ERROR!")); } }
/** * This handles sms alerts subscription via phone * * @param string $message_from Subscriber MSISDN (mobile phone number) * @param string $message_description Message content * @return bool */ public static function mobile_alerts_register($message_from, $message_description) { // Preliminary validation if (empty($message_from) or empty($message_description)) { // Log the error Kohana::log('info', 'Insufficient data to proceed with subscription via mobile phone'); // Return return FALSE; } //Get the message details (location, category, distance) $message_details = explode(" ", $message_description); $message = $message_details[1] . "," . Kohana::config('settings.default_country'); $geocoder = map::geocode($message); // Generate alert code $alert_code = text::random('distinct', 8); // POST variable with items to save $post = array('alert_type' => self::MOBILE_ALERT, 'alert_mobile' => $message_from, 'alert_code' => $alert_code, 'alert_lon' => $geocoder['lon'], 'alert_lat' => $geocoder['lat'], 'alert_radius' => '20', 'alert_confirmed' => '1'); // Create ORM object for the alert and validate $alert_orm = new Alert_Model(); if ($alert_orm->validate($post)) { return self::_send_mobile_alert($post, $alert_orm); } return FALSE; }
public function geocode() { $this->template = ""; $this->auto_render = FALSE; if (isset($_POST['address']) and !empty($_POST['address'])) { $geocode_result = map::geocode($_POST['address']); if ($geocode_result) { echo json_encode(array_merge($geocode_result, array('status' => 'success'))); } else { echo json_encode(array('status' => 'error', 'message' => 'ERROR!')); } } else { echo json_encode(array('status' => 'error', 'message' => 'ERROR!')); } }
/** * Import Reports via XML * @param DOMNodeList Object $report * @return bool */ public function import_reports($reports) { /* Import individual reports */ foreach ($reports->getElementsByTagName('report') as $report) { $this->totalreports++; // Get Report id $report_id = $report->getAttribute('id'); // Check if this incident already exists in the db if (isset($report_id) and isset($this->incident_ids[$report_id])) { $this->notices[] = Kohana::lang('import.incident_exists') . $report_id; } else { /* Step 1: Location information */ $locations = $report->getElementsByTagName('location'); // If location information has been provided if ($locations->length > 0) { $report_location = $locations->item(0); // Location Name $location_name = xml::get_node_text($report_location, 'name'); // Longitude $longitude = xml::get_node_text($report_location, 'longitude'); // Latitude $latitude = xml::get_node_text($report_location, 'latitude'); if ($location_name) { // For geocoding purposes $location_geocoded = map::geocode($location_name); // Save the location $new_location = new Location_Model(); $new_location->location_name = $location_name ? $location_name : NULL; $new_location->location_date = $this->time; // If longitude/latitude values are present if ($latitude and $longitude) { $new_location->latitude = $latitude ? $latitude : 0; $new_location->longitude = $longitude ? $longitude : 0; } else { // Get geocoded lat/lon values $new_location->latitude = $location_geocoded ? $location_geocoded['latitude'] : $latitude; $new_location->longitude = $location_geocoded ? $location_geocoded['longitude'] : $longitude; } $new_location->country_id = $location_geocoded ? $location_geocoded['country_id'] : 0; $new_location->save(); // Add this location to array of imported locations $this->locations_added[] = $new_location->id; } } /* Step 2: Save Report */ // Report Title $report_title = xml::get_node_text($report, 'title'); // Report Date $report_date = xml::get_node_text($report, 'date'); // Missing report title or report date? if (!$report_title or !$report_date) { $this->errors[] = Kohana::lang('import.xml.incident_title_date') . $this->totalreports; } // If report date is not in the required format if (!strtotime($report_date)) { $this->errors[] = Kohana::lang('import.incident_date') . $this->totalreports . ': ' . html::escape($report_date); } else { // Approval status? $approved = $report->getAttribute('approved'); $report_approved = (isset($approved) and in_array($approved, $this->allowable)) ? $approved : 0; // Verified Status? $verified = $report->getAttribute('verified'); $report_verified = (isset($verified) and in_array($verified, $this->allowable)) ? $verified : 0; // Report mode? $allowed_modes = array(1, 2, 3, 4); $mode = $report->getAttribute('mode'); $report_mode = (isset($mode) and in_array($mode, $allowed_modes)) ? $mode : 1; // Report Form $report_form = xml::get_node_text($report, 'form_name', FALSE); if ($report_form) { if (!isset($this->existing_forms[utf8::strtoupper($report_form)])) { $this->notices[] = Kohana::lang('import.xml.no_form_exists') . $this->totalreports . ': "' . $report_form . '"'; } $form_id = isset($this->existing_forms[utf8::strtoupper($report_form)]) ? $this->existing_forms[utf8::strtoupper($report_form)] : 1; } // Report Date added $dateadd = xml::get_node_text($report, 'dateadd'); // Report Description $report_description = xml::get_node_text($report, 'description'); $new_report = new Incident_Model(); $new_report->location_id = isset($new_location) ? $new_location->id : 0; $new_report->user_id = 0; $new_report->incident_title = $report_title; $new_report->incident_description = $report_description ? $report_description : ''; $new_report->incident_date = date("Y-m-d H:i:s", strtotime($report_date)); $new_report->incident_dateadd = ($dateadd and strtotime($dateadd)) ? $dateadd : $this->time; $new_report->incident_active = $report_approved; $new_report->incident_verified = $report_verified; $new_report->incident_mode = $report_mode; $new_report->form_id = isset($form_id) ? $form_id : 1; $new_report->save(); // Increment imported rows counter $this->importedreports++; // Add this report to array of reports added during import $this->incidents_added[] = $new_report->id; /* Step 3: Save Report Categories*/ // Report Categories exist? $reportcategories = $report->getElementsByTagName('report_categories'); if ($reportcategories->length > 0) { $report_categories = $reportcategories->item(0); foreach ($report_categories->getElementsByTagName('category') as $r_category) { $category = trim($r_category->nodeValue); $report_category = (isset($category) and $category != '') ? $category : ''; if ($report_category != '' and isset($this->existing_categories[utf8::strtoupper($report_category)])) { // Save the incident category $new_incident_category = new Incident_Category_Model(); $new_incident_category->incident_id = $new_report->id; $new_incident_category->category_id = $this->existing_categories[utf8::strtoupper($report_category)]; $new_incident_category->save(); // Add this to array of incident categories added $this->incident_categories_added[] = $new_incident_category->id; } if ($report_category != '' and !isset($this->existing_categories[utf8::strtoupper($report_category)])) { $this->notices[] = Kohana::lang('import.xml.no_category_exists') . $this->totalreports . ': "' . $report_category . '"'; } } } /* Step 4: Save Custom form field responses for this report */ // Report Custom Fields $this_form = $new_report->form_id; $reportfields = $report->getElementsByTagName('custom_fields'); if ($reportfields->length > 0) { $report_fields = $reportfields->item(0); $custom_fields = $report_fields->getElementsByTagName('field'); if ($custom_fields->length > 0) { foreach ($custom_fields as $field) { // Field Name $field_name = $field->hasAttribute('name') ? xml::get_node_text($field, 'name', FALSE) : FALSE; if ($field_name) { // If this field exists in the form listed for this report if (isset($this->existing_fields[utf8::strtoupper($field_name)][$this_form])) { // Get field type and default values $match_field_id = $this->existing_fields[utf8::strtoupper($field_name)][$this_form]; // Grab form field object $match_fields = ORM::Factory('form_field', $match_field_id); $match_field_type = $match_fields->field_type; $match_field_defaults = $match_fields->field_default; // Grab form responses $field_response = trim($field->nodeValue); if ($field_response != '') { // Initialize form response model $new_form_response = new Form_Response_Model(); $new_form_response->incident_id = $new_report->id; $new_form_response->form_field_id = $match_field_id; // For radio buttons, checkbox fields and drop downs, make sure form responses are // within bounds of allowable options for that field // Split field defaults into individual values $field_defaults = explode(',', $match_field_defaults); /* Radio buttons and Drop down fields which take single responses */ if ($match_field_type == 5 or $match_field_type == 7) { foreach ($field_defaults as $match_field_default) { // Carry out a case insensitive string comparison $new_form_response->form_response = strcasecmp($match_field_default, $field_response) == 0 ? $match_field_default : NULL; } } // Checkboxes which if ($match_field_type == 6) { // Split user responses into individual value $responses = explode(',', $field_response); $values = array(); foreach ($match_field_defaults as $match_field_default) { foreach ($responses as $response) { $values[] = strcasecmp($match_field_default, $response) == 0 ? $match_field_default : NULL; } } // Concatenate checkbox values into a string, separated by a comma $new_form_response->form_response = implode(",", $values); } else { $new_form_response->form_response = $field_response; } // Only save if form response is not empty if ($new_form_response->form_response != NULL) { $new_form_response->save(); } // Add this to array of form responses added $this->incident_responses_added[] = $new_form_response->id; } } else { $this->notices[] = Kohana::lang('import.xml.form_field_no_match') . $this->totalreports . ': "' . $field_name . '" on form "' . $new_report->form->form_title . '"'; } } } } } /* Step 5: Save incident persons for this report */ // Report Personal Information $personal_info = $report->getElementsByTagName('personal_info'); // If personal info exists if ($personal_info->length > 0) { $report_info = $personal_info->item(0); // First Name $firstname = xml::get_node_text($report_info, 'first_name'); // Last Name $lastname = xml::get_node_text($report_info, 'last_name'); // Email $r_email = xml::get_node_text($report_info, 'email'); $email = ($r_email and valid::email($r_email)) ? $r_email : NULL; $new_incident_person = new Incident_Person_Model(); $new_incident_person->incident_id = $new_report->id; $new_incident_person->person_date = $new_report->incident_dateadd; // Make sure that at least one of the personal info field entries is provided if ($firstname or $lastname or $email != NULL) { $new_incident_person->person_first = $firstname ? $firstname : NULL; $new_incident_person->person_last = $lastname ? $firstname : NULL; $new_incident_person->person_email = $email; $new_incident_person->save(); // Add this to array of incident persons added during import $this->incident_persons_added[] = $new_incident_person->id; } } /* Step 6: Save media links for this report */ // Report Media $media = $report->getElementsByTagName('media'); if ($media->length > 0) { $media = $media->item(0); foreach ($media->getElementsByTagName('item') as $media_element) { $media_link = trim($media_element->nodeValue); $media_date = $media_element->getAttribute('date'); if (!empty($media_link)) { $media_item = new Media_Model(); $media_item->location_id = isset($new_location) ? $new_location->id : 0; $media_item->incident_id = $new_report->id; $media_item->media_type = $media_element->getAttribute('type'); $media_item->media_link = $media_link; $media_item->media_date = !empty($media_date) ? $media_date : $new_report->incident_date; $media_item->save(); } } } } } } // end individual report import // If we have errors, return FALSE, else TRUE return count($this->errors) === 0; }
/** * This handles sms alerts subscription via phone * @params alert,location (city) - required * @params distance, category - optional */ public function mobile_alerts_register($message_from, $message_description) { /** * Get the message details (location, category, distance) */ $message_details = explode(" ", $message_description); $message = $message_details[1] . "," . Kohana::config('settings.default_country'); $geocoder = map::geocode($message); /** * Generate alert code */ $alert_code = text::random('distinct', 8); /* POST variable with items to save */ $post = array('alert_type' => self::MOBILE_ALERT, 'alert_mobile' => $message_from, 'alert_code' => $alert_code, 'alert_lon' => $geocoder['lon'], 'alert_lat' => $geocoder['lat'], 'alert_radius' => '20'); //convert the array to object $p = (object) $post; /** * Save alert details */ $register_sms_alerts = self::_send_mobile_alert($p); }
/** * Function to import a report form a row in the CSV file * @param array $row * @return bool */ function import_report($row) { // If the date is not in proper date format if (!strtotime($row['INCIDENT DATE'])) { $this->errors[] = Kohana::lang('import.incident_date') . ($this->rownumber + 1) . ': ' . $row['INCIDENT DATE']; } // If a value of Yes or No is NOT set for approval status for the imported row if (isset($row["APPROVED"]) and !in_array(utf8::strtoupper($row["APPROVED"]), array('NO', 'YES'))) { $this->errors[] = Kohana::lang('import.csv.approved') . ($this->rownumber + 1); } // If a value of Yes or No is NOT set for verified status for the imported row if (isset($row["VERIFIED"]) and !in_array(utf8::strtoupper($row["VERIFIED"]), array('NO', 'YES'))) { $this->errors[] = Kohana::lang('import.csv.verified') . ($this->rownumber + 1); } if (count($this->errors)) { return false; } // STEP 1: SAVE LOCATION if (isset($row['LOCATION'])) { $location = new Location_Model(); $location->location_name = isset($row['LOCATION']) ? $row['LOCATION'] : ''; // For Geocoding purposes $location_geocoded = map::geocode($location->location_name); // If we have LATITUDE and LONGITUDE use those if (isset($row['LATITUDE']) and isset($row['LONGITUDE'])) { $location->latitude = isset($row['LATITUDE']) ? $row['LATITUDE'] : 0; $location->longitude = isset($row['LONGITUDE']) ? $row['LONGITUDE'] : 0; } else { $location->latitude = $location_geocoded ? $location_geocoded['latitude'] : 0; $location->longitude = $location_geocoded ? $location_geocoded['longitude'] : 0; } $location->country_id = $location_geocoded ? $location_geocoded['country_id'] : 0; $location->location_date = $this->time; $location->save(); $this->locations_added[] = $location->id; } // STEP 2: SAVE INCIDENT $incident = new Incident_Model(); $incident->location_id = isset($row['LOCATION']) ? $location->id : 0; $incident->user_id = 0; $incident->form_id = (isset($row['FORM #']) and Form_Model::is_valid_form($row['FORM #'])) ? $row['FORM #'] : 1; $incident->incident_title = $row['INCIDENT TITLE']; $incident->incident_description = isset($row['DESCRIPTION']) ? $row['DESCRIPTION'] : ''; $incident->incident_date = date("Y-m-d H:i:s", strtotime($row['INCIDENT DATE'])); $incident->incident_dateadd = $this->time; $incident->incident_active = (isset($row['APPROVED']) and utf8::strtoupper($row['APPROVED']) == 'YES') ? 1 : 0; $incident->incident_verified = (isset($row['VERIFIED']) and utf8::strtoupper($row['VERIFIED']) == 'YES') ? 1 : 0; $incident->save(); $this->incidents_added[] = $incident->id; // STEP 3: Save Personal Information if (isset($row['FIRST NAME']) or isset($row['LAST NAME']) or isset($row['EMAIL'])) { $person = new Incident_Person_Model(); $person->incident_id = $incident->id; $person->person_first = isset($row['FIRST NAME']) ? $row['FIRST NAME'] : ''; $person->person_last = isset($row['LAST NAME']) ? $row['LAST NAME'] : ''; $person->person_email = (isset($row['EMAIL']) and valid::email($row['EMAIL'])) ? $row['EMAIL'] : ''; $person->person_date = date("Y-m-d H:i:s", time()); // Make sure that you're not importing an empty record i.e at least one field has been recorded // If all fields are empty i.e you have an empty record, don't save if (!empty($person->person_first) or !empty($person->person_last) or !empty($person->person_email)) { $person->save(); // Add to array of incident persons added $this->incident_persons_added[] = $person->id; } } // STEP 4: SAVE CATEGORIES // If CATEGORY column exists if (isset($row['CATEGORY'])) { $categorynames = explode(',', trim($row['CATEGORY'])); // Trim whitespace from array values $categorynames = array_map('trim', $categorynames); // Get rid of duplicate category entries in a row $categories = array_unique(array_map('strtolower', $categorynames)); // Add categories to incident foreach ($categories as $categoryname) { // Convert the first string character of the category name to Uppercase $categoryname = utf8::ucfirst($categoryname); // For purposes of adding an entry into the incident_category table $incident_category = new Incident_Category_Model(); $incident_category->incident_id = $incident->id; // If category name exists, add entry in incident_category table if ($categoryname != '') { // Check if the category exists (made sure to convert to uppercase for comparison) if (!isset($this->existing_categories[utf8::strtoupper($categoryname)])) { $this->notices[] = Kohana::lang('import.new_category') . $categoryname; $category = new Category_Model(); $category->category_title = $categoryname; // We'll just use black for now. Maybe something random? $category->category_color = '000000'; // because all current categories are of type '5' $category->category_visible = 1; $category->category_description = $categoryname; $category->category_position = count($this->existing_categories); $category->save(); $this->categories_added[] = $category->id; // Now category_id is known: This time, and for the rest of the import. $this->existing_categories[utf8::strtoupper($categoryname)] = $category->id; } $incident_category->category_id = $this->existing_categories[utf8::strtoupper($categoryname)]; $incident_category->save(); $this->incident_categories_added[] = $incident_category->id; } } } // STEP 5: Save Custom form fields responses // Check for form_id $form_id = (isset($row['FORM #']) and Form_Model::is_valid_form($row['FORM #'])) ? $row['FORM #'] : 1; // Get custom form fields for this particular form $custom_titles = customforms::get_custom_form_fields('', $form_id, false); // Do custom form fields exist on this deployment? if (!empty($custom_titles)) { foreach ($custom_titles as $field_name) { // Check if the column exists in the CSV $rowname = utf8::strtoupper($field_name['field_name']); if (isset($row[$rowname . '-' . $form_id])) { $response = $row[$rowname . '-' . $form_id]; // Grab field_id and field_type $field_id = $field_name['field_id']; $field_type = $field_name['field_type']; // Initialize form response model $form_response = new Form_Response_Model(); $form_response->incident_id = $incident->id; $form_response->form_field_id = $field_id; // If form response exists if ($response != '') { /* Handling case sensitivity issues with custom form field upload */ // Check if the field is a radio button, checkbox OR dropdown field if ($field_type == '5' or $field_type == '6' or $field_type == '7') { // Get field option values $field_values = $field_name['field_default']; // Split field options into individual values $options = explode(",", $field_values); // Since radio button and dropdown fields take single responses if ($field_type == '5' or $field_type == '7') { foreach ($options as $option) { // Carry out a case insensitive comparison between individual field options and csv response // If there's a match, store field option value from the db if (strcasecmp($option, $response) == 0) { $form_response->form_response = $option; } } } // For checkboxes, which accomodate multiple responses if ($field_type == '6') { // Split user responses into single values $csvresponses = explode(",", $response); $values = array(); foreach ($options as $option) { foreach ($csvresponses as $csvresponse) { // Carry out a case insensitive comparison between individual field options and csv response // If there's a match if (strcasecmp($option, $csvresponse) == 0) { // Store field option value from the db $values[] = $option; } } } // Concatenate checkbox values into a string, separated by a comma $form_response->form_response = implode(",", $values); } } else { $form_response->form_response = $response; } // If form_response is provided based on conditions set above, Save the form response if ($form_response->form_response != '') { $form_response->save(); // Add to array of field responses added $this->incident_responses_added[] = $form_response->id; } } } } } return true; }