PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/web/form/CFormElement.php

https://bitbucket.org/ddonthula/zurmoemail
PHP | 167 lines | 64 code | 12 blank | 91 comment | 5 complexity | 8f2a03fefbb2fa23da208444bbc17573 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause, GPL-2.0, GPL-3.0
  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. * 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. * Sets value of a property or attribute.
  84. * Do not call this method. This is a PHP magic method that we override
  85. * to allow using the following syntax to set a property or attribute.
  86. * <pre>
  87. * $this->propertyName=$value;
  88. * $this->attributeName=$value;
  89. * </pre>
  90. * @param string $name the property or attribute name
  91. * @param mixed $value the property or attribute value
  92. * @see __get
  93. */
  94. public function __set($name,$value)
  95. {
  96. $setter='set'.$name;
  97. if(method_exists($this,$setter))
  98. $this->$setter($value);
  99. else
  100. $this->attributes[$name]=$value;
  101. }
  102. /**
  103. * Configures this object with property initial values.
  104. * @param mixed $config the configuration for this object. This can be an array
  105. * representing the property names and their initial values.
  106. * It can also be a string representing the file name of the PHP script
  107. * that returns a configuration array.
  108. */
  109. public function configure($config)
  110. {
  111. if(is_string($config))
  112. $config=require(Yii::getPathOfAlias($config).'.php');
  113. if(is_array($config))
  114. {
  115. foreach($config as $name=>$value)
  116. $this->$name=$value;
  117. }
  118. }
  119. /**
  120. * Returns a value indicating whether this element is visible and should be rendered.
  121. * This method will call {@link evaluateVisible} to determine the visibility of this element.
  122. * @return boolean whether this element is visible and should be rendered.
  123. */
  124. public function getVisible()
  125. {
  126. if($this->_visible===null)
  127. $this->_visible=$this->evaluateVisible();
  128. return $this->_visible;
  129. }
  130. /**
  131. * @param boolean $value whether this element is visible and should be rendered.
  132. */
  133. public function setVisible($value)
  134. {
  135. $this->_visible=$value;
  136. }
  137. /**
  138. * @return mixed the direct parent of this element. This could be either a {@link CForm} object or a {@link CBaseController} object
  139. * (a controller or a widget).
  140. */
  141. public function getParent()
  142. {
  143. return $this->_parent;
  144. }
  145. /**
  146. * Evaluates the visibility of this element.
  147. * Child classes should override this method to implement the actual algorithm
  148. * for determining the visibility.
  149. * @return boolean whether this element is visible. Defaults to true.
  150. */
  151. protected function evaluateVisible()
  152. {
  153. return true;
  154. }
  155. }