~horux-dev/horux-webcli/thfo

« back to all changes in this revision

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