CFormElementCollection.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * CFormElementCollection class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CFormElementCollection implements the collection for storing form elements.
  12. *
  13. * Because CFormElementCollection extends from {@link CMap}, it can be used like an associative array.
  14. * For example,
  15. * <pre>
  16. * $element=$collection['username'];
  17. * $collection['username']=array('type'=>'text', 'maxlength'=>128);
  18. * $collection['password']=new CFormInputElement(array('type'=>'password'),$form);
  19. * $collection[]='some string';
  20. * </pre>
  21. *
  22. * CFormElementCollection can store three types of value: a configuration array, a {@link CFormElement}
  23. * object, or a string, as shown in the above example. Internally, these values will be converted
  24. * to {@link CFormElement} objects.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @package system.web.form
  28. * @since 1.1
  29. */
  30. class CFormElementCollection extends CMap
  31. {
  32. private $_form;
  33. private $_forButtons;
  34. /**
  35. * Constructor.
  36. * @param CForm $form the form object that owns this collection
  37. * @param boolean $forButtons whether this collection is used to store buttons.
  38. */
  39. public function __construct($form,$forButtons=false)
  40. {
  41. parent::__construct();
  42. $this->_form=$form;
  43. $this->_forButtons=$forButtons;
  44. }
  45. /**
  46. * Adds an item to the collection.
  47. * This method overrides the parent implementation to ensure
  48. * only configuration arrays, strings, or {@link CFormElement} objects
  49. * can be stored in this collection.
  50. * @param mixed $key key
  51. * @param mixed $value value
  52. * @throws CException if the value is invalid.
  53. */
  54. public function add($key,$value)
  55. {
  56. if(is_array($value))
  57. {
  58. if(is_string($key))
  59. $value['name']=$key;
  60. if($this->_forButtons)
  61. {
  62. $class=$this->_form->buttonElementClass;
  63. $element=new $class($value,$this->_form);
  64. }
  65. else
  66. {
  67. if(!isset($value['type']))
  68. $value['type']='text';
  69. if($value['type']==='string')
  70. {
  71. unset($value['type'],$value['name']);
  72. $element=new CFormStringElement($value,$this->_form);
  73. }
  74. elseif(!strcasecmp(substr($value['type'],-4),'form')) // a form
  75. {
  76. $class=$value['type']==='form' ? get_class($this->_form) : Yii::import($value['type']);
  77. $element=new $class($value,null,$this->_form);
  78. }
  79. else
  80. {
  81. $class=$this->_form->inputElementClass;
  82. $element=new $class($value,$this->_form);
  83. }
  84. }
  85. }
  86. elseif($value instanceof CFormElement)
  87. {
  88. if(property_exists($value,'name') && is_string($key))
  89. $value->name=$key;
  90. $element=$value;
  91. }
  92. else
  93. $element=new CFormStringElement(array('content'=>$value),$this->_form);
  94. parent::add($key,$element);
  95. $this->_form->addedElement($key,$element,$this->_forButtons);
  96. }
  97. /**
  98. * Removes the specified element by key.
  99. * @param string $key the name of the element to be removed from the collection
  100. */
  101. public function remove($key)
  102. {
  103. if(($item=parent::remove($key))!==null)
  104. $this->_form->removedElement($key,$item,$this->_forButtons);
  105. }
  106. }