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:
parent
9221675d2e
commit
040094e984
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Reference in New Issue