Completed
Branch master (a1edd4)
by
unknown
54:09
created

DatePicker   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A renderWidget() 0 7 1
B prepareInput() 0 11 5
A registerAssets() 0 14 3
1
<?php
2
/**
3
 * This file is part of the fangface/yii2-concord package
4
 *
5
 * For the full copyright and license information, please view
6
 * the file LICENSE.md that was distributed with this source code.
7
 *
8
 * @package fangface/yii2-concord
9
 * @author Fangface <[email protected]>
10
 * @copyright Copyright (c) 2014 Fangface <[email protected]>
11
 * @license https://github.com/fangface/yii2-concord/blob/master/LICENSE.md MIT License
12
 *
13
 */
14
15
namespace fangface\widgets;
16
17
use backend\assets\BootstrapDatePickerAsset;
18
use fangface\widgets\InputWidget;
19
use fangface\helpers\Html;
20
21
22
/**
23
 * Date Picker widget
24
 */
25
class DatePicker extends InputWidget
26
{
27
28
    /**
29
     * @var string the name of the jQuery plugin
30
     */
31
    public $pluginName = 'datepicker';
32
    /**
33
     * @var array default widget plugin options that user pluginOptions will be merged into
34
     */
35
    public $defaultPluginOptions = ['format' => 'yyyy-mm-dd', 'todayBtn' => 'linked', 'clearBtn' => true, 'autoclose' => true, 'weekStart' => 1];
36
    /**
37
     * @var array the HTML attributes for the input tag.
38
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
39
     */
40
    public $options = ['class' => 'form-control input-small'];
41
    /**
42
     * @var string element id to use when calling the datepicker
43
     */
44
    public $elementId = null;
45
46
    /**
47
     * Renders the color picker widget
48
     */
49
    protected function renderWidget()
50
    {
51
        $this->prepareInput();
52
        $this->registerAssets();
53
        $this->prepareTemplate();
54
        echo $this->renderTemplate();
55
    }
56
57
    /**
58
     * Prepare the input fields for the input
59
     *
60
     * @return void
61
     */
62
    protected function prepareInput()
63
    {
64
        if ($this->hasModel()) {
65
            if (!isset($this->options['value']) && $this->model->{$this->attribute} == '0000-00-00') {
66
                $this->options['value'] = '';
67
            }
68
            $this->sections['input'] = Html::activeTextInput($this->model, $this->attribute, $this->options);
69
        } else {
70
            $this->sections['input'] = Html::textInput($this->name, ($this->value == '0000-00-00' ? '' : $this->value), $this->options);
71
        }
72
    }
73
74
    /**
75
     * Registers the needed client assets
76
     *
77
     * @return void
78
     */
79
    public function registerAssets()
80
    {
81
        if ($this->disabled) {
82
            return;
83
        }
84
        $view = $this->getView();
85
        BootstrapDatePickerAsset::register($view);
86
        if ($this->elementId !== null) {
87
            $element = "jQuery('#" . $this->elementId . "')";
88
        } else {
89
            $element = "jQuery('#" . $this->options['id'] . "')";
90
        }
91
        $this->registerPlugin($this->pluginName, $element);
92
    }
93
94
}
95