Passed
Push — master ( 745d5e...16e49b )
by WILMER
01:29
created

ToolBar::renderButtonExport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 0
dl 0
loc 25
rs 9.7
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\ArrayHelper;
21
use yii\helpers\Html;
22
use yii\helpers\Url;
23
24
class ToolBar extends Widget
25
{
26
	/**
27
	 * @var string the tag to use to render panel title
28
	 */
29
	public $_tag_container_panel_header = 'div';
30
31
	/**
32
	 * @var array the options to use to render panel title
33
	 */
34
	public $_options_container_panel_header = ['class' => 'peers bg-primary text-white align-content-center p-15'];
35
36
	/**
37
	 * @var string the tag to use to render panel title left
38
	 */
39
	public $_tag_left_panel_header = 'div';
40
41
	/**
42
	 * @var array the options to use to render panel title left
43
	 */
44
	public $_options_left_panel_header = ['class' => 'float-left'];
45
46
	/**
47
	 * @var string the tag to use to render panel title rigth
48
	 */
49
	public $_tag_rigth_panel_header = 'div';
50
51
	/**
52
	 * @var array the options to use to render panel title rigth
53
	 */
54
	public $_options_rigth_panel_header = ['class' => 'float-right ml-auto'];
55
56
	/**
57
	 * @var string use to panel header title
58
	 */
59
	public $_panel_header_title = '';
60
61
	/**
62
	 * @var string the tag to use to render panel title icon
63
	 */
64
	public $_tag_icon_panel = 'i';
65
66
	/**
67
	 * @var string the label to use to render panel title icon
68
	 */
69
	public $_label_icon_panel = '';
70
71
	/**
72
	 * @var array the options to use to render panel title icons
73
	 */
74
	public $_options_icon_panel = ['class' => 'fas fa-th'];
75
76
	/**
77
	 * @var string the label panel title
78
	 */
79
	public $_title_panel = '';
80
81
	/**
82
	 * @var bool Show/Hidden Summary Label
83
	 */
84
	public $_summary = false;
85
86
	/**
87
	 * @var string the tag to use to render container panel button
88
	 */
89
	public $_tag_container_panel_button = 'div';
90
91
	/**
92
	 * @var array the options to use to render container panel button
93
	 */
94
	public $_options_container_panel_button = ['class' => 'peers bd p-15'];
95
96
	/**
97
	 * @var string the tag to use to render panel button left
98
	 */
99
	public $_tag_left_panel_button = 'div';
100
101
	/**
102
	 * @var array the options to use to render panel button left
103
	 */
104
	public $_options_left_panel_button = ['class' => 'float-left'];
105
106
	/**
107
	 * @var string the tag to use to render panel button rigth
108
	 */
109
	public $_tag_rigth_panel_button = 'div';
110
111
	/**
112
	 * @var array the options to use to render panel button rigth
113
	 */
114
	public $_options_rigth_panel_button = ['class' => 'float-right ml-auto'];
115
116
	/**
117
	 * @var array Templates
118
	 */
119
	public $_templates = [];
120
121
	/**
122
	 * @var array Buttons
123
	 */
124
	public $_toolbar = [];
125
126
	/**
127
	 * @var bool show/hidden panel button pages
128
	 */
129
	public $_button_pages = false;
130
131
	/**
132
	 * @var bool show/hidden panel button export
133
	 */
134
	public $_button_export = false;
135
136
137
	/**
138
	 * @var bool whether the label should be HTML-encoded.
139
	 */
140
	public $_encodeLabel = false;
141
142
	/**
143
	 * Initializes the widget.
144
	 * If you override this method, make sure you call the parent implementation first.
145
	 */
146
	public function init()
147
	{
148
		parent::init();
149
150
		$iconpanel = $this->renderIcon();
151
		$titlepanel = $this->renderTitlePanel();
152
		$this->_panel_header_title = $iconpanel . '&nbsp' . '<b>' . $titlepanel . '</b>';
153
154
		echo $this->renderPanelHeader() . $this->renderPanelBar();
155
	}
156
157
	private function renderButtonExport()
158
	{
159
		$button_export = '';
160
161
		if ($this->_button_export) {
162
			$button_export = ButtonDropdown::widget([
163
				'buttonOptions' => ['class' => 'btn-primary ai-c mL-2'],
164
				'encodeLabel' => $this->_encodeLabel,
165
				'label' => Html::tag('i', '', ['class' => 'fas fa-file-export']),
166
				'options' => ['class' => 'float-right'],
167
				'dropdown' => [
168
					'encodeLabels' => $this->_encodeLabel,
169
					'items' => [
170
						Html::tag('h6', \yii::t('toolbar', 'Export Menu'), ['class' => 'dropdown-header']),
171
						Html::tag('div', '', ['class' => 'dropdown-divider']),
172
						['label' => Html::tag('i', '', ['class' => 'fas fa-file-code']) . ' ' . 'CSV', 'url' => Url::current()],
173
						['label' => Html::tag('i', '', ['class' => 'fas fa-file-excel']) . ' ' . 'EXCEL', 'url' => Url::current()],
174
						['label' => Html::tag('i', '', ['class' => 'fas fa-file-pdf']) . ' ' . 'PDF', 'url' => Url::current()],
175
						['label' => Html::tag('i', '', ['class' => 'fas fa-file-word']) . ' ' . 'WORD', 'url' => Url::current()],
176
					],
177
				],
178
			]);
179
		}
180
181
		return $button_export;
182
	}
183
184
	private function renderButtonPages()
185
	{
186
		$button_pages = '';
187
188
		if ($this->_button_pages) {
189
			$button_pages = ButtonDropdown::widget([
190
				'buttonOptions' => ['class' => 'btn-primary ai-c mL-2'],
191
				'encodeLabel' => $this->_encodeLabel,
192
				'label' => Html::tag('i', '', ['class' => 'fas fa-cog']),
193
				'options' => ['class' => 'float-right'],
194
				'dropdown' => [
195
					'items' => [
196
						Html::tag('h6', \yii::t('toolbar', 'Page Size Menu'), ['class' => 'dropdown-header']),
197
						Html::tag('div', '', ['class' => 'dropdown-divider']),
198
						['label' => '1', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '1'])],
199
						['label' => '5', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '5'])],
200
						['label' => '10', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '10'])],
201
						['label' => '20', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '20'])],
202
						['label' => '25', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '25'])],
203
						['label' => '50', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '50'])],
204
					],
205
				],
206
			]);
207
		}
208
209
		return $button_pages;
210
	}
211
212
	private function renderIcon()
213
	{
214
		return Html::tag($this->_tag_icon_panel, $this->_label_icon_panel, $this->_options_icon_panel);
215
	}
216
217
	private function renderPanelBar()
218
	{
219
		$panel_button = Html::begintag($this->_tag_container_panel_button, $this->_options_container_panel_button) .
220
							$this->renderToolBar() .
221
						Html::endTag($this->_tag_container_panel_button);
222
		return $panel_button;
223
	}
224
225
	private function renderPanelHeader()
226
	{
227
		$pageSize = \yii::$app->params['defaultPageSize'];
228
229
		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

229
		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...
230
			$pageSize = \yii::$app->session->get('pageSize');
231
		}
232
233
		$summary = ($this->_summary) ? '{summary}' . ' ' . 'Records per pages: ' . '<b>' . $pageSize . '</b>' : ' ';
234
235
		$panel_header = Html::begintag($this->_tag_container_panel_header, $this->_options_container_panel_header) .
236
							Html::begintag($this->_tag_left_panel_header, $this->_options_left_panel_header) .
237
								$this->_panel_header_title .
238
							Html::endTag($this->_tag_left_panel_header) .
239
							Html::begintag($this->_tag_rigth_panel_header, $this->_options_rigth_panel_header) .
240
								$summary .
241
							Html::endTag($this->_tag_rigth_panel_header) .
242
						Html::endTag($this->_tag_container_panel_header);
243
		return $panel_header;
244
	}
245
246
	private function renderTitlePanel()
247
	{
248
		if (empty($this->_title_panel)) {
249
			$this->_title_panel = \yii::t('toolbar', 'Gridview ToolBar');
250
		}
251
252
		return $this->_title_panel;
253
	}
254
255
	private function renderToolBar()
256
	{
257
		$buttons_left = '';
258
		$buttons_rigth = '';
259
260
		ArrayHelper::setValue($this->_toolbar, 'pages.0', $this->renderButtonPages());
261
		ArrayHelper::setValue($this->_toolbar, 'export.0', $this->renderButtonExport());
262
263
		foreach ($this->_templates as $items => $buttons) {
264
			foreach ($buttons as $item => $button) {
265
				if (!empty(ArrayHelper::getValue($this->_toolbar, $button, ''))) {
266
					switch ($items) {
267
						case 'left':
268
							$buttons_left .= implode(',', ArrayHelper::getValue($this->_toolbar, $button, ''));
269
							break;
270
						case 'rigth':
271
							$buttons_rigth .= implode(',', ArrayHelper::getValue($this->_toolbar, $button, ''));
272
							break;
273
					}
274
				}
275
			}
276
		}
277
278
		return Html::tag($this->_tag_left_panel_button, $buttons_left, $this->_options_left_panel_button) .
279
			   Html::tag($this->_tag_rigth_panel_button, $buttons_rigth, $this->_options_rigth_panel_button);
280
	}
281
}
282