-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDatePicker.php
89 lines (73 loc) · 2.16 KB
/
DatePicker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace enigmatix\widgets;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
/**
* Class Select2
* @package enigmatix\widgets
* @author Joel Small
* @email [email protected]
*
* This class creates a date widget for a yii2 form input.
*
*/
class DatePicker extends \yii\widgets\InputWidget
{
public $startDate;
public $endDate;
public $pluginOptions = [
'format' => 'yyyy-mm-dd',
'startView' => 'decade',
];
protected function getOptions()
{
if($this->startDate !== null){
$this->pluginOptions['startDate'] = new JsExpression("new Date('{$this->startDate}')");
}
if($this->endDate !== null){
$this->pluginOptions['endDate'] = new JsExpression("new Date('{$this->endDate}')");
}
return Json::encode(
$this->pluginOptions
);
}
public function run()
{
$view = $this->getView();
$script = "$('#{$this->id}').datepicker({$this->getOptions()}){$this->getAppendedItems()};";
$view->registerJs($script);
DatepickerAsset::register($view);
$this->outputField();
}
protected function outputField(){
echo "<div class='input-group date' id='{$this->id}'>";
echo "<span class=\"input-group-addon\">
<span class=\"glyphicon glyphicon-calendar\"></span>
</span>";
echo Html::activeTextInput($this->model, $this->attribute,
[
'id' => $this->options['id'],
'class' =>'form-control',
'value' => $this->retrieveValue(),
'readonly' => 'true'
]);
echo "</div>";
}
protected function getAppendedItems()
{
return '';
}
public function getFieldName(){
return Html::getAttributeName($this->attribute);
}
public function retrieveValue(){
$fieldName = $this->getFieldName();
if($this->value != null){
return $this->value;
}
$value = $this->model->$fieldName;
return $value;
}
}