DateTimePicker   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 5
dl 0
loc 145
ccs 54
cts 58
cp 0.931
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 19 3
B run() 0 26 5
C registerClientScript() 0 42 8
1
<?php
2
/**
3
 * @copyright Copyright (c) 2013-2017 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\datetimepicker;
8
9
use yii\helpers\ArrayHelper;
10
use yii\helpers\Html;
11
use yii\helpers\Json;
12
use yii\widgets\InputWidget;
13
14
/**
15
 * DateTimePicker renders a DateTimePicker input.
16
 * @see http://www.malot.fr/bootstrap-datetimepicker/
17
 *
18
 * @author Antonio Ramirez <[email protected]>
19
 * @link http://www.ramirezcobos.com/
20
 * @link http://www.2amigos.us/
21
 * @package common\extensions\widgets\datetimepicker
22
 */
23
class DateTimePicker extends InputWidget
24
{
25
    /**
26
     * @var string the language to use
27
     */
28
    public $language;
29
    /**
30
     * @var array the options for the Bootstrap DatePicker plugin.
31
     * Please refer to the Bootstrap DatePicker plugin Web page for possible options.
32
     * @see http://bootstrap-datepicker.readthedocs.org/en/release/options.html
33
     */
34
    public $clientOptions = [];
35
    /**
36
     * @var array the event handlers for the underlying Bootstrap Switch 3 input JS plugin.
37
     * Please refer to the [DatePicker](http://bootstrap-datepicker.readthedocs.org/en/release/events.html) plugin
38
     * Web page for possible events.
39
     */
40
    public $clientEvents = [];
41
    /**
42
     * @var string the size of the input ('lg', 'md', 'sm', 'xs')
43
     */
44
    public $size;
45
    /**
46
     * @var array HTML attributes to render on the container if its used as a component.
47
     */
48
    public $containerOptions = [];
49
    /**
50
     * @var string the template to render the input. By default, renders as a component, you can render a simple
51
     * input field without pickup and/or reset buttons by modifying the template to `{input}`. `{button}` must exist for
52
     * a component type of datepicker. The following template is invalid `{input}{reset}` and will be treated as `{input}`
53
     */
54
    public $template = "{input}{reset}{button}";
55
    /**
56
     * @var string the icon to use on the reset button
57
     */
58
    public $resetButtonIcon = 'glyphicon glyphicon-remove';
59
    /**
60
     * @var string the icon to use on the pickup button. Defaults to `glyphicon-th`. Other uses are `glyphicon-time` and
61
     * `glyphicon-calendar`.
62
     */
63
    public $pickButtonIcon = 'glyphicon glyphicon-th';
64
    /**
65
     * @var bool whether to render the input as an inline calendar
66
     */
67
    public $inline = false;
68
69
    /**
70
     * @inheritdoc
71
     */
72 5
    public function init()
73
    {
74 5
        parent::init();
75
76 5
        Html::addCssClass($this->containerOptions, 'input-group date');
77 5
        Html::addCssClass($this->options, 'form-control');
78 5
        $this->options['readonly'] = 'readonly';
79 5
        if ($this->size !== null) {
80 1
            $size = 'input-' . $this->size;
81 1
            Html::addCssClass($this->options, $size);
82 1
            Html::addCssClass($this->containerOptions, $size);
83 1
        }
84 5
        if ($this->inline) {
85 1
            $this->clientOptions['linkField'] = $this->options['id'];
86 1
            Html::removeCssClass($this->containerOptions, 'date');
87 1
            Html::removeCssClass($this->containerOptions, 'input-group');
88 1
            Html::addCssClass($this->options, 'text-center');
89 1
        }
90 5
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 4
    public function run()
96
    {
97
98 4
        $input = $this->hasModel()
99 4
            ? Html::activeTextInput($this->model, $this->attribute, $this->options)
100 4
            : Html::textInput($this->name, $this->value, $this->options);
101
102 4
        if (!$this->inline) {
103 3
            $resetIcon = Html::tag('span', '', ['class' => $this->resetButtonIcon]);
104 3
            $pickIcon = Html::tag('span', '', ['class' => $this->pickButtonIcon]);
105 3
            $resetAddon = Html::tag('span', $resetIcon, ['class' => 'input-group-addon']);
106 3
            $pickerAddon = Html::tag('span', $pickIcon, ['class' => 'input-group-addon']);
107 3
        } else {
108 1
            $resetAddon = $pickerAddon = '';
109
        }
110
111 4
        if (strpos($this->template, '{button}') !== false || $this->inline) {
112 4
            $input = Html::tag(
113 4
                'div',
114 4
                strtr($this->template, ['{input}' => $input, '{reset}' => $resetAddon, '{button}' => $pickerAddon]),
115 4
                $this->containerOptions
116 4
            );
117 4
        }
118 4
        echo $input;
119 4
        $this->registerClientScript();
120 4
    }
121
122
    /**
123
     * Registers required script for the plugin to work as a DateTimePicker
124
     */
125 4
    public function registerClientScript()
126
    {
127 4
        $js = [];
128 4
        $view = $this->getView();
129
130
        // @codeCoverageIgnoreStart
131
        if ($this->language !== null) {
132
            $this->clientOptions['language'] = $this->language;
133
            DateTimePickerAsset::register(
134
                $view
135
            )->js[] = 'js/locales/bootstrap-datetimepicker.' . $this->language . '.js';
136
        } else {
137
            DateTimePickerAsset::register($view);
138
        }
139
        // @codeCoverageIgnoreEnd
140
141 4
        $id = $this->options['id'];
142 4
        $selector = ";jQuery('#$id')";
143
144 4
        if (strpos($this->template, '{button}') !== false || $this->inline) {
145 4
            $selector .= ".parent()";
146 4
        }
147
148 4
        $options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
149
150 4
        $js[] = "$selector.datetimepicker($options);";
151
152 4
        if ($this->inline) {
153 1
            $js[] = "$selector.find('.datetimepicker-inline').addClass('center-block');";
154 1
            $js[] = "$selector.find('table.table-condensed').attr('align','center').css('margin','auto');";
155 1
        }
156
157
        // @codeCoverageIgnoreStart
158
        if (!empty($this->clientEvents)) {
159
            foreach ($this->clientEvents as $event => $handler) {
160
                $js[] = "$selector.on('$event', $handler);";
161
            }
162
        }
163
        // @codeCoverageIgnoreEnd
164
165 4
        $view->registerJs(implode("\n", $js));
166 4
    }
167
}
168