|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
|
|
8
|
|
|
class Date extends Text |
|
9
|
|
|
{ |
|
10
|
|
|
use \SleepingOwl\Admin\Traits\DatePicker; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $view = 'column.filter.date'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $format; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $searchFormat = 'Y-m-d'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $seconds = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $width = 150; |
|
36
|
|
|
|
|
37
|
2 |
|
public function initialize() |
|
38
|
|
|
{ |
|
39
|
2 |
|
parent::initialize(); |
|
40
|
2 |
|
$this->setHtmlAttribute('data-type', 'date'); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
7 |
|
public function getFormat() |
|
47
|
|
|
{ |
|
48
|
7 |
|
return $this->format; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $format |
|
53
|
|
|
* |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
10 |
|
public function setFormat($format) |
|
57
|
|
|
{ |
|
58
|
10 |
|
$this->format = $format; |
|
59
|
|
|
|
|
60
|
10 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function hasSeconds() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->seconds; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param bool $status |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this |
|
75
|
|
|
* @deprecated use showSeconds |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setSeconds($status) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->showSeconds($status); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param bool $status |
|
84
|
|
|
* |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function showSeconds($status = true) |
|
88
|
|
|
{ |
|
89
|
1 |
|
$this->seconds = (bool) $status; |
|
90
|
|
|
|
|
91
|
1 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function getPickerFormat() |
|
98
|
|
|
{ |
|
99
|
3 |
|
return $this->pickerFormat ?: config('sleeping_owl.dateFormat'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return int |
|
104
|
|
|
*/ |
|
105
|
5 |
|
public function getWidth() |
|
106
|
|
|
{ |
|
107
|
5 |
|
return $this->width; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param int $width |
|
112
|
|
|
* |
|
113
|
|
|
* @return $this |
|
114
|
|
|
*/ |
|
115
|
5 |
|
public function setWidth($width) |
|
116
|
|
|
{ |
|
117
|
5 |
|
intval($width); |
|
118
|
|
|
|
|
119
|
5 |
|
if ($width < 0) { |
|
120
|
1 |
|
$width = 0; |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
5 |
|
$this->width = (int) $width; |
|
124
|
|
|
|
|
125
|
5 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
public function toArray() |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
return parent::toArray() + [ |
|
134
|
|
|
'seconds' => $this->hasSeconds(), |
|
135
|
|
|
'pickerFormat' => $this->getJsPickerFormat(), |
|
136
|
|
|
'width' => $this->getWidth(), |
|
137
|
|
|
]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $date |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
9 |
View Code Duplication |
public function parseValue($date) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
9 |
|
if (empty($date)) { |
|
148
|
2 |
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
try { |
|
152
|
7 |
|
$date = Carbon::parse($date); |
|
153
|
7 |
|
} catch (Exception $e) { |
|
154
|
|
|
try { |
|
155
|
2 |
|
$date = Carbon::createFromFormat($this->getPickerFormat(), $date); |
|
156
|
2 |
|
} catch (Exception $e) { |
|
157
|
1 |
|
return; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
6 |
|
return $date->format($this->getFormat()); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.