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.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?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map