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