PageRenderTime 203ms CodeModel.GetById 100ms app.highlight 4ms RepoModel.GetById 97ms app.codeStats 0ms

/demo/yii/web/form/CFormElement.php

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