PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/mstowe/zurmo
PHP | 168 lines | 64 code | 12 blank | 92 comment | 7 complexity | 922e8e40e1f8faf62d181818233ce364 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  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. * @version $Id$
  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. private $_parent;
  31. private $_visible;
  32. /**
  33. * Renders this element.
  34. * @return string the rendering result
  35. */
  36. abstract function render();
  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. * Converts the object to a string.
  50. * This is a PHP magic method.
  51. * The default implementation simply calls {@link render} and return
  52. * the rendering result.
  53. * @return string the string representation of this object.
  54. */
  55. public function __toString()
  56. {
  57. return $this->render();
  58. }
  59. /**
  60. * Returns a property value or an attribute value.
  61. * Do not call this method. This is a PHP magic method that we override
  62. * to allow using the following syntax to read a property or attribute:
  63. * <pre>
  64. * $value=$element->propertyName;
  65. * $value=$element->attributeName;
  66. * </pre>
  67. * @param string $name the property or attribute name
  68. * @return mixed the property or attribute value
  69. * @throws CException if the property or attribute is not defined
  70. * @see __set
  71. */
  72. public function __get($name)
  73. {
  74. $getter='get'.$name;
  75. if(method_exists($this,$getter))
  76. return $this->$getter();
  77. else if(isset($this->attributes[$name]))
  78. return $this->attributes[$name];
  79. else
  80. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  81. array('{class}'=>get_class($this), '{property}'=>$name)));
  82. }
  83. /**
  84. * Sets value of a property or attribute.
  85. * Do not call this method. This is a PHP magic method that we override
  86. * to allow using the following syntax to set a property or attribute.
  87. * <pre>
  88. * $this->propertyName=$value;
  89. * $this->attributeName=$value;
  90. * </pre>
  91. * @param string $name the property or attribute name
  92. * @param mixed $value the property or attribute value
  93. * @see __get
  94. */
  95. public function __set($name,$value)
  96. {
  97. $setter='set'.$name;
  98. if(method_exists($this,$setter))
  99. $this->$setter($value);
  100. else
  101. $this->attributes[$name]=$value;
  102. }
  103. /**
  104. * Configures this object with property initial values.
  105. * @param mixed $config the configuration for this object. This can be an array
  106. * representing the property names and their initial values.
  107. * It can also be a string representing the file name of the PHP script
  108. * that returns a configuration array.
  109. */
  110. public function configure($config)
  111. {
  112. if(is_string($config))
  113. $config=require(Yii::getPathOfAlias($config).'.php');
  114. if(is_array($config))
  115. {
  116. foreach($config as $name=>$value)
  117. $this->$name=$value;
  118. }
  119. }
  120. /**
  121. * Returns a value indicating whether this element is visible and should be rendered.
  122. * This method will call {@link evaluateVisible} to determine the visibility of this element.
  123. * @return boolean whether this element is visible and should be rendered.
  124. */
  125. public function getVisible()
  126. {
  127. if($this->_visible===null)
  128. $this->_visible=$this->evaluateVisible();
  129. return $this->_visible;
  130. }
  131. /**
  132. * @param boolean $value whether this element is visible and should be rendered.
  133. */
  134. public function setVisible($value)
  135. {
  136. $this->_visible=$value;
  137. }
  138. /**
  139. * @return mixed the direct parent of this element. This could be either a {@link CForm} object or a {@link CBaseController} object
  140. * (a controller or a widget).
  141. */
  142. public function getParent()
  143. {
  144. return $this->_parent;
  145. }
  146. /**
  147. * Evaluates the visibility of this element.
  148. * Child classes should override this method to implement the actual algorithm
  149. * for determining the visibility.
  150. * @return boolean whether this element is visible. Defaults to true.
  151. */
  152. protected function evaluateVisible()
  153. {
  154. return true;
  155. }
  156. }