CFormElement.php 5.2 KB

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