private function testIncidentsAll() { $UshApiLib_Site_Info = new UshApiLib_Site_Info(url::base() . "api"); echo "<h1>Incidents, All</h1><strong>URL:</strong> " . $UshApiLib_Site_Info->getUrl() . "<br/><br/>"; $params = new UshApiLib_Incidents_Task_Parameter(); $params->setBy(UshApiLib_Incidents_Bys::SHOW_ALL_INCIDENTS); echo "<strong>Query String:</strong> " . Kohana::debug($params->get_query_string()) . "<br/><br/>"; $task = new UshApiLib_Incidents_Task($params, $UshApiLib_Site_Info); $response = $task->execute(); echo "<strong>JSON:</strong> " . $task->getJson() . "<br/><br/>"; echo "<strong>Code:</strong> " . $response->getError_code() . " <strong>Message:</strong> " . $response->getError_message() . "<br/><br/>"; foreach ($response->getIncidents() as $cat) { echo "Incident Title: " . $cat["incident"]->incident_title . "<br/>"; } }
/** * Use remote Ushahidi deployments API to get Incident Data * Limit to 20 not to kill remote server */ private function _process_site_reports($site) { $limit = 20; $since_id = 0; $count = 0; $modified_ids = array(); // this is an array of our primary keys $more_reports_to_pull = TRUE; // @todo grab new reports first while ($more_reports_to_pull == TRUE) { $UshApiLib_Site_Info = new UshApiLib_Site_Info(sharing_helper::clean_url($site->site_url) . "/api", $site->site_username, $site->site_password); $params = new UshApiLib_Incidents_Task_Parameter(); $params->setBy(UshApiLib_Incidents_Bys::INCIDENTS_SINCE_ID); $params->setLimit($limit); $params->setId($since_id); $params->setOrderField(UshApiLib_Task_Base::INCIDENT_ID_INDEX); $params->setSort(0); if (isset($_GET['debug']) and $_GET['debug'] == 1) { echo "<strong>Query String:</strong> " . Kohana::debug($params->get_query_string()) . "<br/><br/>"; } $task = new UshApiLib_Incidents_Task($params, $UshApiLib_Site_Info); $response = $task->execute(); if ($response->getError_code()) { if (isset($_GET['debug']) and $_GET['debug'] == 1) { $json = json_decode($task->getJson(), TRUE); // Check for error code 007 - no results if ($json['error']['code'] == '007') { echo "No results.<BR /><BR />"; } else { echo "Error Code: " . $response->getError_code() . " Message: " . $response->getError_message() . "<BR /><BR />"; var_dump($task->getJson()); } } return; } // Grab existing items $existing_items = ORM::factory('sharing_incident')->where('sharing_site_id', $site->id)->find_all(); // Build array of existing items, key'd by remote id $array = array(); foreach ($existing_items as $item) { $array[$item->remote_incident_id] = $item; } $existing_items = $array; $self_url_parsed = parse_url(URL::site()); // Parse Incidents Into Database $count = 0; foreach ($response->getIncidents() as $remote_incident_id => $incident_json) { if (isset($_GET['debug']) and $_GET['debug'] == 1) { echo "Importing report {$remote_incident_id} : " . $incident_json["incident"]->incident_title . "<br/>"; } $orm_incident = $incident_json['incident']; $json_incident = $incident_json['original']['incident']; // Was this report originally from this site? // Using parse_url so we don't get fooled by tralining slashes or other url crazy bits if (!empty($incident_json['original']['incident']['sharingsourceurl'])) { $source_url_parsed = parse_url($incident_json['original']['incident']['sharingsourceurl']); } else { $source_url_parsed = array("host" => NULL); } // Kohana::log('sharing2import', "[" . $site->id . "] " . $source_url_parsed["host"] . " - " . $self_url_parsed["host"]); if ($source_url_parsed["host"] == $self_url_parsed["host"]) { // Update counts and skip $since_id = $remote_incident_id; $count++; continue; } // Check if we've saved this before. if (isset($existing_items[$remote_incident_id])) { $sharing_incident = $existing_items[$remote_incident_id]; } else { $sharing_incident = ORM::factory('sharing_incident'); } // Find and save categories $category_titles = array(null); foreach ($incident_json['categories'] as $category) { $category_titles[] = $category->category_title; } // If matching categories, load them up $categories = ORM::factory('category')->in('category_title', $category_titles)->find_all(); // Otherwise default to "Other" category if ($categories->count() == 0) { $categories = array(ORM::factory('Category', Settings_Model::get_setting('sharing_other_category_id'))); } // Handle location $existing_location = $sharing_incident->location; if ($existing_location->loaded) { $existing_location->delete(); } $incident_json['location']->save(); $sharing_incident->location_id = $incident_json['location']->id; $date = $orm_incident->incident_date; // if the API sent us a unix timestamp, use it instead if (!empty($json_incident["incidentudate"])) { $date = date("Y-m-d H:i:s", $json_incident["incidentudate"]); } $sharing_incident->incident_title = $orm_incident->incident_title; $sharing_incident->incident_description = $orm_incident->incident_description; $sharing_incident->incident_date = $date; $sharing_incident->incident_mode = $orm_incident->incident_mode; $sharing_incident->incident_active = $orm_incident->incident_active; $sharing_incident->incident_verified = $orm_incident->incident_verified; $sharing_incident->sharing_site_id = $site->id; $sharing_incident->remote_incident_id = $remote_incident_id; $sharing_incident->updated = date("Y-m-d H:i:s", time()); $sharing_incident->save(); // Save media ORM::factory('sharing_incident_media')->where('sharing_incident_id', $sharing_incident->id)->delete_all(); foreach ($incident_json['media'] as $media) { $media->save(); $new_sharing_incident_media = ORM::factory('sharing_incident_media'); $new_sharing_incident_media->media_id = $media->id; $new_sharing_incident_media->sharing_incident_id = $sharing_incident->id; $new_sharing_incident_media->save(); } // Save categories ORM::factory('sharing_incident_category')->where('sharing_incident_id', $sharing_incident->id)->delete_all(); foreach ($categories as $category) { $new_sharing_incident_category = ORM::factory('sharing_incident_category'); $new_sharing_incident_category->category_id = $category->id; $new_sharing_incident_category->sharing_incident_id = $sharing_incident->id; $new_sharing_incident_category->save(); } // Save the primary key of the row we touched. We will be deleting ones that weren't touched. $modified_ids[] = $sharing_incident->id; // Save the highest pulled incident id so we can grab the next set from that id on $since_id = $remote_incident_id; // Save count so we know if we need to pull any more reports or not $count++; } if ($count < $limit) { $more_reports_to_pull = FALSE; } } // Delete the reports that are no longer being displayed on the shared site if (count($modified_ids) > 0) { $sharing_incidents = ORM::factory('sharing_incident')->notin('id', $modified_ids)->where('sharing_site_id', $site->id)->find_all(); if ($sharing_incidents->count() > 0) { ORM::factory('sharing_incident_category')->in('sharing_incident_id', $sharing_incidents->primary_key_array())->delete_all(); ORM::factory('sharing_incident_media')->in('sharing_incident_id', $sharing_incidents->primary_key_array())->delete_all(); ORM::factory('sharing_incident_comment')->in('sharing_incident_id', $sharing_incidents->primary_key_array())->delete_all(); // @todo delete associated categories/location/media $sharing_incidents = ORM::factory('sharing_incident')->notin('id', $modified_ids)->where('sharing_site_id', $site->id)->delete_all(); } } }