1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column\Editable; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use SleepingOwl\Admin\Form\FormDefault; |
8
|
|
|
use SleepingOwl\Admin\Traits\DateFormat; |
9
|
|
|
use SleepingOwl\Admin\Traits\DatePicker; |
10
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface; |
11
|
|
|
|
12
|
|
|
class DateTime extends EditableColumn implements ColumnEditableInterface |
13
|
|
|
{ |
14
|
|
|
use DatePicker, DateFormat; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $format = 'Y-m-d H:i:s'; |
20
|
|
|
// protected $format = 'YYYY-MM-DD'; |
|
|
|
|
21
|
|
|
|
22
|
|
|
protected $type = 'combodate'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $timezone; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $seconds = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $view = 'column.editable.datetime'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Text constructor. |
41
|
|
|
* |
42
|
|
|
* @param $name |
43
|
|
|
* @param $label |
44
|
|
|
*/ |
45
|
|
|
public function __construct($name, $label = null) |
46
|
|
|
{ |
47
|
|
|
parent::__construct($name, $label); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function toArray() |
54
|
|
|
{ |
55
|
|
|
$value = $this->getModelValue(); |
56
|
|
|
|
57
|
|
|
return parent::toArray() + [ |
58
|
|
|
'id' => $this->getModel()->getKey(), |
59
|
|
|
'value' => $this->getFormatedDate($value), |
60
|
|
|
'isEditable' => $this->getModelConfiguration()->isEditable($this->getModel()), |
61
|
|
|
'url' => $this->getUrl(), |
62
|
|
|
|
63
|
|
|
'format' => $this->getJsPickerFormat(), |
64
|
|
|
'viewformat' => $this->getJsPickerFormat(), |
65
|
|
|
'data-date-pickdate' => 'true', |
66
|
|
|
'data-date-picktime' => 'false', |
67
|
|
|
'data-date-useseconds' => $this->hasSeconds() ? 'true' : 'false', |
68
|
|
|
'type' => $this->type, |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $date |
74
|
|
|
* |
75
|
|
|
* @return null|string |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
protected function getFormatedDate($date) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if (! is_null($date)) { |
80
|
|
|
if (! $date instanceof Carbon) { |
81
|
|
|
$date = Carbon::parse($date); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$date = $date->timezone($this->getTimezone())->format($this->getFormat()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $date; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return $this|NamedFormElement|mixed|null|string |
92
|
|
|
*/ |
93
|
|
|
public function getValueFromModel() |
94
|
|
|
{ |
95
|
|
|
$value = parent::getValueFromModel(); |
|
|
|
|
96
|
|
|
if (! empty($value)) { |
97
|
|
|
return $this->parseValue($value); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public function hasSeconds() |
105
|
|
|
{ |
106
|
|
|
return (bool) $this->seconds; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param bool $seconds |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function setSeconds($seconds) |
115
|
|
|
{ |
116
|
|
|
$this->seconds = $seconds; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param mixed $value |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function setModelAttribute($value) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$value = ! empty($value) |
129
|
|
|
? Carbon::createFromFormat($this->getPickerFormat(), $value, $this->getTimezone()) |
130
|
|
|
->timezone(config('app.timezone'))->format($this->getFormat()) |
131
|
|
|
: null; |
132
|
|
|
|
133
|
|
|
parent::setModelAttribute($value); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Request $request |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
|
|
public function save(Request $request) |
142
|
|
|
{ |
143
|
|
|
$form = new FormDefault([ |
144
|
|
|
new \SleepingOwl\Admin\Form\Element\Text( |
145
|
|
|
$this->getName() |
146
|
|
|
), |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
$model = $this->getModel(); |
150
|
|
|
|
151
|
|
|
$request->offsetSet($this->getName(), $request->input('value', null)); |
152
|
|
|
|
153
|
|
|
$form->setModelClass(get_class($model)); |
154
|
|
|
$form->initialize(); |
155
|
|
|
$form->setId($model->getKey()); |
156
|
|
|
|
157
|
|
|
$form->saveForm($request); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getPickerFormat() |
164
|
|
|
{ |
165
|
|
|
return $this->pickerFormat ?: config('sleeping_owl.datetimeFormat'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return $this |
170
|
|
|
* |
171
|
|
|
* SMELLS This function does more than it says. |
172
|
|
|
*/ |
173
|
|
|
public function setCurrentDate() |
174
|
|
|
{ |
175
|
|
|
$this->defaultValue = Carbon::now()->timezone($this->getTimezone())->format($this->getFormat()); |
|
|
|
|
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
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.