From b4c8a3991c94717f40597a84863b0cbe4463de41 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 1 Feb 2014 14:38:38 +0200 Subject: [PATCH] admin: combined filters and searches --- ipf/admin/model.php | 66 ++++++++++++++++------------ ipf/admin/templates/admin/items.html | 9 +++- ipf/http/url.php | 26 +++++------ ipf/project_template.php | 33 ++++++++++++++ 4 files changed, 90 insertions(+), 44 deletions(-) diff --git a/ipf/admin/model.php b/ipf/admin/model.php index 66a1328..9253fe0 100644 --- a/ipf/admin/model.php +++ b/ipf/admin/model.php @@ -67,19 +67,22 @@ class BooleanFilter extends BaseListFilter parent::__construct($title, array( array( 'id' => null, - 'param' => '', + 'param_key' => 'filter_'.$this->column, + 'param_value' => null, 'name' => 'All', 'selected' => true, ), array( 'id' => 'y', - 'param' => 'filter_'.$this->column.'=y', + 'param_key' => 'filter_'.$this->column, + 'param_value' => 'y', 'name' => $trueTitle, 'selected' => false, ), array( 'id' => 'n', - 'param' => 'filter_'.$this->column.'=n', + 'param_key' => 'filter_'.$this->column, + 'param_value' => 'n', 'name' => $falseTitle, 'selected' => false, ), @@ -119,7 +122,8 @@ class ListTreeFilter extends BaseListFilter{ $choices = array(); $choices[] = array( 'id'=>null, - 'param'=>'', + 'param_key'=>'filter_'.$this->name, + 'param_value'=>null, 'name'=>'All', 'original_name'=>'All', 'selected'=>false, @@ -178,7 +182,8 @@ class ListTreeFilter extends BaseListFilter{ $choices[] = array( 'id'=>$id, - 'param'=>'filter_'.$this->name.'='.$id, + 'param_key'=>'filter_'.$this->name, + 'param_value'=>$id, 'original_name'=>$o->name, 'name'=>$name, 'selected'=>false, @@ -239,7 +244,8 @@ class DateHierarchyListFilter extends BaseListFilter { $this->choices[] = array( 'name' => $r['v'], 'selected' => $current == $v, - 'param' => 'filter_' . $this->name . '=' . sprintf($this->getFormat(), $v), + 'param_key' => 'filter_' . $this->name, + 'param_value' => sprintf($this->getFormat(), $v), ); } } @@ -255,7 +261,8 @@ class DateHierarchyListFilter extends BaseListFilter { $this->choices[] = array( 'name' => $r['v'], 'selected' => false, - 'param' => 'filter_' . $this->name . '=' . sprintf($format, $v), + 'param_key' => 'filter_' . $this->name, + 'param_value' => sprintf($format, $v), ); } } @@ -287,28 +294,36 @@ class DateHierarchyListFilter extends BaseListFilter { array( 'name' => 'All', 'selected' => !$this->year, - 'param' => ''), + 'param_key' => 'filter_' . $this->name, + 'param_value' => null, + ), ); if ($this->year) { $this->choices[] = array( 'name' => $this->year, 'selected' => true, - 'param' => 'filter_' . $this->name . '=' . $this->year . '-00-00'); + 'param_key' => 'filter_' . $this->name, + 'param_value' => $this->year . '-00-00', + ); } if ($this->month) { $this->choices[] = array( 'name' => $this->monthName, 'selected' => true, - 'param' => 'filter_' . $this->name . '=' . sprintf('%04d-%02d-00', $this->year, $this->month)); + 'param_key' => 'filter_' . $this->name, + 'param_value' => sprintf('%04d-%02d-00', $this->year, $this->month), + ); } if ($this->day) { $this->choices[] = array( 'name' => $this->day, 'selected' => true, - 'param' => 'filter_' . $this->name . '=' . sprintf('%04d-%02d-%02d', $this->year, $this->month, $this->day)); + 'param_key' => 'filter_' . $this->name, + 'param_value' => sprintf('%04d-%02d-%02d', $this->year, $this->month, $this->day), + ); } if ($this->day) { @@ -795,9 +810,8 @@ class IPF_Admin_Model $this->ListItemsQuery(); $this->_GetFilters($request); - $hasSearch = $this->_ListSearchQuery($request); - if (!$hasSearch || IPF::get('admin_filtered_search', false)) - $this->_ListFilterQuery($request); + $this->_ListSearchQuery($request); + $this->_ListFilterQuery($request); $this->ListItemsHeader(); $currentPage = (int)@$request->GET['page']; @@ -853,9 +867,7 @@ class IPF_Admin_Model protected function _isSearch() { - if (method_exists($this,'_searchFields')) - return true; - return false; + return method_exists($this,'_searchFields'); } protected function _ListSearchQuery($request) @@ -874,9 +886,7 @@ class IPF_Admin_Model $whv[] = '%'.$this->search_value.'%'; } $this->q->addWhere($wh,$whv); - return true; } - return false; } protected function _GetFilters($request) @@ -890,10 +900,11 @@ class IPF_Admin_Model $sel_id = @$request->GET['filter_'.$local]; $choices = array(); $choices[] = array( - 'id'=>null, - 'param'=>'', - 'name'=>'All', - 'selected'=>($sel_id==''), + 'id' => null, + 'param_key' => 'filter_'.$local, + 'param_value' => null, + 'name' => 'All', + 'selected' => ($sel_id==''), ); $table = IPF_ORM::getTable($rels[$f]['class']); @@ -911,10 +922,11 @@ class IPF_Admin_Model if ($sel_id==$id) $selected = true; $choices[] = array( - 'id'=>$id, - 'param'=>'filter_'.$local.'='.$id, - 'name'=>(string)$val, - 'selected'=>$selected, + 'id' => $id, + 'param_key' => 'filter_'.$local, + 'param_value' => $id, + 'name' => (string)$val, + 'selected' => $selected, ); } $this->filters[$f] = new ListFilter($local, $foreign, $choices, 'By '.IPF_Utils::humanTitle($f)); diff --git a/ipf/admin/templates/admin/items.html b/ipf/admin/templates/admin/items.html index c64d553..e6d8e6a 100644 --- a/ipf/admin/templates/admin/items.html +++ b/ipf/admin/templates/admin/items.html @@ -24,13 +24,18 @@
@@ -44,7 +49,7 @@

{$f->title}

{/foreach} diff --git a/ipf/http/url.php b/ipf/http/url.php index b4c60ee..0d8fd5b 100644 --- a/ipf/http/url.php +++ b/ipf/http/url.php @@ -2,23 +2,19 @@ class IPF_HTTP_URL { + public static function generateParams($params=array(), $encode=true) + { + $params_list = array(); + foreach ($params as $key => $value) + $params_list[] = urlencode($key).'='.urlencode($value); + return implode($encode ? '&' : '&', $params_list); + } + public static function generate($action, $params=array(), $encode=true) { - if ($encode) { - $amp = '&'; - } else { - $amp = '&'; - } - $url = $action; - if (count($params) > 0) { - $url .= '?'; - $params_list = array(); - foreach ($params as $key=>$value) { - $params_list[] = urlencode($key).'='.urlencode($value); - } - $url .= implode($amp, $params_list); - } - return $url; + if (count($params) > 0) + $action .= '?' . self::generateParams($params, $encode); + return $action; } public static function urlForView($view, $params=array(), $get_params=array(), $encoded=true) diff --git a/ipf/project_template.php b/ipf/project_template.php index b336a8e..5832d02 100644 --- a/ipf/project_template.php +++ b/ipf/project_template.php @@ -30,6 +30,7 @@ final class IPF_Project_Template } $e->tags['url'] = 'IPF_Project_Template_Tag_Url'; + $e->tags['params'] = 'IPF_Project_Template_Tag_Params'; $e->tags['sql'] = 'IPF_Project_Template_Tag_Sql'; // extra tags $e->tags = array_merge(IPF::get('template_tags', array()), $e->tags); @@ -86,6 +87,38 @@ class IPF_Project_Template_Tag_Url extends IPF_Template_Tag } } +class IPF_Project_Template_Tag_Params extends IPF_Template_Tag +{ + private function setParam(&$params, $key, $value) + { + if ($value === null) + unset($params[$key]); + else + $params[$key] = $value; + } + + function start() + { + $params = array(); + + $args = func_get_args(); + $count = count($args); + for ($i = 0; $i < $count; ++$i) { + if (is_array($args[$i])) { + foreach ($args[$i] as $key => $value) + $this->setParam($params, $key, $value); + } else { + $key = $args[$i]; + $value = $args[$i+1]; + $this->setParam($params, $key, $value); + ++$i; + } + } + + echo IPF_HTTP_URL::generateParams($params); + } +} + class IPF_Project_Template_Tag_Sql extends IPF_Template_Tag { function start() -- 2.49.0