Changeset 1152:fbd922f6ed09 for inc/admin/class.dc.form.php
- Timestamp:
- 04/02/13 13:05:04 (12 years ago)
- Branch:
- twig
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/class.dc.form.php
r1147 r1152 90 90 { 91 91 $this->core = $core; 92 $this->tpl = 'form_layout.html.twig';92 $this->tpl = array('@forms/form_layout.html.twig'); 93 93 $this->forms = array(); 94 $this->blocks = array(); 94 95 $this->currentForm = null; 95 96 } … … 98 99 { 99 100 $this->twig = $environment; 100 $this->template = $this->twig->loadTemplate($this->tpl); 101 $this->blocks = $this->template->getBlocks(); 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 } 102 106 } 103 107 104 108 public function addTemplate($tpl) { 105 $t = $this->twig->loadTemplate($tpl); 106 $this->blocks = array_merge($this->blocks,$t->getBlocks()); 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 } 107 114 } 108 115 … … 115 122 { 116 123 return array( 117 'form_field' => new Twig_Function_Method(118 $this,119 'renderWidget',124 new Twig_SimpleFunction( 125 'widget', 126 array($this,'renderWidget'), 120 127 array('is_safe' => array('html')) 121 128 ), 122 '_form_is_choice_group' => new Twig_Function_Method(123 $this,124 'isChoiceGroup',129 new Twig_SimpleFunction( 130 'haswidget', 131 array($this,'hasWidget'), 125 132 array('is_safe' => array('html')) 126 133 ), 127 '_form_is_choice_selected' => new Twig_Function_Method( 128 $this, 129 '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'), 130 147 array('is_safe' => array('html')) 131 148 ) … … 148 165 } 149 166 150 public function renderBlock($name,$attr) { 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 ''; 151 173 echo $this->template->renderBlock( 152 174 $name, … … 160 182 } 161 183 162 public function render Widget($name,$attributes=array(),$extra=array())184 public function renderField($name,$attributes=array(),$extra=array()) 163 185 { 164 186 $field = $this->currentForm->$name; 165 187 if ($field) { 166 $attr = $field->getAttributes( );188 $attr = $field->getAttributes($attributes); 167 189 if (isset($attr['attr'])) { 168 190 $attr['attr'] = array_merge($attr['attr'],$attributes); … … 170 192 $attr['attr'] = $attributes; 171 193 } 172 $this->render Block(194 $this->renderWidget( 173 195 $field->getWidgetBlock(), 174 196 array_merge( … … 183 205 { 184 206 foreach ($this->currentForm->getHiddenFields() as $h) { 185 $this->render Widget($h->getName());207 $this->renderField($h->getName()); 186 208 } 187 209 } … … 240 262 protected $errors; 241 263 264 public function addTemplate($t) { 265 $this->core->tpl->getExtension('dc_form')->addTemplate($t); 266 } 242 267 243 268 /** … … 355 380 $this->fields[$newname] = $field; 356 381 } 357 //print_r($this->fields);358 382 } 359 383 public function begin() … … 386 410 { 387 411 if (isset($this->fields[$name])) { 388 $this->fields[$name]->set Attribute('value',$value);412 $this->fields[$name]->setValue($value); 389 413 } 390 414 } … … 449 473 * Template form field 450 474 */ 451 abstract class dcField 452 { 453 protected $ attributes;475 abstract class dcField implements Countable 476 { 477 protected $options; 454 478 protected $name; 455 protected $value ;479 protected $values; 456 480 protected $id; 481 protected $multiple; 457 482 protected $defined; 458 483 … … 468 493 } 469 494 470 public function __construct($name,$value ,$attributes=array())495 public function __construct($name,$values,$options=array()) 471 496 { 472 497 $this->setNID($name); 473 $this->attributes = $attributes; 474 $this->setValue($value); 475 $this->attributes['name'] = $this->name; 476 $this->attributes['id'] = $this->id; 498 $this->options = new ArrayObject($options); 499 if ($values === NULL){ 500 $values = array(); 501 } 502 $this->setValues($values); 477 503 $this->defined = false; 478 } 479 480 public function setValue($value) { 481 $this->value = $value; 482 $this->attributes['value'] = $this->value; 483 } 484 485 public function getValue() { 486 return $this->value; 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); 487 542 } 488 543 489 544 public function __toString() 490 545 { 491 return $this->value;546 return join(',',$this->values); 492 547 } 493 548 494 549 abstract public function getWidgetBlock(); 495 550 496 public function setAttribute($name,$value) 497 { 498 $this->attributes[$name] = $value; 499 } 500 501 public function getAttributes() 502 { 503 return $this->attributes; 504 } 505 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 506 575 public function getName() 507 576 { 508 577 return $this->name; 509 578 } 579 510 580 public function setName($name) { 511 581 $this->setNID($name); 512 $this->attributes['name'] = $this->name;513 $this->attributes['id'] = $this->id;514 582 } 515 583 516 584 public function check() 517 585 { 518 if (!$this->defined && $this-> attributes['defined']) {586 if (!$this->defined && $this->options['mandatory']) { 519 587 throw new InvalidFieldException(sprintf( 520 588 'Field "%s" is mandatory', … … 524 592 } 525 593 526 public function parseValue($from) { 527 if (isset($from[$this->id])) { 528 return $from[$this->id]; 529 } 530 return null; 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(); 531 603 } 532 604 533 605 public function setup($from) 534 606 { 535 $value = $this->parseValue($from);536 if ( $value !== null) {537 $this->setValue ($value);607 $values = $this->parseValues($from); 608 if (count($values)) { 609 $this->setValues($values); 538 610 $this->defined = true; 539 611 } … … 546 618 } 547 619 620 548 621 /** 549 622 * Template form field of type "password" … … 599 672 return "field_checkbox"; 600 673 } 674 675 public function getDefaultValue() { 676 return 0; 677 } 601 678 } 602 679 … … 607 684 { 608 685 protected $action; 609 610 public function __construct($name,$value ,$attributes=array())611 { 612 parent::__construct($name,$value ,$attributes);613 614 if (isset($ attributes['action'])) {615 $this->action = $ attributes['action'];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']; 616 693 } else { 617 694 $this->action = NULL; 618 695 } 619 696 } 620 697 621 698 public function getAction() 622 699 { … … 641 718 class dcFieldCombo extends dcField 642 719 { 643 protected $options; 644 645 public function __construct($name,$value,$options,$attributes=array()) 646 { 647 $this->options = $options; 648 parent::__construct($name,$value,$attributes); 649 $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); 650 726 } 651 727 … … 655 731 } 656 732 657 public function parseValue($from) { 658 659 $v = parent::parseValue($from); 660 if (!isset($this->options[$v])) { 661 return $this->value; 662 } else { 663 return $v; 664 } 665 } 666 } 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 667 756 ?>
Note: See TracChangeset
for help on using the changeset viewer.