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

/framework/web/form/CFormElement.php

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