Dotclear

Changeset 755:38c4f75f3dd5


Ignore:
Timestamp:
11/10/11 10:03:55 (14 years ago)
Author:
Dsls <dsls@…>
Branch:
formfilters
Message:

Intermediate commit for form filters, breaks columns for the moment.

More to come soon...

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • admin/posts.php

    r523 r755  
    151151$core->callBehavior('adminPostsFilters',$filterSet); 
    152152 
    153 $filterSet->setFormValues($_GET); 
     153$filterSet->setup($_GET,$_POST); 
    154154 
    155155# Get posts 
     
    171171} 
    172172 
    173 $filterSet->setColumnsForm($post_list->getColumnsForm()); 
     173$filterSet->setExtraData($post_list->getColumnsForm()); 
    174174 
    175175/* DISPLAY 
  • inc/admin/class.dc.filter.php

    r580 r755  
    2020class dcFilterSet { 
    2121 
    22      protected $filters;           /// <b>array</b> lists of defined filters 
     22     protected $lfilters;               /// <b>array</b> lists of defined filters 
     23     protected $efilters;               /// <b>array</b> lists of defined filters 
    2324     protected $form_prefix;       /// <b>string</b> displayed form prefix 
    2425     protected $action;            /// <b>string</b> form action page 
    2526     protected $hideform;          /// <b>boolean</b> start form display hidden by default or not 
    26      protected $columns_form; /// <b>string</b> columns form 
     27     protected $extra_data;   /// <b>string</b> columns form 
    2728     protected $name;              /// <b>string</b> fieldset name 
     29      
    2830     /** 
    2931     Inits dcFilterSet object 
     
    3537          $this->name = $name; 
    3638          $this->form_prefix=$form_prefix; 
    37           $this->filters = array(); 
     39          $this->lfilters = new ArrayObject(); 
     40          $this->efilters = new ArrayObject(); 
    3841          $this->action = $action; 
    39      } 
    40  
     42          $this->extra_data = ''; 
     43     } 
     44      
    4145     /** 
    4246     Adds a new filter to list 
     
    4650     public function addFilter (Filter $filter) { 
    4751          $filter->setFormPrefix($this->form_prefix); 
    48           $this->filters[$filter->id] = $filter; 
     52          $filter->setFilterSet($this); 
     53          $this->efilters[$filter->id] = $filter; 
     54          $this->lfilters[$filter->id] = clone $filter; 
    4955          return $this; 
    5056     } 
     
    5662          $ser = array(); 
    5763          $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); 
    58           foreach($this->filters as $filter) { 
     64          foreach($this->lfilters as $filter) { 
    5965               $ser[$filter->id]=$filter->serialize(); 
    6066          } 
     
    7076          $settings = !is_null($ws->{$this->name}) ? unserialize($ws->{$this->name}) : array(); 
    7177          foreach($settings as $k => $v) { 
    72                $this->filters[$k]->unserialize($v); 
     78               $this->lfilters[$k]->unserialize($v); 
    7379          } 
    7480     } 
     
    8086     @param    form_data <b>array</b>   form values (usually $_GET or $_POST) 
    8187     */ 
    82      public function setFormValues ($form_data) { 
     88     protected function initializeFromData ($filters, $form_data) { 
    8389          $this->hideform = true; 
    84           if (isset($form_data['clear_filters'])) { 
    85                $this->saveFilters(); 
    86                return; 
    87           } 
    88           if (!isset($form_data['apply'])) { 
    89                $this->loadFilters(); 
    90           } 
    91           foreach ($this->filters as $filter) { 
    92                $filter->setFormValues ($form_data); 
    93                if ($filter->isEnabled()) { 
    94                     $this->hideform=false; 
    95                } 
    96           } 
    97           if (isset($form_data['apply'])) { 
    98                if (trim($form_data['apply']) == '+' 
    99                     && isset($form_data['add_filter'])  
    100                     && isset($this->filters[$form_data['add_filter']])) { 
    101                     $this->filters[$form_data['add_filter']]->add(); 
    102                     $this->hideform=false; 
    103                } 
    104           } 
    105           $this->saveFilters(); 
     90          foreach ($filters as $filter) { 
     91               $filter->initializeFromData ($form_data); 
     92          } 
    10693     } 
    10794      
     
    11198     @param    html <b>string</b>       the code to add 
    11299     */ 
    113      public function setColumnsForm($html) 
     100     public function setExtraData($html) 
    114101     { 
    115           $this->columns_form = $html; 
     102          $this->extra_data = $html; 
    116103     } 
    117104      
     
    123110     public function getFormFieldsAsHidden() { 
    124111          $ret=''; 
    125           foreach ($this->filters as $filter) { 
     112          foreach ($this->lfilters as $filter) { 
    126113               $ret.= $filter->getFormFieldAsHidden(); 
    127114          } 
     
    130117 
    131118     /** 
     119     Sets up filterset from $get and $post parameters 
     120      
     121     */ 
     122     public function setup($get,$post) { 
     123          /* Use cases : 
     124               (1) $post not empty for formfilter fields : 
     125                    * efilters are set from $post 
     126                    * lfilters are set from $get 
     127                    * keep filters div shown 
     128               (2) $post empty :  
     129                    * both efilters and lfilters are set from $get 
     130                    * hide filter div 
     131          */ 
     132          $action = false; 
     133          $allowed_actions = array('clear_filters','add','del_','apply','reset'); 
     134          // Fetch each $post parameter to see whether filters are concerned. 
     135          // Only 1 action at a time is allowed. 
     136          foreach ($post as $k => $v) { 
     137               if (strpos($k,$this->form_prefix)===0) { 
     138                    $tmp = substr($k,strlen($this->form_prefix)); 
     139                    foreach ($allowed_actions as $a) { 
     140                         if (strpos($tmp,$a)===0) { 
     141                              $action = $tmp; 
     142                              break; 
     143                         } 
     144                    } 
     145               } 
     146          } 
     147          if ($action !== false) { 
     148               // Use case (1) 
     149               if ($action != 'clear_filters' && $action != 'reset')  { 
     150                    $this->initializeFromData($this->efilters,$post); 
     151                    if ($action == 'add'){ 
     152                         if (isset($post['add_filter'])  
     153                              && isset($this->efilters[$post['add_filter']])) { 
     154                              echo 'addadd'; 
     155                         $this->efilters[$post['add_filter']]->add(); 
     156                         } 
     157                    } elseif (strpos($action,'del_') === 0) { 
     158 
     159                         $count = preg_match('#del_(.+)_([0-9]+)#',$action,$match); 
     160                         if (($count == 1) && isset($this->efilters[$match[1]])) { 
     161                              $this->efilters[$match[1]]->remove($match[2]); 
     162                         } 
     163                    } elseif ($action=="apply") { 
     164                         http::redirect($this->action.'?'.http_build_query($this->getFiltersAsParams($this->efilters),'','&')); 
     165                         exit; 
     166                    } 
     167               } 
     168               if (isset($post[$this->form_prefix."query"])) { 
     169                    parse_str($post[$this->form_prefix."query"],$out); 
     170                    $this->initializeFromData($this->lfilters,$out); 
     171                    if ($action == 'reset') { 
     172                         $this->initializeFromData($this->efilters,$out); 
     173                    } 
     174               } 
     175               $this->hideform=false; 
     176          } else { 
     177               $this->initializeFromData($this->efilters, $get); 
     178               $this->initializeFromData($this->lfilters, $get); 
     179          } 
     180     } 
     181     /** 
    132182     Retrieves filterset generated form 
    133183      
    134184     @param    method    <b>string</b>       form method to use (default: "get") 
    135185     */ 
    136      public function getForm($method="get") { 
     186     public function getForm() { 
    137187          $ret = ''; 
    138188           
     
    142192               $formclass=''; 
    143193          } 
    144           $ret .= '<p><img alt="" src="images/minus.png" /> <a href="#" id="toggle-filters">'.__('Toggle filters and display options').'</a></p>'; 
     194          $ret .= '<p><img alt="" src="images/minus.png" /> '. 
     195               '<a href="#" id="toggle-filters">'. 
     196               __('Toggle filters and display options'). 
     197               '</a></p>'; 
     198           
    145199          $ret .= 
    146200               '<div class="two-cols">'. 
    147                '<form id="filters" action="'.$this->action.'" method="'.$method.'"'.$formclass.'>'. 
     201               '<form id="filters" action="'.$this->action.'" method="post"'.$formclass.'>'. 
    148202               '<div class="col70">'. 
    149                '<h3>'.__('Entries filters').'</h3>'; 
    150                 
     203               '<h3>'.__('Entries filters').'</h3>'. 
     204               '<table summary="'.__('Query filters').'" id="tfilters">'. 
     205               '<tbody>'; 
    151206          $count=0; 
    152207          $form_combo=array(); 
    153208          $form_combo['-']=''; 
    154           if (count($this->filters)) { 
    155                $ret .= '<ul>'; 
    156                foreach ($this->filters as $filter) { 
     209          if (count($this->efilters)) { 
     210               foreach ($this->efilters as $filter) { 
    157211                    if ($filter->isEnabled()) { 
    158212                         $ret .= $filter->getFormLine(); 
     
    161215                    $count++; 
    162216               } 
    163                $ret .= '</ul>'; 
    164           } 
    165           $ret .=  
    166                '<p class="clear"><input class="delete" type="submit" value="'.__('Delete all filters').'" name="clear_filters" /></p>'. 
     217          } 
     218          $ret .= '</tbody></table>'. 
     219               '<p class="clear"><input class="delete" type="submit" value="'.__('Delete all filters').'" name="'. 
     220               $this->form_prefix.'clear_filters" />'. 
     221               '&nbsp;<input  type="submit" value="'.__('Reset').'" name="'. 
     222               $this->form_prefix.'reset" /></p>'. 
    167223               '<h3 class="margintop">'.__('Add a filter').'</h3>'. 
    168224               '<p id="available_filters">'. 
    169225               form::combo("add_filter",$form_combo). 
    170                '<input type="submit" value=" + " title="'.__('Add this filter').'" name="apply" />'. 
     226               '<input type="submit" value=" + " title="'.__('Add this filter').'" name="'.$this->form_prefix.'add" />'. 
    171227               '</p>'. 
    172                '</div>'. 
    173                '<div class="col30">'. 
    174                $this->columns_form. 
    175                '</div>'. 
    176                '<p class="clear margintop"><input type="submit" value="'.__('Apply filters and display options').'" name="apply" /></p>'. 
    177  
    178                '</form></div>'; 
     228               '</div>'; 
     229          if ($this->extra_data != '') { 
     230               $ret .= 
     231                    '<div class="col30">'. 
     232                    $this->extra_data. 
     233                    '</div>'; 
     234          } 
     235          $ret .= 
     236               '<p class="clear margintop">'. 
     237               '<input type="submit" value="'.__('Apply filters and display options'). 
     238               '" name="'.$this->form_prefix.'apply" /></p>'. 
     239               form::hidden($this->form_prefix."query",http_build_query($this->getFiltersAsParams($this->lfilters))). 
     240               $GLOBALS['core']->formNonce(). 
     241               '</form>'. 
     242               '</div>'; 
     243          return $ret; 
     244     } 
     245     /** 
     246     Retrieves the filters values as parameters 
     247      
     248     @param    filters   <b>array</b>   list of concerned filters 
     249      
     250     @return   <b>array</b>   the array of parameters 
     251 
     252     */ 
     253 
     254     protected function getFiltersAsParams($filters) { 
     255          $arr = new ArrayObject(); 
     256          foreach ($filters as $f) { 
     257               if ($f->isEnabled()) 
     258                    $f->fillQS($arr); 
     259          } 
     260          return $arr; 
     261     } 
     262      
     263     protected function displayFilters($filters) { 
     264          $ret = '<ul>'; 
     265          foreach ($filters as $f) { 
     266               if ($f->isEnabled()) 
     267               $ret .= '<li>'.$f->getAsText().'</li>'."\n"; 
     268          } 
     269          $ret .= '</ul>'; 
    179270          return $ret; 
    180271     } 
     
    205296     public function applyFilters($params) { 
    206297          $filtered = false; 
    207           foreach ($this->filters as $filter) { 
     298          foreach ($this->lfilters as $filter) { 
    208299               if ($filter->isEnabled()) { 
    209300                    $filter->applyFilter($params); 
     
    214305     } 
    215306      
     307     public function getDelName($field_id,$pos) { 
     308          return $this->form_prefix.'del_'.$field_id.'_'.$pos; 
     309     } 
    216310} 
    217311 
     
    226320*/ 
    227321abstract class Filter { 
     322     public $filterset;            ///<b>string</b> filterset parent 
    228323     public $id;                        ///<b>string</b> field id (local to fieldset) 
    229324     public $name;                 ///<b>string</b> filter name 
     
    251346      
    252347     /** 
     348     Defines the filterset containing this filter 
     349      
     350     @param    prefix         <b>dcFilterset</b>  the filterset 
     351     */ 
     352     public function setFilterSet($fs) { 
     353          $this->filterset = $fs; 
     354     } 
     355      
     356     /** 
     357     Defines form prefix for filter 
     358      
     359     @param    prefix         <b>string</b>  the form prefix 
     360     */ 
     361     public function setFormPrefix($prefix) { 
     362          $this->field_id = $prefix.$this->id; 
     363     } 
     364      
     365     /** 
    253366     Get a field id 
    254367      
     
    282395      
    283396     /** 
    284      Defines form prefix for filter 
    285       
    286      @param    prefix         <b>string</b>  the form prefix 
    287      */ 
    288      public function setFormPrefix($prefix) { 
    289           $this->field_id = $prefix.$this->id; 
    290      } 
    291       
     397     Removes a value from filter 
     398     */ 
     399     public function remove($pos) { 
     400          if (isset($this->values[$pos])) { 
     401               array_splice($this->values,$pos,1); 
     402               $this->enabled = (count($this->values)!=0); 
     403          } 
     404     } 
    292405      
    293406     /** 
    294407     Returns HTML code for form field 
    295408      
    296      @param    pos       <b>integer</b> position of the field to display (in case of multiple values) 
     409     @param    pos       <b>integer</b> position of the field to display  
     410                                             (in case of multiple values) 
    297411     @return <b>string</b> the html code 
    298412     */ 
    299      public function getFormFields($pos=0) { 
    300           return ''; 
    301      } 
    302       
    303      /** 
    304      Returns filter values il a serialized way (array) 
    305       
    306      @return        <b>array</b>   serialized data 
    307      */ 
    308      public function serialize() { 
    309           return array( 
    310                'values' => $this->values, 
    311                'enabled' => $this->enabled 
    312           ); 
    313      } 
    314       
    315      /** 
    316      Defines filter values from serialized data (array) 
    317      To be used in conjunction with serialize method 
    318       
    319      @param    $data     <b>array</b>   serialized data to retrieve 
    320      */ 
    321      public function unserialize ($data) { 
    322           $this->values = $data['values']; 
    323           $this->enabled = $data['enabled']; 
    324      } 
    325       
    326      /** 
    327      Set filter values from form_data (usually $_GET)   
    328      @param    $form_data     <b>array</b>   form data 
    329      */ 
    330      public function setFormValues($form_data) { 
     413     abstract protected function getFormFields($pos=0); 
     414      
     415     /** 
     416     Extract values from data (data being an array, such as $_GET or $_POST) 
     417      
     418     @param    $data     <b>array</b>   data to parse 
     419     @return   <b>array</b>   field values 
     420      
     421     */ 
     422     protected function getValuesFromData($data) { 
    331423          $count=0; 
    332           while (isset($form_data[$this->getFieldId($count)])) { 
    333                if (!isset($form_data['del_'.$this->getFieldId($count)])) { 
    334                     $this->values[$count] = $form_data[$this->getFieldId($count)]; 
    335                } elseif (isset($this->values[$count])) { 
    336                     unset($this->values[$count]); 
    337                } 
     424          $arr = array(); 
     425          while (isset($data[$this->getFieldId($count)])) { 
     426               $arr[$count] = $data[$this->getFieldId($count)]; 
    338427               $count++; 
    339  
    340           } 
    341           $this->values = array_values($this->values); 
     428          } 
     429          return $arr; 
     430     } 
     431      
     432     public function initializeFromData($form_data) { 
     433          $this->values = $this->getValuesFromData($form_data); 
    342434          $this->enabled = (count($this->values)!=0); 
    343435     } 
    344436      
    345           /** 
     437     /** 
     438     Returns HTML code for the hole filter lines 
     439      
     440     @return <b>string</b> the html code 
     441     */ 
     442      
     443     public function getFormLine() { 
     444          $ret=''; 
     445          for ($cur=0; $cur < count($this->values); $cur++) { 
     446               $ret .= '<tr class="'.$this->id.'">'; 
     447               $del_id = $this->filterset->getDelName($this->id,$cur); 
     448               $ret .= '<td><input id="'.$del_id.'" class="delete" '. 
     449                         'type="submit" title="Delete the following filter : " value=" - " name="'.$del_id.'"/></td>'. 
     450                         $this->getFormFields($cur); 
     451               $ret .= '</tr>'; 
     452          } 
     453          return $ret; 
     454     } 
     455      
     456     public function fillQS($arr) { 
     457          for ($cur=0; $cur < count($this->values); $cur++) { 
     458               $arr[$this->getFieldId($cur)]=$this->values[$cur]; 
     459          } 
     460     } 
     461      
     462     /** 
    346463     Returns form fields as hidden fields 
    347464      
     
    354471          } 
    355472     } 
    356      /** 
    357      Returns HTML code for the hole filter lines 
    358       
    359      @return <b>string</b> the html code 
    360      */ 
    361       
    362      public function getFormLine() { 
    363           $ret=""; 
    364           for ($cur=0; $cur < count($this->values); $cur++) { 
    365                $ret .= '<li id="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'. 
    366                     $this->getFormFields($cur). 
    367                     '<input id="del_'.$this->getFieldId($cur).'" class="delete" '. 
    368                     'type="submit" title="Delete the following filter : " value=" - " name="del_'.$this->getFieldId($cur).'"/>'. 
    369                     '</li>'; 
    370           } 
    371           return $ret; 
    372      } 
    373473      
    374474     /** 
     
    387487          return $this->values; 
    388488     } 
     489 
     490     public abstract function getAsText(); 
     491 
    389492      
    390493} 
     
    412515     } 
    413516      
     517     protected function getValuesFromData($data) { 
     518          $val = parent::getValuesFromData($data); 
     519          if (isset($data[$this->field_id.'_v'])) { 
     520               $verb = $data[$this->field_id.'_v']; 
     521          } else { 
     522               $verb = "is"; 
     523          } 
     524          $arr = array( 
     525               'values' => $val, 
     526               'verb' => $verb 
     527          ); 
     528          return $arr; 
     529     } 
     530      
    414531     public function add() { 
    415532          parent::add(); 
     
    434551     } 
    435552      
    436      public function setFormValues($form_data) { 
    437           parent::setFormValues($form_data); 
    438           if (isset($form_data[$this->field_id."_v"])) { 
    439                $this->verb = ($form_data[$this->field_id."_v"] == 'is') ? 'is' : 'isnot'; 
    440           } 
     553     public function initializeFromData($form_data) { 
     554          $arr = $this->getValuesFromData($form_data); 
     555          $this->values = $arr['values']; 
     556          $this->verb = $arr['verb']; 
     557          $this->enabled = (count($this->values) != 0); 
     558     } 
     559 
     560     public function getFormFields($pos=0) { 
     561          if ($pos == 0) { 
     562               $ret = '<td id="'.$this->getFieldId($cur).'" title="'.$this->desc.'" class="filter-title">'. 
     563                    ''.$this->desc.' : </td>'. 
     564                    '<td>'. 
     565                    form::combo($this->field_id.'_v', 
     566                         array(__('is')=>'is',__('is not')=>'isnot'),$this->verb,'','', 
     567                         false,'title="'.sprintf(__('%s is or is not'),$this->desc).'"'). 
     568                    '</td>'; 
     569          } else { 
     570               $ret = '<td id="'.$this->getFieldId($cur).'" title="or" colspan="2" class="or">'. 
     571                    __('or').' : </td>'; 
     572          }; 
     573          $ret .= '<td>'.form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 
     574               '','',false,'title="'.__('Choose an option').'"').'</td>'; 
     575          return $ret; 
    441576     } 
    442577 
     
    444579          return parent::getFormFieldAsHidden().form::hidden($this->field_id."_v",$this->verb); 
    445580     } 
    446  
    447      public function getFormFields($pos=0) { 
     581      
     582     public function fillQS($arr) { 
     583          parent::fillQS($arr); 
    448584           
    449           if ($pos == 0) { 
    450                $desc = $this->desc.' : '; 
    451                $labelclass="filter-title"; 
    452           } else { 
    453                $desc = __('or'); 
    454                $labelclass = 'or'; 
    455           }; 
    456           return '<span class="'.$labelclass.'">'.$desc.'</span>'. 
    457                (($pos == 0)  
    458                     ?form::combo($this->field_id.'_v', 
    459                          array(__('is')=>'is',__('is not')=>'isnot'),$this->verb,'','', 
    460                          false,'title="'.sprintf(__('%s is or is not'),$this->desc).'"')  
    461                     :''). 
    462                form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 
    463                     '','',false,'title="'.__('Choose an option').'"'); 
     585          $arr[$this->field_id.'_v']=$this->verb; 
    464586     } 
    465587      
     
    473595          else 
    474596               $params[$attr]=$this->values; 
     597} 
     598      
     599     public function getValues() { 
     600          return array_merge($this->values,array($this->field_id.'_v',$this->verb)); 
     601     } 
     602      
     603     public function getAsText() { 
     604          return sprintf("%s %s %s",$this->desc,$this->verb,join(',',$this->values)); 
    475605     } 
    476606} 
     
    502632 
    503633     public function getFormFields($pos=0) { 
    504           return '<span class="'.$labelclass.'">'.$this->desc.'</span>'. 
     634          return '<td><span class="'.$labelclass.'">'.$this->desc.'</span></td><td>'. 
    505635               form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 
    506                     '','',false,'title="'.__('Choose an option').'"'); 
     636                    '','',false,'title="'.__('Choose an option').'"').'</td>'; 
    507637     } 
    508638      
    509639     public function applyFilter($params) { 
    510640          $params[$this->request_param]=$this->values[0]; 
     641     } 
     642      
     643     public function getAsText() { 
     644          return sprintf("%s %s",$this->desc,$this->values[0]); 
    511645     } 
    512646} 
     
    546680     } 
    547681 
    548      public function getValue() { 
    549           $v = parent::getValue(); 
    550           return $v[0]; 
     682     public function getAsText() { 
     683          return sprintf("%s %s",$this->desc,$this->values[0]); 
    551684     } 
    552685      
Note: See TracChangeset for help on using the changeset viewer.

Sites map