From 424cb0ea4f469605cdb5a2384d738ef8300afdf1 Mon Sep 17 00:00:00 2001 From: abijah Date: Thu, 30 Jul 2009 20:59:33 +0000 Subject: [PATCH] Tweaked filtering to allow for non-trivial conditions git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716@431 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/app_controller.php | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/site/app_controller.php b/site/app_controller.php index 8fa66bc..30c93f5 100644 --- a/site/app_controller.php +++ b/site/app_controller.php @@ -306,8 +306,8 @@ class AppController extends Controller { if (empty($params['post']['filter'])) return $query; - foreach ($params['post']['filter'] AS $filter => $value) { - $split = $this->gridDataFilterSplit($params, $model, $filter); + foreach ($params['post']['filter'] AS $filter => $filter_value) { + $split = $this->gridDataFilterSplit($params, $model, $filter, $filter_value); /* pr(array('AppController::gridDataFilterTable' => */ /* array('checkpoint' => "Filter split") */ @@ -379,19 +379,16 @@ class AppController extends Controller { if (empty($params['post']['filter'])) return $conditions; - foreach ($params['post']['filter'] AS $filter => $value) { - $split = $this->gridDataFilterSplit($params, $model, $filter); + foreach ($params['post']['filter'] AS $filter => $filter_value) { + $split = $this->gridDataFilterSplit($params, $model, $filter, $filter_value); $table = $this->gridDataFilterConditionsTable($params, $model, $split['table']); - if (!$table) - continue; - $key = $this->gridDataFilterConditionsKey($params, $model, $split['table'], $split['field']); if (!$key) continue; $conditions[] - = $this->gridDataFilterConditionsStatement($params, $model, $split['table'], $key, $value); + = $this->gridDataFilterConditionsStatement($params, $model, $table, $key, $split['value']); } return $conditions; @@ -411,24 +408,39 @@ class AppController extends Controller { } function gridDataFilterConditionsStatement(&$params, &$model, $table, $key, $value) { - return array("{$table}.{$key}" => $value); + $key = (empty($table)?"":"{$table}.") . $key; + if (isset($value)) + return array($key => $value); + else + return array($key); } - function gridDataFilterSplit(&$params, &$model, $filter) { + function gridDataFilterSplit(&$params, &$model, $filter, $value) { + if (!is_string($filter)) { + // only a filter condition was set, not filter=>value + $filter = $value; + $value = null; + } + $table = $field = null; - if (preg_match("/\./", $filter)) { + if (preg_match("/^\w+\.\w+$/", $filter)) { list($table, $field) = explode(".", $filter); } - elseif (preg_match('/^[A-Z]/', $filter)) { + elseif (preg_match('/^[A-Z][A-Za-z]*$/', $filter)) { $table = $filter; $field = null; } - else { + elseif (preg_match('/^\w+$/', $filter)) { $table = $model->alias; $field = $filter; } + else { + // $filter must be a function or other complicated condition + $table = null; + $field = $filter; + } - return array('table' => $table, 'field' => $field); + return array('table' => $table, 'field' => $field, 'value' => $value); } function gridDataFilterTableName(&$params, &$model, $table) {