-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTimePicker.php
65 lines (56 loc) · 1.76 KB
/
DateTimePicker.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
<?php
namespace drexlerux\datetimepicker;
use Yii;
use yii\bootstrap\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* This is just an example.
*/
class DateTimePicker extends InputWidget
{
public $options = [];
public $widgetOptions = [];
private $inputId;
public function init()
{
$defaults = [
'data-field' => 'date',
'readonly' => true,
'class'=>'form-control'
];
$this->options = array_merge($defaults, $this->options);
$this->inputId = Html::getInputId($this->model, $this->attribute);
$defaultsWidgetOptions = [
'language' => Yii::$app->language,
'dateTimeFormat' => 'yyyy-MM-dd hh:mm:ss',
'dateFormat' => 'yyyy-MM-dd',
];
$this->widgetOptions = array_merge($defaultsWidgetOptions, $this->widgetOptions);
$this->widgetOptions = Json::encode($this->widgetOptions);
}
public function run()
{
$this->registerScript();
$input = Html::activeTextInput($this->model, $this->attribute, $this->options);
$box = "<div id=\"box_$this->inputId\"></div>";
echo "<div class=\"flat-datetimepicker\">$input $box</div>";
}
public function registerScript(){
$view = $this->getView();
$asset = DateTimePickerAsset::register($view);
$js = "
$(document).ready(function(){
$('#box_$this->inputId').DateTimePicker($this->widgetOptions);
});
";
$css = "
.flat-datetimepicker input[type=\"text\"]{
background-color: #FFFFFF;
cursor: pointer;
}
";
$view->registerJs($js);
$view->registerCss($css, [], 'flat-datetimepicker');
}
}