Completed
Push — master ( 495558...30e492 )
by Vladymyr
03:15
created

Flatpickr::renderContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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
    /**
29
     * @var array
30
     */
31
    public $defaultOptions = [
32
        'allowInput' => true
33
    ];
34
35
    /**
36
     * @var string
37
     */
38
    private $_locale;
39
40
    /**
41
     * @var string
42
     */
43
    public $theme;
44
45
    /**
46
     * @var string
47
     */
48
    private $_id;
49
50
    /**
51
     * @inheritdoc
52
     */
53 2
    public function init()
54
    {
55 2
        parent::init();
56
57 2
        if (isset($this->clientOptions['locale'])) {
58 1
            $this->_locale = $this->clientOptions['locale'];
59
        } else {
60 1
            $language = explode('-', \Yii::$app->language);
61 1
            $this->_locale = $language[0];
62
        }
63 2
    }
64
65
    /**
66
     * @inheritdoc
67
     * @return string
68
     */
69 2
    public function run()
70
    {
71 2
        parent::run();
72
73 2
        $this->registerClientScript();
74
75 2
        $content = $this->renderContent();
76
77 2
        return $content;
78
    }
79
80
    /**
81
     * Registers client scripts.
82
     */
83 2
    private function registerClientScript()
84
    {
85 2
        $this->_id = $this->options['id'];
86 2
        $view = $this->getView();
87
88 2
        $config = Json::encode(ArrayHelper::merge($this->defaultOptions, $this->clientOptions));
89
90 2
        $asset = FlatpickrAsset::register($view);
91 2
        $asset->locale = $this->_locale;
92 2
        $asset->theme = $this->theme;
93
94 2
        $view->registerJs("flatpickr('#{$this->_id}', {$config});");
95 2
    }
96
97
    /**
98
     * Renders widget
99
     * @return string
100
     */
101 2
    private function renderContent()
102
    {
103 2
        $options = ArrayHelper::merge(['class' => 'form-control', 'data-input' => true], $this->options);
104 2
        $content = $this->hasModel()
105 1
            ? Html::activeTextInput($this->model, $this->attribute, $options)
106 2
            : Html::textInput($this->name, $this->value, $options);
107
108 2
        return $content;
109
    }
110
}
111