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
|
|
|
|