Passed
Push — master ( 6f45f7...4b0928 )
by WILMER
01:18
created

ToolBar::renderPanelBar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
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\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 = true;
130
131
	/**
132
	 * @var bool whether the label should be HTML-encoded.
133
	 */
134
	public $_encodeLabel = true;
135
136
137
	/**
138
	 * Initializes the widget.
139
	 * If you override this method, make sure you call the parent implementation first.
140
	 */
141
	public function init()
142
	{
143
		parent::init();
144
145
		$iconpanel = $this->renderIcon();
146
		$titlepanel = $this->renderTitlePanel();
147
		$this->_panel_header_title = $iconpanel . '&nbsp' . '<b>' . $titlepanel . '</b>';
148
149
		echo $this->renderPanelHeader() . $this->renderPanelBar();
150
	}
151
152
	private function renderToolBar()
153
	{
154
		$buttons_left = '';
155
		$buttons_rigth = '';
156
157
		ArrayHelper::setValue($this->_toolbar, 'pages.0', $this->renderButtonPages());
158
159
		foreach ($this->_templates as $items => $buttons) {
160
			foreach ($buttons as $item => $button) {
161
				switch ($items) {
162
					case 'left':
163
						if (!empty(ArrayHelper::getValue($this->_toolbar, $button, ''))) {
164
							$buttons_left .= implode(',', ArrayHelper::getValue($this->_toolbar, $button, ''));
165
						}
166
						break;
167
					case 'rigth':
168
						if (!empty(ArrayHelper::getValue($this->_toolbar, $button, ''))) {
169
							$buttons_rigth .= implode(',', ArrayHelper::getValue($this->_toolbar, $button, ''));
170
						}
171
						break;
172
				}
173
			}
174
		}
175
176
		return Html::tag($this->_tag_left_panel_button, $buttons_left, $this->_options_left_panel_button) .
177
			   Html::tag($this->_tag_rigth_panel_button, $buttons_rigth, $this->_options_rigth_panel_button);
178
	}
179
180
	private function renderButtonPages()
181
	{
182
		$button_pages = '';
183
184
		if ($this->_button_pages) {
185
			$button_pages = ButtonDropdown::widget([
186
				'buttonOptions' => ['class' => 'btn-primary ai-c mL-2'],
187
				'label' => '',
188
				'options' => ['class' => 'float-right'],
189
				'dropdown' => [
190
					'items' => [
191
						['label' => '1', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '1'])],
192
						['label' => '5', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '5'])],
193
						['label' => '10', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '10'])],
194
						['label' => '20', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '20'])],
195
						['label' => '25', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '25'])],
196
						['label' => '50', 'url' => Url::current(['index', 'page' => 1, 'pageSize' => '50'])],
197
					],
198
				],
199
			]);
200
		}
201
202
		return $button_pages;
203
	}
204
205
	private function renderIcon()
206
	{
207
		return Html::tag($this->_tag_icon_panel, $this->_label_icon_panel, $this->_options_icon_panel);
208
	}
209
210
	private function renderPanelBar()
211
	{
212
		$panel_button = Html::begintag($this->_tag_container_panel_button, $this->_options_container_panel_button) .
213
							$this->renderToolBar() .
214
						Html::endTag($this->_tag_container_panel_button);
215
		return $panel_button;
216
	}
217
218
	private function renderPanelHeader()
219
	{
220
		$pageSize = \yii::$app->params['defaultPageSize'];
221
222
		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

222
		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...
223
			$pageSize = \yii::$app->session->get('pageSize');
224
		}
225
226
		$summary = ($this->_summary) ? '{summary}' . ' ' . 'Records per pages: ' . '<b>' . $pageSize . '</b>' : ' ';
227
228
		$panel_header = Html::begintag($this->_tag_container_panel_header, $this->_options_container_panel_header) .
229
							Html::begintag($this->_tag_left_panel_header, $this->_options_left_panel_header) .
230
								$this->_panel_header_title .
231
							Html::endTag($this->_tag_left_panel_header) .
232
							Html::begintag($this->_tag_rigth_panel_header, $this->_options_rigth_panel_header) .
233
								$summary .
234
							Html::endTag($this->_tag_rigth_panel_header) .
235
						Html::endTag($this->_tag_container_panel_header);
236
		return $panel_header;
237
	}
238
239
	private function renderTitlePanel()
240
	{
241
		if (empty($this->_title_panel)) {
242
			$this->_title_panel = \yii::t('toolbar', 'Gridview ToolBar');
243
		}
244
245
		return $this->_title_panel;
246
	}
247
}
248