@@ -23,28 +23,27 @@ class Incidents_Api_Object extends Api_Object_Core {
* @var string
*/
private $sort;

/**
* Column name by which to order the records
* @var string
*/
private $order_field;



/**
* Should the response include comments
* @var string
*/
private $comments;

/**
* Constructor
*/
public function __construct($api_service)
{
parent::__construct($api_service);
}

/**
* Implementation of abstract method in parent
*
@@ -62,10 +61,10 @@ public function perform_task()
{
$this->by = $this->request['by'];
}

// Check optional parameters
$this->_check_optional_parameters();

// Begin task switching
switch ($this->by)
{
@@ -95,13 +94,13 @@ public function perform_task()
case "latlon":
if ($this->api_service->verify_array_index($this->request, 'latitude')
AND $this->api_service->verify_array_index($this->request, 'longitude'))
{
{
// Build out the parameters
$params = array(
'l.latitude = '.$this->check_id_value($this->request['latitude']),
'l.longitude ='.$this->check_id_value($this->request['longitude'])
);

// Fetch the incidents
$this->response_data = $this->_get_incidents($params);
}
@@ -110,7 +109,7 @@ public function perform_task()
$this->set_error_message(array(
"error" => $this->api_service->get_error_msg(001, 'latitude or longitude')
));

return;
}
break;
@@ -130,7 +129,7 @@ public function perform_task()
$params = array(
'i.location_id = '.$this->check_id_value($this->request['id'])
);

$this->response_data = $this->_get_incidents($params);
}
break;
@@ -140,7 +139,7 @@ public function perform_task()
if ( ! $this->api_service->verify_array_index($this->request, 'name'))
{
$this->set_error_message(array(
"error" => $this->api_service->get_error_msg(001, 'name')
"error" => $this->api_service->get_error_msg(001, 'name')
));

return;
@@ -150,7 +149,7 @@ public function perform_task()
$params = array(
'l.location_name = "'.$this->request['name'].'"'
);

$this->response_data = $this->_get_incidents($params);
}
break;
@@ -172,7 +171,7 @@ public function perform_task()
'c.id = '.$category_id,
'c.category_visible = 1'
);

$this->response_data = $this->_get_incidents($params);
}
break;
@@ -192,7 +191,7 @@ public function perform_task()
'c.category_title LIKE "%'.$this->request['name'].'%"',
'c.category_visible = 1'
);

$this->response_data = $this->_get_incidents($params);
}
break;
@@ -217,7 +216,7 @@ public function perform_task()
$params = array(
'i.id > '.$this->check_id_value($this->request['id'])
);

$this->response_data = $this->_get_incidents($params);
}
break;
@@ -237,7 +236,7 @@ public function perform_task()
$params = array(
'i.id < '.$this->check_id_value($this->request['id'])
);

$this->response_data = $this->_get_incidents($params);
}
break;
@@ -247,7 +246,7 @@ public function perform_task()
$this->response_data = $this->_get_incidents_by_bounds($this->request['sw'],$this->request['ne'],$this->request['c']);
break;

// Error therefore set error message
// Error therefore set error message
default:
$this->set_error_message(array(
"error" => $this->api_service->get_error_msg(002)
@@ -275,7 +274,7 @@ private function _check_optional_parameters()
if ($this->api_service->verify_array_index($this->request, 'limit'))
{
$this->set_list_limit($this->request['limit']);
}
}

// Check if the orderfield parameter has been specified
if ($this->api_service->verify_array_index($this->request, 'orderfield'))
@@ -302,8 +301,8 @@ private function _check_optional_parameters()
{
$this->order_field = 'i.incident_date';
}


// Check if the 'comments' parameter has been specified
if ( ! $this->api_service->verify_array_index($this->request, 'comments'))
{
@@ -328,18 +327,18 @@ public function _get_incidents($where = array())
// STEP 1.
// Get the incidents
$items = Incident_Model::get_incidents($where, $this->list_limit, $this->order_field, $this->sort);

//No record found.
if ($items->count() == 0)
{
return $this->response(4, $this->error_messages);
}

// Records found - proceed

// Set the no. of records returned
$this->record_count = $items->count();

// Will hold the XML/JSON string to return
$ret_json_or_xml = '';

@@ -348,7 +347,7 @@ public function _get_incidents($where = array())
$json_report_categories = array();
$json_incident_media = array();
$upload_path = str_replace("media/uploads/", "", Kohana::config('upload.relative_directory')."/");

//XML elements
$xml = new XmlWriter();
$xml->openMemory();
@@ -357,63 +356,63 @@ public function _get_incidents($where = array())
$xml->startElement('payload');
$xml->writeElement('domain',$this->domain);
$xml->startElement('incidents');

// Records found, proceed
// Store the incident ids
$incidents_ids = array();
foreach ($items as $item)
{
$incident_ids[] = $item->incident_id;
}
//

//
// STEP 2.
// Fetch the incident categories
//
//
$this->query = "SELECT c.category_title AS categorytitle, ic.incident_id, "
. "c.id AS cid "
. "FROM ".$this->table_prefix."category AS c "
. "INNER JOIN ". $this->table_prefix."incident_category AS ic ON ic.category_id = c.id "
. "WHERE ic.incident_id IN (".implode(',', $incident_ids).")";

// Execute the query
$incident_categories = $this->db->query($this->query);

// To hold the incident category items
$category_items = array();

// Temporary counter
$i = 1;

// Fetch items into array
foreach ($incident_categories as $incident_category)
{
$category_items[$incident_category->incident_id][$i]['cid'] = $incident_category->cid;
$category_items[$incident_category->incident_id][$i]['categorytitle'] = $incident_category->categorytitle;
$i++;
}

// Free temporary variables from memory
unset ($incident_categories);
//

//
// STEP 3.
// Fetch the media associated with all the incidents
//
//
$this->query = "SELECT i.id AS incident_id, m.id AS mediaid, m.media_title AS mediatitle, "
. "m.media_type AS mediatype, m.media_link AS medialink, m.media_thumb AS mediathumb "
. "FROM ".$this->table_prefix."media AS m "
. "FROM ".$this->table_prefix."media AS m "
. "INNER JOIN ".$this->table_prefix."incident AS i ON i.id = m.incident_id "
. "WHERE i.id IN (".implode(",", $incident_ids).")";

$media_items_result = $this->db->query($this->query);

// To store the fetched media items
$media_items = array();

// Reset the temporary counter
$i = 1;

// Fetch items into array
foreach ($media_items_result as $media_item)
{
@@ -424,29 +423,29 @@ public function _get_incidents($where = array())
$media_items[$media_item->incident_id][$i]['mediathumb'] = $media_item->mediathumb;
$i++;
}

// Free temporary variables
unset ($media_items_result, $i);
//

//
// STEP 4.
// Fetch the comments associated with the incidents
//
//
if ($this->comments) {
$this->query = "SELECT id, incident_id, comment_author, comment_email, "
. "comment_description, comment_rating, comment_date "
. "FROM ".$this->table_prefix."comment AS c "
. "WHERE c.incident_id IN (".implode(',', $incident_ids).")";

// Execute the query
$incident_comments = $this->db->query($this->query);

// To hold the incident category items
$comment_items = array();

// Temporary counter
$i = 1;

// Fetch items into array
foreach ($incident_comments as $incident_comment)
{
@@ -463,10 +462,10 @@ public function _get_incidents($where = array())
unset ($incident_comments);
}

//
//
// STEP 5.
// Return XML
//
//
foreach ($items as $item)
{
// Build xml file
@@ -488,7 +487,7 @@ public function _get_incidents($where = array())
$xml->startElement('categories');

$json_report_categories[$item->incident_id] = array();

// Check if the incident id exists
if (isset($category_items[$item->incident_id]))
{
@@ -502,8 +501,8 @@ public function _get_incidents($where = array())
"title" => $category_item['categorytitle']
)
);
}
else
}
else
{
$xml->startElement('category');
$xml->writeElement('id',$category_item['cid']);
@@ -519,7 +518,7 @@ public function _get_incidents($where = array())
$xml->startElement('comments');

$json_report_comments[$item->incident_id] = array();

// Check if the incident id exists
if (isset($comment_items[$item->incident_id]))
{
@@ -530,8 +529,8 @@ public function _get_incidents($where = array())
$json_report_comments[$item->incident_id][] = array(
"comment"=> $comment_item
);
}
else
}
else
{
$xml->startElement('comment');
$xml->writeElement('id',$comment_item['id']);
@@ -547,10 +546,9 @@ public function _get_incidents($where = array())

// End comments
$xml->endElement();



$json_report_media[$item->incident_id] = array();

if (count($media_items) > 0)
{
if (isset($media_items[$item->incident_id]) AND count($media_items[$item->incident_id]) > 0)
@@ -559,12 +557,21 @@ public function _get_incidents($where = array())

foreach ($media_items[$item->incident_id] as $media_item)
{

$url_prefix = url::base().Kohana::config('upload.relative_directory').'/';

// If our media is not an image, we don't need to show an upload path
if ($media_item['mediatype'] != 1)
{
$upload_path = "";
$upload_path = '';
}
elseif ($media_item['mediatype'] == 1 AND valid::url($media_item['medialink']) == TRUE)
{
// If our media is an img and is a valid URL, don't show the upload path or prefix
$upload_path = '';
$url_prefix = '';
}

$url_prefix = url::base().Kohana::config('upload.relative_directory').'/';
if($this->response_type == 'json')
{
$json_report_media[$item->incident_id][] = array(
@@ -580,45 +587,45 @@ public function _get_incidents($where = array())
// Grab that last key up there
$add_to_key = key($json_report_media[$item->incident_id]) + 1;

// Give a full absolute URL to the image
// Give a full absolute URL to the image
$json_report_media[$item->incident_id][$add_to_key]["thumb_url"] = $url_prefix.$upload_path.$media_item['mediathumb'];

$json_report_media[$item->incident_id][$add_to_key]["link_url"] = $url_prefix.$upload_path.$media_item['medialink'];
}
}
else
}
else
{
$xml->startElement('media');

if( $media_item['mediaid'] != "" )
{
$xml->writeElement('id',$media_item['mediaid']);
}

if( $media_item['mediatitle'] != "" )
{
$xml->writeElement('title', $media_item['mediatitle']);
}

if( $media_item['mediatype'] != "" )
{
$xml->writeElement('type', $media_item['mediatype']);
}
if( $media_item['medialink'] != "" )

if( $media_item['medialink'] != "" )
{
$xml->writeElement('link', $upload_path.$media_item['medialink']);
}
if( $media_item['mediathumb'] != "" )

if( $media_item['mediathumb'] != "" )
{
$xml->writeElement('thumb', $upload_path.$media_item['mediathumb']);
}

if( $media_item['mediathumb'] != "" AND $media_item['mediatype'] == 1 )
{
$add_to_key = key($json_report_media[$item->incident_id]) + 1;

$xml->writeElement('thumb_url', $url_prefix.$upload_path.$media_item['mediathumb']);

$xml->writeElement('link_url', $url_prefix.$upload_path.$media_item['medialink']);
@@ -629,9 +636,9 @@ public function _get_incidents($where = array())
$xml->endElement(); // Media
}
}

$xml->endElement(); // End incident

// Check for response type
if ($this->response_type == 'json')
{
@@ -648,14 +655,14 @@ public function _get_incidents($where = array())
"locationname" => $item->location_name,
"locationlatitude" => $item->latitude,
"locationlongitude" => $item->longitude
),
"categories" => $json_report_categories[$item->incident_id],
),
"categories" => $json_report_categories[$item->incident_id],
"media" => $json_report_media[$item->incident_id],
"comments" => $json_report_comments[$item->incident_id]
);
}
}

// Create the JSON array
$data = array(
"payload" => array(
@@ -664,13 +671,13 @@ public function _get_incidents($where = array())
),
"error" => $this->api_service->get_error_msg(0)
);

if ($this->response_type == 'json')
{
return $this->array_as_json($data);

}
else
}
else
{
$xml->endElement(); //end incidents
$xml->endElement(); // end payload
@@ -688,7 +695,7 @@ public function _get_incidents($where = array())
* Returns the number of reports in each category
*/
private function _get_incident_counts_per_category()
{
{
$this->query = 'SELECT category_id, COUNT(category_id) AS reports FROM '.$this->table_prefix.'incident_category '
. 'WHERE incident_id IN (SELECT id FROM '.$this->table_prefix.'incident WHERE incident_active = 1) '
. 'GROUP BY category_id';
@@ -729,8 +736,7 @@ private function _get_incident_counts_per_category()

echo $this->response_data;
}



/**
* Get incidents within a certain lat,lon bounding box
*
@@ -753,7 +759,7 @@ private function _get_incidents_by_bounds($sw, $ne, $c = 0)
{
$northeast = explode(",",$ne);
}

// To hold the parameters
$params = array();
if ( count($southwest) == 2 AND count($northeast) == 2 )
@@ -762,7 +768,7 @@ private function _get_incidents_by_bounds($sw, $ne, $c = 0)
$lon_max = (float) $northeast[0];
$lat_min = (float) $southwest[1];
$lat_max = (float) $northeast[1];

// Add parameters
array_push($params,
'l.latitude >= '.$lat_min,
@@ -771,22 +777,22 @@ private function _get_incidents_by_bounds($sw, $ne, $c = 0)
'l.longitude <= '.$lon_max
);
}

// Fix for pulling categories using the bounding box
// Credits to Antonoio Lettieri http://github.com/alettieri
// Check if the specified category id is valid
if (Category_Model::is_valid_category($c))
{
array_push($params, 'c.id = '.$c);
}

return $this->_get_incidents($params);

}

/**
* Gets the number of approved reports
*
*
* @param string response_type - XML or JSON
* @return string
*/
@@ -827,7 +833,7 @@ public function get_incident_count()
? $this->array_as_json($data)
: $this->array_as_xml($data, $this->replar);
}

/**
* Get an approximate geographic midpoint of al approved reports.
*
@@ -838,11 +844,11 @@ public function get_geographic_midpoint()
{
$json_latlon = array();

$this->query = 'SELECT AVG( latitude ) AS avglat, AVG( longitude )
AS avglon FROM '.$this->table_prefix.'location WHERE id IN
(SELECT location_id FROM '.$this->table_prefix.'incident WHERE
$this->query = 'SELECT AVG( latitude ) AS avglat, AVG( longitude )
AS avglon FROM '.$this->table_prefix.'location WHERE id IN
(SELECT location_id FROM '.$this->table_prefix.'incident WHERE
incident_active = 1)';

$items = $this->db->query($this->query);

foreach ($items as $item)
@@ -851,24 +857,24 @@ public function get_geographic_midpoint()
$longitude = $item->avglon;
break;
}

if ($this->response_type == 'json')
{
$json_latlon[] = array(
"latitude" => $latitude,
"latitude" => $latitude,
"longitude" => $longitude
);
}
else
{
$json_latlon['geographic_midpoint'] = array(
"latitude" => $latitude,
"latitude" => $latitude,
"longitude" => $longitude
);

$replar[] = 'geographic_midpoint';
}

// Create the JSON array
$data = array(
"payload" => array(