/protected/libs/yii/web/form/CFormButtonElement.php

https://github.com/allinside/Yii-CMS · PHP · 137 lines · 61 code · 7 blank · 69 comment · 7 complexity · 41266c28e65a75afdc8cf8048afd628f MD5 · raw file

  1. <?php
  2. /**
  3. * CFormButtonElement 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. * CFormButtonElement represents a form button element.
  12. *
  13. * CFormButtonElement can represent the following types of button based on {@link type} property:
  14. * <ul>
  15. * <li>htmlButton: a normal button generated using {@link CHtml::htmlButton}</li>
  16. * <li>htmlReset a reset button generated using {@link CHtml::htmlButton}</li>
  17. * <li>htmlSubmit: a submit button generated using {@link CHtml::htmlButton}</li>
  18. * <li>submit: a submit button generated using {@link CHtml::submitButton}</li>
  19. * <li>button: a normal button generated using {@link CHtml::button}</li>
  20. * <li>image: an image button generated using {@link CHtml::imageButton}</li>
  21. * <li>reset: a reset button generated using {@link CHtml::resetButton}</li>
  22. * <li>link: a link button generated using {@link CHtml::linkButton}</li>
  23. * </ul>
  24. * The {@link type} property can also be a class name or a path alias to the class. In this case,
  25. * the button is generated using a widget of the specified class. Note, the widget must
  26. * have a property called "name".
  27. *
  28. * Because CFormElement is an ancestor class of CFormButtonElement, a value assigned to a non-existing property will be
  29. * stored in {@link attributes} which will be passed as HTML attribute values to the {@link CHtml} method
  30. * generating the button or initial values of the widget properties.
  31. *
  32. * @author Qiang Xue <qiang.xue@gmail.com>
  33. * @version $Id: CFormButtonElement.php 3001 2011-02-24 16:42:44Z alexander.makarow $
  34. * @package system.web.form
  35. * @since 1.1
  36. */
  37. class CFormButtonElement extends CFormElement
  38. {
  39. /**
  40. * @var array Core button types (alias=>CHtml method name)
  41. */
  42. public static $coreTypes=array(
  43. 'htmlButton'=>'htmlButton',
  44. 'htmlSubmit'=>'htmlButton',
  45. 'htmlReset'=>'htmlButton',
  46. 'button'=>'button',
  47. 'submit'=>'submitButton',
  48. 'reset'=>'resetButton',
  49. 'image'=>'imageButton',
  50. 'link'=>'linkButton',
  51. );
  52. /**
  53. * @var string the type of this button. This can be a class name, a path alias of a class name,
  54. * or a button type alias (submit, button, image, reset, link, htmlButton, htmlSubmit, htmlReset).
  55. */
  56. public $type;
  57. /**
  58. * @var string name of this button
  59. */
  60. public $name;
  61. /**
  62. * @var string the label of this button. This property is ignored when a widget is used to generate the button.
  63. */
  64. public $label;
  65. private $_on;
  66. /**
  67. * Returns a value indicating under which scenarios this button is visible.
  68. * If the value is empty, it means the button is visible under all scenarios.
  69. * Otherwise, only when the model is in the scenario whose name can be found in
  70. * this value, will the button be visible. See {@link CModel::scenario} for more
  71. * information about model scenarios.
  72. * @return string scenario names separated by commas. Defaults to null.
  73. */
  74. public function getOn()
  75. {
  76. return $this->_on;
  77. }
  78. /**
  79. * @param string $value scenario names separated by commas.
  80. */
  81. public function setOn($value)
  82. {
  83. $this->_on=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY);
  84. }
  85. /**
  86. * Returns this button.
  87. * @return string the rendering result
  88. */
  89. public function render()
  90. {
  91. $attributes=$this->attributes;
  92. if(isset(self::$coreTypes[$this->type]))
  93. {
  94. $method=self::$coreTypes[$this->type];
  95. if($method==='linkButton')
  96. {
  97. if(!isset($attributes['params'][$this->name]))
  98. $attributes['params'][$this->name]=1;
  99. }
  100. else if($method==='htmlButton')
  101. {
  102. $attributes['type']=$this->type==='htmlSubmit' ? 'submit' : ($this->type==='htmlReset' ? 'reset' : 'button');
  103. $attributes['name']=$this->name;
  104. }
  105. else
  106. $attributes['name']=$this->name;
  107. if($method==='imageButton')
  108. return CHtml::imageButton(isset($attributes['src']) ? $attributes['src'] : '',$attributes);
  109. else
  110. return CHtml::$method($this->label,$attributes);
  111. }
  112. else
  113. {
  114. $attributes['name']=$this->name;
  115. ob_start();
  116. $this->getParent()->getOwner()->widget($this->type, $attributes);
  117. return ob_get_clean();
  118. }
  119. }
  120. /**
  121. * Evaluates the visibility of this element.
  122. * This method will check the {@link on} property to see if
  123. * the model is in a scenario that should have this string displayed.
  124. * @return boolean whether this element is visible.
  125. */
  126. protected function evaluateVisible()
  127. {
  128. return empty($this->_on) || in_array($this->getParent()->getModel()->getScenario(),$this->_on);
  129. }
  130. }