Completed
Push — master ( c9093b...9b49f7 )
by Song
02:43
created

DateRange::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 22
rs 9.568
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 prepare($value)
43
    {
44
        if ($value === '') {
45
            $value = null;
46
        }
47
48
        return $value;
49
    }
50
51
    public function render()
52
    {
53
        $this->options['locale'] = config('app.locale');
54
55
        $startOptions = json_encode($this->options);
56
        $endOptions = json_encode($this->options + ['useCurrent' => false]);
57
58
        $class = $this->getElementClassSelector();
59
60
        $this->script = <<<EOT
61
            $('{$class['start']}').datetimepicker($startOptions);
62
            $('{$class['end']}').datetimepicker($endOptions);
63
            $("{$class['start']}").on("dp.change", function (e) {
64
                $('{$class['end']}').data("DateTimePicker").minDate(e.date);
65
            });
66
            $("{$class['end']}").on("dp.change", function (e) {
67
                $('{$class['start']}').data("DateTimePicker").maxDate(e.date);
68
            });
69
EOT;
70
71
        return parent::render();
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::render(); of type Illuminate\Contracts\Vie...minate\View\View|string adds the type Illuminate\Contracts\View\Factory to the return on line 71 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
72
    }
73
}
74