1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Form Element DateRange |
7
|
|
|
* |
8
|
|
|
* @package Sco\Admin\Form\Elements |
9
|
|
|
* @see http://element.eleme.io/#/en-US/component/date-picker |
10
|
|
|
*/ |
11
|
|
|
class DateRange extends Date |
12
|
|
|
{ |
13
|
|
|
protected $type = 'daterange'; |
14
|
|
|
|
15
|
|
|
protected $defaultValue = []; |
16
|
|
|
|
17
|
|
|
protected $pickerFormat = 'yyyy-MM-dd'; |
18
|
|
|
|
19
|
|
|
protected $attributes = []; |
20
|
|
|
|
21
|
|
|
public function __construct($name, string $title) |
22
|
|
|
{ |
23
|
|
|
$name = (array) $name; |
24
|
|
|
|
25
|
|
|
$this->attributes = $name; |
26
|
|
|
|
27
|
|
|
if (count($this->attributes) == 1) { |
28
|
|
|
$this->setCast('json'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
parent::__construct(implode('_', $name), $title); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getValue() |
35
|
|
|
{ |
36
|
|
|
$model = $this->getModel(); |
37
|
|
|
$value = $this->getDefaultValue(); |
38
|
|
|
if (is_null($model) || ! $model->exists) { |
39
|
|
|
return $value; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (count($this->attributes) == 2) { |
|
|
|
|
43
|
|
|
} else { |
44
|
|
|
$value = $model->getAttribute(array_shift($this->attributes)); |
45
|
|
|
if (($value = json_decode($value, true)) === false || is_null($value)) { |
46
|
|
|
$value = explode(',', $value); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return [ |
51
|
|
|
$this->dateToString($model->getAttribute($this->getStartName())), |
52
|
|
|
$this->dateToString($model->getAttribute($this->getEndName())), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function getDefaultValidationRules() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
'array' => 'array', |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function save() |
64
|
|
|
{ |
65
|
|
|
$model = $this->getModel(); |
66
|
|
|
|
67
|
|
|
list($startValue, $endValue) = $this->getValueFromRequest(); |
68
|
|
|
$model->setAttribute($this->getStartName(), $this->prepareValue($startValue)); |
69
|
|
|
$model->setAttribute($this->getEndName(), $this->prepareValue($endValue)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.