1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Form Element DateRange |
9
|
|
|
* |
10
|
|
|
* @package Sco\Admin\Form\Elements |
11
|
|
|
* @see http://element.eleme.io/#/en-US/component/date-picker |
12
|
|
|
*/ |
13
|
|
|
class DateRange extends Date |
14
|
|
|
{ |
15
|
|
|
protected $type = 'daterange'; |
16
|
|
|
|
17
|
|
|
protected $defaultValue = []; |
18
|
|
|
|
19
|
|
|
protected $pickerFormat = 'yyyy-MM-dd'; |
20
|
|
|
|
21
|
|
|
protected $attributes = []; |
22
|
|
|
|
23
|
|
|
public function __construct($name, string $title) |
24
|
|
|
{ |
25
|
|
|
$name = (array) $name; |
26
|
|
|
|
27
|
|
|
$this->attributes = $name; |
28
|
|
|
|
29
|
|
|
if (count($this->attributes) == 1) { |
30
|
|
|
$this->setCast('json'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
parent::__construct(implode('_', $name), $title); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getValue() |
37
|
|
|
{ |
38
|
|
|
if (count($this->attributes) == 1) { |
39
|
|
|
list($start, $end) = parent::getValue(); |
40
|
|
|
} else { |
41
|
|
|
$model = $this->getModel(); |
42
|
|
|
if (is_null($model) || ! $model->exists) { |
43
|
|
|
return []; |
44
|
|
|
} |
45
|
|
|
list($startName, $endName) = $this->attributes; |
46
|
|
|
$start = $model->getAttribute($startName); |
47
|
|
|
$end = $model->getAttribute($endName); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($start && $end) { |
51
|
|
|
return [ |
52
|
|
|
$this->fromDateTime(Carbon::parse($start)), |
53
|
|
|
$this->fromDateTime(Carbon::parse($end)), |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getDefaultValidationRules() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
'array' => 'array', |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function save() |
68
|
|
|
{ |
69
|
|
|
if (count($this->attributes) == 1) { |
70
|
|
|
return parent::save(); |
71
|
|
|
} else { |
72
|
|
|
$model = $this->getModel(); |
73
|
|
|
list($startValue, $endValue) = $this->getValueFromRequest(); |
74
|
|
|
list($startName, $endName) = $this->attributes; |
75
|
|
|
|
76
|
|
|
$model->setAttribute($startName, $this->prepareValue($startValue)); |
77
|
|
|
$model->setAttribute($endName, $this->prepareValue($endValue)); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|