-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBasePicker.php
More file actions
269 lines (229 loc) · 7.63 KB
/
Copy pathBasePicker.php
File metadata and controls
269 lines (229 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace bootui\datetimepicker;
use yii\widgets\InputWidget;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use moonland\helpers\JSON;
/**
* BasePicker widgets
* @author Moh Khoirul Anam <moh.khoirul.anaam@gmail.com>
* @copyright moonlandsoft 2015
* @since 1
*
*/
class BasePicker extends InputWidget
{
// SIZE Input
const SIZE_LARGE = 'lg';
const SIZE_SMALL = 'sm';
// Datepicker Type
const TYPE_RANGE = 'range';
const TYPE_EMBEDDED = 'embedded';
const TYPE_COMP_LEFT = 'comp_left';
const TYPE_COMP_RIGHT = 'comp_right';
// Datepicker Min View Mode
const VIEW_DAYS = 0;
const VIEW_MONTHS = 1;
const VIEW_YEARS = 2;
// Datepicker Start View
const START_MONTH = 0;
const START_YEAR = 1;
const START_DECADE = 2;
public $data = [];
/**
* @var string type datepicker
*/
public $type;
/**
* @var string datepicker size
*/
public $size;
/**
* @var array bootstrap addon :
* - append
* - prepend
*/
public $addon = [];
protected $hasGroup = false;
/**
* @var string sparator in range
*/
public $sparator = 'to';
public $groupOptions = [
'class' => 'input-group',
];
/**
* @var array config datepicker
*/
protected $clientOptions = [
//'useCurrent' => false,
];
public function __set($name, $value)
{
if (isset($this->{$name}))
parent::__set($name, $value);
else
$this->clientOptions[$name] = $value;
}
public function init()
{
if (!empty($this->addon))
$this->hasGroup = true;
if (!$this->hasModel() && $this->name === null) {
throw new InvalidConfigException("Either 'name', or 'model' and 'attribute' properties must be specified.");
}
if (isset($this->type) && $this->type == static::TYPE_RANGE) {
$this->groupOptions['id'] = $this->type . '-' . $this->getId();
Html::addCssClass($this->groupOptions, 'input-daterange');
Html::addCssClass($this->options[0], 'form-control');
Html::addCssClass($this->options[1], 'form-control');
} else {
if (!isset($this->options['id'])) {
$this->options['id'] = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->getId();
}
if (isset($this->type) && ($this->type == static::TYPE_COMP_LEFT || $this->type == static::TYPE_COMP_RIGHT)) {
Html::addCssClass($this->groupOptions, 'date');
$this->groupOptions['id'] = $this->type . '-' . $this->options['id'] . '-' . $this->getId();
}
Html::addCssClass($this->options, 'form-control');
}
}
public function run()
{
// To set pick the date or time
foreach ($this->data as $option=>$value) {
$this->options['data-' . $option] = $value;
}
if ($this->type == self::TYPE_RANGE) {
$this->registerPlugin($this->groupOptions['id'] . '.input-daterange');
} elseif ($this->type == self::TYPE_COMP_LEFT || $this->type == self::TYPE_COMP_RIGHT) {
$this->registerPlugin($this->groupOptions['id'] . '.date');
} else
$this->registerPlugin($this->options['id']);
return $this->renderInput();
}
protected function renderInput()
{
if (isset($this->clientOptions['inline']) && $this->clientOptions['inline']==true) {
Html::addCssClass($this->options, 'hidden');
}
if (isset($this->size) && in_array($this->size, ['sm','lg']))
{
Html::addCssClass($this->options, 'input-' . $this->size);
Html::addCssClass($this->groupOptions, 'input-group-' . $this->size);
}
if (isset($this->type) && $this->type == static::TYPE_RANGE) {
$this->hasGroup = true;
$input[] = $this->hasModel() ?
Html::activeTextInput($this->model, $this->attribute[0], $this->options[0]) :
Html::textInput($this->name[0], isset($this->value[0]) ? $this->value[0] : null, $this->options[0]);
$input[] = Html::tag('span', $this->sparator, ['class' => 'input-group-addon']);
$input[] = $this->hasModel() ?
Html::activeTextInput($this->model, $this->attribute[1], $this->options[1]) :
Html::textInput($this->name[1], isset($this->value[1]) ? $this->value[1] : null, $this->options[1]);
$input = implode('', $input);
}
elseif (isset($this->type) && ($this->type == static::TYPE_COMP_LEFT || $this->type == static::TYPE_COMP_RIGHT)) {
$this->hasGroup = true;
if ($this->type == static::TYPE_COMP_LEFT)
$input[] = Html::tag('span', '<i class="glyphicon glyphicon-calendar"></i>', ['class' => 'input-group-addon']);
$input[] = $this->hasModel() ?
Html::activeTextInput($this->model, $this->attribute, $this->options) :
Html::textInput($this->name, $this->value, $this->options);
if ($this->type == static::TYPE_COMP_RIGHT)
$input[] = Html::tag('span', '<i class="glyphicon glyphicon-calendar"></i>', ['class' => 'input-group-addon']);
$input = implode('', $input);
}
else {
$input = $this->hasModel() ?
Html::activeTextInput($this->model, $this->attribute, $this->options) :
Html::textInput($this->name, $this->value, $this->options);
}
if ($this->hasGroup)
$input = Html::tag('div', $this->prepareAddon($input), $this->groupOptions);
return $input;
}
protected function prepareAddon($input)
{
extract($this->addon);
$template = "{prepend}\n{input}\n{append}";
$appendContent = [];
$prependContent = [];
if (isset($append)) {
if (is_array($append)) {
if (is_string($append[0]) && is_bool($append[1])) {
if ($append[1] == true)
$appendContent[] = Html::tag('span', $append[0], ['class' => 'input-group-btn']);
} else {
foreach ($append as $content) {
$appendContent[] = $this->prepareContent($content);
}
}
} else {
$appendContent[] = Html::tag('span', $append, ['class' => 'input-group-addon']);
}
}
if (isset($prepend)) {
if (is_array($prepend)) {
if (is_string($prepend[0]) && is_bool($prepend[1])) {
if ($prepend[1] == true)
$prependContent[] = Html::tag('span', $prepend[0], ['class' => 'input-group-btn']);
} else {
foreach ($prepend as $content) {
$prependContent[] = $this->prepareContent($content);
}
}
} else {
$prependContent[] = Html::tag('span', $prepend, ['class' => 'input-group-addon']);
}
}
return strtr($template, [
'{prepend}' => implode('', $prependContent),
'{input}' => $input,
'{append}' => implode('', $appendContent),
]);
}
protected function prepareContent($content)
{
if (is_array($content)) {
if (!isset($content['class']) && isset($content[0]))
$content['class'] = ArrayHelper::remove($content, 0);
if (!isset($content['options']) && isset($content[1])) {
if (is_bool($content[1]))
$content['asButton'] = ArrayHelper::remove($content, 1);
else
$content['options'] = ArrayHelper::remove($content, 1);
}
if (!isset($content['asButton']))
$content['asButton'] = ArrayHelper::remove($content, 2, false);
$class = $content['class'];
$options = isset($content['options']) ? $content['options'] : [];
$asButton = $content['asButton'];
if ($asButton)
Html::addCssClass($tagOptions, 'input-group-btn');
else
Html::addCssClass($tagOptions, 'input-group-addon');
if (class_exists($class)) {
$inContent = $class::widget($options);
} else {
$inContent = $class;
}
$content = Html::tag('span', $inContent, $tagOptions);
} else {
if ($content instanceof Widget)
Html::addCssClass($tagOptions, 'input-group-btn');
else
Html::addCssClass($tagOptions, 'input-group-addon');
$content = Html::tag('span', $content, $tagOptions);
}
return $content;
}
protected function registerPlugin($selector)
{
$view = $this->getView();
DatepickerPlugin::register($view);
$options = JSON::encode($this->clientOptions);
$js = "jQuery('#$selector').datetimepicker({$options});";
$view->registerJs($js);
}
}