~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/web/form/CFormElementCollection.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CFormElementCollection class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 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
 * @version $Id: CFormElementCollection.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
29
 * @package system.web.form
 
30
 * @since 1.1
 
31
 */
 
32
class CFormElementCollection extends CMap
 
33
{
 
34
        private $_form;
 
35
        private $_forButtons;
 
36
 
 
37
        /**
 
38
         * Constructor.
 
39
         * @param CForm $form the form object that owns this collection
 
40
         * @param boolean $forButtons whether this collection is used to store buttons.
 
41
         */
 
42
        public function __construct($form,$forButtons=false)
 
43
        {
 
44
                parent::__construct();
 
45
                $this->_form=$form;
 
46
                $this->_forButtons=$forButtons;
 
47
        }
 
48
 
 
49
        /**
 
50
         * Adds an item to the collection.
 
51
         * This method overrides the parent implementation to ensure
 
52
         * only configuration arrays, strings, or {@link CFormElement} objects
 
53
         * can be stored in this collection.
 
54
         * @param mixed $key key
 
55
         * @param mixed $value value
 
56
         * @throws CException if the value is invalid.
 
57
         */
 
58
        public function add($key,$value)
 
59
        {
 
60
                if(is_array($value))
 
61
                {
 
62
                        if(is_string($key))
 
63
                                $value['name']=$key;
 
64
 
 
65
                        if($this->_forButtons)
 
66
                        {
 
67
                                $class=$this->_form->buttonElementClass;
 
68
                                $element=new $class($value,$this->_form);
 
69
                        }
 
70
                        else
 
71
                        {
 
72
                                if(!isset($value['type']))
 
73
                                        $value['type']='text';
 
74
                                if($value['type']==='string')
 
75
                                {
 
76
                                        unset($value['type'],$value['name']);
 
77
                                        $element=new CFormStringElement($value,$this->_form);
 
78
                                }
 
79
                                else if(!strcasecmp(substr($value['type'],-4),'form'))  // a form
 
80
                                {
 
81
                                        $class=$value['type']==='form' ? get_class($this->_form) : Yii::import($value['type']);
 
82
                                        $element=new $class($value,null,$this->_form);
 
83
                                }
 
84
                                else
 
85
                                {
 
86
                                        $class=$this->_form->inputElementClass;
 
87
                                        $element=new $class($value,$this->_form);
 
88
                                }
 
89
                        }
 
90
                }
 
91
                else if($value instanceof CFormElement)
 
92
                {
 
93
                        if(property_exists($value,'name') && is_string($key))
 
94
                                $value->name=$key;
 
95
                        $element=$value;
 
96
                }
 
97
                else
 
98
                        $element=new CFormStringElement(array('content'=>$value),$this->_form);
 
99
                parent::add($key,$element);
 
100
                $this->_form->addedElement($key,$element,$this->_forButtons);
 
101
        }
 
102
 
 
103
        /**
 
104
         * Removes the specified element by key.
 
105
         * @param string $key the name of the element to be removed from the collection
 
106
         */
 
107
        public function remove($key)
 
108
        {
 
109
                if(($item=parent::remove($key))!==null)
 
110
                        $this->_form->removedElement($key,$item,$this->_forButtons);
 
111
        }
 
112
}