1
+ <?php
2
+
3
+ /**
4
+ * @link https://github.com/unclead/yii2-multiple-input
5
+ * @copyright Copyright (c) 2014 unclead
6
+ * @license https://github.com/unclead/yii2-multiple-input/blob/master/LICENSE.md
7
+ */
8
+
9
+ namespace unclead \widgets ;
10
+
11
+ use Yii ;
12
+ use yii \helpers \Url ;
13
+ use yii \helpers \Html ;
14
+ use yii \db \ActiveRecord ;
15
+
16
+ /**
17
+ * Class Button
18
+ * @package unclead\widgets
19
+ */
20
+ class Button extends \yii \bootstrap \Button
21
+ {
22
+ use TranslationTrait;
23
+
24
+ const TYPE_SUCCESS = 'btn-success ' ;
25
+ const TYPE_DEFAULT = 'btn-default ' ;
26
+ const TYPE_PRIMARY = 'btn-primary ' ;
27
+ const TYPE_INFO = 'btn-info ' ;
28
+ const TYPE_DANGER = 'btn-danger ' ;
29
+ const TYPE_WARNING = 'btn-warning ' ;
30
+
31
+ const SIZE_LARGE = 'btn-lg ' ;
32
+ const SIZE_SMALL = 'btn-sm ' ;
33
+ const SIZE_EXTRA_SMALL = 'btn-xs ' ;
34
+
35
+ /**
36
+ * @var null icon css class
37
+ */
38
+ public $ iconClass = null ;
39
+
40
+ /**
41
+ * @var string button type
42
+ */
43
+ public $ type = self ::TYPE_DEFAULT ;
44
+
45
+ /**
46
+ * @var string button size
47
+ */
48
+ public $ size ;
49
+
50
+ /**
51
+ * @var string tag name
52
+ */
53
+ public $ tagName = 'a ' ;
54
+
55
+ /**
56
+ * @var string
57
+ */
58
+ public $ url ;
59
+
60
+ public function init ()
61
+ {
62
+ self ::registerTranslations ();
63
+ parent ::init ();
64
+ }
65
+
66
+ /**
67
+ * Renders the widget.
68
+ */
69
+ public function run ()
70
+ {
71
+ if (!empty ($ this ->iconClass )) {
72
+ $ icon = Html::tag ('i ' , null , ['class ' => $ this ->iconClass ]);
73
+ $ this ->label = $ icon . ($ this ->label ? ' ' . $ this ->label : '' );
74
+ $ this ->encodeLabel = false ;
75
+ }
76
+
77
+ if (!empty ($ this ->type )) {
78
+ Html::addCssClass ($ this ->options , $ this ->type );
79
+ }
80
+
81
+ if (!empty ($ this ->size )) {
82
+ Html::addCssClass ($ this ->options , $ this ->size );
83
+ }
84
+
85
+ if (!empty ($ this ->url )) {
86
+ $ this ->options ['href ' ] = Url::to ($ this ->url );
87
+ }
88
+
89
+ echo Html::tag ($ this ->tagName , $ this ->encodeLabel ? Html::encode ($ this ->label ) : $ this ->label , $ this ->options );
90
+ $ this ->registerPlugin ('button ' );
91
+ }
92
+
93
+ /**
94
+ * Render "Cancel" button.
95
+ *
96
+ * @return string
97
+ */
98
+ public static function cancel () {
99
+ self ::registerTranslations ();
100
+ return static ::widget ([
101
+ 'label ' => Yii::t ('unclead-widgets ' , 'BUTTON_CANCEL ' ),
102
+ 'type ' => self ::TYPE_DEFAULT ,
103
+ 'url ' => ['index ' ]
104
+ ]);
105
+ }
106
+
107
+ /**
108
+ * Render "Create" или "Update" button.
109
+ *
110
+ * @param ActiveRecord $model
111
+ * @return string
112
+ */
113
+ public static function submit ($ model = null ) {
114
+ self ::registerTranslations ();
115
+ if (!is_null ($ model )) {
116
+ return Html::submitButton (
117
+ $ model ->isNewRecord ? Yii::t ('unclead-widgets ' , 'BUTTON_CREATE ' ) : Yii::t ('unclead-widgets ' , 'BUTTON_UPDATE ' ),
118
+ ['class ' => $ model ->isNewRecord ? 'btn btn-success ' : 'btn btn-primary ' ]
119
+ );
120
+ } else {
121
+ return Html::submitButton (Yii::t ('unclead-widgets ' , 'BUTTON_SUBMIT ' ), ['class ' => 'btn btn-primary ' ]);
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Render "Update" button.
127
+ *
128
+ * @return string
129
+ */
130
+ public static function update () {
131
+ self ::registerTranslations ();
132
+ return Html::submitButton (Yii::t ('unclead-widgets ' , 'BUTTON_UPDATE ' ), ['class ' => 'btn btn-primary ' ]);
133
+ }
134
+
135
+ /**
136
+ * Render "Delete button"
137
+ *
138
+ * @param ActiveRecord $model
139
+ * @return string
140
+ */
141
+ public static function delete ($ model ) {
142
+ self ::registerTranslations ();
143
+ return $ model ->isNewRecord ? '' : static ::widget ([
144
+ 'label ' => Yii::t ('unclead-widgets ' , 'BUTTON_DELETE ' ),
145
+ 'type ' => self ::TYPE_DANGER ,
146
+ 'url ' => ['delete ' , 'id ' => $ model ->primaryKey ],
147
+ 'options ' => [
148
+ 'data-method ' => 'post ' ,
149
+ 'data-confirm ' => Yii::t ('unclead-widgets ' , 'CONFIRM_DELETE ' ),
150
+ ],
151
+ ]);
152
+ }
153
+ }
0 commit comments