1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Form Element Date |
9
|
|
|
* |
10
|
|
|
* @package Sco\Admin\Form\Elements |
11
|
|
|
* @see http://element.eleme.io/#/en-US/component/date-picker |
12
|
|
|
*/ |
13
|
|
|
class Date extends Input |
14
|
|
|
{ |
15
|
|
|
protected $type = 'date'; |
16
|
|
|
|
17
|
|
|
protected $cast = 'date'; |
18
|
|
|
|
19
|
|
|
protected $defaultValue = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Date Picker format |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $pickerFormat = 'yyyy-MM-dd'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Datetime timezone. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $timezone; |
34
|
|
|
|
35
|
|
|
protected $editable = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getFormat() |
41
|
|
|
{ |
42
|
|
|
return $this->convertPickerFormat(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function convertPickerFormat() |
49
|
|
|
{ |
50
|
|
|
return strtr($this->getPickerFormat(), [ |
51
|
|
|
'yyyy' => 'Y', |
52
|
|
|
'MM' => 'm', |
53
|
|
|
'dd' => 'd', |
54
|
|
|
'HH' => 'H', |
55
|
|
|
'mm' => 'i', |
56
|
|
|
'ss' => 's', |
57
|
|
|
]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
protected function getPickerFormat() |
64
|
|
|
{ |
65
|
|
|
return $this->pickerFormat; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $value |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function setPickerFormat(string $value) |
74
|
|
|
{ |
75
|
|
|
$this->pickerFormat = $value; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function getTimezone() |
84
|
|
|
{ |
85
|
|
|
if ($this->timezone) { |
86
|
|
|
return $this->timezone; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return config('app.timezone'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $timezone |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setTimezone(string $timezone) |
98
|
|
|
{ |
99
|
|
|
$this->timezone = $timezone; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function isEditable() |
108
|
|
|
{ |
109
|
|
|
return $this->editable; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Allow edit |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function enableEdit() |
118
|
|
|
{ |
119
|
|
|
$this->editable = true; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function toArray() |
125
|
|
|
{ |
126
|
|
|
return parent::toArray() + [ |
127
|
|
|
//'format' => $this->getFormat(), |
|
|
|
|
128
|
|
|
'pickerFormat' => $this->getPickerFormat(), |
129
|
|
|
'editable' => $this->isEditable(), |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return a timestamp as DateTime object. |
135
|
|
|
* |
136
|
|
|
* @param mixed $value |
137
|
|
|
* @return Carbon |
138
|
|
|
*/ |
139
|
|
|
protected function asDateTime($value) |
140
|
|
|
{ |
141
|
|
|
if ($value instanceof Carbon) { |
142
|
|
|
return $value->timezone($this->getTimezone()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($value instanceof \DateTimeInterface) { |
146
|
|
|
return new Carbon( |
147
|
|
|
$value->format('Y-m-d H:i:s.u'), |
148
|
|
|
$value->getTimezone() |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (is_numeric($value)) { |
153
|
|
|
return Carbon::createFromTimestamp($value)->timezone($this->getTimezone()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return Carbon::createFromFormat($this->getFormat(), $value); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Convert a DateTime to a storable string. |
161
|
|
|
* |
162
|
|
|
* @param \DateTime|int $value |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function fromDateTime($value) |
166
|
|
|
{ |
167
|
|
|
return empty($value) ? $value : $this->asDateTime($value) |
168
|
|
|
->timezone($this->getTimezone()) |
169
|
|
|
->format($this->getFormat()); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.