Add initial API Filter component

The purpose of this is to turn ZoneMinder filter queries into usable
CakePHP find() conditions.
As example would be turning
api/events/index/MonitorId:7/MonitorId:8/StartTime >=:2015-01-05.json
into an array like this:
'conditions' => array(
	'MonitorId' => (7, 8),
	'StartTime >=' => '2015-01-05'
)
This commit is contained in:
Kyle Johnson 2015-01-05 14:02:42 -05:00
parent 9221675d2e
commit 040094e984
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<?php
App::uses('Component', 'Controller');
class FilterComponent extends Component {
// Build a CakePHP find() condition based on the named parameters
// that are passed in
public function buildFilter($namedParams) {
if ($namedParams) {
$conditions = array();
foreach ($namedParams as $attribute => $value) {
// If the named param contains an array, we want to turn it into an IN condition
// Otherwise, we add it right into the $conditions array
if (is_array($value)) {
$array = array();
foreach ($value as $term) {
array_push($array, $term);
}
$query = array($attribute => $array);
array_push($conditions, $query);
} else {
array_push($conditions, array($attribute => $value));
}
}
}
return $conditions;
}
}
?>