Completed
Push — master ( f13bf0...57ec7d )
by Song
02:39
created

DateRange::value()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 15
rs 9.7666
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 static $css = [
10
        '/vendor/laravel-admin/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css',
11
    ];
12
13
    protected static $js = [
14
        '/vendor/laravel-admin/moment/min/moment-with-locales.min.js',
15
        '/vendor/laravel-admin/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js',
16
    ];
17
18
    protected $format = 'YYYY-MM-DD';
19
20
    /**
21
     * Column name.
22
     *
23
     * @var array
24
     */
25
    protected $column = [];
26
27
    public function __construct($column, $arguments)
28
    {
29
        $this->column['start'] = $column;
30
        $this->column['end'] = $arguments[0];
31
32
        array_shift($arguments);
33
        $this->label = $this->formatLabel($arguments);
34
        $this->id = $this->formatId($this->column);
35
36
        $this->options(['format' => $this->format]);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function value($value = null)
43
    {
44
        if (is_null($value)) {
45
46
            if (is_null($this->value['start']) && is_null($this->value['end'])) {
47
                return $this->getDefault();
48
            }
49
50
            return $this->value;
51
        }
52
53
        $this->value = $value;
54
55
        return $this;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function prepare($value)
62
    {
63
        if ($value === '') {
64
            $value = null;
65
        }
66
67
        return $value;
68
    }
69
70
    public function render()
71
    {
72
        $this->options['locale'] = config('app.locale');
73
74
        $startOptions = json_encode($this->options);
75
        $endOptions = json_encode($this->options + ['useCurrent' => false]);
76
77
        $class = $this->getElementClassSelector();
78
79
        $this->script = <<<EOT
80
            $('{$class['start']}').datetimepicker($startOptions);
81
            $('{$class['end']}').datetimepicker($endOptions);
82
            $("{$class['start']}").on("dp.change", function (e) {
83
                $('{$class['end']}').data("DateTimePicker").minDate(e.date);
84
            });
85
            $("{$class['end']}").on("dp.change", function (e) {
86
                $('{$class['start']}').data("DateTimePicker").maxDate(e.date);
87
            });
88
EOT;
89
90
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type string|Illuminate\View\V...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 90 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
91
    }
92
}
93