-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatpickr.php
142 lines (119 loc) · 3.48 KB
/
Flatpickr.php
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
<?php
/**
* Class Flatpickr
* @package ommu\flatpickr
*
* @author Putra Sudaryanto <[email protected]>
* @contact (+62)856-299-4114
* @copyright Copyright (c) 2020 OMMU (www.ommu.id)
* @created date 11 August 2020, 14:29 WIB
* @link https://github.com/ommu/yii2-flatpickr
*
*/
namespace ommu\flatpickr;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
use ommu\flatpickr\FlatpickrAsset;
use ommu\flatpickr\FlatpickrPluginAsset;
class Flatpickr extends \yii\widgets\InputWidget
{
/**
* @var array the HTML attributes for the form input
*/
public $options = ['class' => 'form-control'];
/**
* {@inheritdoc}
*/
public $pluginName = 'flatpickr';
/**
* {@inheritdoc}
*/
public $theme;
/**
* {@inheritdoc}
*/
public $placeholder;
/**
* @var array Flatpickr options
* @link https://flatpickr.js.org/options/
*/
public $clientOptions = [];
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// add data-toggle attribute
$this->options = ArrayHelper::merge($this->options, ['data-toggle' => $this->pluginName]);
// add placeholder attribute
if (isset($this->placeholder)) {
$this->options = ArrayHelper::merge($this->options, ['placeholder' => $this->placeholder]);
}
if (is_array($this->clientOptions) && !empty($this->clientOptions)) {
$this->options = ArrayHelper::merge($this->options, $this->getAttributeData($this->clientOptions));
}
// set value from query params
$queryParams = Yii::$app->request->queryParams;
if (is_array($queryParams) && !empty($queryParams) && array_key_exists($this->getInputId(), $queryParams) && $queryParams != '') {
$this->options = ArrayHelper::merge($this->options, ['value' => Yii::$app->request->get($this->getInputId())]);
}
}
/**
* {@inheritdoc}
*/
public function run()
{
$this->registerAssets();
$this->renderContent();
}
/**
* Return input id
*
* @return string
*/
public function getInputId(): string
{
return $this->options['id'];
}
/**
* {@inheritdoc}
*/
public function getAttributeData($data): array
{
foreach ($data as $key => $val) {
if ($val == '') {
continue;
}
$newKey = join('-', ['data', $this->pluginName, Inflector::camel2id($key)]);
if (is_bool($val)) {
$val = $val == true ? 'true' : 'false';
}
$data[$newKey] = $val;
unset($data[$key]);
}
return $data;
}
/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
FlatpickrAsset::register($view);
$flatpickrPluginAsset = FlatpickrPluginAsset::register($view);
$view->registerCssFile(join('/', [$flatpickrPluginAsset->baseUrl, 'themes', ($this->theme ? $this->theme : 'airbnb')]).'.css', ['depends' => [FlatpickrPluginAsset::className()]]);
}
/**
* Renders widget
*/
private function renderContent()
{
$options = ArrayHelper::merge($this->options, ['id' => $this->id]);
echo $this->hasModel()
? Html::activeTextInput($this->model, $this->attribute, $options)
: Html::textInput($this->name, $this->value, $options);
}
}