Passed
Push — master ( 8e95c3...f6b90d )
by WILMER
01:24
created

ToolBar::renderButtonFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * (c) CJT TERABYTE INC
5
 * For the full copyright and license information, please view the LICENSE.md
6
 * file that was distributed with this source code.
7
 *
8
 *        @link: https://gitlab.com/cjtterabytesoft/tadweb
9
 *      @author: Wilmer Arámbula <[email protected]>
10
 *   @copyright: (c) CJT TERABYTE INC
11
 *     @widgets: [ToolBar]
12
 *       @since: 1.0
13
 *         @yii: 3.0
14
 **/
15
16
namespace cjtterabytesoft\widgets;
17
18
use yii\base\Widget;
19
use yii\bootstrap4\ButtonDropdown;
20
use yii\helpers\Html;
21
use yii\helpers\Url;
22
23
class ToolBar extends Widget
24
{
25
	/**
26
	 * @var string the tag to use to render panel title icon
27
	 */
28
	public $_tag_icon_panel = 'i';
29
30
	/**
31
	 * @var string the label to use to render panel title icon
32
	 */
33
	public $_label_icon_panel = '';
34
35
	/**
36
	 * @var array the options to use to render panel title icons
37
	 */
38
	public $_options_icon_panel = ['class' => 'fas fa-th'];
39
40
	/**
41
	 * @var string the label panel title
42
	 */
43
	public $_title_panel = '';
44
45
	/**
46
	 * @var string the tag to use to render panel title
47
	 */
48
	public $_tag_container_panel_header = 'div';
49
50
	/**
51
	 * @var array the options to use to render panel title
52
	 */
53
	public $_options_container_panel_header = ['class' => 'peers bg-primary text-white align-content-center p-15'];
54
55
	/**
56
	 * @var string the tag to use to render panel title left
57
	 */
58
	public $_tag_left_panel_header = 'div';
59
60
	/**
61
	 * @var array the options to use to render panel title left
62
	 */
63
	public $_options_left_panel_header = ['class' => 'float-left'];
64
65
	/**
66
	 * @var string the tag to use to render panel title rigth
67
	 */
68
	public $_tag_rigth_panel_header = 'div';
69
70
	/**
71
	 * @var array the options to use to render panel title rigth
72
	 */
73
	public $_options_rigth_panel_header = ['class' => 'float-right ml-auto'];
74
75
	/**
76
	 * @var string the tag to use to render container panel button
77
	 */
78
	public $_tag_container_panel_button = 'div';
79
80
	/**
81
	 * @var array the options to use to render container panel button
82
	 */
83
	public $_options_container_panel_button = ['class' => 'peers bd p-15'];
84
85
	/**
86
	 * @var string the tag to use to render panel button left
87
	 */
88
	public $_tag_left_panel_button = 'div';
89
90
	/**
91
	 * @var array the options to use to render panel button left
92
	 */
93
	public $_options_left_panel_button = ['class' => 'float-left'];
94
95
	/**
96
	 * @var string the tag to use to render panel button rigth
97
	 */
98
	public $_tag_rigth_panel_button = 'div';
99
100
	/**
101
	 * @var array the options to use to render panel button rigth
102
	 */
103
	public $_options_rigth_panel_button = ['class' => 'float-right ml-auto'];
104
105
	/**
106
	 * @var bool whether the label should be HTML-encoded.
107
	 */
108
	public $_encodeLabel = true;
109
110
	/**
111
	 * @var string use to panel header title
112
	 */
113
	public $_panel_header_title = '';
114
	
115
	/**
116
	 * @var boolean show/hidden panel button create
117
	 */
118
	public $_button_create = true;
119
120
	/**
121
	 * @var boolean show/hidden panel button delete
122
	 */
123
	public $_button_delete = false;
124
125
	/**
126
	 * @var boolean show/hidden panel button filter
127
	 */
128
	public $_button_filter = true;
129
130
	/**
131
	 * @var boolean show/hidden panel button pages
132
	 */
133
	public $_button_pages = true;
134
135
	/**
136
	 * @var boolean show/hidden panel button reset
137
	 */
138
	public $_button_reset = true;
139
140
	/**
141
	 * @var boolean show/hidden panel button update
142
	 */
143
	public $_button_update = false;
144
145
	/**
146
	 * @var array ModelClass
147
	 */
148
	public $_model = [];
149
150
	/**
151
	 * Initializes the widget.
152
	 * If you override this method, make sure you call the parent implementation first.
153
	 */
154
	public function init()
155
	{
156
		parent::init();
157
158
		$iconpanel = $this->renderIcon();
159
		$titlepanel = $this->renderTitlePanel();
160
		$this->_panel_header_title = $iconpanel . '&nbsp' . '<b>' . $titlepanel . '</b>';
161
162
		echo $this->renderPanelHeader() . $this->renderPanelBar();
163
	}
164
165
	private function renderButtonCreate()
166
	{
167
		$button_create = '';
168
169
		if ($this->_button_create) {
170
			$button_create = Html::a(
171
				Html::tag('i', '', ['class' => 'fas fa-plus']),
172
				['create'],
173
				['class' => 'btn btn-lg bgc-green-500 c-white', 'title' => \yii::t('toolbar', 'Add')]
174
			);
175
		}
176
177
		return $button_create;
178
	}
179
180
	private function renderButtonDelete()
0 ignored issues
show
Unused Code introduced by
The method renderButtonDelete() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
181
	{
182
		$button_delete = '';
183
184
		if ($this->_button_delete) {
185
			$button_create = Html::a(
0 ignored issues
show
Unused Code introduced by
The assignment to $button_create is dead and can be removed.
Loading history...
186
				Html::tag('i', '', ['class' => 'fas fa-trash']),
187
				['delete', 'id' => $this->_model->tableSchema->primaryKey],
188
				[
189
					'class' => 'btn btn-lg btn-danger c-white',
190
					'title' => \yii::t('toolbar', 'Delete'),
191
					'data' => [
192
						'confirm' => \yii::t('toolbar', 'Are you sure you want to delete this item?'),
193
						'method' => 'post',
194
					],
195
				]
196
			);
197
		}
198
199
		return $button_delete;
200
	}
201
202
	private function renderButtonFilter()
203
	{
204
		$button_filter = '';
205
206
		if ($this->_button_filter) {
207
			$button_filter = Html::a(
208
				Html::tag('i', '', ['class' => 'fas fa-filter']),
209
				Url::current(),
210
				['id' => 'filter-checked-btn', 'class' => 'simple btn btn-lg bgc-blue-500 mL-2 c-white', 'title' => \yii::t('toolbar', 'Filter')]
211
			);
212
		}
213
214
		return $button_filter;
215
	}
216
217
	private function renderButtonPages()
218
	{
219
		$button_pages = '';
220
221
		if ($this->_button_pages) {
222
			$button_pages = ButtonDropdown::widget([
223
				'buttonOptions' => ['class' => 'btn-sm btn-primary ai-c'],
224
				'label' => \yii::t('toolbar', 'Page Size'),
225
				'options' => ['class' => 'float-right'],
226
				'dropdown' => [
227
					'items' => [
228
						['label' => '1', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '1'])],
229
						['label' => '5', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '5'])],
230
						['label' => '10', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '10'])],
231
						['label' => '20', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '20'])],
232
						['label' => '25', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '25'])],
233
						['label' => '50', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '50'])],
234
					],
235
				],
236
			]);
237
		}
238
239
		return $button_pages;
240
	}
241
	
242
	private function renderButtonReset()
243
	{
244
		$button_reset = '';
245
246
		if ($this->_button_reset) {
247
			$button_reset = Html::a(
248
				Html::tag('i', '', ['class' => 'fas fa-sync-alt']),
249
				['index', [], []],
250
				['class' => 'btn btn-lg bgc-indigo-500 mL-2 c-white', 'title' => \yii::t('toolbar', 'Reset')]
251
			);
252
		}
253
254
		return $button_reset;
255
	}
256
257
	private function renderButtonUpdate($model)
0 ignored issues
show
Unused Code introduced by
The method renderButtonUpdate() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

257
	private function renderButtonUpdate(/** @scrutinizer ignore-unused */ $model)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
258
	{
259
		$button_update = '';
260
261
		if ($this->_button_update) {
262
			$button_update = Html::a(
263
				Html::tag('i', '', ['class' => 'fas fa-edit']),
264
				['update', 'id' => $this->_model->tableSchema->primaryKey],
265
				['class' => 'btn btn-lg btn-info mL-2 c-white', 'title' => \yii::t('toolbar', 'Update')]
266
			);
267
		}
268
269
		return $button_update;
270
	}
271
272
	private function renderIcon()
273
	{
274
		return Html::tag($this->_tag_icon_panel, $this->_label_icon_panel, $this->_options_icon_panel);
275
	}
276
277
	private function renderPanelBar()
278
	{
279
		$panel_button = Html::begintag($this->_tag_container_panel_button, $this->_options_container_panel_button) .
280
							Html::begintag($this->_tag_left_panel_button, $this->_options_left_panel_button) .
281
								$this->renderButtonPages() .
282
							Html::endTag($this->_tag_left_panel_button) .
283
							Html::begintag($this->_tag_rigth_panel_button, $this->_options_rigth_panel_button) .
284
								$this->renderButtonCreate() .
285
								$this->renderButtonFilter() .
286
								$this->renderButtonReset() .
287
							Html::endTag($this->_tag_rigth_panel_button) .
288
						Html::endTag($this->_tag_container_panel_button);
289
		return $panel_button;
290
	}
291
292
	private function renderPanelHeader()
293
	{
294
		$pageSize = \yii::$app->params['defaultPageSize'];
295
296
		if (!is_null(\yii::$app->session->get('pageSize'))) {
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

296
		if (!is_null(\yii::$app->session->/** @scrutinizer ignore-call */ get('pageSize'))) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
297
			$pageSize = \yii::$app->session->get('pageSize');
298
		}
299
300
		$panel_header = Html::begintag($this->_tag_container_panel_header, $this->_options_container_panel_header) .
301
							Html::begintag($this->_tag_left_panel_header, $this->_options_left_panel_header) .
302
								$this->_panel_header_title .
303
							Html::endTag($this->_tag_left_panel_header) .
304
							Html::begintag($this->_tag_rigth_panel_header, $this->_options_rigth_panel_header) .
305
								'{summary}' . ' ' . 'Records per pages: ' . '<b>' . $pageSize . '</b>' .
306
							Html::endTag($this->_tag_rigth_panel_header) .
307
						Html::endTag($this->_tag_container_panel_header);
308
		return $panel_header;
309
	}
310
311
	private function renderTitlePanel()
312
	{
313
		if (empty($this->_title_panel)) {
314
			$this->_title_panel = \yii::t('toolbar', 'Gridview ToolBar');
315
		}
316
317
		return $this->_title_panel;
318
	}
319
}
320