CFormElement.php
4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* CFormElement class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CFormElement is the base class for presenting all kinds of form element.
*
* CFormElement implements the way to get and set arbitrary attributes.
*
* @property boolean $visible Whether this element is visible and should be rendered.
* @property mixed $parent The direct parent of this element. This could be either a {@link CForm} object or a {@link CBaseController} object
* (a controller or a widget).
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web.form
* @since 1.1
*/
abstract class CFormElement extends CComponent
{
/**
* @var array list of attributes (name=>value) for the HTML element represented by this object.
*/
public $attributes=array();
private $_parent;
private $_visible;
/**
* Renders this element.
* @return string the rendering result
*/
abstract function render();
/**
* Constructor.
* @param mixed $config the configuration for this element.
* @param mixed $parent the direct parent of this element.
* @see configure
*/
public function __construct($config,$parent)
{
$this->configure($config);
$this->_parent=$parent;
}
/**
* Converts the object to a string.
* This is a PHP magic method.
* The default implementation simply calls {@link render} and return
* the rendering result.
* @return string the string representation of this object.
*/
public function __toString()
{
return $this->render();
}
/**
* Returns a property value or an attribute value.
* Do not call this method. This is a PHP magic method that we override
* to allow using the following syntax to read a property or attribute:
* <pre>
* $value=$element->propertyName;
* $value=$element->attributeName;
* </pre>
* @param string $name the property or attribute name
* @return mixed the property or attribute value
* @throws CException if the property or attribute is not defined
* @see __set
*/
public function __get($name)
{
$getter='get'.$name;
if(method_exists($this,$getter))
return $this->$getter();
else if(isset($this->attributes[$name]))
return $this->attributes[$name];
else
throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
array('{class}'=>get_class($this), '{property}'=>$name)));
}
/**
* Sets value of a property or attribute.
* Do not call this method. This is a PHP magic method that we override
* to allow using the following syntax to set a property or attribute.
* <pre>
* $this->propertyName=$value;
* $this->attributeName=$value;
* </pre>
* @param string $name the property or attribute name
* @param mixed $value the property or attribute value
* @see __get
*/
public function __set($name,$value)
{
$setter='set'.$name;
if(method_exists($this,$setter))
$this->$setter($value);
else
$this->attributes[$name]=$value;
}
/**
* Configures this object with property initial values.
* @param mixed $config the configuration for this object. This can be an array
* representing the property names and their initial values.
* It can also be a string representing the file name of the PHP script
* that returns a configuration array.
*/
public function configure($config)
{
if(is_string($config))
$config=require(Yii::getPathOfAlias($config).'.php');
if(is_array($config))
{
foreach($config as $name=>$value)
$this->$name=$value;
}
}
/**
* Returns a value indicating whether this element is visible and should be rendered.
* This method will call {@link evaluateVisible} to determine the visibility of this element.
* @return boolean whether this element is visible and should be rendered.
*/
public function getVisible()
{
if($this->_visible===null)
$this->_visible=$this->evaluateVisible();
return $this->_visible;
}
/**
* @param boolean $value whether this element is visible and should be rendered.
*/
public function setVisible($value)
{
$this->_visible=$value;
}
/**
* @return mixed the direct parent of this element. This could be either a {@link CForm} object or a {@link CBaseController} object
* (a controller or a widget).
*/
public function getParent()
{
return $this->_parent;
}
/**
* Evaluates the visibility of this element.
* Child classes should override this method to implement the actual algorithm
* for determining the visibility.
* @return boolean whether this element is visible. Defaults to true.
*/
protected function evaluateVisible()
{
return true;
}
}