Dotclear


Ignore:
Timestamp:
04/02/13 13:05:04 (12 years ago)
Author:
Dsls <dsls@…>
Branch:
twig
Message:

form filters reloaded, first try for lists (to be completed)
form fields now support multiple values.

File:
1 edited

Legend:

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

    r1148 r1152  
    1414 
    1515 
    16 class dcFilterSetExtension extends Twig_Extension { 
    17      private $filtersets; 
    18  
    19      public function __construct($core) 
    20      { 
    21           $this->core = $core; 
    22           $this->tpl = 'formfilter_layout.html.twig'; 
    23      } 
    24       
    25      public function initRuntime(Twig_Environment $environment) 
    26      { 
    27           $this->core->tpl->getExtension('dc_form')->addTemplate($this->tpl); 
    28      } 
    29       
    30       
    31      public function getFunctions() 
    32      { 
    33           return array( 
    34                'filterset' => new Twig_Function_Method( 
    35                     $this, 
    36                     'renderFilterSet', 
    37                     array( 
    38                          'is_safe' => array('html'), 
    39                          'needs_context' => true 
    40           ))); 
    41      } 
    42  
    43       
    44  
    45      public function renderFilterSet($context,$name,$attributes=array()) 
    46      { 
    47           $context['filtersetname']=$name; 
    48           echo $this->core->tpl->getExtension('dc_form')->renderBlock( 
    49                'filterset', 
    50                $context 
    51           ); 
    52      } 
    53       
    54  
    55      public function getName() 
    56      { 
    57           return 'dc_filterset'; 
    58      } 
    59  
    60 } 
    61  
    62  
    6316class dcFilterSet extends dcForm { 
    6417     protected $filters;      /// <b>array</b> lists of currently applied filters 
     18     protected $static_filters; 
     19     protected $all_filters; 
    6520     protected $form_prefix;       /// <b>string</b> displayed form prefix 
    6621     protected $action;            /// <b>string</b> form action page 
     
    6924     protected $core; 
    7025 
     26     public static function __init__($env) { 
     27          $env->getExtension('dc_form')->addTemplate('@forms/formfilter_layout.html.twig'); 
     28          $env->addFunction( 
     29               new Twig_SimpleFunction( 
     30                    'filterset', 
     31                    'dcFilterSet::renderFilterSet', 
     32                    array( 
     33                         'is_safe' => array('html'), 
     34                         'needs_context' => true, 
     35                         'needs_environment' => true 
     36          ))); 
     37     } 
     38      
     39     public static function renderFilterSet($env,$context,$name,$attributes=array()) 
     40     { 
     41          $context['filtersetname']=$name; 
     42          echo $env->getExtension('dc_form')->renderWidget( 
     43               'filterset', 
     44               $context 
     45          ); 
     46     } 
    7147     /** 
    7248     Inits dcFilterSet object 
     
    7854          $this->form_prefix=$form_prefix; 
    7955          $this->filters = new ArrayObject(); 
     56          $this->static_filters = new ArrayObject(); 
     57          $this->all_filters = new ArrayObject(); 
    8058          $this->action = $action; 
    8159          $this->filtered = false; 
     
    8765          $form_combo = array(); 
    8866          $form_combo['-'] = ''; 
     67          foreach ($this->all_filters as $filter) { 
     68               $filter->init(); 
     69          } 
    8970          foreach ($this->filters as $filter) { 
    90                $filter->init(); 
    9171               $form_combo[$filter->id]=$filter->name; 
    9272          } 
     
    11090          ; 
    11191          $this->setupFields(); 
    112           $submitted = $this->getSubmittedFields(); 
    11392          /* Use cases : 
    11493               (1) $_POST not empty for formfilter fields : 
     
    130109                    $tmp = substr($k,strlen($this->form_prefix)); 
    131110                    $count = preg_match($allowed_actions,$tmp,$match); 
    132                     echo $action; 
    133111                    if ($count==1) { 
    134112                         $action = $match[1]; 
     
    137115               } 
    138116          } 
     117 
    139118          if ($action !== false) { 
    140119               // Use case (1) 
    141120               if ($action != 'clear_filters' && $action != 'reset')  { 
    142                     $this->setupEditFilters($_POST); 
     121                    $this->setupEditFilters($this->all_filters,$_POST); 
    143122                    if ($action == 'add'){ 
    144                          if (isset($_POST[$p.'add_filter']) 
    145                               && isset($this->filters[$_POST[$p.'add_filter']])) { 
    146                          $this->filters[$_POST[$p.'add_filter']]->add(); 
     123                         $fname = $p.'add_filter'; 
     124                         if (isset($_POST[$fname]) 
     125                              && isset($this->filters[$_POST[$fname]])) { 
     126                         $this->filters[$_POST[$fname]]->add(); 
    147127                         } 
    148128                    } elseif (strpos($action,'del_') === 0) { 
    149129                         $count = preg_match('#del_(.+)_([0-9]+)#',$action,$match); 
    150                          echo "action=".$action; 
    151130                         if (($count == 1) && isset($this->filters[$match[1]])) { 
    152131                              $this->filters[$match[1]]->remove($match[2]); 
    153                               echo "remove"; 
    154132                         } 
    155133                    } elseif ($action=="apply") { 
     
    165143               if (isset($_POST[$p."query"])) { 
    166144                    parse_str($_POST[$p."query"],$out); 
    167                     $this->setupAppliedFilters($out); 
     145                    $this->setupAppliedFilters($this->all_filters,$out); 
    168146                    if ($action == 'reset') { 
    169                          $this->setupEditFilters($out); 
     147                         $this->setupEditFilters($this->all_filters,$out); 
     148                    } elseif ($action == 'clear_filters') { 
     149                         $this->setupEditFilters($this->static_filters,$out); 
     150                         foreach ($this->filters as $f) { 
     151                              $f->cleanup(); 
     152                         } 
    170153                    } 
    171154               } 
     
    184167                    $get = new ArrayObject(array_merge($this->loadFilters(),$get)); 
    185168               } 
    186                $this->setupEditFilters($get); 
    187                $this->setupAppliedFilters($get); 
    188           } 
    189           $queryParams = $this->getListedFilters(); 
     169               $this->setupEditFilters($this->all_filters,$get); 
     170 
     171               $this->setupAppliedFilters($this->all_filters,$get); 
     172          } 
     173          foreach ($this->static_filters as $f) { 
     174               if (!$f->isEnabled()) { 
     175                    $f->add(); 
     176               } 
     177          } 
     178          $queryParams = $this->getAppliedFilters(); 
    190179          $this->addField( 
    191180               new dcFieldHidden($this->form_prefix.'query', 
     
    224213     @param    form_data <b>array</b>   form values (usually $_GET or $_POST) 
    225214     */ 
    226      protected function setupEditFilters ($form_data) { 
     215     protected function setupEditFilters ($filters,$form_data) { 
    227216          $this->hideform = true; 
    228           foreach ($this->filters as $filter) { 
     217          foreach ($filters as $filter) { 
    229218               $filter->setupFields ($form_data); 
    230219          } 
    231220     } 
    232      protected function setupAppliedFilters ($form_data) { 
    233           foreach ($this->filters as $filter) { 
     221     protected function setupAppliedFilters ($filters,$form_data) { 
     222          foreach ($filters as $filter) { 
    234223               $filter->setupAppliedFilter ($form_data); 
    235224          } 
     
    251240               } 
    252241          } 
     242          foreach ($this->static_filters as $f) { 
     243               $f->serialize($arr); 
     244          } 
    253245          return $arr; 
    254246     } 
     
    261253          $filter->setFormPrefix($this->form_prefix); 
    262254          $filter->setFilterSet($this); 
    263           $this->filters[$filter->id] = $filter; 
     255          $this->all_filters[$filter->id] = $filter; 
     256          if ($filter->isStatic()) { 
     257               $this->static_filters[$filter->id] = $filter; 
     258          } else { 
     259               $this->filters[$filter->id] = $filter; 
     260          } 
    264261          return $this; 
    265262     } 
     263 
    266264     public function getContext() { 
    267265          $fcontext = new ArrayObject(); 
     266          $sfcontext = new ArrayObject(); 
    268267          foreach ($this->filters as $f) { 
    269268               if($f->isEnabled()) { 
     
    271270               } 
    272271          } 
     272          foreach ($this->static_filters as $f) { 
     273               $f->appendEditLines ($sfcontext); 
     274          } 
    273275          return array( 
    274276               'active_filters' => $fcontext,  
     277               'static_filters' => $sfcontext, 
    275278               'prefix'=>$this->form_prefix); 
    276279     } 
    277280 
    278      protected function getListedFilters() { 
     281     protected function getAppliedFilters() { 
    279282          $arr = new ArrayObject(); 
    280           foreach ($this->filters as $f) { 
    281                if ($f->isEnabled()) 
     283          foreach ($this->all_filters as $f) { 
     284               if ($f->isApplied()) 
    282285                    $f->updateAppliedValues($arr); 
    283286          } 
     
    292295     */ 
    293296     public function applyFilters($params) { 
    294           foreach ($this->filters as $filter) { 
     297          foreach ($this->all_filters as $filter) { 
    295298               if ($filter->isApplied()) { 
    296299                    $filter->applyFilter($params); 
     
    318321     public $name;                 ///<b>string</b> filter name 
    319322     public $desc;                 ///<b>string</b> field description 
     323     public $filter_id;            ///<b>string</b> field id (global to the page) 
    320324     protected $request_param;     ///<b>string</b> resulting parameter array key 
    321      protected $enabled;           ///<b>string</b> true if filter is enabled 
    322      protected $fields;            ///<b>array</b> currently edited values 
     325     protected $field;             ///<b>array</b> currently edited values 
    323326     protected $avalues;           ///<b>array</b> currently applied values 
    324      public $filter_id;            ///<b>string</b> field id (global to the page) 
    325  
    326      public function __construct ($id,$name,$desc,$request_param) { 
    327           $this->id = $id; 
    328           $this->name=$name; 
    329           $this->desc = $desc; 
    330           $this->request_param = $request_param; 
    331           $this->enabled=false; 
    332           $this->avalues = array(); 
    333           $this->filter_id = $this->id; 
    334           $this->fields = array(); 
    335      } 
    336  
    337      public function setupFields($data) { 
    338           $val = $this->parseData($data); 
    339           $pos=0; 
    340           foreach($val['values'] as $v) { 
    341                $this->addField($pos,$v); 
    342                $pos++; 
    343           } 
    344           $this->enabled = ($pos> 0); 
    345      }//  abstract public function setupFields($data); 
    346  
    347  
    348      public function init() { 
     327     protected $static; 
     328     protected $options; 
     329     protected $multiple; 
     330 
     331     public function __construct ($id,$name,$desc,$request_param,$options=array()) { 
     332          $this->id                     = $id; 
     333          $this->name              =$name; 
     334          $this->desc              = $desc; 
     335          $this->request_param     = $request_param; 
     336          $this->avalues           = array(); 
     337          $this->filter_id         = $this->id; 
     338          $this->static            = false; 
     339          $this->multiple          = false; 
     340          $this->options           = $options; 
     341          if (isset($options['static']) && $options['static']) { 
     342               $this->static       = true; 
     343          } 
     344          if (isset($options['multiple']) && $options['multiple']) { 
     345               $this->multiple          = true; 
     346          } 
    349347     } 
    350348 
     
    357355     */ 
    358356     protected function parseData($data) { 
    359           $count=0; 
    360           $arr = array(); 
    361           while (isset($data[$this->getFieldId($count)])) { 
    362                $arr[] = $data[$this->getFieldId($count)]; 
    363                $count++; 
    364           } 
     357          $arr = $this->field->parseValues($data); 
    365358          return array('values' => $arr); 
    366359     } 
    367       
     360 
     361     public function isStatic() { 
     362          return $this->static; 
     363     } 
     364 
     365     public function setupFields($data) { 
     366          $this->field->setup($data); 
     367     } 
     368 
     369 
     370     public function init() { 
     371     } 
     372 
     373     public function cleanup() { 
     374          $this->field->setValues(array()); 
     375     } 
     376 
    368377     public function setupAppliedFilter($data) { 
    369378          $this->avalues = $this->parseData($data); 
     
    371380 
    372381     public function updateAppliedValues($arr) { 
    373           foreach ($this->avalues['values'] as $k=>$v) { 
    374                $arr[$this->getFieldID($k)]=$v; 
    375           } 
     382          $arr[$this->filter_id] = $this->avalues['values']; 
    376383     } 
    377384 
     
    396403 
    397404     /** 
    398      Get a field id 
    399       
    400      @param    pos       <b>integer</b> position of field, in case of multiple field (0 if only 1 field set, default value) 
    401      @return   <b>string</b> The field ID 
    402      */ 
    403      protected function getFieldId($pos=0) { 
    404           if ($pos == 0) { 
    405                return $this->filter_id; 
    406           } else { 
    407                return $this->filter_id.'_'.$pos; 
    408           } 
    409      } 
    410       
    411       
    412      /** 
    413405     Tells whether the filter is enabled or not 
    414406      
     
    416408     */ 
    417409     public function isEnabled() { 
    418           return $this->enabled; 
    419      } 
    420       
    421      protected abstract function addField($pos,$value=NULL); 
     410          return count($this->field) != 0; 
     411     } 
     412      
     413     protected abstract function addValue($value=NULL); 
    422414 
    423415     /** 
     
    425417     */ 
    426418     public function add() { 
    427           $this->addField(count($this->fields)); 
    428           $this->enabled = true; 
     419          if (count($this->field) > 1 && !$this->multiple) 
     420               return; 
     421          $this->addValue(); 
    429422     } 
    430423      
     
    433426     */ 
    434427     public function remove($pos) { 
    435           if (isset($this->fields[$pos])) { 
    436                $this->filterset->removeField($this->fields[$pos]); 
    437                array_splice($this->fields,$pos,1); 
    438                for ($cur=$pos; $cur<count($this->fields);$cur++) { 
    439                     $this->filterset->renameField($this->fields[$cur],$this->getFieldID($cur)); 
    440                } 
    441                $this->enabled = (count($this->fields)!=0); 
     428          $values = $this->field->getValues(); 
     429          if (isset($values[$pos])) { 
     430               $this->field->delValue($pos); 
    442431          } 
    443432 
     
    447436 
    448437     public function appendEditLines($ctx) { 
    449           foreach ($this->fields as $cur => $f) { 
     438          foreach ($this->field->getValues() as $cur => $f) { 
    450439               $line = new ArrayObject(); 
    451440               $line['lineclass'] = $this->id; 
     
    465454 
    466455     public function serialize($arr) { 
    467           for ($cur=0; $cur < count($this->fields); $cur++) { 
    468                $arr[$this->getFieldId($cur)]=$this->fields[$cur]->getValue(); 
     456          if (count($this->fields) == 1) { 
     457               $arr[$this->filter_id]=$this->field->getValue(); 
     458          } else { 
     459               $arr[$this->filter_id]=$this->field->getValues(); 
    469460          } 
    470461     } 
     
    492483 
    493484 
     485 
     486class dcFilterText extends dcFilter { 
     487 
     488     public function init() { 
     489          $this->field = new dcFieldText( 
     490               $this->filter_id, 
     491               NULL); 
     492          $this->filterset->addField($this->field); 
     493          $this->multiple = false; 
     494     } 
     495 
     496 
     497     public function appendSingleLine($line,$pos) { 
     498          $line['ffield'] = $this->field->getName(); 
     499          if ($this->static) { 
     500               $line['display_inline'] = true; 
     501          } 
     502 
     503          if ($pos == 0) { 
     504               $line['fwidget']='filter_text'; 
     505               $line['desc']=$this->desc; 
     506          }; 
     507     } 
     508 
     509     protected function addValue($value=NULL) { 
     510          if ($value === NULL) { 
     511               $value = ''; 
     512          } 
     513          $this->field->addValue($value); 
     514     } 
     515 
     516     public function applyFilter($params) { 
     517          $params[$this->request_param]=$this->avalues['values'][0]; 
     518     } 
     519      
     520} 
    494521/** 
    495522@ingroup DC_CORE 
     
    499526Handle combo filter on admin side. Can be single or multi-valued 
    500527*/ 
    501 class comboFilter extends dcFilter { 
    502      protected $options; 
     528class dcFilterCombo extends dcFilter { 
     529     protected $combo; 
     530      
     531     public function __construct($id,$name,$desc,$request_param,$combo,$options=array()) { 
     532          parent::__construct($id,$name,$desc,$request_param,$options); 
     533          $this->combo = $combo; 
     534     } 
     535     public function init() { 
     536          $this->field = new dcFieldCombo( 
     537               $this->filter_id, 
     538               NULL, 
     539               $this->combo); 
     540          $this->filterset->addField($this->field); 
     541     } 
     542 
     543     protected function addValue($value=NULL) { 
     544          if ($value === NULL) { 
     545               $value = current($this->combo); 
     546          } 
     547          $this->field->addValue($value); 
     548     } 
     549 
     550     public function appendSingleLine($line,$pos) { 
     551          if ($this->static) { 
     552               $line['display_inline'] = true; 
     553          } 
     554          $line['ffield'] = $this->field->getName(); 
     555          $line['foffset'] = $pos; 
     556          if ($pos == 0) { 
     557               $line['fwidget']='filter_combo'; 
     558               $line['desc']=$this->desc; 
     559          } else { 
     560               $line['fwidget']='filter_combo_cont'; 
     561          }; 
     562     } 
     563      
     564     public function applyFilter($params) { 
     565          $attr = $this->request_param; 
     566          if ($this->multiple) 
     567               $params[$attr]=$this->avalues['values']; 
     568          else 
     569               $params[$attr]=$this->avalues['values'][0]; 
     570     } 
     571 
     572} 
     573 
     574/** 
     575@ingroup DC_CORE 
     576@nosubgrouping 
     577@brief abstract filter class. 
     578 
     579Handle combo filter on admin side. Can be single or multi-valued 
     580*/ 
     581class dcFilterRichCombo extends dcFilterCombo { 
    503582     protected $verb; 
    504      protected $extra; 
    505       
    506      public function __construct($id,$name,$desc,$request_param,$options,$extra=array()) { 
    507           parent::__construct($id,$name,$desc,$request_param); 
    508           $this->options = $options; 
    509           $this->extra = $extra; 
    510      } 
    511583 
    512584     public function init() { 
     
    519591                    'isnot'=>__('is not')) 
    520592          ); 
     593          $this->filterset->addField($this->verb); 
    521594     } 
    522595 
    523596     protected function parseData($data) { 
    524597          $val = parent::parseData($data); 
    525           $val['verb'] = $this->verb->parseValue($data); 
     598          $v = $this->verb->parseValues($data); 
     599          if (isset($v[0]) && $v[0] === 'isnot') 
     600               $val['verb'] = 'isnot'; 
     601          else 
     602               $val['verb'] ='is'; 
    526603          return $val; 
    527604     } 
    528605 
    529      protected function addField($pos,$value=NULL) { 
    530           if ($value === NULL) { 
    531                $value = current($this->options); 
    532           } 
    533           $f = new dcFieldCombo( 
    534                $this->getFieldID($pos), 
    535                $value, 
    536                $this->options); 
    537           $this->filterset->addField($f); 
    538           $this->fields[]=$f; 
    539      } 
    540  
    541606     public function setupFields($data) { 
    542           /*$val = $this->parseData($data); 
    543           $this->verb->setup($data); 
    544           $pos=0; 
    545           foreach($val['values'] as $v) { 
    546                $this->addField($pos,$v); 
    547           } 
    548           $this->enabled = (count($this->fields) != 0);*/ 
    549607          parent::setupFields($data); 
    550608          $this->verb->setup($data); 
    551           $this->filterset->addField($this->verb); 
    552      } 
     609     } 
     610 
    553611     public function updateAppliedValues($arr) { 
    554612          parent::updateAppliedValues($arr); 
    555           if ($this->enabled) { 
    556                $arr[$this->verb->getName()] = $this->verb->getValue(); 
    557           } 
    558      } 
    559      public function add() { 
    560           if (isset($this->extra['singleval']) && (count($this->fields) > 0)) 
    561                return; 
    562           parent::add(); 
     613          $arr['verb'] = $this->verb->getValue(); 
    563614     } 
    564615 
    565616     public function appendSingleLine($line,$pos) { 
    566           $f = $this->fields[$pos]; 
    567           $line['ffield'] = $f->getName(); 
    568  
     617          parent::appendSingleLine($line,$pos); 
    569618          if ($pos == 0) { 
    570                $line['fwidget']='filter_combo'; 
    571619               $line['fverb'] = $this->verb->getName(); 
    572                $line['desc']=$this->desc; 
    573           } else { 
    574                $line['fwidget']='filter_combo_cont'; 
    575           }; 
     620               $line['fwidget']='filter_richcombo'; 
     621          }  
    576622     } 
    577623      
     
    582628      
    583629     public function applyFilter($params) { 
     630          parent::applyFilter($params); 
    584631          $attr = $this->request_param; 
    585632          if ($this->avalues['verb'] != "is") { 
    586633               $params[$attr."_not"] = true; 
    587634          } 
    588           if (isset($this->extra['singleval'])) 
    589                $params[$attr]=$this->avalues['values'][0]; 
    590           else 
    591                $params[$attr]=$this->avalues['values']; 
    592635     } 
    593636 
    594637} 
    595638 
    596 class booleanFilter extends dcFilter { 
    597      protected $options; 
    598      protected $verb; 
    599       
    600      public function __construct($id,$name,$desc,$request_param,$options) { 
    601           parent::__construct($id,$name,$desc,$request_param); 
    602           $this->options = $options; 
    603      } 
    604  
    605  
    606  
    607      public function appendSingleLine($line,$pos) { 
    608           $f = $this->fields[$pos]; 
    609           $line['ffield'] = $f->getName(); 
    610  
    611           if ($pos == 0) { 
    612                $line['fwidget']='filter_boolean'; 
    613                $line['desc']=$this->desc; 
    614           }; 
    615      } 
    616  
    617      protected function addField($pos,$value=NULL) { 
    618           if (count($this->fields)>0) 
    619                return; 
    620           if ($value === NULL) { 
    621                $value = 1; 
    622           } 
    623           $f = new dcFieldCombo( 
    624                $this->getFieldID($pos), 
    625                $value, 
    626                $this->options); 
    627           $this->filterset->addField($f); 
    628           $this->fields[]=$f; 
    629      } 
    630  
    631      public function applyFilter($params) { 
    632           $params[$this->request_param]=$this->avalues['values'][0]; 
    633      } 
    634       
    635 } 
    636  
    637 class textFilter extends dcFilter { 
    638      protected $options; 
    639      protected $verb; 
    640       
    641      public function __construct($id,$name,$desc,$request_param,$options) { 
    642           parent::__construct($id,$name,$desc,$request_param); 
    643           $this->options = $options; 
    644      } 
    645  
    646      public function setupFields($data) { 
    647           $val = $this->parseData($data); 
    648           foreach($val['values'] as $k=>$v) { 
    649                $this->addField($k,$v); 
    650           } 
    651           $this->enabled = (count($this->fields) != 0); 
    652      } 
    653  
    654      public function appendSingleLine($line,$pos) { 
    655           $f = $this->fields[$pos]; 
    656           $line['ffield'] = $f->getName(); 
    657  
    658           if ($pos == 0) { 
    659                $line['fwidget']='filter_boolean'; 
    660                $line['desc']=$this->desc; 
    661           }; 
    662      } 
    663  
    664      protected function addField($pos,$value=NULL) { 
    665           if (count($this->fields)>0) 
    666                return; 
    667           if ($value === NULL) { 
    668                $value = ''; 
    669           } 
    670           $f = new dcFieldText( 
    671                $this->getFieldID($pos), 
    672                $value); 
    673           $this->filterset->addField($f); 
    674           $this->fields[]=$f; 
    675      } 
    676  
    677      public function applyFilter($params) { 
    678           $params[$this->request_param]=$this->avalues['values'][0]; 
    679      } 
    680       
    681 } 
    682  
     639 
     640 
     641dcFilterSet::__init__($GLOBALS['core']->tpl); 
    683642?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map