Dotclear

Changeset 1152:fbd922f6ed09


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.

Files:
4 added
7 edited
2 moved

Legend:

Unmodified
Added
Removed
  • admin/posts.php

    r1147 r1152  
    126126 
    127127 
    128 class monthComboFilter extends comboFilter { 
     128class monthdcFilterCombo extends dcFilterCombo { 
    129129     public function applyFilter($params) { 
    130130          $month=$this->avalues['values'][0]; 
     
    138138 
    139139$filterSet 
    140      ->addFilter(new comboFilter( 
    141           'users',__('Author'), __('Author'), 'user_id', $users_combo)) 
    142      ->addFilter(new comboFilter( 
     140     ->addFilter(new dcFilterRichCombo( 
     141          'users',__('Author'), __('Author'), 'user_id', $users_combo,array( 
     142               'multiple' => true))) 
     143     ->addFilter(new dcFilterRichCombo( 
    143144          'category',__('Category'), __('Category'), 'cat_id', $categories_combo)) 
    144      ->addFilter(new comboFilter( 
     145     ->addFilter(new dcFilterRichCombo( 
    145146          'post_status',__('Status'), __('Status'), 'post_status', $status_combo)) 
    146      ->addFilter(new comboFilter( 
     147     ->addFilter(new dcFilterRichCombo( 
    147148          'lang',__('Lang'), __('Lang'), 'post_lang', $lang_combo)) 
    148      ->addFilter(new booleanFilter( 
     149     ->addFilter(new dcFilterCombo( 
    149150          'selected',__('Selected'), __('The post : '),'post_selected', $selected_combo)) 
    150      ->addFilter(new monthComboFilter( 
     151     ->addFilter(new monthdcFilterCombo( 
    151152          'month',__('Month'),__('Month'), 'post_month', $dt_m_combo,array('singleval' => 1))) 
    152      ->addFilter(new textFilter( 
     153     ->addFilter(new dcFilterText( 
    153154          'search',__('Contains'),__('The entry contains'), 'search',20,255)); 
    154155 
     156 
     157 
     158$lposts = new dcItemList ($core,array('lposts','form-entries'),'posts_actions.php'); 
     159$lposts->addTemplate('posts_cols.html.twig'); 
     160 
     161$lposts->setFilterSet($filterSet); 
     162 
     163$lposts 
     164     ->addColumn(new dcColumn('title',__('Title'),'post_title')) 
     165     ->addColumn(new dcColumn('cat',__('Category'),'cat_title')) 
     166     ->addColumn(new dcColumn('date',__('Date'),'post_date')) 
     167     ->addColumn(new dcColumn('datetime',__('Date and Time'),'post_date')) 
     168     ->addColumn(new dcColumn('author',__('Author'),'post_author')) 
     169     ->addColumn(new dcColumn('status',__('Status'),'post_status')); 
     170 
     171 
     172$lposts->setup(); 
    155173$filterSet->setup(); 
    156174 
     
    159177$params=new ArrayObject(); 
    160178$filterSet->applyFilters($params); 
     179$posts = $core->blog->getPosts($params); 
     180$lposts->setEntries($posts); 
    161181$_ctx->filters = '['.print_r($params->getArrayCopy(),true).']'; 
    162182 
  • 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?> 
  • inc/admin/class.dc.form.php

    r1147 r1152  
    9090     { 
    9191          $this->core = $core; 
    92           $this->tpl = 'form_layout.html.twig'; 
     92          $this->tpl = array('@forms/form_layout.html.twig'); 
    9393          $this->forms = array(); 
     94          $this->blocks = array(); 
    9495          $this->currentForm = null; 
    9596     } 
     
    9899     { 
    99100          $this->twig = $environment; 
    100           $this->template = $this->twig->loadTemplate($this->tpl); 
    101           $this->blocks = $this->template->getBlocks(); 
     101          $this->twig->getLoader()->addPath(dirname(__FILE__).'/default-templates/forms','forms'); 
     102          foreach ($this->tpl as $tpl) { 
     103               $this->template = $this->twig->loadTemplate($tpl); 
     104               $this->blocks = array_merge($this->blocks,$this->template->getBlocks()); 
     105          } 
    102106     } 
    103107      
    104108     public function addTemplate($tpl) { 
    105           $t = $this->twig->loadTemplate($tpl); 
    106           $this->blocks = array_merge($this->blocks,$t->getBlocks()); 
     109          $this->tpl[]=$tpl; 
     110          if (isset($this->twig)) { 
     111               $this->template = $this->twig->loadTemplate($tpl); 
     112               $this->blocks = array_merge($this->blocks,$this->template->getBlocks()); 
     113          } 
    107114     } 
    108115 
     
    115122     { 
    116123          return array( 
    117                'form_field' => new Twig_Function_Method( 
    118                     $this, 
    119                     'renderWidget', 
     124               new Twig_SimpleFunction( 
     125                    'widget', 
     126                    array($this,'renderWidget'), 
    120127                    array('is_safe' => array('html')) 
    121128               ), 
    122                '_form_is_choice_group' => new Twig_Function_Method( 
    123                     $this, 
    124                     'isChoiceGroup', 
     129               new Twig_SimpleFunction( 
     130                    'haswidget', 
     131                    array($this,'hasWidget'), 
    125132                    array('is_safe' => array('html')) 
    126133               ), 
    127                '_form_is_choice_selected' => new Twig_Function_Method( 
    128                     $this, 
    129                     'isChoiceSelected', 
     134               new Twig_SimpleFunction( 
     135                    'form_field', 
     136                    array($this,'renderField'), 
     137                    array('is_safe' => array('html')) 
     138               ), 
     139               new Twig_SimpleFunction( 
     140                    '_form_is_choice_group', 
     141                    array($this,'isChoiceGroup'), 
     142                    array('is_safe' => array('html')) 
     143               ), 
     144               new Twig_SimpleFunction( 
     145                    '_form_is_choice_selected', 
     146                    array($this,'isChoiceSelected'), 
    130147                    array('is_safe' => array('html')) 
    131148               ) 
     
    148165     } 
    149166      
    150      public function renderBlock($name,$attr) { 
     167     public function hasWidget($name) { 
     168          return isset($this->blocks[$name]); 
     169     } 
     170     public function renderWidget($name,$attr) { 
     171          if (!isset($this->blocks[$name])) 
     172               return ''; 
    151173          echo $this->template->renderBlock( 
    152174               $name, 
     
    160182     } 
    161183 
    162      public function renderWidget($name,$attributes=array(),$extra=array()) 
     184     public function renderField($name,$attributes=array(),$extra=array()) 
    163185     { 
    164186          $field = $this->currentForm->$name; 
    165187          if ($field) { 
    166                $attr = $field->getAttributes(); 
     188               $attr = $field->getAttributes($attributes); 
    167189               if (isset($attr['attr'])) { 
    168190                    $attr['attr'] = array_merge($attr['attr'],$attributes); 
     
    170192                    $attr['attr'] = $attributes; 
    171193               } 
    172                $this->renderBlock( 
     194               $this->renderWidget( 
    173195                    $field->getWidgetBlock(), 
    174196                    array_merge( 
     
    183205     { 
    184206          foreach ($this->currentForm->getHiddenFields() as $h) { 
    185                $this->renderWidget($h->getName()); 
     207               $this->renderField($h->getName()); 
    186208          } 
    187209     } 
     
    240262     protected $errors; 
    241263      
     264     public function addTemplate($t) { 
     265          $this->core->tpl->getExtension('dc_form')->addTemplate($t); 
     266     } 
    242267 
    243268    /** 
     
    355380               $this->fields[$newname] = $field; 
    356381          } 
    357           //print_r($this->fields); 
    358382     } 
    359383     public function begin() 
     
    386410     { 
    387411          if (isset($this->fields[$name])) { 
    388                $this->fields[$name]->setAttribute('value',$value); 
     412               $this->fields[$name]->setValue($value); 
    389413          } 
    390414     } 
     
    449473 * Template form field 
    450474 */ 
    451 abstract class dcField 
    452 { 
    453      protected $attributes; 
     475abstract class dcField implements Countable 
     476{ 
     477     protected $options; 
    454478     protected $name; 
    455      protected $value; 
     479     protected $values; 
    456480     protected $id; 
     481     protected $multiple; 
    457482     protected $defined; 
    458483      
     
    468493     } 
    469494      
    470      public function __construct($name,$value,$attributes=array()) 
     495     public function __construct($name,$values,$options=array()) 
    471496     { 
    472497          $this->setNID($name); 
    473           $this->attributes = $attributes; 
    474           $this->setValue($value); 
    475           $this->attributes['name'] = $this->name; 
    476           $this->attributes['id'] = $this->id; 
     498          $this->options = new ArrayObject($options); 
     499          if ($values === NULL){ 
     500               $values = array(); 
     501          } 
     502          $this->setValues($values); 
    477503          $this->defined = false; 
    478      } 
    479       
    480      public function setValue($value) { 
    481           $this->value = $value; 
    482           $this->attributes['value'] = $this->value; 
    483      } 
    484  
    485      public function getValue() { 
    486           return $this->value; 
     504          $this->multiple = (isset($options['multiple']) && $options['multiple']); 
     505 
     506     } 
     507      
     508     public function setValue($value,$offset=0) { 
     509          $this->values[$offset] = $value; 
     510     } 
     511 
     512     public function setValues($values) { 
     513          if (is_array($values)) { 
     514               $this->values = $values; 
     515          } elseif ($values !== NULL) { 
     516               $this->values = array($values); 
     517          } 
     518 
     519     } 
     520 
     521     public function getValues() { 
     522          return $this->values; 
     523     } 
     524 
     525     public function getValue($offset=0) { 
     526          if (isset($this->values[$offset])) { 
     527               return $this->values[$offset]; 
     528          } 
     529     } 
     530 
     531     public function addValue($value) { 
     532          $this->values[] = $value; 
     533     } 
     534     public function delValue($offset) { 
     535          if (isset($this->values[$offset])) { 
     536               array_splice($this->values,$offset,1); 
     537          } 
     538     } 
     539 
     540     public function count() { 
     541          return count($this->values); 
    487542     } 
    488543 
    489544     public function __toString() 
    490545     { 
    491           return $this->value; 
     546          return join(',',$this->values); 
    492547     } 
    493548      
    494549     abstract public function getWidgetBlock(); 
    495550      
    496      public function setAttribute($name,$value) 
    497      { 
    498           $this->attributes[$name] = $value; 
    499      } 
    500       
    501      public function getAttributes() 
    502      { 
    503           return $this->attributes; 
    504      } 
    505       
     551     public function getAttributes($options) 
     552     { 
     553          $offset = isset($options['offset']) ? $options['offset'] : 0; 
     554 
     555          $attr = $this->options->getArrayCopy(); 
     556          if (isset($this->values[$offset])) { 
     557               $attr['value'] = $this->values[$offset]; 
     558          } else { 
     559               $attr['value'] = $this->getDefaultValue(); 
     560          } 
     561          if ($offset==0) { 
     562               $attr['id']=$this->id; 
     563          } 
     564          $attr['name'] = $this->name; 
     565          if ($this->multiple) { 
     566               $attr['name'] = $attr['name'].'[]'; 
     567          } 
     568          return $attr; 
     569     } 
     570      
     571     public function getDefaultValue() { 
     572          return ''; 
     573     } 
     574 
    506575     public function getName() 
    507576     { 
    508577          return $this->name; 
    509578     } 
     579 
    510580     public function setName($name) { 
    511581          $this->setNID($name); 
    512           $this->attributes['name'] = $this->name; 
    513           $this->attributes['id'] = $this->id; 
    514582     } 
    515583 
    516584     public function check() 
    517585     { 
    518           if (!$this->defined && $this->attributes['defined']) { 
     586          if (!$this->defined && $this->options['mandatory']) { 
    519587               throw new InvalidFieldException(sprintf( 
    520588                    'Field "%s" is mandatory', 
     
    524592     } 
    525593      
    526      public function parseValue($from) { 
    527           if (isset($from[$this->id])) { 
    528                return $from[$this->id]; 
    529           } 
    530           return null; 
     594     public function parseValues($from) { 
     595          if (isset($from[$this->name])) { 
     596               $n = $from[$this->name]; 
     597               if (!is_array($n)) { 
     598                    $n = array($n); 
     599               } 
     600               return $n; 
     601          } 
     602          return array(); 
    531603     } 
    532604 
    533605     public function setup($from) 
    534606     { 
    535           $value = $this->parseValue($from); 
    536           if ($value !== null) { 
    537                $this->setValue($value); 
     607          $values = $this->parseValues($from); 
     608          if (count($values)) { 
     609               $this->setValues($values); 
    538610               $this->defined = true; 
    539611          } 
     
    546618} 
    547619 
     620 
    548621/** 
    549622 * Template form field of type "password" 
     
    599672          return "field_checkbox"; 
    600673     } 
     674 
     675     public function getDefaultValue() { 
     676          return 0; 
     677     } 
    601678} 
    602679 
     
    607684{ 
    608685     protected $action; 
    609       
    610      public function __construct($name,$value,$attributes=array()) 
    611      { 
    612           parent::__construct($name,$value,$attributes); 
    613            
    614           if (isset($attributes['action'])) { 
    615                $this->action = $attributes['action']; 
     686 
     687     public function __construct($name,$values,$options=array()) 
     688     { 
     689          parent::__construct($name,$values,$options); 
     690 
     691          if (isset($options['action'])) { 
     692               $this->action = $options['action']; 
    616693          } else { 
    617694               $this->action = NULL; 
    618695          } 
    619696     } 
    620       
     697 
    621698     public function getAction() 
    622699     { 
     
    641718class dcFieldCombo extends dcField 
    642719{ 
    643      protected $options; 
    644       
    645      public function __construct($name,$value,$options,$attributes=array()) 
    646      { 
    647           $this->options = $options; 
    648           parent::__construct($name,$value,$attributes); 
    649           $this->attributes['options']=$options; 
     720     protected $combo; 
     721      
     722     public function __construct($name,$value,$combo,$options=array()) 
     723     { 
     724          $this->combo = $combo; 
     725          parent::__construct($name,$value,$options); 
    650726     } 
    651727      
     
    655731     } 
    656732 
    657      public function parseValue($from) { 
    658  
    659           $v = parent::parseValue($from); 
    660           if (!isset($this->options[$v])) { 
    661                return $this->value; 
    662           } else { 
    663                return $v; 
    664           } 
    665      } 
    666 } 
     733     public function getDefaultValue() { 
     734          return current($this->combo); 
     735     } 
     736 
     737     public function parseValues($from) { 
     738          $values = parent::parseValues($from); 
     739          if (!is_array($values)) { 
     740               $values = array($values); 
     741          } 
     742          foreach ($values as &$v) { 
     743               if (!isset($this->combo[$v])) 
     744               $v = $this->getDefaultValue(); 
     745          } 
     746          return $values; 
     747     } 
     748 
     749     public function getAttributes($options) { 
     750          $attr = parent::getAttributes($options); 
     751          $attr['options'] = $this->combo; 
     752          return $attr; 
     753     } 
     754} 
     755 
    667756?> 
  • inc/admin/default-templates/forms/form_layout.html.twig

    r1148 r1152  
    4848 
    4949{% block field_checkbox %} 
    50 {% spaceless %} 
     50{% set type = type|default('checkbox') %} 
     51{% set nestedlabel = true %} 
     52{% set labelclass = "classic" %} 
     53    {{ block('field_input') }} 
     54{#{% spaceless %} 
     55 
    5156     {% if label is not empty %} 
    5257          <label for="{{name}}" class="classic"> 
     
    5661          {{ label }}</label> 
    5762     {% endif %} 
    58 {% endspaceless %} 
     63{% endspaceless %}#} 
    5964{% endblock field_checkbox %} 
    6065 
     
    105110    {% if label is not empty %} 
    106111        {% if required is not empty %} 
    107             {% set labelclass = labelclass + " required" %} 
     112            {% set labelclass = labelclass ~ " required" %} 
    108113        {% endif %} 
    109114        <label for="{{name}}" {% if labelclass is not empty %}class="{{labelclass}}"{% endif %} {% if required is not empty %}<abbr title="{{__('Required field')}}">*</abbr>{% else %}>{% endif %} {{label}} 
  • inc/admin/default-templates/forms/formfilter_layout.html.twig

    r1147 r1152  
    3131     </div> 
    3232     <div class="col30"> 
     33          {% for f in fenv.static_filters %} 
     34               {{block(f.fwidget)}} 
     35          {% endfor %} 
    3336     </div> 
    3437     <p class="clear margintop"></p> 
     
    5053 
    5154{% block filter_combo %} 
     55     {% if f.display_inline is defined -%} 
     56          <p id='{{f.filter_id}}'>{{form_field(f.ffield,{},{'label':f.desc,'nestedlabel': true, 'labelclass':'classic' })}}</p> 
     57     {%- else -%} 
     58          <td id='{{f.filter_id}}' title='{{f.desc}}' class="filter-title" colspan="2">{{f.desc}} : </td> 
     59          <td >{{ form_field(f.ffield) }}</td> 
     60     {%- endif %} 
     61{% endblock %} 
     62 
     63{% block filter_combo_cont %} 
     64     {% if f.display_inline is defined -%} 
     65          <p id='{{filter_id}}'>{{form_field(f.ffield,{'label':__('or :')})}}</p> 
     66     {%- else -%} 
     67          <td id='{{filter_id}}' colspan="2">{{ __('or :') }} </td><td>{{ form_field(f.ffield) }}</td> 
     68     {%- endif %} 
     69{% endblock %} 
     70 
     71 
     72{% block filter_richcombo %} 
    5273     <td id='{{filterd_id}}' title='{{f.desc}}' class="filter-title">{{f.desc}} : </td> 
    5374     <td>{{ form_field(f.fverb)}}</td> 
     
    5576{% endblock %} 
    5677 
    57 {% block filter_combo_cont %} 
    58      <td id='{{filter_id}}' colspan="2">{{ __('or :') }} </td><td>{{ form_field(f.ffield) }}</td> 
    59 {% endblock %} 
    6078 
    6179{% block filter_boolean %} 
     
    6482{% endblock %} 
    6583 
     84{% block filter_text %} 
     85     {% if f.display_inline is defined -%} 
     86          <p id='{{filter_id}}'>{{form_field(f.ffield,{},{'label':f.desc,'nestedlabel': true, 'labelclass':'classic' })}}</p> 
     87     {%- else -%} 
     88          <td id='{{filterd_id}}' title='{{f.desc}}' class="filter-title" colspan="2">{{f.desc}}</td> 
     89          <td>{{ form_field(f.ffield) }}</td> 
     90     {% endif %} 
     91{% endblock %} 
    6692 
    67 {% block entrieslist %} 
    68 {% if entries %} 
    69      <table class="maximal clear"> 
    70      <thead> 
    71           <tr> 
    72           {% for h in columns %} 
    7393 
    74           {% endfor %} 
    75           </tr> 
    76      </thead> 
    77  
    78 {% else %} 
    79      <p>{{ __('No entries') }}</p> 
    80 {% endif %} 
    81  
    82 {% endblock %} 
  • inc/admin/default-templates/posts.html.twig

    r1147 r1152  
    77     {{ js.modal }} 
    88     {{ js.meta_editor }} 
    9      <script type="text/javascript" src="{{theme_url}}js/_post.js"></script> 
     9     <script type="text/javascript" src="{{theme_url}}js/_posts_list.js"></script> 
     10     <script type="text/javascript" src="{{theme_url}}js/filters.js"></script> 
    1011     {{ js.confirm_close(['entry-form','comment-form']) }} 
    1112     {{ js.page_tabs(default_tab) }} 
     
    1819{% endblock %} 
    1920 
     21 
     22 
    2023{% block content %} 
    2124{{ parent() }} 
     
    2528<p> Applied filters : {{filters}} </p> 
    2629 
    27  
    28  
     30{{ listitems('lposts')}} 
    2931 
    3032{% endblock %} 
  • inc/core/class.dc.blog.php

    r1052 r1152  
    805805           
    806806          if (!empty($params['user_id'])) { 
    807                $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' "; 
     807               $strReq .= "AND U.user_id ".$this->con->in($params['user_id'])." "; 
    808808          } 
    809809           
  • inc/core/class.dc.core.php

    r1147 r1152  
    9696          $this->addFormater('xhtml', create_function('$s','return $s;')); 
    9797          $this->addFormater('wiki', array($this,'wikiTransform')); 
    98            
    9998          $this->loadTemplateEnvironment(); 
    10099     } 
     
    129128     $core->tpl->getLoader()->prependPath('PATH_TO/MY_THEME'); 
    130129     */ 
    131      protected function loadTemplateEnvironment() 
    132      { 
    133           # If cache dir is writable, use it. 
     130     public function loadTemplateEnvironment() 
     131     { 
    134132          $cache_dir = path::real(DC_TPL_CACHE.'/twtpl',false); 
    135133          if (!is_dir($cache_dir)) { 
     
    155153          ); 
    156154          $this->tpl->addExtension(new dcFormExtension($this)); 
    157           $this->tpl->addExtension(new dcFilterSetExtension($this)); 
    158155          $this->tpl->addExtension(new dcTabExtension($this)); 
    159156     } 
  • inc/prepend.php

    r1149 r1152  
    6868$__autoload['dcFormExtension']               = dirname(__FILE__).'/admin/class.dc.form.php'; 
    6969$__autoload['dcTabExtension']           = dirname(__FILE__).'/admin/class.dc.tab.php'; 
    70 $__autoload['dcFilterSetExtension']               = dirname(__FILE__).'/admin/class.dc.filter.php'; 
     70$__autoload['dcItemList']               = dirname(__FILE__).'/admin/class.dc.list.php'; 
     71 
     72foreach (array('dcFilterSet', 'dcFilter','dcFilterCombo','dcFilterText','dcFilterBoolean') as $c) { 
     73     $__autoload[$c] = dirname(__FILE__).'/admin/class.dc.filter.php'; 
     74} 
    7175 
    7276# Clearbricks extensions 
Note: See TracChangeset for help on using the changeset viewer.

Sites map