Dotclear


Ignore:
File:
1 edited

Legend:

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

    r1137 r1153  
    1313 
    1414/** 
    15  * Template form node 
    16  */ 
     15* dcFormNode 
     16* 
     17* @uses     Twig_Node 
     18* 
     19*/ 
    1720class dcFormNode extends Twig_Node 
    1821{ 
     
    3033     { 
    3134          $compiler 
    32                ->addDebugInfo($this) 
    33                ->write("\$context['dc_form']->beginForm('". 
    34                     $this->getAttribute('name')."');\n") 
     35               ->addDebugInfo($this); 
     36          $compiler 
     37               ->write("\$context['dc_form']->beginForm(") 
     38               ->subcompile($this->getAttribute('name')) 
     39               ->write(");\n"); 
     40          $compiler 
    3541               ->subcompile($this->getNode('body')) 
    3642               ->write("\$context['dc_form']->renderHiddenWidgets();\n") 
     
    4955          $lineno = $token->getLine(); 
    5056          $stream = $this->parser->getStream(); 
    51           $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); 
     57          $name = $this->parser->getExpressionParser()->parseExpression(); 
    5258          $stream->expect(Twig_Token::BLOCK_END_TYPE); 
    5359          $body = $this->parser->subparse(array($this,'decideBlockEnd'),true); 
     
    7783     protected $core; 
    7884     protected $twig; 
    79      protected $blocks; 
    8085     protected $forms; 
    8186     protected $currentForm; 
     87     protected $blocks; 
    8288      
    8389     public function __construct($core) 
    8490     { 
    8591          $this->core = $core; 
    86           $this->tpl = 'form_layout.html.twig'; 
     92          $this->tpl = array('@forms/form_layout.html.twig'); 
    8793          $this->forms = array(); 
     94          $this->blocks = array(); 
    8895          $this->currentForm = null; 
    8996     } 
     
    9299     { 
    93100          $this->twig = $environment; 
    94           $this->template = $this->twig->loadTemplate($this->tpl); 
    95           $this->blocks = $this->template->getBlocks(); 
    96      } 
    97       
     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          } 
     106     } 
     107      
     108     public function addTemplate($tpl) { 
     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          } 
     114     } 
     115 
    98116     public function getGlobals() 
    99117     { 
     
    104122     { 
    105123          return array( 
    106                'form_field' => new Twig_Function_Method( 
    107                     $this, 
    108                     'renderWidget', 
     124               new Twig_SimpleFunction( 
     125                    'widget', 
     126                    array($this,'renderWidget'), 
    109127                    array('is_safe' => array('html')) 
    110128               ), 
    111                '_form_is_choice_group' => new Twig_Function_Method( 
    112                     $this, 
    113                     'isChoiceGroup', 
     129               new Twig_SimpleFunction( 
     130                    'haswidget', 
     131                    array($this,'hasWidget'), 
    114132                    array('is_safe' => array('html')) 
    115133               ), 
    116                '_form_is_choice_selected' => new Twig_Function_Method( 
    117                     $this, 
    118                     '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'), 
    119147                    array('is_safe' => array('html')) 
    120148               ) 
     
    137165     } 
    138166      
    139      public function renderWidget($name,$attributes=array()) 
     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 ''; 
     173          echo $this->template->renderBlock( 
     174               $name, 
     175               $attr, 
     176               $this->blocks 
     177          ); 
     178     } 
     179 
     180     public function getCurrentForm() { 
     181          return $this->currentForm; 
     182     } 
     183 
     184     public function renderField($name,$attributes=array(),$extra=array()) 
    140185     { 
    141186          $field = $this->currentForm->$name; 
    142187          if ($field) { 
    143                echo $this->template->renderBlock( 
     188               $attr = $field->getAttributes($attributes); 
     189               if (isset($attr['attr'])) { 
     190                    $attr['attr'] = array_merge($attr['attr'],$attributes); 
     191               } else { 
     192                    $attr['attr'] = $attributes; 
     193               } 
     194               $this->renderWidget( 
    144195                    $field->getWidgetBlock(), 
    145196                    array_merge( 
    146                          $field->getAttributes(), 
    147                          array('attr' => $attributes) 
    148                     ), 
    149                     $this->blocks 
     197                         $attr, 
     198                         $extra 
     199                    ) 
    150200               ); 
    151201          } 
     
    155205     { 
    156206          foreach ($this->currentForm->getHiddenFields() as $h) { 
    157                $this->renderWidget($h->getName()); 
     207               $this->renderField($h->getName()); 
    158208          } 
    159209     } 
     
    197247 
    198248/** 
    199  * Template form 
    200  */ 
     249* dcForm - Template form 
     250* 
     251*/ 
    201252class dcForm 
    202253{ 
     
    211262     protected $errors; 
    212263      
    213      private function addNonce() 
     264     public function addTemplate($t) { 
     265          $this->core->tpl->getExtension('dc_form')->addTemplate($t); 
     266     } 
     267 
     268    /** 
     269     * addNonce -- adds dc nonce to form fields 
     270     *  
     271     * @access protected 
     272     * 
     273     * @return nothing 
     274     */ 
     275     protected function addNonce() 
    214276     { 
    215277          $this->addField( 
     
    219281     } 
    220282      
    221      protected function getNID($nid) 
     283 
     284    /** 
     285     * Defines Name & ID from field 
     286     *  
     287     * @param mixed $nid either an array (name, id) or a string (name only, id will be set to null). 
     288     * 
     289     * @access protected 
     290     * 
     291     * @return nothing. 
     292     */ 
     293     protected function setNID($nid) 
    222294     { 
    223295          if (is_array($nid)) { 
     
    231303     } 
    232304      
     305     public function getContext() { 
     306          return array(); 
     307     } 
     308 
     309    /** 
     310     * Class constructor 
     311     *  
     312     * @param mixed  $core   dotclear core 
     313     * @param mixed  $name   form name 
     314     * @param mixed  $action form action 
     315     * @param string $method form method ('GET' or 'POST') 
     316     * 
     317     * @access public 
     318     * 
     319     * @return mixed Value. 
     320     */ 
    233321     public function __construct($core,$name,$action,$method='POST') 
    234322     { 
    235323          $this->core = $core; 
    236           $this->getNID($name); 
     324          $this->setNID($name); 
    237325          $this->method = $method; 
    238326          $this->action = $action; 
     
    247335     } 
    248336      
     337 
     338    /** 
     339     * Returns form name 
     340     *  
     341     * @access public 
     342     * 
     343     * @return mixed Value. 
     344     */ 
    249345     public function getName() 
    250346     { 
     
    270366     } 
    271367      
     368     public function removeField(dcField $f) { 
     369          $n = $f->getName(); 
     370          if (isset($this->fields[$n])){ 
     371               unset($this->fields[$n]); 
     372          } 
     373 
     374     } 
     375     public function renameField($field,$newname) { 
     376          $oldname = $field->getName(); 
     377          if (isset($this->fields[$oldname])) { 
     378               unset($this->fields[$oldname]); 
     379               $field->setName($newname); 
     380               $this->fields[$newname] = $field; 
     381          } 
     382     } 
    272383     public function begin() 
    273384     { 
     
    292403     public function __get($name) 
    293404     { 
    294           return isset($this->fields[$name]) ?  
     405          return isset($this->fields[$name]) ? 
    295406               $this->fields[$name] : null; 
    296407     } 
    297       
     408 
    298409     public function __set($name,$value) 
    299410     { 
    300411          if (isset($this->fields[$name])) { 
    301                $this->fields[$name]->setAttribute('value',$value); 
    302           } 
    303      } 
    304       
     412               $this->fields[$name]->setValue($value); 
     413          } 
     414     } 
     415 
    305416     public function isSubmitted() 
    306417     { 
    307418          $from = $this->method == 'POST' ? $_POST : $_GET; 
    308           echo "form fields :\n"; 
    309      } 
    310       
    311      public function setup() 
    312      { 
     419     } 
     420 
     421     protected function setupFields() { 
    313422          $from = $this->method == 'POST' ? $_POST : $_GET; 
    314423          foreach ($this->fields as $f) { 
    315424               $f->setup($from); 
    316425          } 
     426     } 
     427 
     428     protected function handleActions($submitted) { 
     429          foreach ($submitted as $f) { 
     430               $action = $f->getAction(); 
     431               if ($action != NULL) { 
     432                    $ret = call_user_func($action,$this); 
     433               } 
     434          } 
     435     } 
     436 
     437     protected function getSubmittedFields() { 
     438          $s = array(); 
    317439          foreach ($this->submitfields as $f) { 
    318440               if ($f->isDefined()) { 
    319                     $ret = call_user_func($f->getAction(),$this); 
    320                     return; 
     441                    $s[$f->getName()] = $f; 
    321442               } 
    322443          } 
    323      } 
    324       
     444          return $s; 
     445     } 
     446 
     447     public function setup() 
     448     { 
     449          $this->setupFields(); 
     450          $submitted = $this->getSubmittedFields(); 
     451          $this->handleActions($submitted); 
     452     } 
     453 
    325454     public function check() 
    326455     { 
     
    344473 * Template form field 
    345474 */ 
    346 abstract class dcField 
    347 { 
    348      protected $attributes; 
     475abstract class dcField implements Countable 
     476{ 
     477     protected $options; 
    349478     protected $name; 
    350      protected $value; 
     479     protected $values; 
    351480     protected $id; 
     481     protected $multiple; 
    352482     protected $defined; 
    353483      
    354      protected function getNID($nid) 
     484     protected function setNID($nid) 
    355485     { 
    356486          if (is_array($nid)) { 
     
    363493     } 
    364494      
    365      public function __construct($name,$value,$attributes=array()) 
    366      { 
    367           $this->getNID($name); 
    368           $this->attributes = $attributes; 
    369           $this->value = $value; 
    370           $this->attributes['name'] = $this->name; 
    371           $this->attributes['id'] = $this->id; 
    372           $this->attributes['value'] = $this->value; 
     495     public function __construct($name,$values,$options=array()) 
     496     { 
     497          $this->setNID($name); 
     498          $this->options = new ArrayObject($options); 
     499          if ($values === NULL){ 
     500               $values = array(); 
     501          } 
     502          $this->setValues($values); 
    373503          $this->defined = false; 
    374      } 
    375       
     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); 
     542     } 
     543 
    376544     public function __toString() 
    377545     { 
    378           return (string) $this->value; 
     546          return join(',',$this->values); 
    379547     } 
    380548      
    381549     abstract public function getWidgetBlock(); 
    382550      
    383      public function setAttribute($name,$value) 
    384      { 
    385           $this->attributes[$name] = $value; 
    386      } 
    387       
    388      public function getAttributes() 
    389      { 
    390           return $this->attributes; 
    391      } 
    392       
     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 
    393575     public function getName() 
    394576     { 
    395577          return $this->name; 
    396578     } 
    397       
     579 
     580     public function setName($name) { 
     581          $this->setNID($name); 
     582     } 
     583 
    398584     public function check() 
    399585     { 
    400           if (!$this->defined && $this->attributes['defined']) { 
     586          if (!$this->defined && $this->options['mandatory']) { 
    401587               throw new InvalidFieldException(sprintf( 
    402588                    'Field "%s" is mandatory', 
     
    406592     } 
    407593      
     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(); 
     603     } 
     604 
    408605     public function setup($from) 
    409606     { 
    410           if (isset($from[$this->id])) { 
    411                $this->value = $from[$this->id]; 
     607          $values = $this->parseValues($from); 
     608          if (count($values)) { 
     609               $this->setValues($values); 
    412610               $this->defined = true; 
    413611          } 
     
    420618} 
    421619 
     620 
    422621/** 
    423622 * Template form field of type "password" 
     
    473672          return "field_checkbox"; 
    474673     } 
     674 
     675     public function getDefaultValue() { 
     676          return 0; 
     677     } 
    475678} 
    476679 
     
    481684{ 
    482685     protected $action; 
    483       
    484      public function __construct($name,$value,$attributes=array()) 
    485      { 
    486           parent::__construct($name,$value,$attributes); 
    487            
    488           if (isset($attributes['action'])) { 
    489                $this->action = $attributes['action']; 
    490           } 
    491      } 
    492       
     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']; 
     693          } else { 
     694               $this->action = NULL; 
     695          } 
     696     } 
     697 
    493698     public function getAction() 
    494699     { 
     
    513718class dcFieldCombo extends dcField 
    514719{ 
    515      protected $options; 
    516       
    517      public function __construct($name,$value,$options,$attributes=array()) 
    518      { 
    519           $this->options = $options; 
    520           parent::__construct($name,$value,$attributes); 
    521           $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); 
    522726     } 
    523727      
     
    527731     } 
    528732 
    529 } 
     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 
    530756?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map