1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
class Date extends Input |
8
|
|
|
{ |
9
|
|
|
protected $type = 'date'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Date Picker format |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $pickerFormat = 'yyyy-MM-dd'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Datetime timezone. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $timezone; |
24
|
|
|
|
25
|
|
|
protected $editable = false; |
26
|
|
|
|
27
|
|
|
public function getFormat() |
28
|
|
|
{ |
29
|
|
|
return $this->convertPickerFormat(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/*public function setFormat($value) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->format = $value; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
}*/ |
38
|
|
|
|
39
|
|
|
protected function convertPickerFormat() |
40
|
|
|
{ |
41
|
|
|
return strtr($this->getPickerFormat(), [ |
42
|
|
|
'yyyy' => 'Y', |
43
|
|
|
//'yy' => 'y', |
|
|
|
|
44
|
|
|
'MM' => 'm', |
45
|
|
|
//'M' => 'n', |
|
|
|
|
46
|
|
|
'dd' => 'd', |
47
|
|
|
//'d' => 'j', |
|
|
|
|
48
|
|
|
'HH' => 'H', |
49
|
|
|
//'H' => 'G', |
|
|
|
|
50
|
|
|
'mm' => 'i', |
51
|
|
|
'ss' => 's', |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function getPickerFormat() |
56
|
|
|
{ |
57
|
|
|
return $this->pickerFormat; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $value |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function setPickerFormat($value) |
66
|
|
|
{ |
67
|
|
|
$this->pickerFormat = $value; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getTimezone() |
76
|
|
|
{ |
77
|
|
|
if ($this->timezone) { |
78
|
|
|
return $this->timezone; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return config('app.timezone'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $timezone |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function setTimezone($timezone) |
90
|
|
|
{ |
91
|
|
|
$this->timezone = $timezone; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function isEditable() |
97
|
|
|
{ |
98
|
|
|
return $this->editable; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Allow edit |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function enableEdit() |
107
|
|
|
{ |
108
|
|
|
$this->editable = true; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function toArray() |
114
|
|
|
{ |
115
|
|
|
return parent::toArray() + [ |
116
|
|
|
//'format' => $this->getFormat(), |
|
|
|
|
117
|
|
|
'pickerFormat' => $this->getPickerFormat(), |
118
|
|
|
'editable' => $this->isEditable(), |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function getDefaultValue() |
123
|
|
|
{ |
124
|
|
|
return Carbon::now() |
125
|
|
|
->timezone($this->getTimezone()) |
126
|
|
|
->format($this->getFormat()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getValue() |
130
|
|
|
{ |
131
|
|
|
$value = parent::getValue(); |
132
|
|
|
if (empty($value)) { |
133
|
|
|
return ''; |
134
|
|
|
} |
135
|
|
|
return $this->dateToString($value); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function dateToString($value) |
139
|
|
|
{ |
140
|
|
|
if (!($value instanceof Carbon)) { |
141
|
|
|
$value = Carbon::parse($value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$value->timezone($this->getTimezone()); |
|
|
|
|
145
|
|
|
|
146
|
|
|
return $value->format($this->getFormat()); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
protected function prepareValue($value) |
150
|
|
|
{ |
151
|
|
|
$value = Carbon::parse($value); |
152
|
|
|
$value->timezone($this->getTimezone()); |
153
|
|
|
return $value; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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.