1: <?php
2: /**
3: * CJuiButton class file.
4: *
5: * @author Sebastian Thierer <sebas@artfos.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: Yii::import('zii.widgets.jui.CJuiInputWidget');
12:
13: /**
14: * CJuiButton displays a button widget.
15: *
16: * CJuiButton encapsulates the {@link http://jqueryui.com/button/ JUI Button}
17: * plugin.
18: *
19: * To use this widget as a submit button, you may insert the following code in a view:
20: * <pre>
21: * $this->widget('zii.widgets.jui.CJuiButton',array(
22: * 'buttonType'=>'submit',
23: * 'name'=>'btnSubmit',
24: * 'value'=>'1',
25: * 'caption'=>'Submit form',
26: * 'htmlOptions'=>array('class'=>'ui-button-primary')
27: * ),
28: * ));
29: * </pre>
30: *
31: * To use this widget as a button, you may insert the following code in a view:
32: * <pre>
33: * $this->widget('zii.widgets.jui.CJuiButton',array(
34: * 'buttonType'=>'button',
35: * 'name'=>'btnSave',
36: * 'caption'=>'Save',
37: * 'onclick'=>new CJavaScriptExpression('function(){alert("Save button clicked"); this.blur(); return false;}'),
38: * ));
39: * </pre>
40: *
41: * By configuring the {@link options} property, you may specify the options
42: * that need to be passed to the JUI button plugin. Please refer to
43: * the {@link http://api.jqueryui.com/button/ JUI Button API} documentation
44: * for possible options (name-value pairs) and
45: * {@link http://jqueryui.com/button/ JUI Button page} for general description
46: * and demo.
47: *
48: * @author Sebastian Thierer <sebathi@gmail.com>
49: * @package zii.widgets.jui
50: * @since 1.1.3
51: */
52: class CJuiButton extends CJuiInputWidget
53: {
54: /**
55: * @var string The button type (possible types: submit, button, link, radio, checkbox, buttonset).
56: * "submit" is used as default.
57: */
58: public $buttonType='submit';
59: /**
60: * @var string The default html tag for the buttonset
61: */
62: public $htmlTag='div';
63: /**
64: * @var mixed a URL or an action route that can be used to create a URL. Used when a buttonType "link" is selected.
65: * See {@link normalizeUrl} for more details about how to specify this parameter.
66: */
67: public $url=null;
68: /**
69: * @var mixed The value of the current item. Used only for "radio" and "checkbox"
70: */
71: public $value;
72: /**
73: * @var string The button text
74: */
75: public $caption="";
76: /**
77: * @var string The javascript function to be raised when this item is clicked (client event).
78: */
79: public $onclick;
80:
81: /**
82: * (non-PHPdoc)
83: * @see framework/zii/widgets/jui/CJuiWidget::init()
84: */
85: public function init()
86: {
87: parent::init();
88:
89: if($this->buttonType=='buttonset')
90: {
91: if(!isset($this->htmlOptions['id']))
92: $this->htmlOptions['id']=$this->getId();
93:
94: echo CHtml::openTag($this->htmlTag,$this->htmlOptions);
95: }
96: }
97:
98: /**
99: * (non-PHPdoc)
100: * @see framework/CWidget::run()
101: */
102: public function run()
103: {
104: $cs=Yii::app()->getClientScript();
105: list($name,$id)=$this->resolveNameID();
106:
107: if(isset($this->htmlOptions['id']))
108: $id=$this->htmlOptions['id'];
109: else
110: $this->htmlOptions['id']=$id;
111: if(isset($this->htmlOptions['name']))
112: $name=$this->htmlOptions['name'];
113: else
114: $this->htmlOptions['name']=$name;
115:
116: if($this->buttonType=='buttonset')
117: {
118: echo CHtml::closeTag($this->htmlTag);
119: $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').buttonset();");
120: }
121: else
122: {
123: switch($this->buttonType)
124: {
125: case 'submit':
126: echo CHtml::submitButton($this->caption,$this->htmlOptions)."\n";
127: break;
128: case 'button':
129: echo CHtml::htmlButton($this->caption,$this->htmlOptions)."\n";
130: break;
131: case 'link':
132: echo CHtml::link($this->caption,$this->url,$this->htmlOptions)."\n";
133: break;
134: case 'radio':
135: if($this->hasModel())
136: {
137: echo CHtml::activeRadioButton($this->model,$this->attribute,$this->htmlOptions);
138: echo CHtml::label($this->caption,CHtml::activeId($this->model,$this->attribute))."\n";
139: }
140: else
141: {
142: echo CHtml::radioButton($name,$this->value,$this->htmlOptions);
143: echo CHtml::label($this->caption,$id)."\n";
144: }
145: break;
146: case 'checkbox':
147: if($this->hasModel())
148: {
149: echo CHtml::activeCheckbox($this->model,$this->attribute,$this->htmlOptions);
150: echo CHtml::label($this->caption,CHtml::activeId($this->model,$this->attribute))."\n";
151: }
152: else
153: {
154: echo CHtml::checkbox($name,$this->value,$this->htmlOptions);
155: echo CHtml::label($this->caption,$id)."\n";
156: }
157: break;
158: default:
159: throw new CException(Yii::t('zii','The button type "{type}" is not supported.',array('{type}'=>$this->buttonType)));
160: }
161:
162: $options=CJavaScript::encode($this->options);
163: if($this->onclick!==null)
164: {
165: if(!($this->onclick instanceof CJavaScriptExpression))
166: $this->onclick=new CJavaScriptExpression($this->onclick);
167: $click=CJavaScript::encode($this->onclick);
168: $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').button($options).click($click);");
169: }
170: else
171: $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').button($options);");
172: }
173: }
174: }
175: