Changeset 1156:92f840a91f98 for inc/admin/class.dc.form.php
- Timestamp:
- 04/26/13 16:23:16 (12 years ago)
- Branch:
- twig
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/class.dc.form.php
r1154 r1156 13 13 14 14 /** 15 * dcFormNode 15 * dcFormNode Twig Node for Form handling 16 16 * 17 17 * @uses Twig_Node … … 57 57 class dcFormTokenParser extends Twig_TokenParser 58 58 { 59 /** 60 * parse - parses form tag 61 * General syntax is : 62 * {% form 'formname' %} 63 * ... {{ form_field (...)}} 64 * {% endform %} 65 * Specific attributes can be passed to the form, enabling to set 66 * attributes to the form template : 67 * {% form 'formname' with {'id':'myform'} %} 68 * @param mixed \Twig_Token Description. 69 * 70 * @access public 71 * 72 * @return mixed Value. 73 */ 59 74 public function parse(Twig_Token $token) 60 75 { … … 63 78 $name = $this->parser->getExpressionParser()->parseExpression(); 64 79 $attr = null; 80 /* parse optional context */ 65 81 if ($stream->test(Twig_Token::NAME_TYPE, 'with')) { 66 82 $stream->next(); … … 70 86 $body = $this->parser->subparse(array($this,'decideBlockEnd'),true); 71 87 $stream->expect(Twig_Token::BLOCK_END_TYPE); 72 88 73 89 return new dcFormNode($name,$body,$attr,$token->getLine(),$this->getTag()); 74 90 } 75 91 76 92 public function decideBlockEnd(Twig_Token $token) 77 93 { 78 94 return $token->test('endform'); 79 95 } 80 96 81 97 public function getTag() 82 98 { … … 97 113 protected $currentForm; 98 114 protected $blocks; 99 115 100 116 public function __construct($core) 101 117 { … … 106 122 $this->currentForm = null; 107 123 } 108 124 109 125 public function initRuntime(Twig_Environment $environment) 110 126 { … … 116 132 } 117 133 } 118 134 119 135 public function addTemplate($tpl) { 120 136 $this->tpl[]=$tpl; … … 129 145 return array('dc_form' => $this); 130 146 } 131 147 132 148 public function getFunctions() 133 149 { … … 160 176 ); 161 177 } 162 178 179 /** 180 * isChoiceGroup - binding for twig function "_form_is_choice_group" 181 * returns whether a choice is a group or not 182 * @param mixed $choice the choice. 183 * 184 * @access public 185 * 186 * @return boolean true is choice is a group (optgroup). 187 */ 163 188 public function isChoiceGroup($choice) 164 189 { 165 190 return is_array($choice); 166 191 } 167 192 193 /** 194 * isChoiceSelected - binding for twig function "_form_is_choice_selected" 195 * returns whether current choice matches a value or not 196 * @param mixed $choice the choixe. 197 * @param mixed $value the value to check matching. 198 * 199 * @access public 200 * 201 * @return boolean if choice is matching the value. 202 */ 168 203 public function isChoiceSelected($choice,$value) 169 204 { 170 205 return $choice == $value; 171 206 } 172 207 208 /** 209 * getTokenParsers returns token parsers 210 * 211 * @access public 212 * 213 * @return mixed Value. 214 */ 173 215 public function getTokenParsers() 174 216 { 175 217 return array(new dcFormTokenParser()); 176 218 } 177 219 220 /** 221 * hasWidget - binding for twig "haswidget" function 222 * returns whether a widget is defined or not 223 * 224 * @param mixed $name the widget name. 225 * 226 * @access public 227 * 228 * @return boolean true if the widget exists. 229 */ 178 230 public function hasWidget($name) { 179 231 return isset($this->blocks[$name]); 180 232 } 233 234 /** 235 * renderWidget - binding for 'widget' twig function 236 * behaves exactly like "block" function, except that a context 237 * can be passed to the function 238 * 239 * @param mixed $name the widget (block) name to render. 240 * @param mixed $attr Description the context for this block. 241 * 242 * 243 * @return mixed Value. 244 */ 181 245 public function renderWidget($name,$attr) { 182 246 if (!isset($this->blocks[$name])) … … 189 253 } 190 254 255 /** 256 * getCurrentForm - returns current form if called within a {% form %} tag 257 * 258 * @access public 259 * 260 * @return string the current form. 261 */ 191 262 public function getCurrentForm() { 192 263 return $this->currentForm; 193 264 } 194 265 266 /** 267 * renderField - binding for 'form_field' twig function; renders a field 268 * 269 * @param mixed $name field name as defined on php side. 270 * @param array $attributes html attributes for field (ex : class, ...). 271 * @param array $extra extra attributes that may be template specific. 272 * 273 * @access public 274 * 275 * @return mixed Value. 276 */ 195 277 public function renderField($name,$attributes=array(),$extra=array()) 196 278 { … … 213 295 } 214 296 297 /** 298 * renderHiddenWidgets -- renders all form hidden fields 299 * 300 * @access public 301 * 302 * @return mixed Value. 303 */ 215 304 public function renderHiddenWidgets() 216 305 { … … 225 314 } 226 315 316 /** 317 * addForm -- registers a new form 318 * 319 * @param mixed \dcForm Description. 320 * 321 * @access public 322 * 323 * @return mixed Value. 324 */ 227 325 public function addForm(dcForm $form) 228 326 { … … 230 328 } 231 329 330 /** 331 * beginForm -- displays form beginning 332 * 333 * @param mixed $name form name. 334 * @param array $attr extra attributes. 335 * 336 * @access public 337 * 338 * @return mixed Value. 339 */ 232 340 public function beginForm($name,$attr=array()) 233 341 { … … 243 351 } 244 352 } 245 353 354 /** 355 * endForm -- displays form ending 356 * 357 * @access public 358 * 359 * @return mixed Value. 360 */ 246 361 public function endForm() 247 362 { … … 263 378 class dcForm 264 379 { 380 /** @var string form id */ 265 381 protected $id; 382 /** @var string form name */ 266 383 protected $name; 384 /** @var dcCore dcCore instance */ 267 385 protected $core; 386 /** @var string form action */ 268 387 protected $action; 388 /** @var array(dcField) list of form fields */ 269 389 protected $fields; 390 /** @var string form method (GET/POST) */ 270 391 protected $method; 392 /** @var array(dcField) list of submit fields */ 271 393 protected $submitfields; 394 /** @var array(dcField) list of hidden fields */ 272 395 protected $hiddenfields; 396 /** @var array(dcField) list of form errors */ 273 397 protected $errors; 274 275 public function addTemplate($t) {276 $this->core->tpl->getExtension('dc_form')->addTemplate($t);277 }278 279 /**280 * addNonce -- adds dc nonce to form fields281 *282 * @access protected283 *284 * @return nothing285 */286 protected function addNonce()287 {288 $this->addField(289 new dcFieldHidden(array('xd_check'),290 $this->core->getNonce())291 );292 }293 294 295 /**296 * Defines Name & ID from field297 *298 * @param mixed $nid either an array (name, id) or a string (name only, id will be set to null).299 *300 * @access protected301 *302 * @return nothing.303 */304 protected function setNID($nid)305 {306 if (is_array($nid)) {307 $this->name = $nid[0];308 $this->id = !empty($nid[1]) ? $nid[1] : null;309 }310 else {311 $this->id = null;312 $this->name = $nid;313 }314 }315 316 public function getContext() {317 return array();318 }319 398 320 399 /** … … 322 401 * 323 402 * @param mixed $core dotclear core 324 * @param mixed $name form name 403 * @param mixed $name form name - can be an array (name,id) 325 404 * @param mixed $action form action 326 405 * @param string $method form method ('GET' or 'POST') … … 345 424 } 346 425 } 347 426 427 /** 428 * addTemplate - Adds a template file to enrich form fields 429 * 430 * @param string $t the template file. 431 * 432 * @access public 433 */ 434 public function addTemplate($t) { 435 $this->core->tpl->getExtension('dc_form')->addTemplate($t); 436 } 437 438 /** 439 * addNonce -- adds dc nonce to form fields 440 * 441 * @access protected 442 * 443 * @return nothing 444 */ 445 protected function addNonce() 446 { 447 $this->addField( 448 new dcFieldHidden(array('xd_check'), 449 $this->core->getNonce()) 450 ); 451 } 452 453 /** 454 * Defines Name & ID from field 455 * 456 * @param mixed $nid either an array (name, id) or a string 457 * (name only, id will be set to null). 458 * 459 * @access protected 460 * 461 * @return nothing. 462 */ 463 protected function setNID($nid) 464 { 465 if (is_array($nid)) { 466 $this->name = $nid[0]; 467 $this->id = !empty($nid[1]) ? $nid[1] : null; 468 } 469 else { 470 $this->id = null; 471 $this->name = $nid; 472 } 473 } 474 475 /** 476 * getContext - returns form context (to fill-in twig context for instance), 477 * if any 478 * 479 * @access public 480 * 481 * @return array the form context. 482 */ 483 public function getContext() { 484 return array(); 485 } 486 348 487 349 488 /** 350 489 * Returns form name 351 * 490 * 352 491 * @access public 353 492 * … … 358 497 return $this->name; 359 498 } 360 499 500 /** 501 * getErrors - returns form errors 502 * 503 * @access public 504 * 505 * @return array the list of errors. 506 */ 361 507 public function getErrors() 362 508 { 363 509 return $this->errors; 364 510 } 365 511 512 /** 513 * addField - adds a new field to form 514 * 515 * @param mixed \dcField the field to add. 516 * 517 * @access public 518 * 519 * @return dcForm the form instance (therefore addField can be chained) 520 */ 366 521 public function addField(dcField $f) 367 522 { … … 373 528 } 374 529 $this->fields[$f->getName()] = $f; 375 530 376 531 return $this; 377 532 } 378 533 534 /** 535 * removeField - removes a field 536 * 537 * @param mixed \dcField the field to remove. 538 * 539 * @access public 540 * 541 * @return dcForm the form instance (therefore addField can be chained) 542 */ 379 543 public function removeField(dcField $f) { 380 544 $n = $f->getName(); … … 382 546 unset($this->fields[$n]); 383 547 } 384 385 } 548 return $this; 549 } 550 551 552 /** 553 * renameField - renames a field 554 * 555 * @param mixed $field the field to rename. 556 * @param mixed $newname new field name 557 * 558 * @access public 559 * 560 * 561 * @return dcForm the form instance (therefore addField can be chained) 562 */ 386 563 public function renameField($field,$newname) { 387 564 $oldname = $field->getName(); … … 391 568 $this->fields[$newname] = $field; 392 569 } 393 } 570 return $this; 571 } 572 573 /** 574 * begin - begins a form. Should be not be called directly, it is handled 575 * by the Twig Form extension. 576 * 577 * @param array $attr form extra attributes, if any. 578 * 579 * @access public 580 * 581 * @return mixed Value. 582 */ 394 583 public function begin($attr=array()) 395 584 { … … 403 592 $attr); 404 593 } 405 594 595 /** 596 * end - ends a form. Should be not be called directly, it is handled 597 * by the Twig Form extension. 598 * 599 * @access public 600 * 601 * @return mixed Value. 602 */ 406 603 public function end() 407 604 { 408 echo '</form>'; 409 } 410 605 $this->core->tpl->getExtension('dc_form')->renderWidget( 606 'endform'); 607 } 608 609 /** 610 * __isset - magic method isset, checks whether a field exists 611 * example : if (isset($form->field1)) 612 * 613 * @param mixed $name field name to check. 614 * 615 * @access public 616 * 617 * @return boolean true if the field exists. 618 */ 411 619 public function __isset($name) 412 620 { 413 621 return isset($this->fields[$name]); 414 622 } 415 623 624 /** 625 * __get -- magic method, retrieves a field from a form 626 * example : $f = $form->field1 627 * 628 * @param mixed $name Description. 629 * 630 * @access public 631 * 632 * @return mixed Value. 633 */ 416 634 public function __get($name) 417 635 { … … 420 638 } 421 639 640 /** 641 * __set -- magic method, sets a value for a given form field 642 * example : $form->field1 = 'my value'; 643 * 644 * @param mixed $name the field name. 645 * @param mixed $value the field value. 646 * 647 * @access public 648 */ 422 649 public function __set($name,$value) 423 650 { … … 427 654 } 428 655 429 public function isSubmitted()656 /* public function isSubmitted() 430 657 { 431 658 $from = $this->method == 'POST' ? $_POST : $_GET; 432 659 } 433 660 */ 661 662 /** 663 * setupFields - initializes form & fields from $_GET or $_POST 664 * 665 * @access protected 666 */ 434 667 protected function setupFields() { 435 668 $from = $this->method == 'POST' ? $_POST : $_GET; … … 439 672 } 440 673 674 /** 675 * handleActions - handle appropriate actions, according to submitted fields 676 * 677 * @param mixed $submitted the fields that have been submitted. 678 * 679 * @access protected 680 */ 441 681 protected function handleActions($submitted) { 442 682 foreach ($submitted as $f) { … … 448 688 } 449 689 690 /** 691 * getSubmittedFields - retrieves fields that have been submitted, if any 692 * 693 * @access protected 694 * 695 * @return array the list of submitted fields. 696 */ 450 697 protected function getSubmittedFields() { 451 698 $s = array(); … … 458 705 } 459 706 707 /** 708 * setup - sets up the form, given the parameters given to the page 709 * should be called after fields have been defined. 710 * 711 * @access public 712 * 713 * @return mixed Value. 714 */ 460 715 public function setup() 461 716 { … … 465 720 } 466 721 722 /** 723 * check - checks if the form is valid, errors are filled in, in case of 724 * incorrect fields 725 * 726 * @access public 727 */ 467 728 public function check() 468 729 { … … 476 737 } 477 738 } 478 739 740 /** 741 * getHiddenFields - returns the list of hidden fields 742 * 743 * @access public 744 * 745 * @return array the list of hidden fields. 746 */ 479 747 public function getHiddenFields() 480 748 { … … 488 756 abstract class dcField implements Countable 489 757 { 758 /** @var string field options */ 490 759 protected $options; 760 /** @var string field name */ 491 761 protected $name; 762 /** @var string field values */ 492 763 protected $values; 764 /** @var string field id */ 493 765 protected $id; 766 /** @var boolean true if field can contain multiple values */ 494 767 protected $multiple; 768 /** @var boolean true if the field has been defined */ 495 769 protected $defined; 496 497 protected function setNID($nid) 498 { 499 if (is_array($nid)) { 500 $this->name = $nid[0]; 501 $this->id = !empty($nid[1]) ? $nid[1] : null; 502 } 503 else { 504 $this->id = $this->name = $nid; 505 } 506 } 507 770 771 /** 772 * __construct - constructor 773 * 774 * @param string $name Name or array(name,id) for field. 775 * @param array $values field values. 776 * @param array $options options 777 * 778 * Currently globally available options are : 779 * * multiple : true/false. Enable multiple values for field 780 * @access public 781 * 782 * @return mixed Value. 783 */ 508 784 public function __construct($name,$values,$options=array()) 509 785 { … … 518 794 519 795 } 520 796 797 /** 798 * setNID - defines fiels name & id 799 * 800 * @param mixed $nid field name (string) or an array containing name (1st) 801 * and id (2nd field). 802 * 803 * @access protected 804 */ 805 protected function setNID($nid) 806 { 807 if (is_array($nid)) { 808 $this->name = $nid[0]; 809 $this->id = !empty($nid[1]) ? $nid[1] : null; 810 } 811 else { 812 $this->id = $this->name = $nid; 813 } 814 } 815 816 /** 817 * setValue - sets field value 818 * 819 * @param mixed $value field value. 820 * @param int $offset value offset to define (default 0). 821 * 822 * @access public 823 */ 521 824 public function setValue($value,$offset=0) { 522 825 $this->values[$offset] = $value; 523 826 } 524 827 828 /** 829 * setValues - set field values 830 * 831 * @param mixed $values the array of values. If not an array, the parameter 832 * will be converted to an array containing the value 833 * 834 * @access public 835 */ 525 836 public function setValues($values) { 526 837 if (is_array($values)) { … … 532 843 } 533 844 845 /** 846 * getValues - return field values 847 * 848 * @access public 849 * 850 * @return mixed the array of values. 851 */ 534 852 public function getValues() { 535 853 return $this->values; 536 854 } 537 855 856 /** 857 * getValue - retrieves a field value 858 * 859 * @param int $offset value offset (by default 1st value is returned). 860 * 861 * @access public 862 * 863 * @return mixed the field value. 864 */ 538 865 public function getValue($offset=0) { 539 866 if (isset($this->values[$offset])) { … … 542 869 } 543 870 871 /** 872 * addValue -- Adds a value to the field values. 873 * 874 * @param mixed $value Description. 875 * 876 * @access public 877 * 878 * @return mixed Value. 879 */ 544 880 public function addValue($value) { 545 881 $this->values[] = $value; … … 551 887 } 552 888 889 /** 890 * count -- returns the number of field values 891 * 892 * @access public 893 * 894 * @return integer the number of values. 895 */ 553 896 public function count() { 554 897 return count($this->values); … … 562 905 abstract public function getWidgetBlock(); 563 906 907 908 /** 909 * getAttributes - retrieve field attributes that will be given to the 910 * twig widget 911 * 912 * @param array $options extra options given to the widget 913 * 914 * @access public 915 * 916 * @return array the attributes. 917 */ 564 918 public function getAttributes($options) 565 919 { … … 581 935 return $attr; 582 936 } 583 937 938 /** 939 * getDefaultValue - returns field default value 940 * 941 * @access public 942 * 943 * @return mixed the field default value. 944 */ 584 945 public function getDefaultValue() { 585 946 return ''; 586 947 } 587 948 949 /** 950 * getName - returns field name 951 * 952 * @access public 953 * 954 * @return string the field name. 955 */ 588 956 public function getName() 589 957 { … … 591 959 } 592 960 961 /** 962 * setName - defines field name and/or field id 963 * 964 * @param mixed $name the field name or an array containing name and id. 965 * 966 * @access public 967 */ 593 968 public function setName($name) { 594 969 $this->setNID($name); 595 970 } 596 971 972 /** 973 * check - checks whether the field is valid or not - raises an exception 974 * if not valid 975 * @access public 976 */ 597 977 public function check() 598 978 { … … 604 984 } 605 985 } 606 986 987 /** 988 * parseValues - parses field value from context (GET or POST) 989 * and returns parsed value(s) 990 * NOTE : the field is not updated with this method 991 * use setup() to also update the field. 992 * @param mixed $from the context (usually $_GET or $_POST). 993 * 994 * @access public 995 * 996 * @return array the list of values (empty array if no value). 997 */ 607 998 public function parseValues($from) { 608 999 if (isset($from[$this->name])) { … … 616 1007 } 617 1008 1009 /** 1010 * setup - sets up the field from conetxt (GET or $POST) 1011 * 1012 * @param mixed $from the context (usually $_GET or $_POST). 1013 * 1014 * @access public 1015 * 1016 * @return mixed Value. 1017 */ 618 1018 public function setup($from) 619 1019 { … … 624 1024 } 625 1025 } 626 1026 1027 /** 1028 * isDefined - returns whether the field is defined or not 1029 * (a field is defined if some values are set after setup()) 1030 * @access public 1031 * 1032 * @return mixed Value. 1033 */ 627 1034 public function isDefined() 628 1035 { … … 732 1139 { 733 1140 protected $combo; 734 1141 735 1142 public function __construct($name,$value,$combo,$options=array()) 736 1143 { … … 738 1145 parent::__construct($name,$value,$options); 739 1146 } 740 1147 741 1148 public function getWidgetBlock() 742 1149 {
Note: See TracChangeset
for help on using the changeset viewer.