]> git.andy128k.dev Git - ipf.git/commitdiff
admin: combined filters and searches
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 1 Feb 2014 12:38:38 +0000 (14:38 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 1 Feb 2014 12:38:38 +0000 (14:38 +0200)
ipf/admin/model.php
ipf/admin/templates/admin/items.html
ipf/http/url.php
ipf/project_template.php

index 66a13289b47c77d24f56c2bc3305ec9aeacb2299..9253fe06c66a595dad6d27ec1a91dcd13041daee 100644 (file)
@@ -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));
index c64d553a91c2b1236a5518bcd41dd93b850ff746..e6d8e6a00535ce612c7298e2b1c5ec64fe168e6a 100644 (file)
       <div id="toolbar">
         <form id="changelist-search" method="get" action="">
           <div>
+            {foreach $request->GET as $k => $v}
+              {if $k !== 'q'}
+                <input type="hidden" name="{$k}" value="{$v}">
+              {/if}
+            {/foreach}
             <label for="searchbar">
               <img alt="Search" src="{$ADMIN_MEDIA_URL}img/icon_searchbox.png" />
             </label>
             <input id="searchbar" type="text" value="{$search_value}" name="q" size="40"/>
             <input type="submit" value="Go"/>
             {if $search_value}
-            <span class="small quiet">{$pager->getPager()->getNumResults()} results (<a href="{url 'IPF_Admin_Views_ListItems', array($lapp, $lmodel)}">reset</a>)</span>
+            <span class="small quiet">{$pager->getPager()->getNumResults()} results (<a href="?{params $request->GET, 'q', null}">reset</a>)</span>
             {/if}
           </div>
         </form>
@@ -44,7 +49,7 @@
         <h3>{$f->title}</h3>
         <ul>
           {foreach $f->choices as $ch}
-          {block filterchoice}<li{if $ch['selected']} class="selected"{/if}><a href="?{$ch['param']}">{$ch['name']}</a></li>{/block}
+          {block filterchoice}<li{if $ch['selected']} class="selected"{/if}><a href="?{params $request->GET, $ch['param_key'], $ch['param_value']}">{$ch['name']}</a></li>{/block}
           {/foreach}
         </ul>
         {/foreach}
index b4c60ee666d09228291b70dfa03b729623e94a68..0d8fd5baecabf756c1c8b219828dfca5ced4a29d 100644 (file)
@@ -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 ? '&amp;' : '&', $params_list);
+    }
+
     public static function generate($action, $params=array(), $encode=true)
     {
-        if ($encode) {
-            $amp = '&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)
index b336a8e06e7c947a1a9045041eeb1557d9d3196b..5832d026f6320569af480f233126bcf5db1d76ea 100644 (file)
@@ -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()