Passed
Push — master ( 16e49b...7b1d72 )
by WILMER
02:04
created

ToolBar::run()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 0
dl 0
loc 15
rs 9.6111
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 cjtterabytesoft\widgets\assets\ToolBarAsset;
19
use yii\base\Widget;
20
use yii\bootstrap4\ButtonDropdown;
21
use yii\helpers\ArrayHelper;
22
use yii\helpers\Html;
23
use yii\helpers\Url;
24
25
class ToolBar extends Widget
26
{
27
	/**
28
	 * @var string the tag to use to render panel title
29
	 */
30
	public $_tag_container_panel_header = 'div';
31
32
	/**
33
	 * @var array the options to use to render panel title
34
	 */
35
	public $_options_container_panel_header = ['class' => 'peers bg-primary text-white align-content-center p-15'];
36
37
	/**
38
	 * @var string the tag to use to render panel title left
39
	 */
40
	public $_tag_left_panel_header = 'div';
41
42
	/**
43
	 * @var array the options to use to render panel title left
44
	 */
45
	public $_options_left_panel_header = ['class' => 'float-left'];
46
47
	/**
48
	 * @var string the tag to use to render panel title rigth
49
	 */
50
	public $_tag_rigth_panel_header = 'div';
51
52
	/**
53
	 * @var array the options to use to render panel title rigth
54
	 */
55
	public $_options_rigth_panel_header = ['class' => 'float-right ml-auto'];
56
57
	/**
58
	 * @var string use to panel header title
59
	 */
60
	public $_panel_header_title = '';
61
62
	/**
63
	 * @var string the tag to use to render panel title icon
64
	 */
65
	public $_tag_icon_panel = 'i';
66
67
	/**
68
	 * @var string the label to use to render panel title icon
69
	 */
70
	public $_label_icon_panel = '';
71
72
	/**
73
	 * @var array the options to use to render panel title icons
74
	 */
75
	public $_options_icon_panel = ['class' => 'fas fa-th'];
76
77
	/**
78
	 * @var string the label panel title
79
	 */
80
	public $_title_panel = '';
81
82
	/**
83
	 * @var bool Show/Hidden Summary Label
84
	 */
85
	public $_summary = false;
86
87
	/**
88
	 * @var string the tag to use to render container panel button
89
	 */
90
	public $_tag_container_panel_button = 'div';
91
92
	/**
93
	 * @var array the options to use to render container panel button
94
	 */
95
	public $_options_container_panel_button = ['class' => 'peers bd p-15'];
96
97
	/**
98
	 * @var string the tag to use to render panel button left
99
	 */
100
	public $_tag_left_panel_button = 'div';
101
102
	/**
103
	 * @var array the options to use to render panel button left
104
	 */
105
	public $_options_left_panel_button = ['class' => 'float-left'];
106
107
	/**
108
	 * @var string the tag to use to render panel button rigth
109
	 */
110
	public $_tag_rigth_panel_button = 'div';
111
112
	/**
113
	 * @var array the options to use to render panel button rigth
114
	 */
115
	public $_options_rigth_panel_button = ['class' => 'float-right ml-auto'];
116
117
	/**
118
	 * @var array Templates
119
	 */
120
	public $_templates = [];
121
122
	/**
123
	 * @var array Buttons
124
	 */
125
	public $_toolbar = [];
126
127
	/**
128
	 * @var bool show/hidden panel button pages
129
	 */
130
	public $_button_pages = false;
131
132
	/**
133
	 * @var bool show/hidden panel button export
134
	 */
135
	public $_button_export = false;
136
137
	/**
138
	 * @var bool whether the label should be HTML-encoded.
139
	 */
140
	public $_encodeLabel = false;
141
142
	/**
143
	 * @var bool whether the JS file should be registered.
144
	 */
145
	public $_registerJs = true;
146
147
	/**
148
	 * @var array Options Filter.
149
	 */
150
	public $_optionsFilter = [];
151
152
	/**
153
	 * @var string Message Filter Error.
154
	 */
155
	public $_messageFilter = '';
156
157
	/**
158
	 * @var array Model Search.
159
	 */
160
	public $_modelSearch = [];
161
162
	/**
163
	 * @var string Query Params.
164
	 */
165
	public $_queryParams = '';
166
167
	/**
168
	 * @var string Record All.
169
	 */
170
	public $_getall = '';
171
172
173
	/**
174
	 * Initializes the widget.
175
	 * If you override this method, make sure you call the parent implementation first.
176
	 */
177
	public function init()
178
	{
179
		parent::init();
180
181
		if (empty($this->_optionsFilter['message_filter'])) {
182
			$this->_messageFilter = \yii::t('toolbar', 'Please select one or more items from the list.');
183
		}
184
185
		$iconpanel = $this->renderIcon();
186
		$titlepanel = $this->renderTitlePanel();
187
188
		$this->_panel_header_title = $iconpanel . '&nbsp' . '<b>' . $titlepanel . '</b>';
189
	}
190
191
	/**
192
	 * Run the widget.
193
	 * If you override this method, make sure you call the parent implementation first.
194
	 */
195
	public function run()
196
	{
197
		if (!empty($this->_optionsFilter['id_grid']) && !empty($this->_optionsFilter['id_button']) && !empty($this->_optionsFilter['route_filter'])) {
198
			$id_grid = $this->_optionsFilter['id_grid'];
199
			$id_button = $this->_optionsFilter['id_button'];
200
			$route_filter = $this->_optionsFilter['route_filter'];
201
			$this->view->registerJs("filterGridSelect('#$id_grid', '#$id_button', '$route_filter', '$this->_messageFilter');");
202
		}
203
204
		if ($this->_registerJs) {
205
			$toolbar = new ToolBarAsset();
206
			$toolbar->register($this->view);
207
		}
208
209
		echo $this->renderPanelHeader() . $this->renderPanelBar();
210
	}
211
212
	private function renderButtonExport()
213
	{
214
		$button_export = '';
215
216
		if ($this->_button_export) {
217
			$button_export = ButtonDropdown::widget([
218
				'buttonOptions' => ['class' => 'btn-primary ai-c mL-2'],
219
				'encodeLabel' => $this->_encodeLabel,
220
				'label' => Html::tag('i', '', ['class' => 'fas fa-file-export']),
221
				'options' => ['class' => 'float-right'],
222
				'dropdown' => [
223
					'encodeLabels' => $this->_encodeLabel,
224
					'items' => [
225
						Html::tag('h6', \yii::t('toolbar', 'Export Menu'), ['class' => 'dropdown-header']),
226
						Html::tag('div', '', ['class' => 'dropdown-divider']),
227
						[
228
							'label' => Html::tag('i', '', ['class' => 'fas fa-file-code']) . ' ' . 'CSV',
229
							'url' => Url::to(['export/csv']),
230
							'linkOptions' => [
231
								'data' => [
232
									'method' => 'post',
233
									'params' => [
234
										'model' => $this->_modelSearch,
235
									],
236
								],
237
							],
238
						],
239
						['label' => Html::tag('i', '', ['class' => 'fas fa-file-excel']) . ' ' . 'EXCEL', 'url' => Url::current()],
240
						['label' => Html::tag('i', '', ['class' => 'fas fa-file-pdf']) . ' ' . 'PDF', 'url' => Url::current()],
241
						['label' => Html::tag('i', '', ['class' => 'fas fa-file-word']) . ' ' . 'WORD', 'url' => Url::current()],
242
					],
243
				],
244
			]);
245
		}
246
247
		return $button_export;
248
	}
249
250
	private function renderButtonPages()
251
	{
252
		$button_pages = '';
253
254
		if ($this->_button_pages) {
255
			$button_pages = ButtonDropdown::widget([
256
				'buttonOptions' => ['class' => 'btn-primary ai-c mL-2'],
257
				'encodeLabel' => $this->_encodeLabel,
258
				'label' => Html::tag('i', '', ['class' => 'fas fa-cog']),
259
				'options' => ['class' => 'float-right'],
260
				'dropdown' => [
261
					'items' => [
262
						Html::tag('h6', \yii::t('toolbar', 'Page Size Menu'), ['class' => 'dropdown-header']),
263
						Html::tag('div', '', ['class' => 'dropdown-divider']),
264
						['label' => '1', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '1'])],
265
						['label' => '5', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '5'])],
266
						['label' => '10', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '10'])],
267
						['label' => '20', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '20'])],
268
						['label' => '25', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '25'])],
269
						['label' => '50', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '50'])],
270
					],
271
				],
272
			]);
273
		}
274
275
		return $button_pages;
276
	}
277
278
	private function renderIcon()
279
	{
280
		return Html::tag($this->_tag_icon_panel, $this->_label_icon_panel, $this->_options_icon_panel);
281
	}
282
283
	private function renderPanelBar()
284
	{
285
		$panel_button = Html::begintag($this->_tag_container_panel_button, $this->_options_container_panel_button) .
286
							$this->renderToolBar() .
287
						Html::endTag($this->_tag_container_panel_button);
288
		return $panel_button;
289
	}
290
291
	private function renderPanelHeader()
292
	{
293
		$pageSize = \yii::$app->params['defaultPageSize'];
294
295
		if (\yii::$app->session->get('pageSize') !== null) {
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

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

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...
296
			$pageSize = \yii::$app->session->get('pageSize');
297
		}
298
299
		$summary = ($this->_summary) ? '{summary}' . ' ' . 'Records per pages: ' . '<b>' . $pageSize . '</b>' : ' ';
300
301
		$panel_header = Html::begintag($this->_tag_container_panel_header, $this->_options_container_panel_header) .
302
							Html::begintag($this->_tag_left_panel_header, $this->_options_left_panel_header) .
303
								$this->_panel_header_title .
304
							Html::endTag($this->_tag_left_panel_header) .
305
							Html::begintag($this->_tag_rigth_panel_header, $this->_options_rigth_panel_header) .
306
								$summary .
307
							Html::endTag($this->_tag_rigth_panel_header) .
308
						Html::endTag($this->_tag_container_panel_header);
309
		return $panel_header;
310
	}
311
312
	private function renderTitlePanel()
313
	{
314
		if (empty($this->_title_panel)) {
315
			$this->_title_panel = \yii::t('toolbar', 'Gridview ToolBar');
316
		}
317
318
		return $this->_title_panel;
319
	}
320
321
	private function renderToolBar()
322
	{
323
		$buttons_left = '';
324
		$buttons_rigth = '';
325
326
		ArrayHelper::setValue($this->_toolbar, 'pages.0', $this->renderButtonPages());
327
		ArrayHelper::setValue($this->_toolbar, 'export.0', $this->renderButtonExport());
328
329
		foreach ($this->_templates as $items => $buttons) {
330
			foreach ($buttons as $item => $button) {
331
				if (!empty(ArrayHelper::getValue($this->_toolbar, $button, ''))) {
332
					switch ($items) {
333
						case 'left':
334
							$buttons_left .= implode(',', ArrayHelper::getValue($this->_toolbar, $button, ''));
335
							break;
336
						case 'rigth':
337
							$buttons_rigth .= implode(',', ArrayHelper::getValue($this->_toolbar, $button, ''));
338
							break;
339
					}
340
				}
341
			}
342
		}
343
344
		return Html::tag($this->_tag_left_panel_button, $buttons_left, $this->_options_left_panel_button) .
345
			   Html::tag($this->_tag_rigth_panel_button, $buttons_rigth, $this->_options_rigth_panel_button);
346
	}
347
}
348