Completed
Pull Request — master (#51)
by
unknown
12:49
created

DatePicker::init()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 8
nop 0
crap 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
4
 * @link http://2amigos.us
5
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
6
 */
7
namespace dosamigos\datepicker;
8
9
use yii\helpers\Html;
10
use yii\helpers\Json;
11
use yii\widgets\InputWidget;
12
13
/**
14
 * DatePicker renders a DatePicker input.
15
 *
16
 * @author Antonio Ramirez <[email protected]>
17
 * @link http://www.ramirezcobos.com/
18
 * @link http://www.2amigos.us/
19
 * @package dosamigos\datepicker
20
 */
21
class DatePicker extends InputWidget
22
{
23
    use DatePickerTrait;
24
25
    /**
26
     * @var string the addon markup if you wish to display the input as a component. If you don't wish to render as a
27
     * component then set it to null or false.
28
     */
29
    public $addon = '<i class="glyphicon glyphicon-calendar"></i>';
30
    /**
31
     * @var string the template to render the input.
32
     */
33
    public $template = '{input}{addon}';
34
    /**
35
     * @var bool whether to render the input as an inline calendar
36
     */
37
    public $inline = false;
38
39
    public $link = false;
40
41
    /**
42 18
     * @inheritdoc
43
     */
44 18
    public function init()
45
    {
46 18
        parent::init();
47 3
48 3
        if ($this->inline) {
49 3
            $this->options['readonly'] = 'readonly';
50 18
            Html::addCssClass($this->options, 'text-center');
51 3
        }
52 3
        if ($this->size) {
53 3
            Html::addCssClass($this->options, 'input-' . $this->size);
54 18
            Html::addCssClass($this->containerOptions, 'input-group-' . $this->size);
55 18
        }
56 18
        if (!$this->link) Html::addCssClass($this->options, 'form-control');
57
        Html::addCssClass($this->containerOptions, 'input-group date');
58
    }
59
60
    /**
61 15
     * @inheritdoc
62
     */
63
    public function run()
64 15
    {
65 15
66 15
        $input = $this->hasModel()
67
            ? Html::activeTextInput($this->model, $this->attribute, $this->options)
68 15
            : Html::textInput($this->name, $this->value, $this->options);
69 3
        
70 3
        if ($this->link) $input = Html::a($this->value, '#', $this->options);
71 15
72 12
        if ($this->inline) {
73 12
            $input .= '<div></div>';
74 12
        }
75 12
        if ($this->addon && !$this->inline) {
76 15
            $addon = Html::tag('span', $this->addon, ['class' => 'input-group-addon']);
77 3
            $input = strtr($this->template, ['{input}' => $input, '{addon}' => $addon]);
78 3
            $input = Html::tag('div', $input, $this->containerOptions);
79 15
        }
80
        if ($this->inline) {
81 15
            $input = strtr($this->template, ['{input}' => $input, '{addon}' => '']);
82 15
        }
83
        echo $input;
84
85
        $this->registerClientScript();
86
    }
87 15
88
    /**
89 15
     * Registers required script for the plugin to work as DatePicker
90 15
     */
91
    public function registerClientScript()
92
    {
93
        $js = [];
94
        $view = $this->getView();
95
96
        // @codeCoverageIgnoreStart
97
        if ($this->language !== null && $this->language !== 'en') {
98
            $this->clientOptions['language'] = $this->language;
99
            DatePickerLanguageAsset::register($view)->js[] = 'bootstrap-datepicker.' . $this->language . '.min.js';
100
        } else {
101 15
            DatePickerAsset::register($view);
102 15
        }
103
        
104 15
        // @codeCoverageIgnoreEnd
105 15
106 15
        $id = $this->options['id'];
107
        $selector = ";jQuery('#$id')";
108 15
109
        if ($this->addon || $this->inline) {
110 15
            $selector .= ".parent()";
111 3
        }
112 3
113
        $options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
114 15
115
        if ($this->inline) {
116 15
            $this->clientEvents['changeDate'] = "function (e){ jQuery('#$id').val(e.format());}";
117 3
        }
118 3
119 3
        $js[] = "$selector.datepicker($options);";
120 3
121 15
        if (!empty($this->clientEvents)) {
122 15
            foreach ($this->clientEvents as $event => $handler) {
123
                $js[] = "$selector.on('$event', $handler);";
124
            }
125
        }
126
        
127
        $view->registerJs(implode("\n", $js));
128
    }
129
130
}
131