Dotclear


Ignore:
Timestamp:
12/10/12 02:45:43 (13 years ago)
Author:
JcDenis
Branch:
twig
Message:
  • Clean up dcForm classes according to Dotclear's coding standards
  • Added magic toString method to dcField
File:
1 edited

Legend:

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

    r1056 r1058  
    11<?php 
    2  
    3  
    4 class dcFormNode extends Twig_Node { 
    5     public function __construct($name, Twig_NodeInterface $body, $lineno, $tag = null) 
    6     { 
    7         parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag); 
    8     } 
    9  
    10     /** 
    11      * Compiles the node to PHP. 
    12      * 
    13      * @param Twig_Compiler A Twig_Compiler instance 
    14      */ 
    15     public function compile(Twig_Compiler $compiler) 
    16     { 
    17         $compiler 
    18             ->addDebugInfo($this) 
    19             ->write("\$context['dc_form']->beginForm('". 
    20                 $this->getAttribute('name')."');\n") 
    21             ->subcompile($this->getNode('body')) 
    22             ->write("\$context['dc_form']->renderHiddenWidgets();\n") 
    23             ->write("\$context['dc_form']->endForm();\n") 
    24         ; 
    25     } 
    26  
    27 } 
    28  
    29 class dcFormTokenParser extends Twig_TokenParser { 
    30  
    31      public function parse(Twig_Token $token) { 
    32         $lineno = $token->getLine(); 
    33         $stream = $this->parser->getStream(); 
    34         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); 
    35         $stream->expect(Twig_Token::BLOCK_END_TYPE); 
    36         $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); 
    37         $stream->expect(Twig_Token::BLOCK_END_TYPE); 
    38         return new dcFormNode ($name, $body, $token->getLine(), $this->getTag()); 
    39      } 
    40  
     2# -- BEGIN LICENSE BLOCK --------------------------------------- 
     3# 
     4# This file is part of Dotclear 2. 
     5# 
     6# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear 
     7# Licensed under the GPL version 2.0 license. 
     8# See LICENSE file or 
     9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
     10# 
     11# -- END LICENSE BLOCK ----------------------------------------- 
     12if (!defined('DC_RC_PATH')) { return; } 
     13 
     14/** 
     15 * Template form node 
     16 */ 
     17class dcFormNode extends Twig_Node 
     18{ 
     19     public function __construct($name,Twig_NodeInterface $body,$lineno,$tag=null) 
     20     { 
     21          parent::__construct(array('body' => $body),array('name' => $name),$lineno,$tag); 
     22     } 
     23      
     24     /** 
     25     * Compiles the node to PHP. 
     26     * 
     27     * @param Twig_Compiler A Twig_Compiler instance 
     28     */ 
     29     public function compile(Twig_Compiler $compiler) 
     30     { 
     31          $compiler 
     32               ->addDebugInfo($this) 
     33               ->write("\$context['dc_form']->beginForm('". 
     34                    $this->getAttribute('name')."');\n") 
     35               ->subcompile($this->getNode('body')) 
     36               ->write("\$context['dc_form']->renderHiddenWidgets();\n") 
     37               ->write("\$context['dc_form']->endForm();\n") 
     38          ; 
     39     } 
     40} 
     41 
     42/** 
     43 * Template form token parser 
     44 */ 
     45class dcFormTokenParser extends Twig_TokenParser 
     46{ 
     47     public function parse(Twig_Token $token) 
     48     { 
     49          $lineno = $token->getLine(); 
     50          $stream = $this->parser->getStream(); 
     51          $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); 
     52          $stream->expect(Twig_Token::BLOCK_END_TYPE); 
     53          $body = $this->parser->subparse(array($this,'decideBlockEnd'),true); 
     54          $stream->expect(Twig_Token::BLOCK_END_TYPE); 
     55           
     56          return new dcFormNode($name,$body,$token->getLine(),$this->getTag()); 
     57     } 
     58      
    4159     public function decideBlockEnd(Twig_Token $token) 
    42     { 
    43         return $token->test('endform'); 
    44     } 
    45  
    46  
    47     public function getTag() 
    48     { 
    49         return 'form'; 
    50     } 
    51  
    52 } 
    53  
    54 class dcFormExtension extends Twig_Extension { 
     60     { 
     61          return $token->test('endform'); 
     62     } 
     63      
     64     public function getTag() 
     65     { 
     66          return 'form'; 
     67     } 
     68} 
     69 
     70/** 
     71 * Template form extension 
     72 */ 
     73class dcFormExtension extends Twig_Extension 
     74{ 
    5575     protected $template; 
    5676     protected $tpl; 
     
    6080     protected $forms; 
    6181     protected $currentForm; 
    62  
    63      public function __construct($core){ 
     82      
     83     public function __construct($core) 
     84     { 
    6485          $this->core = $core; 
    6586          $this->tpl = 'form_layout.html.twig'; 
     
    6788          $this->currentForm = null; 
    6889     } 
    69  
    70     public function initRuntime(Twig_Environment $environment) 
    71     { 
    72     $this->twig = $environment; 
     90      
     91     public function initRuntime(Twig_Environment $environment) 
     92     { 
     93          $this->twig = $environment; 
    7394          $this->template = $this->twig->loadTemplate($this->tpl); 
    74      $this->blocks = $this->template->getBlocks(); 
    75     } 
    76  
    77     public function getGlobals() { 
    78      return array('dc_form' => $this); 
    79     } 
    80  
    81      public function getFunctions() { 
     95          $this->blocks = $this->template->getBlocks(); 
     96     } 
     97      
     98     public function getGlobals() 
     99     { 
     100          return array('dc_form' => $this); 
     101     } 
     102      
     103     public function getFunctions() 
     104     { 
    82105          return array( 
    83                'form_field' => new Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))), 
    84             '_form_is_choice_group'    => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))), 
    85             '_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))) 
    86  
     106               'form_field' => new Twig_Function_Method( 
     107                    $this, 
     108                    'renderWidget', 
     109                    array('is_safe' => array('html')) 
     110               ), 
     111               '_form_is_choice_group' => new Twig_Function_Method( 
     112                    $this, 
     113                    'isChoiceGroup', 
     114                    array('is_safe' => array('html')) 
     115               ), 
     116               '_form_is_choice_selected' => new Twig_Function_Method( 
     117                    $this, 
     118                    'isChoiceSelected', 
     119                    array('is_safe' => array('html')) 
     120               ) 
    87121          ); 
    88122     } 
    89  
    90      public function isChoiceGroup($choice) { 
     123      
     124     public function isChoiceGroup($choice) 
     125     { 
    91126          return is_array($choice); 
    92127     } 
    93      public function isChoiceSelected($choice,$value) { 
     128      
     129     public function isChoiceSelected($choice,$value) 
     130     { 
    94131          return $choice == $value; 
    95  
    96      } 
    97  
    98      public function getTokenParsers() { 
     132     } 
     133      
     134     public function getTokenParsers() 
     135     { 
    99136          return array(new dcFormTokenParser()); 
    100137     } 
    101  
    102      public function renderWidget($name,$attributes=array()) { 
     138      
     139     public function renderWidget($name,$attributes=array()) 
     140     { 
    103141          $field = $this->currentForm->$name; 
    104142          if ($field) { 
    105143               echo $this->template->renderBlock( 
    106144                    $field->getWidgetBlock(), 
    107                     array_merge($field->getAttributes(),array('attr'=>$attributes)), 
    108                     $this->blocks); 
    109           } 
    110      } 
    111  
    112      public function renderHiddenWidgets() { 
     145                    array_merge( 
     146                         $field->getAttributes(), 
     147                         array('attr' => $attributes) 
     148                    ), 
     149                    $this->blocks 
     150               ); 
     151          } 
     152     } 
     153 
     154     public function renderHiddenWidgets() 
     155     { 
    113156          foreach ($this->currentForm->getHiddenFields() as $h) { 
    114157               $this->renderWidget($h->getName()); 
     
    116159     } 
    117160 
    118      public function getName() { 
     161     public function getName() 
     162     { 
    119163          return 'dc_form'; 
    120164     } 
    121165 
    122      public function addForm(dcForm $form) { 
    123           $this->forms[$form->getName()]=$form; 
    124      } 
    125  
    126      public function beginForm($name) { 
     166     public function addForm(dcForm $form) 
     167     { 
     168          $this->forms[$form->getName()] = $form; 
     169     } 
     170 
     171     public function beginForm($name) 
     172     { 
    127173          if (isset($this->forms[$name])) { 
    128174               $this->currentForm = $this->forms[$name]; 
    129175               $this->currentForm->begin(); 
    130           } else { 
    131                throw new Twig_Error_Runtime("Form '".$name."' does not exist"); 
    132           } 
    133      } 
    134      public function endForm() { 
     176          } 
     177          else { 
     178               throw new Twig_Error_Runtime(sprintf( 
     179                    'Form "%s" does not exist', 
     180                    $name 
     181               )); 
     182          } 
     183     } 
     184      
     185     public function endForm() 
     186     { 
    135187          $this->currentForm->end(); 
    136188          $this->currentForm = null; 
    137189     } 
    138  
    139 } 
    140  
     190} 
     191 
     192/** 
     193 * Template form exception 
     194 */ 
    141195class InvalidFieldException extends Exception { 
    142196} 
    143197 
    144  
    145 class dcForm { 
     198/** 
     199 * Template form 
     200 */ 
     201class dcForm 
     202{ 
    146203     protected $name; 
    147204     protected $core; 
     
    153210     protected $errors; 
    154211      
    155      private function addNonce() { 
    156           $this->addField(new dcFieldHidden(array('xd_check'), $this->core->getNonce())); 
    157      } 
    158       
    159      public function __construct($core,$name,$action, $method='POST') { 
     212     private function addNonce() 
     213     { 
     214          $this->addField( 
     215               new dcFieldHidden(array('xd_check'), 
     216               $this->core->getNonce()) 
     217          ); 
     218     } 
     219      
     220     public function __construct($core,$name,$action,$method='POST') 
     221     { 
    160222          $this->core = $core; 
    161223          $this->name = $name; 
     
    171233          } 
    172234     } 
    173  
    174      public function getName() { 
     235      
     236     public function getName() 
     237     { 
    175238          return $this->name; 
    176239     } 
    177  
    178      public function getErrors() { 
     240      
     241     public function getErrors() 
     242     { 
    179243          return $this->errors; 
    180244     } 
    181245      
    182      public function addField(dcField $f) { 
     246     public function addField(dcField $f) 
     247     { 
     248          if ($f instanceof dcFieldAction) { 
     249               $this->submitfields[$f->getName()] = $f; 
     250          } 
     251          if ($f instanceof dcFieldHidden) { 
     252               $this->hiddenfields[$f->getName()] = $f; 
     253          } 
     254          $this->fields[$f->getName()] = $f; 
    183255           
    184           if ($f instanceof dcFieldAction) { 
    185                $this->submitfields[$f->getName()]=$f; 
    186           } 
    187           if ($f instanceof dcFieldHidden) { 
    188                $this->hiddenfields[$f->getName()]=$f; 
    189           } 
    190  
    191           $this->fields[$f->getName()]=$f; 
    192256          return $this; 
    193257     } 
    194  
    195      public function begin() { 
    196           echo '<form method="'.$this->method.'" action="'.$this->action.'">'; 
    197      } 
    198  
    199      public function end() { 
     258      
     259     public function begin() 
     260     { 
     261          echo sprintf( 
     262               '<form method="%s" action="%s">', 
     263               $this->method, 
     264               $this->action 
     265          ); 
     266     } 
     267      
     268     public function end() 
     269     { 
    200270          echo '</form>'; 
    201271     } 
    202  
    203  
    204     public function __isset($name) { 
     272      
     273     public function __isset($name) 
     274     { 
    205275          return isset($this->fields[$name]); 
    206     } 
    207  
    208     public function __get($name) { 
    209           if (isset($this->fields[$name])) { 
    210                return $this->fields[$name]; 
    211           } else { 
    212                return null; 
    213           } 
    214     } 
    215  
    216     public function __set($name,$value) { 
     276     } 
     277      
     278     public function __get($name) 
     279     { 
     280          return isset($this->fields[$name]) ?  
     281               $this->fields[$name] : null; 
     282     } 
     283      
     284     public function __set($name,$value) 
     285     { 
    217286          if (isset($this->fields[$name])) { 
    218287               $this->fields[$name]->setAttribute('value',$value); 
    219288          } 
    220     } 
    221  
    222      public function isSubmitted() { 
    223           $from = ($this->method == 'POST')?$_POST:$_GET; 
     289     } 
     290      
     291     public function isSubmitted() 
     292     { 
     293          $from = $this->method == 'POST' ? $_POST : $_GET; 
    224294          echo "form fields :\n"; 
    225295     } 
    226296      
    227      public function setup() { 
    228           $from = ($this->method=='POST')?$_POST:$_GET; 
     297     public function setup() 
     298     { 
     299          $from = $this->method == 'POST' ? $_POST : $_GET; 
    229300          foreach ($this->fields as $f) { 
    230301               $f->setup($from); 
     
    232303          foreach ($this->submitfields as $f) { 
    233304               if ($f->isDefined()) { 
    234                     $ret = call_user_func ($f->getAction(),$this); 
     305                    $ret = call_user_func($f->getAction(),$this); 
    235306                    return; 
    236307               } 
     
    238309     } 
    239310      
    240      public function check() { 
     311     public function check() 
     312     { 
    241313          foreach ($this->fields as $f) { 
    242314               try { 
    243315                    $f->check(); 
    244                } catch (InvalidFieldException $e) { 
    245                     $this->errors[]=$e->getMessage(); 
    246316               } 
    247           } 
    248      } 
    249       
    250      public function getHiddenFields() { 
     317               catch (InvalidFieldException $e) { 
     318                    $this->errors[] = $e->getMessage(); 
     319               } 
     320          } 
     321     } 
     322      
     323     public function getHiddenFields() 
     324     { 
    251325          return $this->hiddenfields; 
    252326     } 
    253       
    254 } 
    255  
    256 abstract class dcField { 
     327} 
     328 
     329/** 
     330 * Template form field 
     331 */ 
     332abstract class dcField 
     333{ 
    257334     protected $attributes; 
    258335     protected $name; 
     
    261338     protected $defined; 
    262339      
    263      protected function getNID($nid) { 
     340     protected function getNID($nid) 
     341     { 
    264342          if (is_array($nid)) { 
    265343               $this->name = $nid[0]; 
    266344               $this->id = !empty($nid[1]) ? $nid[1] : null; 
    267           } else { 
     345          } 
     346          else { 
    268347               $this->id = $this->name = $nid; 
    269348          } 
    270349     } 
    271  
    272      public function __construct($name, $value, $attributes = array()) { 
     350      
     351     public function __construct($name,$value,$attributes=array()) 
     352     { 
    273353          $this->getNID($name); 
    274354          $this->attributes = $attributes; 
     
    278358          $this->attributes['value'] = $this->value; 
    279359          $this->defined = false; 
    280  
    281      } 
    282  
     360     } 
     361      
     362     public function __toString() 
     363     { 
     364          return $this->value; 
     365     } 
     366      
    283367     abstract public function getWidgetBlock(); 
    284  
    285      public function setAttribute ($name,$value) { 
     368      
     369     public function setAttribute($name,$value) 
     370     { 
    286371          $this->attributes[$name] = $value; 
    287372     } 
    288  
    289      public function getAttributes() { 
     373      
     374     public function getAttributes() 
     375     { 
    290376          return $this->attributes; 
    291  
    292      } 
    293  
    294      public function getName(){ 
     377     } 
     378      
     379     public function getName() 
     380     { 
    295381          return $this->name; 
    296382     } 
    297  
    298      public function check() { 
     383      
     384     public function check() 
     385     { 
    299386          if (!$this->defined && $this->attributes['defined']) { 
    300                throw new InvalidFieldException(sprintf('Field "%s" is mandatory'),$this->attributes['label']); 
    301           } 
    302  
    303      } 
    304       
    305      public function setup($from) { 
     387               throw new InvalidFieldException(sprintf( 
     388                    'Field "%s" is mandatory', 
     389                    $this->attributes['label']) 
     390               ); 
     391          } 
     392     } 
     393      
     394     public function setup($from) 
     395     { 
    306396          if (isset($from[$this->id])) { 
    307397               $this->value = $from[$this->id]; 
     
    310400     } 
    311401      
    312      public function isDefined() { 
     402     public function isDefined() 
     403     { 
    313404          return $this->defined; 
    314405     } 
    315       
    316 } 
    317  
    318  
    319 class dcFieldText extends dcField { 
    320  
    321      public function getWidgetBlock() { 
    322           return "field_text"; 
    323      } 
    324  
    325 } 
    326  
    327 class dcFieldTextArea extends dcField { 
    328  
    329      public function getWidgetBlock() { 
     406} 
     407 
     408/** 
     409 * Template form field of type "password" 
     410 */ 
     411class dcFieldPassword extends dcField 
     412{ 
     413     public function getWidgetBlock() 
     414     { 
     415          return "field_password"; 
     416     } 
     417} 
     418 
     419/** 
     420 * Template form field of type "text" 
     421 */ 
     422class dcFieldText extends dcField 
     423{ 
     424     public function getWidgetBlock() 
     425     { 
     426     return "field_text"; 
     427     } 
     428} 
     429 
     430/** 
     431 * Template form field of type "textarea" 
     432 */ 
     433class dcFieldTextArea extends dcField 
     434{ 
     435     public function getWidgetBlock() 
     436     { 
    330437          return "field_textarea"; 
    331438     } 
    332  
    333 } 
    334  
    335 class dcFieldHidden extends dcField { 
    336  
    337      public function getWidgetBlock() { 
     439} 
     440 
     441/** 
     442 * Template form field of type "hidden" 
     443 */ 
     444class dcFieldHidden extends dcField 
     445{ 
     446     public function getWidgetBlock() 
     447     { 
    338448          return "field_hidden"; 
    339449     } 
    340  
    341 } 
    342  
    343 class dcFieldCheckbox extends dcField { 
    344  
    345      public function getWidgetBlock() { 
     450} 
     451 
     452/** 
     453 * Template form field of type "checkbox" 
     454 */ 
     455class dcFieldCheckbox extends dcField 
     456{ 
     457     public function getWidgetBlock() 
     458     { 
    346459          return "field_checkbox"; 
    347460     } 
    348  
    349 } 
    350  
    351 abstract class dcFieldAction extends dcField { 
     461} 
     462 
     463/** 
     464 * Template form action 
     465 */ 
     466abstract class dcFieldAction extends dcField 
     467{ 
    352468     protected $action; 
    353      public function __construct($name, $value, $attributes = array()) { 
    354           parent::__construct($name, $value, $attributes); 
     469      
     470     public function __construct($name,$value,$attributes=array()) 
     471     { 
     472          parent::__construct($name,$value,$attributes); 
     473           
    355474          if (isset($attributes['action'])) { 
    356475               $this->action = $attributes['action']; 
    357476          } 
    358477     } 
    359      public function getAction() { 
     478      
     479     public function getAction() 
     480     { 
    360481          return $this->action; 
    361482     } 
    362483} 
    363484 
    364 class dcFieldSubmit extends dcFieldAction { 
    365  
    366      public function getWidgetBlock() { 
     485/** 
     486 * Template form field of type "submit" 
     487 */ 
     488class dcFieldSubmit extends dcFieldAction 
     489{ 
     490     public function getWidgetBlock() 
     491     { 
    367492          return "field_submit"; 
    368493     } 
    369  
    370 } 
    371  
    372 class dcFieldCombo extends dcField { 
     494} 
     495 
     496/** 
     497 * Template form field of type "combo" 
     498 */ 
     499class dcFieldCombo extends dcField 
     500{ 
    373501     protected $options; 
    374  
    375      public function __construct($name, $value, $options, $attributes = array()) { 
     502      
     503     public function __construct($name,$value,$options,$attributes=array()) 
     504     { 
    376505          $this->options = $options; 
    377506          parent::__construct($name,$value,$attributes); 
    378507          $this->attributes['options']=$options; 
    379508     } 
    380  
    381      public function getWidgetBlock() { 
     509      
     510     public function getWidgetBlock() 
     511     { 
    382512          return "field_combo"; 
    383513     } 
    384514 
    385515} 
    386  
    387516?> 
Note: See TracChangeset for help on using the changeset viewer.

Sites map