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