Changeset 1058:d839f35806af for inc/admin
- Timestamp:
- 12/10/12 02:45:43 (13 years ago)
- Branch:
- twig
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/class.dc.form.php
r1056 r1058 1 1 <?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 ----------------------------------------- 12 if (!defined('DC_RC_PATH')) { return; } 13 14 /** 15 * Template form node 16 */ 17 class 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 */ 45 class 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 41 59 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 */ 73 class dcFormExtension extends Twig_Extension 74 { 55 75 protected $template; 56 76 protected $tpl; … … 60 80 protected $forms; 61 81 protected $currentForm; 62 63 public function __construct($core){ 82 83 public function __construct($core) 84 { 64 85 $this->core = $core; 65 86 $this->tpl = 'form_layout.html.twig'; … … 67 88 $this->currentForm = null; 68 89 } 69 70 71 72 90 91 public function initRuntime(Twig_Environment $environment) 92 { 93 $this->twig = $environment; 73 94 $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 { 82 105 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 ) 87 121 ); 88 122 } 89 90 public function isChoiceGroup($choice) { 123 124 public function isChoiceGroup($choice) 125 { 91 126 return is_array($choice); 92 127 } 93 public function isChoiceSelected($choice,$value) { 128 129 public function isChoiceSelected($choice,$value) 130 { 94 131 return $choice == $value; 95 96 }97 98 public function getTokenParsers(){132 } 133 134 public function getTokenParsers() 135 { 99 136 return array(new dcFormTokenParser()); 100 137 } 101 102 public function renderWidget($name,$attributes=array()) { 138 139 public function renderWidget($name,$attributes=array()) 140 { 103 141 $field = $this->currentForm->$name; 104 142 if ($field) { 105 143 echo $this->template->renderBlock( 106 144 $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 { 113 156 foreach ($this->currentForm->getHiddenFields() as $h) { 114 157 $this->renderWidget($h->getName()); … … 116 159 } 117 160 118 public function getName() { 161 public function getName() 162 { 119 163 return 'dc_form'; 120 164 } 121 165 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 { 127 173 if (isset($this->forms[$name])) { 128 174 $this->currentForm = $this->forms[$name]; 129 175 $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 { 135 187 $this->currentForm->end(); 136 188 $this->currentForm = null; 137 189 } 138 139 } 140 190 } 191 192 /** 193 * Template form exception 194 */ 141 195 class InvalidFieldException extends Exception { 142 196 } 143 197 144 145 class dcForm { 198 /** 199 * Template form 200 */ 201 class dcForm 202 { 146 203 protected $name; 147 204 protected $core; … … 153 210 protected $errors; 154 211 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 { 160 222 $this->core = $core; 161 223 $this->name = $name; … … 171 233 } 172 234 } 173 174 public function getName() { 235 236 public function getName() 237 { 175 238 return $this->name; 176 239 } 177 178 public function getErrors() { 240 241 public function getErrors() 242 { 179 243 return $this->errors; 180 244 } 181 245 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; 183 255 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;192 256 return $this; 193 257 } 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 { 200 270 echo '</form>'; 201 271 } 202 203 204 public function __isset($name){272 273 public function __isset($name) 274 { 205 275 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 { 217 286 if (isset($this->fields[$name])) { 218 287 $this->fields[$name]->setAttribute('value',$value); 219 288 } 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; 224 294 echo "form fields :\n"; 225 295 } 226 296 227 public function setup() { 228 $from = ($this->method=='POST')?$_POST:$_GET; 297 public function setup() 298 { 299 $from = $this->method == 'POST' ? $_POST : $_GET; 229 300 foreach ($this->fields as $f) { 230 301 $f->setup($from); … … 232 303 foreach ($this->submitfields as $f) { 233 304 if ($f->isDefined()) { 234 $ret = call_user_func 305 $ret = call_user_func($f->getAction(),$this); 235 306 return; 236 307 } … … 238 309 } 239 310 240 public function check() { 311 public function check() 312 { 241 313 foreach ($this->fields as $f) { 242 314 try { 243 315 $f->check(); 244 } catch (InvalidFieldException $e) {245 $this->errors[]=$e->getMessage();246 316 } 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 { 251 325 return $this->hiddenfields; 252 326 } 253 254 } 255 256 abstract class dcField { 327 } 328 329 /** 330 * Template form field 331 */ 332 abstract class dcField 333 { 257 334 protected $attributes; 258 335 protected $name; … … 261 338 protected $defined; 262 339 263 protected function getNID($nid) { 340 protected function getNID($nid) 341 { 264 342 if (is_array($nid)) { 265 343 $this->name = $nid[0]; 266 344 $this->id = !empty($nid[1]) ? $nid[1] : null; 267 } else { 345 } 346 else { 268 347 $this->id = $this->name = $nid; 269 348 } 270 349 } 271 272 public function __construct($name, $value, $attributes = array()) { 350 351 public function __construct($name,$value,$attributes=array()) 352 { 273 353 $this->getNID($name); 274 354 $this->attributes = $attributes; … … 278 358 $this->attributes['value'] = $this->value; 279 359 $this->defined = false; 280 281 } 282 360 } 361 362 public function __toString() 363 { 364 return $this->value; 365 } 366 283 367 abstract public function getWidgetBlock(); 284 285 public function setAttribute ($name,$value) { 368 369 public function setAttribute($name,$value) 370 { 286 371 $this->attributes[$name] = $value; 287 372 } 288 289 public function getAttributes() { 373 374 public function getAttributes() 375 { 290 376 return $this->attributes; 291 292 }293 294 public function getName(){377 } 378 379 public function getName() 380 { 295 381 return $this->name; 296 382 } 297 298 public function check() { 383 384 public function check() 385 { 299 386 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 { 306 396 if (isset($from[$this->id])) { 307 397 $this->value = $from[$this->id]; … … 310 400 } 311 401 312 public function isDefined() { 402 public function isDefined() 403 { 313 404 return $this->defined; 314 405 } 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 */ 411 class 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 */ 422 class 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 */ 433 class dcFieldTextArea extends dcField 434 { 435 public function getWidgetBlock() 436 { 330 437 return "field_textarea"; 331 438 } 332 333 } 334 335 class dcFieldHidden extends dcField { 336 337 public function getWidgetBlock() { 439 } 440 441 /** 442 * Template form field of type "hidden" 443 */ 444 class dcFieldHidden extends dcField 445 { 446 public function getWidgetBlock() 447 { 338 448 return "field_hidden"; 339 449 } 340 341 } 342 343 class dcFieldCheckbox extends dcField { 344 345 public function getWidgetBlock() { 450 } 451 452 /** 453 * Template form field of type "checkbox" 454 */ 455 class dcFieldCheckbox extends dcField 456 { 457 public function getWidgetBlock() 458 { 346 459 return "field_checkbox"; 347 460 } 348 349 } 350 351 abstract class dcFieldAction extends dcField { 461 } 462 463 /** 464 * Template form action 465 */ 466 abstract class dcFieldAction extends dcField 467 { 352 468 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 355 474 if (isset($attributes['action'])) { 356 475 $this->action = $attributes['action']; 357 476 } 358 477 } 359 public function getAction() { 478 479 public function getAction() 480 { 360 481 return $this->action; 361 482 } 362 483 } 363 484 364 class dcFieldSubmit extends dcFieldAction { 365 366 public function getWidgetBlock() { 485 /** 486 * Template form field of type "submit" 487 */ 488 class dcFieldSubmit extends dcFieldAction 489 { 490 public function getWidgetBlock() 491 { 367 492 return "field_submit"; 368 493 } 369 370 } 371 372 class dcFieldCombo extends dcField { 494 } 495 496 /** 497 * Template form field of type "combo" 498 */ 499 class dcFieldCombo extends dcField 500 { 373 501 protected $options; 374 375 public function __construct($name, $value, $options, $attributes = array()) { 502 503 public function __construct($name,$value,$options,$attributes=array()) 504 { 376 505 $this->options = $options; 377 506 parent::__construct($name,$value,$attributes); 378 507 $this->attributes['options']=$options; 379 508 } 380 381 public function getWidgetBlock() { 509 510 public function getWidgetBlock() 511 { 382 512 return "field_combo"; 383 513 } 384 514 385 515 } 386 387 516 ?>
Note: See TracChangeset
for help on using the changeset viewer.