ToolBar::renderPanelHeader()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 0
dl 0
loc 19
rs 9.8333
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\toolbar;
17
18
use cjtterabytesoft\toolbar\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' => 'peers 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' => 'peers 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
	 * Initializes the widget.
159
	 * If you override this method, make sure you call the parent implementation first.
160
	 */
161
	public function init()
162
	{
163
		parent::init();
164
165
		if (empty($this->_optionsFilter['message_filter'])) {
166
			$this->_messageFilter = \yii::t('toolbar', 'Please select one or more items from the list.');
167
		}
168
169
		$iconpanel = $this->renderIcon();
170
		$titlepanel = $this->renderTitlePanel();
171
172
		$this->_panel_header_title = $iconpanel . '&nbsp' . '<b>' . $titlepanel . '</b>';
173
	}
174
175
	/**
176
	 * Run the widget.
177
	 * If you override this method, make sure you call the parent implementation first.
178
	 */
179
	public function run()
180
	{
181
		if (!empty($this->_optionsFilter['id_grid']) && !empty($this->_optionsFilter['id_button']) && !empty($this->_optionsFilter['route_filter'])) {
182
			$id_grid = $this->_optionsFilter['id_grid'];
183
			$id_button = $this->_optionsFilter['id_button'];
184
			$route_filter = $this->_optionsFilter['route_filter'];
185
			$this->view->registerJs("filterGridSelect('#$id_grid', '#$id_button', '$route_filter', '$this->_messageFilter');");
186
		}
187
188
		if ($this->_registerJs) {
189
			$toolbar = new ToolBarAsset();
190
			$toolbar->register($this->view);
191
		}
192
193
		echo $this->renderPanelHeader() . $this->renderPanelBar();
194
	}
195
196
	private function renderButtonExport()
197
	{
198
		$button_export = '';
199
		if ($this->_button_export) {
200
			$button_export = ButtonDropdown::widget([
201
				'buttonOptions' => ['class' => 'btn-primary ai-c mL-2'],
202
				'encodeLabel' => $this->_encodeLabel,
203
				'label' => Html::tag('i', '', ['class' => 'fas fa-file-export']),
204
				'options' => ['class' => 'dropleft'],
205
				'dropdown' => [
206
					'encodeLabels' => $this->_encodeLabel,
207
					'items' => [
208
						Html::tag('h6', \yii::t('toolbar', 'Export Menu'), ['class' => 'dropdown-header']),
209
						Html::tag('div', '', ['class' => 'dropdown-divider']),
210
						[
211
							'label' => Html::tag('i', '', ['class' => 'fas fa-file']) . ' ' . 'Csv',
212
							'url' => Url::current(['export']),
213
							'linkOptions' => ['data' => ['method' => 'post', 'params' => ['actionExport' => 'Csv']]],
214
						],
215
						[
216
							'label' => Html::tag('i', '', ['class' => 'fas fa-file-excel']) . ' ' . 'Excel',
217
							'url' => Url::current(['export']),
218
							'linkOptions' => ['data' => ['method' => 'post', 'params' => ['actionExport' => 'Excel']]],
219
						],
220
						[
221
							'label' => Html::tag('i', '', ['class' => 'fas fa-file-code']) . ' ' . 'Html',
222
							'url' => Url::current(['export']),
223
							'linkOptions' => ['data' => ['method' => 'post', 'params' => ['actionExport' => 'Html']]],
224
						],
225
						[
226
							'label' => Html::tag('i', '', ['class' => 'fas fa-file-pdf']) . ' ' . 'Pdf',
227
							'url' => Url::current(['export']),
228
							'linkOptions' => ['data' => ['method' => 'post', 'params' => ['actionExport' => 'Pdf']]],
229
						],
230
						[
231
							'label' => Html::tag('i', '', ['class' => 'fas fa-file-word']) . ' ' . 'Word',
232
							'url' => Url::current(['export']),
233
							'linkOptions' => ['data' => ['method' => 'post', 'params' => ['actionExport' => 'Word']]],
234
						],
235
					],
236
				],
237
			]);
238
		}
239
240
		return $button_export;
241
	}
242
243
	private function renderButtonPages()
244
	{
245
		$button_pages = '';
246
247
		if ($this->_button_pages) {
248
			$button_pages = ButtonDropdown::widget([
249
				'buttonOptions' => ['class' => 'btn-primary ai-c mL-2'],
250
				'encodeLabel' => $this->_encodeLabel,
251
				'label' => Html::tag('i', '', ['class' => 'fas fa-cog']),
252
				'options' => ['class' => 'float-right'],
253
				'dropdown' => [
254
					'items' => [
255
						Html::tag('h6', \yii::t('toolbar', 'Page Size Menu'), ['class' => 'dropdown-header']),
256
						Html::tag('div', '', ['class' => 'dropdown-divider']),
257
						['label' => '1', 'url' => Url::current(['index', 'page' => 1, 'per-page' => '1'])],
258
						['label' => '5', 'url' => Url::current(['index', 'page' => 1, 'per-page' => '5'])],
259
						['label' => '10', 'url' => Url::current(['index', 'page' => 1, 'per-page' => '10'])],
260
						['label' => '20', 'url' => Url::current(['index', 'page' => 1, 'per-page' => '20'])],
261
						['label' => '25', 'url' => Url::current(['index', 'page' => 1, 'per-page' => '25'])],
262
						['label' => '50', 'url' => Url::current(['index', 'page' => 1, 'per-page' => '50'])],
263
					],
264
				],
265
			]);
266
		}
267
268
		return $button_pages;
269
	}
270
271
	private function renderIcon()
272
	{
273
		return Html::tag($this->_tag_icon_panel, $this->_label_icon_panel, $this->_options_icon_panel);
274
	}
275
276
	private function renderPanelBar()
277
	{
278
		$panel_button = Html::begintag($this->_tag_container_panel_button, $this->_options_container_panel_button) .
279
							$this->renderToolBar() .
280
						Html::endTag($this->_tag_container_panel_button);
281
		return $panel_button;
282
	}
283
284
	private function renderPanelHeader()
285
	{
286
		$pageSize = \yii::$app->params['defaultPageSize'];
287
288
		if (\yii::$app->session->get('per-page') !== 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

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