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