Dotclear

Changeset 438:5197b0c2826f for inc/admin


Ignore:
Timestamp:
06/29/11 11:07:52 (14 years ago)
Author:
Dsls <dsls@…>
Branch:
formfilters
Children:
439:f6cd5149f3ad, 450:d6b8e3e74f10
Message:
  • Cleaned up and documented filters code
  • added booleanFilter
  • some l10n added (to be completed)
  • filters are now saved in user preferences
  • fixed a bug in categories filter: cat_id "null ?not" selector now works
File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/class.dc.filter.php

    r417 r438  
    2222# ***** END LICENSE BLOCK ***** 
    2323 
     24/** 
     25@ingroup DC_CORE 
     26@nosubgrouping 
     27@brief Dotclear FilterSet class. 
     28 
     29Dotclear FilterSet handles filters and columns when displaying items lists. 
     30*/ 
    2431class dcFilterSet { 
    2532 
    26      protected $filters; 
    27      protected $form_prefix; 
    28      protected $action; 
    29      protected $hideform; 
    30      protected $columns_form; 
    31       
    32      public function __construct($action,$form_prefix="f_") { 
     33     protected $filters;           /// <b>array</b> lists of defined filters 
     34     protected $form_prefix;       /// <b>string</b> displayed form prefix 
     35     protected $action;            /// <b>string</b> form action page 
     36     protected $hideform;          /// <b>boolean</b> start form display hidden by default or not 
     37     protected $columns_form; /// <b>string</b> columns form 
     38     protected $name;              /// <b>string</b> fieldset name 
     39     /** 
     40     Inits dcFilterSet object 
     41      
     42     @param    core      <b>dcCore</b>       Dotclear core reference 
     43     @param    form_prefix    <b>string</b>       form prefix to use for parameters 
     44     */ 
     45     public function __construct($name,$action,$form_prefix="f_") { 
     46          $this->name = $name; 
    3347          $this->form_prefix=$form_prefix; 
    3448          $this->filters = array(); 
     
    3650     } 
    3751 
     52     /** 
     53     Adds a new filter to list 
     54      
     55     @param    filter         <b>dcFilter</b>          the filter to add 
     56     */ 
    3857     public function addFilter (Filter $filter) { 
    3958          $filter->setFormPrefix($this->form_prefix); 
     
    4160          return $this; 
    4261     } 
    43  
    44      // Retrieves filter values from context 
     62      
     63     /** 
     64     Saves user filters to preferences 
     65     */ 
     66     protected function saveFilters() { 
     67          $ser = array(); 
     68          $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); 
     69          foreach($this->filters as $filter) { 
     70               $ser[$filter->id]=$filter->serialize(); 
     71          } 
     72          $ws->put($this->name,serialize($ser),'string'); 
     73     } 
     74      
     75     /** 
     76     Loads user filters from preferences 
     77     */ 
     78     protected function loadFilters() { 
     79          $ws = $GLOBALS['core']->auth->user_prefs->addWorkspace('filters'); 
     80           
     81          $settings = !is_null($ws->{$this->name}) ? unserialize($ws->{$this->name}) : array(); 
     82          foreach($settings as $k => $v) { 
     83               $this->filters[$k]->unserialize($v); 
     84          } 
     85     } 
     86      
     87     /** 
     88     Updates filters values according to form_data 
     89     To be called before any call to display() or getForm() 
     90      
     91     @param    form_data <b>array</b>   form values (usually $_GET or $_POST) 
     92     */ 
    4593     public function setValues ($form_data) { 
    4694          $this->hideform = true; 
    47           if (isset($form_data['clear_filters'])) 
     95          if (isset($form_data['clear_filters'])) { 
     96               $this->saveFilters(); 
    4897               return; 
     98          } 
     99          if (!isset($form_data['apply'])) { 
     100               $this->loadFilters(); 
     101          } 
    49102          foreach ($this->filters as $filter) { 
    50103               $filter->setValues ($form_data); 
    51                if ($filter->isEnabled()) 
     104               if ($filter->isEnabled()) { 
    52105                    $this->hideform=false; 
    53           } 
    54           if (isset($form_data['apply']) && (trim($form_data['apply']) == '+') 
    55                && isset($form_data['add_filter']) && isset($this->filters[$form_data['add_filter']])) { 
    56                $this->filters[$form_data['add_filter']]->add(); 
    57                $this->hideform=false; 
    58           } 
    59      } 
    60       
     106               } 
     107          } 
     108          if (isset($form_data['apply'])) { 
     109               if (trim($form_data['apply']) == '+' 
     110                    && isset($form_data['add_filter'])  
     111                    && isset($this->filters[$form_data['add_filter']])) { 
     112                    $this->filters[$form_data['add_filter']]->add(); 
     113                    $this->hideform=false; 
     114               } 
     115          } 
     116          $this->saveFilters(); 
     117     } 
     118      
     119     /** 
     120     Defines additional form in layout (right column) 
     121      
     122     @param    html <b>string</b>       the code to add 
     123     */ 
    61124     public function setColumnsForm($html) 
    62125     { 
     
    64127     } 
    65128      
     129     /** 
     130     Returns form fields as hidden fields 
     131      
     132     @return   <b>string</b>  the corresponding html code 
     133     */ 
    66134     public function getFormFieldsAsHidden() { 
    67135          $ret=''; 
     
    72140     } 
    73141 
    74      public function getForm($action,$extra_content,$method="get",$nb_cols=3) { 
     142     /** 
     143     Retrieves filterset generated form 
     144      
     145     @param    method    <b>string</b>       form method to use (default: "get") 
     146     */ 
     147     public function getForm($method="get") { 
    75148          $ret = ''; 
    76149           
     
    83156          $ret .= 
    84157               '<div class="two-cols">'. 
    85                '<form id="filters" action="'.$this->action.'" method="get" id="filters-form"'.$formclass.'>'. 
     158               '<form id="filters" action="'.$this->action.'" method="'.$method.'" id="filters-form"'.$formclass.'>'. 
    86159               '<div class="col70">'. 
    87160               '<h3>'.__('Entries filters').'</h3>'; 
     
    90163          $form_combo=array(); 
    91164          $form_combo['-']=''; 
    92           foreach ($this->filters as $filter) { 
    93                if ($filter->isEnabled()) { 
    94                     $ret .= $filter->getFormLine(); 
     165          if (count($this->filters)) { 
     166               $ret .= '<ul>'; 
     167               foreach ($this->filters as $filter) { 
     168                    if ($filter->isEnabled()) { 
     169                         $ret .= $filter->getFormLine(); 
     170                    } 
     171                    $form_combo[$filter->desc]=$filter->id; 
     172                    $count++; 
    95173               } 
    96                $form_combo[$filter->desc]=$filter->id; 
    97                $count++; 
     174               $ret .= '</ul>'; 
    98175          } 
    99176          $ret .=  
     
    113190          return $ret; 
    114191     } 
    115  
     192      
     193     /** 
     194     Displays required fieldset http header 
     195     To be called in page header, of course. 
     196     */ 
    116197     public function header() { 
    117198          return dcPage::jsLoad('js/filters.js'); 
    118199     } 
     200      
     201      
     202     /** 
     203     Displays the fieldset 
     204     */ 
    119205     public function display() { 
    120           echo $this->getForm("#",""); 
    121      } 
    122  
     206          echo $this->getForm(); 
     207     } 
     208 
     209     /** 
     210     Applies fieldset and return resulting parameters for request 
     211      
     212     @param    method    <b>string</b>       form method to use (default: "get") 
     213     @param    method    <b>string</b>       form method to use (default: "get") 
     214      
     215     */ 
    123216     public function applyFilters($params) { 
    124217          $filtered = false; 
     
    135228 
    136229 
     230/** 
     231@ingroup DC_CORE 
     232@nosubgrouping 
     233@brief abstract filter class. 
     234 
     235Dotclear Filter handles administration filters for each list 
     236A filter fills in a parameter array, as defined in dcBlog class 
     237*/ 
    137238abstract class Filter { 
    138      public $id; 
    139      public $desc; 
    140      protected $request_param; 
    141      protected $enabled; 
    142      protected $values; 
    143      public $field_id; 
    144       
    145       
     239     public $id;                        ///< <b>string</b> field id (local to fieldset) 
     240     public $desc;                 ///< <b>string</b> field description 
     241     protected $request_param;     ///< <b>string</b> resulting parameter array key 
     242     protected $enabled;           ///< <b>string</b> true if filter is enabled 
     243     protected $values;            ///< <b>array</b> possible filter values 
     244     public $field_id;             ///< <b>string</b> field id (global to the page) 
     245      
     246     /** 
     247     Inits Filter object 
     248      
     249     @param    id        <b>string</b>  field id 
     250     @param    form_prefix    <b>string</b>       form prefix to use for parameters 
     251     */ 
    146252     public function __construct ($id,$desc,$request_param) { 
    147253          $this->id = $id; 
     
    153259     } 
    154260      
     261     /** 
     262     Get a field id 
     263      
     264     @param    pos       <b>integer</b> position of field, in case of multiple field (0 if only 1 field set, default value) 
     265     @return   <b>string</b> The field ID 
     266     */ 
    155267     protected function getFieldId($pos=0) { 
    156268          if ($pos == 0) { 
     
    161273     } 
    162274      
     275     /** 
     276     Tells whether the filter is enabled or not 
     277      
     278     @return   <b>boolean</b> true if enabled, false otherwise 
     279     */ 
    163280     public function isEnabled() { 
    164281          return $this->enabled; 
    165282     } 
    166283      
     284     /** 
     285     Adds the current filter to the list 
     286     */ 
    167287     public function add() { 
     288          // By default here, only 1 value allowed. Simply enable the filter 
    168289          $this->enabled = true; 
    169290     } 
    170291      
     292     /** 
     293     Defines form prefix for filter 
     294      
     295     @param    prefix         <b>string</b>  the form prefix 
     296     */ 
    171297     public function setFormPrefix($prefix) { 
    172298          $this->field_id = $prefix.$this->id; 
    173299     } 
    174300      
    175      public abstract function getType(); 
    176       
    177      public function getFormFields() { 
     301      
     302     /** 
     303     Returns HTML code for form field 
     304      
     305     @param    pos       <b>integer</b> position of the field to display (in case of multiple values) 
     306     @return <b>string</b> the html code 
     307     */ 
     308     public function getFormFields($pos=0) { 
    178309          return ''; 
    179310     } 
    180  
     311      
     312     /** 
     313     Returns filter values il a serialized way (array) 
     314      
     315     @return        <b>array</b>   serialized data 
     316     */ 
     317     public function serialize() { 
     318          return array( 
     319               'values' => $this->values, 
     320               'enabled' => $this->enabled 
     321          ); 
     322     } 
     323      
     324     /** 
     325     Defines filter values from serialized data (array) 
     326     To be used in conjunction with serialize method 
     327      
     328     @param    $data     <b>array</b>   serialized data to retrieve 
     329     */ 
     330     public function unserialize ($data) { 
     331          $this->values = $data['values']; 
     332          $this->enabled = $data['enabled']; 
     333     } 
     334      
     335     /** 
     336     Set filter values from form_data (usually $_GET)   
     337     @param    $form_data     <b>array</b>   form data 
     338     */ 
    181339     public function setValues($form_data) { 
    182 /*        if (isset($form_data['c_'.$this->field_id])) { 
    183                $this->enabled = true; 
    184           }*/ 
    185340          $count=0; 
    186341          while (isset($form_data[$this->getFieldId($count)])) { 
    187342               if (!isset($form_data['del_'.$this->getFieldId($count)])) { 
    188                     $this->values[] = $form_data[$this->getFieldId($count)]; 
    189                     $this->enabled = true; 
     343                    $this->values[$count] = $form_data[$this->getFieldId($count)]; 
     344               } elseif (isset($this->values[$count])) { 
     345                    unset($this->values[$count]); 
    190346               } 
    191347               $count++; 
    192           } 
    193      } 
    194       
     348 
     349          } 
     350          $this->values = array_values($this->values); 
     351          $this->enabled = (count($this->values)!=0); 
     352     } 
     353      
     354          /** 
     355     Returns form fields as hidden fields 
     356      
     357     @return   <b>string</b>  the corresponding html code 
     358     */    
    195359     public function getFormFieldAsHidden () { 
    196360          $ret=''; 
     
    199363          } 
    200364     } 
     365     /** 
     366     Returns HTML code for the hole filter lines 
     367      
     368     @return <b>string</b> the html code 
     369     */ 
     370      
    201371     public function getFormLine() { 
    202372          $ret=""; 
    203373          for ($cur=0; $cur < count($this->values); $cur++) { 
    204                $ret .= '<p id="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'. 
     374               $ret .= '<li id="'.$this->getFieldId($cur).'" class="line" title="'.$this->desc.'">'. 
    205375                    $this->getFormFields($cur). 
    206376                    '<input id="del_'.$this->getFieldId($cur).'" class="delete" '. 
    207                     'type="submit" title="Delete this filter" value=" - " name="del_'.$this->getFieldId($cur).'"/>'. 
    208                     '</p>'; 
     377                    'type="submit" title="Delete the following filter : " value=" - " name="del_'.$this->getFieldId($cur).'"/>'. 
     378                    '</li>'; 
    209379          } 
    210380          return $ret; 
    211381     } 
    212382      
     383     /** 
     384     Convert filter values into a $param filter, used for the upcoming SQL request 
     385      
     386     @param <b>ArrayObject</b> the parameters array to enrich 
     387     */ 
    213388     public function applyFilter($params) { 
    214389     } 
     
    216391} 
    217392 
     393/** 
     394@ingroup DC_CORE 
     395@nosubgrouping 
     396@brief abstract filter class. 
     397 
     398Handle combo filter on admin side. Can be single or multi-valued 
     399*/ 
    218400class comboFilter extends Filter { 
    219401     protected $options; 
     
    242424          return "combo"; 
    243425     } 
     426 
     427     public function serialize() { 
     428          $data = parent::serialize(); 
     429          $data['verb'] = $this->verb; 
     430          return $data; 
     431     } 
     432      
     433     public function unserialize ($data) { 
     434          parent::unserialize($data); 
     435          $this->verb = $data['verb']; 
     436     } 
    244437      
    245438     public function setValues($form_data) { 
     
    258451          if ($pos == 0) { 
    259452               $desc = $this->desc.' : '; 
    260                $labelclass=""; 
     453               $labelclass="filter-title"; 
    261454          } else { 
    262455               $desc = __('or'); 
    263                $labelclass = ' class="or"'; 
     456               $labelclass = 'or'; 
    264457          }; 
    265           return '<span class="filter-title">'.$desc.'</span>'. 
     458          return '<span class="'.$labelclass.'">'.$desc.'</span>'. 
    266459               (($pos == 0)  
    267460                    ?form::combo($this->field_id.'_v', 
     
    284477     } 
    285478} 
     479 
     480/** 
     481@ingroup DC_CORE 
     482@nosubgrouping 
     483@brief abstract filter class. 
     484 
     485Handle boolean filter on admin side. 
     486*/ 
     487class booleanFilter extends Filter { 
     488     protected $options; 
     489      
     490     public function __construct($id,$desc,$request_param,$options,$extra=array()) { 
     491          parent::__construct($id,$desc,$request_param); 
     492          $this->options = $options; 
     493          $this->values=array(); 
     494     } 
     495      
     496      
     497     public function getType() { 
     498          return "boolean"; 
     499     } 
     500     public function add() { 
     501          parent::add(); 
     502          $this->values[]=$options[0]; 
     503     } 
     504 
     505     public function getFormFields($pos=0) { 
     506          return '<span class="'.$labelclass.'">'.$this->desc.'</span>'. 
     507               form::combo($this->getFieldId($pos),$this->options,$this->values[$pos], 
     508                    '','',false,'title="'.__('Choose an option').'"'); 
     509     } 
     510      
     511     public function applyFilter($params) { 
     512          $params[$this->request_param]=$this->values[0]; 
     513     } 
     514} 
     515 
    286516?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map