1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/codenix-sv/yii2-flatpickr |
4
|
|
|
* @copyright Copyright (c) 2017 codenix-sv |
5
|
|
|
* @license https://github.com/codenix-sv/yii2-flatpickr/blob/master/LICENSE |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace codenixsv\flatpickr; |
9
|
|
|
|
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
use yii\helpers\Html; |
13
|
|
|
use yii\widgets\InputWidget; |
14
|
|
|
use codenixsv\flatpickr\assets\FlatpickrAsset; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Flatpickr |
18
|
|
|
* @package codenixsv\flatpickr |
19
|
|
|
*/ |
20
|
|
|
class Flatpickr extends InputWidget |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @link https://chmln.github.io/flatpickr/options/ Flatpickr options |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public $clientOptions = []; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
public $defaultOptions = [ |
30
|
|
|
'allowInput' => true |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $_locale; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
public $theme; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
private $_id; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
2 |
|
public function init() |
46
|
|
|
{ |
47
|
2 |
|
parent::init(); |
48
|
|
|
|
49
|
2 |
|
if (isset($this->clientOptions['locale'])) { |
50
|
1 |
|
$this->_locale = $this->clientOptions['locale']; |
51
|
|
|
} else { |
52
|
1 |
|
$language = explode('-', \Yii::$app->language); |
53
|
1 |
|
$this->_locale = $language[0]; |
54
|
|
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
2 |
|
public function run(): string |
61
|
|
|
{ |
62
|
2 |
|
parent::run(); |
63
|
|
|
|
64
|
2 |
|
$this->registerClientScript(); |
65
|
|
|
|
66
|
2 |
|
return $this->renderContent(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Registers client scripts. |
71
|
|
|
*/ |
72
|
2 |
|
private function registerClientScript() |
73
|
|
|
{ |
74
|
2 |
|
$this->_id = $this->options['id']; |
75
|
2 |
|
$view = $this->getView(); |
76
|
|
|
|
77
|
2 |
|
$config = Json::encode(ArrayHelper::merge($this->defaultOptions, $this->clientOptions)); |
78
|
|
|
|
79
|
2 |
|
$asset = FlatpickrAsset::register($view); |
80
|
2 |
|
$asset->locale = $this->_locale; |
81
|
2 |
|
$asset->theme = $this->theme; |
82
|
|
|
|
83
|
2 |
|
$view->registerJs("flatpickr('#{$this->_id}', {$config});"); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Renders widget |
88
|
|
|
*/ |
89
|
2 |
|
private function renderContent(): string |
90
|
|
|
{ |
91
|
2 |
|
$options = ArrayHelper::merge(['class' => 'form-control', 'data-input' => true], $this->options); |
92
|
|
|
|
93
|
2 |
|
return $this->hasModel() |
94
|
1 |
|
? Html::activeTextInput($this->model, $this->attribute, $options) |
95
|
2 |
|
: Html::textInput($this->name, $this->value, $options); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|