DateRangePicker::init()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 3
nop 0
crap 6
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\base\InvalidConfigException;
10
use yii\helpers\Html;
11
use yii\helpers\Json;
12
use yii\widgets\InputWidget;
13
14
/**
15
 * DateRangePicker renders a DatePicker range input.
16
 *
17
 * @author Antonio Ramirez <[email protected]>
18
 * @link http://www.ramirezcobos.com/
19
 * @link http://www.2amigos.us/
20
 * @package dosamigos\datepicker
21
 */
22
class DateRangePicker extends InputWidget
23
{
24
    use DatePickerTrait;
25
26
    /**
27
     * @var string the attribute name for date range (to Date)
28
     */
29
    public $attributeTo;
30
    /**
31
     * @var string the name for date range (to Date)
32
     */
33
    public $nameTo;
34
    /**
35
     * @var string the value for date range (to Date value)
36
     */
37
    public $valueTo;
38
    /**
39
     * @var array HTML attributes for the date to input
40
     */
41
    public $optionsTo;
42
    /**
43
     * @var string the label to. Defaults to 'to'.
44
     */
45
    public $labelTo = 'to';
46
    /**
47
     * @var \yii\widgets\ActiveForm useful for client validation of attributeTo
48
     */
49
    public $form;
50
    /**
51
     * @var string the template to render. Used internally.
52
     */
53
    protected $_template = '{inputFrom}<span class="input-group-addon">{labelTo}</span>{inputTo}';
54
55
56
    /**
57
     * @inheritdoc
58
     * @throws \yii\base\InvalidConfigException
59
     */
60 12
    public function init()
61
    {
62 12
        parent::init();
63 9
        if ((!$this->hasModel() && $this->nameTo === null) || ($this->hasModel() && $this->attributeTo === null)) {
64
            // @codeCoverageIgnoreStart
65
            throw new InvalidConfigException("Either 'nameTo', or 'model' and 'attributeTo' properties must be specified.");
66
            // @codeCoverageIgnoreEnd
67
        }
68 9
        if ($this->size) {
69 3
            Html::addCssClass($this->options, 'input-' . $this->size);
70 3
            Html::addCssClass($this->optionsTo, 'input-' . $this->size);
71 3
            Html::addCssClass($this->containerOptions, 'input-group-' . $this->size);
72 3
        }
73 9
        Html::addCssClass($this->containerOptions, 'input-group input-daterange');
74 9
        Html::addCssClass($this->options, 'form-control');
75 12
        Html::addCssClass($this->optionsTo, 'form-control');
76 9
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 9
    public function run()
82
    {
83 9
        if ($this->form) {
84
            Html::addCssClass($this->options, 'datepicker-from');
85
            Html::addCssClass($this->optionsTo, 'datepicker-to');
86
            $inputFrom = $this->form->field(
87
                $this->model,
88
                $this->attribute,
89
                [
90
                    'template' => '{input}{error}',
91
                    'options' => ['class' => 'input-group datepicker-range'],
92
                ]
93
            )->textInput($this->options);
94
            $inputTo = $this->form->field(
95
                $this->model,
96
                $this->attributeTo,
97
                [
98
                    'template' => '{input}{error}',
99
                    'options' => ['class' => 'input-group datepicker-range'],
100
                ]
101
            )->textInput($this->optionsTo);
102
        } else {
103 9
            $inputFrom = $this->hasModel()
104 9
                ? Html::activeTextInput($this->model, $this->attribute, $this->options)
105 9
                : Html::textInput($this->name, $this->value, $this->options);
106 9
            $inputTo = $this->hasModel()
107 9
                ? Html::activeTextInput($this->model, $this->attributeTo, $this->optionsTo)
108 9
                : Html::textInput($this->nameTo, $this->valueTo, $this->optionsTo);
109
        }
110 9
        echo Html::tag(
111 9
            'div',
112 9
            strtr(
113 9
                $this->_template,
114 9
                ['{inputFrom}' => $inputFrom, '{labelTo}' => $this->labelTo, '{inputTo}' => $inputTo]
115 9
            ), $this->containerOptions);
116
117 9
        $this->registerClientScript();
118 9
    }
119
120
    /**
121
     * Registers required script for the plugin to work as DateRangePicker
122
     */
123
    public function registerClientScript()
124
    {
125
        $js = [];
126
        $view = $this->getView();
127
128
        // @codeCoverageIgnoreStart
129
        if($this->language !== null) {
130
            $this->clientOptions['language'] = $this->language;
131
            DatePickerLanguageAsset::register($view)->js[] = 'bootstrap-datepicker.' . $this->language . '.min.js';
132
        } else {
133
            DateRangePickerAsset::register($view);
134
        }
135
        // @codeCoverageIgnoreEnd
136
137
        $id = $this->options['id'];
138
        $selector = ";jQuery('#$id').parent()";
139
        if($this->form && $this->hasModel()) {
140
            // @codeCoverageIgnoreStart
141
            $selector .= '.parent()';
142
            $class = "field-" . Html::getInputId($this->model, $this->attribute);
143
            $js[] = "$selector.closest('.$class').removeClass('$class');";
144
            // @codeCoverageIgnoreEnd
145
        }
146
147
        $options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
148
149
        $js[] = "$selector.datepicker($options);";
150
151
        // @codeCoverageIgnoreStart
152
        if (!empty($this->clientEvents)) {
153
            foreach ($this->clientEvents as $event => $handler) {
154
                $js[] = "$selector.on('$event', $handler);";
155
            }
156
        }
157
        // @codeCoverageIgnoreEnd
158
        $view->registerJs(implode("\n", $js));
159
    }
160
161
}
162