-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPwtDatepicker.php
65 lines (53 loc) · 1.57 KB
/
PwtDatepicker.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
/**
* @link https://github.com/hadi-aj/yii2-pwtdatepicker
* @package yii2-widgets
* @subpackage yii2-pwt-datepicker
* @version 1
* @license http://www.wtfpl.net/
*/
namespace hadiaj\pwtdatepicker;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* PwtDatepicker
*
* @author Hadi AlizadehJalali <hadi.alizadeh.jalali@gmail.com>
*/
class PwtDatepicker extends InputWidget {
public $pluginOptions;
public function init() {
parent::init();
if ($this->options['class'] == NULL) {
$this->options['class'] = 'form-control text-left ltr';
}
if (!$this->pluginOptions['format']) {
$this->pluginOptions['format'] = 'YYYY/MM/DD';
}
if ($this->pluginOptions['initialValue'] === NULL) {
$this->pluginOptions['initialValue'] = (boolean) $this->model->{$this->attribute};
}
}
public function run() {
echo $this->renderInput();
PwtDatepickerAsset::register($this->getView());
$this->renderJsCode();
}
/**
* Render input.
*/
function renderInput() {
if ($this->hasModel()) {
return Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
return Html::textInput($this->name, $this->value, $this->options);
}
}
function renderJsCode() {
$id = $this->options['id'];
$options = Json::encode($this->pluginOptions);
$js = "$('#{$id}').persianDatepicker({$options});";
$this->getView()->registerJs($js);
}
}