|
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
|
|
|
/** |
|
40
|
|
|
* @inheritdoc |
|
41
|
|
|
*/ |
|
42
|
18 |
|
public function init() |
|
43
|
|
|
{ |
|
44
|
18 |
|
parent::init(); |
|
45
|
|
|
|
|
46
|
18 |
|
if ($this->inline) { |
|
47
|
3 |
|
$this->options['readonly'] = 'readonly'; |
|
48
|
3 |
|
Html::addCssClass($this->options, 'text-center'); |
|
49
|
3 |
|
} |
|
50
|
18 |
|
if ($this->size) { |
|
51
|
3 |
|
Html::addCssClass($this->options, 'input-' . $this->size); |
|
52
|
3 |
|
Html::addCssClass($this->containerOptions, 'input-group-' . $this->size); |
|
53
|
3 |
|
} |
|
54
|
18 |
|
Html::addCssClass($this->options, 'form-control'); |
|
55
|
18 |
|
Html::addCssClass($this->containerOptions, 'input-group date'); |
|
56
|
18 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
15 |
|
public function run() |
|
62
|
|
|
{ |
|
63
|
|
|
|
|
64
|
15 |
|
$input = $this->hasModel() |
|
65
|
15 |
|
? Html::activeTextInput($this->model, $this->attribute, $this->options) |
|
66
|
15 |
|
: Html::textInput($this->name, $this->value, $this->options); |
|
67
|
|
|
|
|
68
|
15 |
|
if ($this->inline) { |
|
69
|
3 |
|
$input .= '<div></div>'; |
|
70
|
3 |
|
} |
|
71
|
15 |
|
if ($this->addon && !$this->inline) { |
|
72
|
12 |
|
$addon = Html::tag('span', $this->addon, ['class' => 'input-group-addon']); |
|
73
|
12 |
|
$input = strtr($this->template, ['{input}' => $input, '{addon}' => $addon]); |
|
74
|
12 |
|
$input = Html::tag('div', $input, $this->containerOptions); |
|
75
|
12 |
|
} |
|
76
|
15 |
|
if ($this->inline) { |
|
77
|
3 |
|
$input = strtr($this->template, ['{input}' => $input, '{addon}' => '']); |
|
78
|
3 |
|
} |
|
79
|
15 |
|
echo $input; |
|
80
|
|
|
|
|
81
|
15 |
|
$this->registerClientScript(); |
|
82
|
15 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Registers required script for the plugin to work as DatePicker |
|
86
|
|
|
*/ |
|
87
|
15 |
|
public function registerClientScript() |
|
88
|
|
|
{ |
|
89
|
15 |
|
$js = []; |
|
90
|
15 |
|
$view = $this->getView(); |
|
91
|
|
|
|
|
92
|
|
|
// @codeCoverageIgnoreStart |
|
93
|
|
|
if ($this->language !== null && $this->language !== 'en') { |
|
94
|
|
|
$this->clientOptions['language'] = $this->language; |
|
95
|
|
|
DatePickerLanguageAsset::register($view)->js[] = 'bootstrap-datepicker.' . $this->language . '.min.js'; |
|
96
|
|
|
} else { |
|
97
|
|
|
DatePickerAsset::register($view); |
|
98
|
|
|
} |
|
99
|
|
|
// @codeCoverageIgnoreEnd |
|
100
|
|
|
|
|
101
|
15 |
|
$id = $this->options['id']; |
|
102
|
15 |
|
$selector = ";jQuery('#$id')"; |
|
103
|
|
|
|
|
104
|
15 |
|
if ($this->addon || $this->inline) { |
|
105
|
15 |
|
$selector .= ".parent()"; |
|
106
|
15 |
|
} |
|
107
|
|
|
|
|
108
|
15 |
|
$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : ''; |
|
109
|
|
|
|
|
110
|
15 |
|
if ($this->inline) { |
|
111
|
3 |
|
$this->clientEvents['changeDate'] = "function (e){ jQuery('#$id').val(e.format());}"; |
|
112
|
3 |
|
} |
|
113
|
|
|
|
|
114
|
15 |
|
$js[] = "$selector.datepicker($options);"; |
|
115
|
|
|
|
|
116
|
15 |
|
if (!empty($this->clientEvents)) { |
|
117
|
3 |
|
foreach ($this->clientEvents as $event => $handler) { |
|
118
|
3 |
|
$js[] = "$selector.on('$event', $handler);"; |
|
119
|
3 |
|
} |
|
120
|
3 |
|
} |
|
121
|
15 |
|
$view->registerJs(implode("\n", $js)); |
|
122
|
15 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|