[992] | 1 | <?php |
---|
[1058] | 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; } |
---|
[992] | 13 | |
---|
[1058] | 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 | } |
---|
[992] | 40 | } |
---|
| 41 | |
---|
[1058] | 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()); |
---|
[992] | 57 | } |
---|
[1058] | 58 | |
---|
[992] | 59 | public function decideBlockEnd(Twig_Token $token) |
---|
[1058] | 60 | { |
---|
| 61 | return $token->test('endform'); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public function getTag() |
---|
| 65 | { |
---|
| 66 | return 'form'; |
---|
| 67 | } |
---|
[992] | 68 | } |
---|
| 69 | |
---|
[1058] | 70 | /** |
---|
| 71 | * Template form extension |
---|
| 72 | */ |
---|
| 73 | class dcFormExtension extends Twig_Extension |
---|
| 74 | { |
---|
[992] | 75 | protected $template; |
---|
| 76 | protected $tpl; |
---|
| 77 | protected $core; |
---|
| 78 | protected $twig; |
---|
| 79 | protected $blocks; |
---|
| 80 | protected $forms; |
---|
| 81 | protected $currentForm; |
---|
[1058] | 82 | |
---|
| 83 | public function __construct($core) |
---|
| 84 | { |
---|
[992] | 85 | $this->core = $core; |
---|
| 86 | $this->tpl = 'form_layout.html.twig'; |
---|
| 87 | $this->forms = array(); |
---|
| 88 | $this->currentForm = null; |
---|
| 89 | } |
---|
[1058] | 90 | |
---|
| 91 | public function initRuntime(Twig_Environment $environment) |
---|
| 92 | { |
---|
| 93 | $this->twig = $environment; |
---|
[992] | 94 | $this->template = $this->twig->loadTemplate($this->tpl); |
---|
[1058] | 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 | { |
---|
[992] | 105 | return array( |
---|
[1058] | 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 | ) |
---|
[992] | 121 | ); |
---|
| 122 | } |
---|
[1058] | 123 | |
---|
| 124 | public function isChoiceGroup($choice) |
---|
| 125 | { |
---|
[992] | 126 | return is_array($choice); |
---|
| 127 | } |
---|
[1058] | 128 | |
---|
| 129 | public function isChoiceSelected($choice,$value) |
---|
| 130 | { |
---|
[992] | 131 | return $choice == $value; |
---|
| 132 | } |
---|
[1058] | 133 | |
---|
| 134 | public function getTokenParsers() |
---|
| 135 | { |
---|
[992] | 136 | return array(new dcFormTokenParser()); |
---|
| 137 | } |
---|
[1058] | 138 | |
---|
| 139 | public function renderWidget($name,$attributes=array()) |
---|
| 140 | { |
---|
[992] | 141 | $field = $this->currentForm->$name; |
---|
[1053] | 142 | if ($field) { |
---|
| 143 | echo $this->template->renderBlock( |
---|
| 144 | $field->getWidgetBlock(), |
---|
[1058] | 145 | array_merge( |
---|
| 146 | $field->getAttributes(), |
---|
| 147 | array('attr' => $attributes) |
---|
| 148 | ), |
---|
| 149 | $this->blocks |
---|
| 150 | ); |
---|
[1053] | 151 | } |
---|
| 152 | } |
---|
[992] | 153 | |
---|
[1058] | 154 | public function renderHiddenWidgets() |
---|
| 155 | { |
---|
[1053] | 156 | foreach ($this->currentForm->getHiddenFields() as $h) { |
---|
| 157 | $this->renderWidget($h->getName()); |
---|
| 158 | } |
---|
[992] | 159 | } |
---|
| 160 | |
---|
[1058] | 161 | public function getName() |
---|
| 162 | { |
---|
[992] | 163 | return 'dc_form'; |
---|
| 164 | } |
---|
| 165 | |
---|
[1058] | 166 | public function addForm(dcForm $form) |
---|
| 167 | { |
---|
| 168 | $this->forms[$form->getName()] = $form; |
---|
[992] | 169 | } |
---|
| 170 | |
---|
[1058] | 171 | public function beginForm($name) |
---|
| 172 | { |
---|
[992] | 173 | if (isset($this->forms[$name])) { |
---|
| 174 | $this->currentForm = $this->forms[$name]; |
---|
| 175 | $this->currentForm->begin(); |
---|
[1058] | 176 | } |
---|
| 177 | else { |
---|
| 178 | throw new Twig_Error_Runtime(sprintf( |
---|
| 179 | 'Form "%s" does not exist', |
---|
| 180 | $name |
---|
| 181 | )); |
---|
[992] | 182 | } |
---|
| 183 | } |
---|
[1058] | 184 | |
---|
| 185 | public function endForm() |
---|
| 186 | { |
---|
[992] | 187 | $this->currentForm->end(); |
---|
| 188 | $this->currentForm = null; |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
[1058] | 192 | /** |
---|
| 193 | * Template form exception |
---|
| 194 | */ |
---|
[1053] | 195 | class InvalidFieldException extends Exception { |
---|
| 196 | } |
---|
| 197 | |
---|
[1058] | 198 | /** |
---|
| 199 | * Template form |
---|
| 200 | */ |
---|
| 201 | class dcForm |
---|
| 202 | { |
---|
[992] | 203 | protected $name; |
---|
| 204 | protected $core; |
---|
| 205 | protected $action; |
---|
| 206 | protected $fields; |
---|
[1053] | 207 | protected $method; |
---|
| 208 | protected $submitfields; |
---|
| 209 | protected $hiddenfields; |
---|
| 210 | protected $errors; |
---|
| 211 | |
---|
[1058] | 212 | private function addNonce() |
---|
| 213 | { |
---|
| 214 | $this->addField( |
---|
| 215 | new dcFieldHidden(array('xd_check'), |
---|
| 216 | $this->core->getNonce()) |
---|
| 217 | ); |
---|
[1053] | 218 | } |
---|
| 219 | |
---|
[1058] | 220 | public function __construct($core,$name,$action,$method='POST') |
---|
| 221 | { |
---|
[992] | 222 | $this->core = $core; |
---|
| 223 | $this->name = $name; |
---|
| 224 | $this->method = $method; |
---|
| 225 | $this->action = $action; |
---|
| 226 | $this->fields = array(); |
---|
[1056] | 227 | $this->core->tpl->getExtension('dc_form')->addForm($this); |
---|
[1053] | 228 | $this->submitfields = array(); |
---|
| 229 | $this->hiddenfields = array(); |
---|
| 230 | $this->errors = array(); |
---|
| 231 | if ($method == 'POST') { |
---|
| 232 | $this->addNonce(); |
---|
| 233 | } |
---|
[992] | 234 | } |
---|
[1058] | 235 | |
---|
| 236 | public function getName() |
---|
| 237 | { |
---|
[992] | 238 | return $this->name; |
---|
| 239 | } |
---|
[1058] | 240 | |
---|
| 241 | public function getErrors() |
---|
| 242 | { |
---|
[1053] | 243 | return $this->errors; |
---|
| 244 | } |
---|
| 245 | |
---|
[1058] | 246 | public function addField(dcField $f) |
---|
| 247 | { |
---|
[1053] | 248 | if ($f instanceof dcFieldAction) { |
---|
[1058] | 249 | $this->submitfields[$f->getName()] = $f; |
---|
[1053] | 250 | } |
---|
| 251 | if ($f instanceof dcFieldHidden) { |
---|
[1058] | 252 | $this->hiddenfields[$f->getName()] = $f; |
---|
[1053] | 253 | } |
---|
[1058] | 254 | $this->fields[$f->getName()] = $f; |
---|
| 255 | |
---|
[992] | 256 | return $this; |
---|
| 257 | } |
---|
[1058] | 258 | |
---|
| 259 | public function begin() |
---|
| 260 | { |
---|
| 261 | echo sprintf( |
---|
| 262 | '<form method="%s" action="%s">', |
---|
| 263 | $this->method, |
---|
| 264 | $this->action |
---|
| 265 | ); |
---|
[992] | 266 | } |
---|
[1058] | 267 | |
---|
| 268 | public function end() |
---|
| 269 | { |
---|
[992] | 270 | echo '</form>'; |
---|
| 271 | } |
---|
[1058] | 272 | |
---|
| 273 | public function __isset($name) |
---|
| 274 | { |
---|
[992] | 275 | return isset($this->fields[$name]); |
---|
[1058] | 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 | { |
---|
[992] | 286 | if (isset($this->fields[$name])) { |
---|
| 287 | $this->fields[$name]->setAttribute('value',$value); |
---|
| 288 | } |
---|
[1058] | 289 | } |
---|
| 290 | |
---|
| 291 | public function isSubmitted() |
---|
| 292 | { |
---|
| 293 | $from = $this->method == 'POST' ? $_POST : $_GET; |
---|
[1053] | 294 | echo "form fields :\n"; |
---|
| 295 | } |
---|
| 296 | |
---|
[1058] | 297 | public function setup() |
---|
| 298 | { |
---|
| 299 | $from = $this->method == 'POST' ? $_POST : $_GET; |
---|
[1053] | 300 | foreach ($this->fields as $f) { |
---|
| 301 | $f->setup($from); |
---|
| 302 | } |
---|
| 303 | foreach ($this->submitfields as $f) { |
---|
| 304 | if ($f->isDefined()) { |
---|
[1058] | 305 | $ret = call_user_func($f->getAction(),$this); |
---|
[1053] | 306 | return; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
[1058] | 311 | public function check() |
---|
| 312 | { |
---|
[1053] | 313 | foreach ($this->fields as $f) { |
---|
| 314 | try { |
---|
| 315 | $f->check(); |
---|
[1058] | 316 | } |
---|
| 317 | catch (InvalidFieldException $e) { |
---|
| 318 | $this->errors[] = $e->getMessage(); |
---|
[1053] | 319 | } |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
[1058] | 323 | public function getHiddenFields() |
---|
| 324 | { |
---|
[1053] | 325 | return $this->hiddenfields; |
---|
| 326 | } |
---|
[992] | 327 | } |
---|
| 328 | |
---|
[1058] | 329 | /** |
---|
| 330 | * Template form field |
---|
| 331 | */ |
---|
| 332 | abstract class dcField |
---|
| 333 | { |
---|
[992] | 334 | protected $attributes; |
---|
| 335 | protected $name; |
---|
| 336 | protected $value; |
---|
| 337 | protected $id; |
---|
[1053] | 338 | protected $defined; |
---|
[992] | 339 | |
---|
[1058] | 340 | protected function getNID($nid) |
---|
| 341 | { |
---|
[992] | 342 | if (is_array($nid)) { |
---|
| 343 | $this->name = $nid[0]; |
---|
| 344 | $this->id = !empty($nid[1]) ? $nid[1] : null; |
---|
[1058] | 345 | } |
---|
| 346 | else { |
---|
[992] | 347 | $this->id = $this->name = $nid; |
---|
| 348 | } |
---|
| 349 | } |
---|
[1058] | 350 | |
---|
| 351 | public function __construct($name,$value,$attributes=array()) |
---|
| 352 | { |
---|
[992] | 353 | $this->getNID($name); |
---|
| 354 | $this->attributes = $attributes; |
---|
| 355 | $this->value = $value; |
---|
| 356 | $this->attributes['name'] = $this->name; |
---|
| 357 | $this->attributes['id'] = $this->id; |
---|
| 358 | $this->attributes['value'] = $this->value; |
---|
[1053] | 359 | $this->defined = false; |
---|
[992] | 360 | } |
---|
[1058] | 361 | |
---|
| 362 | public function __toString() |
---|
| 363 | { |
---|
| 364 | return $this->value; |
---|
| 365 | } |
---|
| 366 | |
---|
[992] | 367 | abstract public function getWidgetBlock(); |
---|
[1058] | 368 | |
---|
| 369 | public function setAttribute($name,$value) |
---|
| 370 | { |
---|
[992] | 371 | $this->attributes[$name] = $value; |
---|
| 372 | } |
---|
[1058] | 373 | |
---|
| 374 | public function getAttributes() |
---|
| 375 | { |
---|
[992] | 376 | return $this->attributes; |
---|
| 377 | } |
---|
[1058] | 378 | |
---|
| 379 | public function getName() |
---|
| 380 | { |
---|
[992] | 381 | return $this->name; |
---|
| 382 | } |
---|
[1058] | 383 | |
---|
| 384 | public function check() |
---|
| 385 | { |
---|
[1053] | 386 | if (!$this->defined && $this->attributes['defined']) { |
---|
[1058] | 387 | throw new InvalidFieldException(sprintf( |
---|
| 388 | 'Field "%s" is mandatory', |
---|
| 389 | $this->attributes['label']) |
---|
| 390 | ); |
---|
[1053] | 391 | } |
---|
[992] | 392 | } |
---|
[1053] | 393 | |
---|
[1058] | 394 | public function setup($from) |
---|
| 395 | { |
---|
[1053] | 396 | if (isset($from[$this->id])) { |
---|
| 397 | $this->value = $from[$this->id]; |
---|
| 398 | $this->defined = true; |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | |
---|
[1058] | 402 | public function isDefined() |
---|
| 403 | { |
---|
[1053] | 404 | return $this->defined; |
---|
| 405 | } |
---|
[992] | 406 | } |
---|
| 407 | |
---|
[1058] | 408 | /** |
---|
| 409 | * Template form field of type "password" |
---|
| 410 | */ |
---|
| 411 | class dcFieldPassword extends dcField |
---|
| 412 | { |
---|
| 413 | public function getWidgetBlock() |
---|
| 414 | { |
---|
| 415 | return "field_password"; |
---|
[992] | 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
[1058] | 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 | } |
---|
[992] | 429 | |
---|
[1058] | 430 | /** |
---|
| 431 | * Template form field of type "textarea" |
---|
| 432 | */ |
---|
| 433 | class dcFieldTextArea extends dcField |
---|
| 434 | { |
---|
| 435 | public function getWidgetBlock() |
---|
| 436 | { |
---|
[992] | 437 | return "field_textarea"; |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | |
---|
[1058] | 441 | /** |
---|
| 442 | * Template form field of type "hidden" |
---|
| 443 | */ |
---|
| 444 | class dcFieldHidden extends dcField |
---|
| 445 | { |
---|
| 446 | public function getWidgetBlock() |
---|
| 447 | { |
---|
[992] | 448 | return "field_hidden"; |
---|
| 449 | } |
---|
| 450 | } |
---|
| 451 | |
---|
[1058] | 452 | /** |
---|
| 453 | * Template form field of type "checkbox" |
---|
| 454 | */ |
---|
| 455 | class dcFieldCheckbox extends dcField |
---|
| 456 | { |
---|
| 457 | public function getWidgetBlock() |
---|
| 458 | { |
---|
[992] | 459 | return "field_checkbox"; |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | |
---|
[1058] | 463 | /** |
---|
| 464 | * Template form action |
---|
| 465 | */ |
---|
| 466 | abstract class dcFieldAction extends dcField |
---|
| 467 | { |
---|
[1053] | 468 | protected $action; |
---|
[1058] | 469 | |
---|
| 470 | public function __construct($name,$value,$attributes=array()) |
---|
| 471 | { |
---|
| 472 | parent::__construct($name,$value,$attributes); |
---|
| 473 | |
---|
[1053] | 474 | if (isset($attributes['action'])) { |
---|
| 475 | $this->action = $attributes['action']; |
---|
| 476 | } |
---|
| 477 | } |
---|
[1058] | 478 | |
---|
| 479 | public function getAction() |
---|
| 480 | { |
---|
[1053] | 481 | return $this->action; |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | |
---|
[1058] | 485 | /** |
---|
| 486 | * Template form field of type "submit" |
---|
| 487 | */ |
---|
| 488 | class dcFieldSubmit extends dcFieldAction |
---|
| 489 | { |
---|
| 490 | public function getWidgetBlock() |
---|
| 491 | { |
---|
[992] | 492 | return "field_submit"; |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | |
---|
[1058] | 496 | /** |
---|
| 497 | * Template form field of type "combo" |
---|
| 498 | */ |
---|
| 499 | class dcFieldCombo extends dcField |
---|
| 500 | { |
---|
[992] | 501 | protected $options; |
---|
[1058] | 502 | |
---|
| 503 | public function __construct($name,$value,$options,$attributes=array()) |
---|
| 504 | { |
---|
[992] | 505 | $this->options = $options; |
---|
| 506 | parent::__construct($name,$value,$attributes); |
---|
| 507 | $this->attributes['options']=$options; |
---|
| 508 | } |
---|
[1058] | 509 | |
---|
| 510 | public function getWidgetBlock() |
---|
| 511 | { |
---|
[992] | 512 | return "field_combo"; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | } |
---|
[1014] | 516 | ?> |
---|