Completed
Push — master ( facdbd...6ea48b )
by Song
02:36
created

DateRange::prepare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Form\Field;
6
7
class DateRange extends Field
8
{
9
    protected $format = 'YYYY-MM-DD';
10
11
    public function __construct($column, $arguments)
12
    {
13
        $this->column['start'] = $column;
14
        $this->column['end'] = $arguments[0];
15
16
        array_shift($arguments);
17
        $this->label = $this->formatLabel($arguments);
18
        $this->id = $this->formatId($this->column);
19
20
        $this->options(['format' => $this->format]);
21
    }
22
23
    public function prepare($value)
24
    {
25
        if ($value === '') {
26
            $value = null;
27
        }
28
29
        return $value;
30
    }
31
32
    public function render()
33
    {
34
        $this->options['locale'] = config('app.locale');
35
36
        $startOptions = json_encode($this->options);
37
        $endOptions = json_encode($this->options + ['useCurrent' => false]);
38
39
        $this->script = <<<EOT
40
            $('#{$this->id['start']}').datetimepicker($startOptions);
41
            $('#{$this->id['end']}').datetimepicker($endOptions);
42
            $("#{$this->id['start']}").on("dp.change", function (e) {
43
                $('#{$this->id['end']}').data("DateTimePicker").minDate(e.date);
44
            });
45
            $("#{$this->id['end']}").on("dp.change", function (e) {
46
                $('#{$this->id['start']}').data("DateTimePicker").maxDate(e.date);
47
            });
48
EOT;
49
50
        return parent::render();
51
    }
52
}
53