Completed
Push — master ( 82be35...1537c0 )
by Song
02:28
created

src/Form/Field/DateRange.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
            if (!is_null($this->value) && is_null($this->value['start']) && is_null($this->value['end'])) {
46
                return $this->getDefault();
47
            }
48
49
            return $this->value;
50
        }
51
52
        $this->value = $value;
53
54
        return $this;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function prepare($value)
61
    {
62
        if ($value === '') {
63
            $value = null;
64
        }
65
66
        return $value;
67
    }
68
69
    public function render()
70
    {
71
        $this->options['locale'] = config('app.locale');
72
73
        $startOptions = json_encode($this->options);
74
        $endOptions = json_encode($this->options + ['useCurrent' => false]);
75
76
        $class = $this->getElementClassSelector();
77
78
        $this->script = <<<EOT
79
            $('{$class['start']}').datetimepicker($startOptions);
80
            $('{$class['end']}').datetimepicker($endOptions);
81
            $("{$class['start']}").on("dp.change", function (e) {
82
                $('{$class['end']}').data("DateTimePicker").minDate(e.date);
83
            });
84
            $("{$class['end']}").on("dp.change", function (e) {
85
                $('{$class['start']}').data("DateTimePicker").maxDate(e.date);
86
            });
87
EOT;
88
89
        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 89 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
90
    }
91
}
92