Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Fixing the filterArgs initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Jan 20, 2014
1 parent 57a4ddc commit 6ae2212
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Model/Behavior/SearchableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,22 @@ class SearchableBehavior extends ModelBehavior {
public function setup(Model $Model, $config = array()) {
$this->_defaults = array_merge($this->_defaults, (array)Configure::read('Search.Searchable'));
$this->settings[$Model->alias] = array_merge($this->_defaults, $config);
}

/**
* Prepares the filter args based on the model information and calls
* Model::getFilterArgs if present to set up the filterArgs with proper model
* aliases.
*
* @param Model $Model
* @return boolean|array
*/
public function setupFilterArgs(Model $Model) {
if (method_exists($Model, 'getFilterArgs')) {
$Model->getFilterArgs();
}
if (empty($Model->filterArgs)) {
return;
return false;
}
foreach ($Model->filterArgs as $key => $val) {
if (!isset($val['name'])) {
Expand All @@ -58,6 +72,7 @@ public function setup(Model $Model, $config = array()) {
$Model->filterArgs[$key]['type'] = 'value';
}
}
return $Model->filterArgs;
}

/**
Expand All @@ -70,7 +85,9 @@ public function setup(Model $Model, $config = array()) {
* @return array Array of conditions that express the conditions needed for the search
*/
public function parseCriteria(Model $Model, $data) {
$this->setupFilterArgs($Model);
$conditions = array();

foreach ($Model->filterArgs as $field) {
// If this field was not passed and a default value exists, use that instead.
if (!array_key_exists($field['name'], $data) && array_key_exists('defaultValue', $field)) {
Expand Down Expand Up @@ -120,6 +137,8 @@ public function validateSearch(Model $Model, $data = null) {
* @return array, filtered args
*/
public function passedArgs(Model $Model, $vars) {
$this->setupFilterArgs($Model);

$result = array();
foreach ($vars as $var => $val) {
if (in_array($var, Set::extract($Model->filterArgs, '{n}.name'))) {
Expand Down

0 comments on commit 6ae2212

Please sign in to comment.