CFormButtonElement.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * CFormButtonElement 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. * 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. * @property string $on Scenario names separated by commas. Defaults to null.
  33. *
  34. * @author Qiang Xue <qiang.xue@gmail.com>
  35. * @package system.web.form
  36. * @since 1.1
  37. */
  38. class CFormButtonElement extends CFormElement
  39. {
  40. /**
  41. * @var array Core button types (alias=>CHtml method name)
  42. */
  43. public static $coreTypes=array(
  44. 'htmlButton'=>'htmlButton',
  45. 'htmlSubmit'=>'htmlButton',
  46. 'htmlReset'=>'htmlButton',
  47. 'button'=>'button',
  48. 'submit'=>'submitButton',
  49. 'reset'=>'resetButton',
  50. 'image'=>'imageButton',
  51. 'link'=>'linkButton',
  52. );
  53. /**
  54. * @var string the type of this button. This can be a class name, a path alias of a class name,
  55. * or a button type alias (submit, button, image, reset, link, htmlButton, htmlSubmit, htmlReset).
  56. */
  57. public $type;
  58. /**
  59. * @var string name of this button
  60. */
  61. public $name;
  62. /**
  63. * @var string the label of this button. This property is ignored when a widget is used to generate the button.
  64. */
  65. public $label;
  66. private $_on;
  67. /**
  68. * Returns a value indicating under which scenarios this button is visible.
  69. * If the value is empty, it means the button is visible under all scenarios.
  70. * Otherwise, only when the model is in the scenario whose name can be found in
  71. * this value, will the button be visible. See {@link CModel::scenario} for more
  72. * information about model scenarios.
  73. * @return string scenario names separated by commas. Defaults to null.
  74. */
  75. public function getOn()
  76. {
  77. return $this->_on;
  78. }
  79. /**
  80. * @param string $value scenario names separated by commas.
  81. */
  82. public function setOn($value)
  83. {
  84. $this->_on=preg_split('/[\s,]+/',$value,-1,PREG_SPLIT_NO_EMPTY);
  85. }
  86. /**
  87. * Returns this button.
  88. * @return string the rendering result
  89. */
  90. public function render()
  91. {
  92. $attributes=$this->attributes;
  93. if(isset(self::$coreTypes[$this->type]))
  94. {
  95. $method=self::$coreTypes[$this->type];
  96. if($method==='linkButton')
  97. {
  98. if(!isset($attributes['params'][$this->name]))
  99. $attributes['params'][$this->name]=1;
  100. }
  101. elseif($method==='htmlButton')
  102. {
  103. $attributes['type']=$this->type==='htmlSubmit' ? 'submit' : ($this->type==='htmlReset' ? 'reset' : 'button');
  104. $attributes['name']=$this->name;
  105. }
  106. else
  107. $attributes['name']=$this->name;
  108. if($method==='imageButton')
  109. return CHtml::imageButton(isset($attributes['src']) ? $attributes['src'] : '',$attributes);
  110. else
  111. return CHtml::$method($this->label,$attributes);
  112. }
  113. else
  114. {
  115. $attributes['name']=$this->name;
  116. ob_start();
  117. $this->getParent()->getOwner()->widget($this->type, $attributes);
  118. return ob_get_clean();
  119. }
  120. }
  121. /**
  122. * Evaluates the visibility of this element.
  123. * This method will check the {@link on} property to see if
  124. * the model is in a scenario that should have this string displayed.
  125. * @return boolean whether this element is visible.
  126. */
  127. protected function evaluateVisible()
  128. {
  129. return empty($this->_on) || in_array($this->getParent()->getModel()->getScenario(),$this->_on);
  130. }
  131. }