1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column\Filter; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use SleepingOwl\Admin\Traits\Assets; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use KodiComponents\Support\HtmlAttributes; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
use Illuminate\Contracts\Support\Renderable; |
11
|
|
|
use SleepingOwl\Admin\Traits\SqlQueryOperators; |
12
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnInterface; |
13
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnMetaInterface; |
14
|
|
|
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface; |
15
|
|
|
|
16
|
|
|
abstract class BaseColumnFilter implements Renderable, ColumnFilterInterface, Arrayable |
17
|
|
|
{ |
18
|
|
|
use SqlQueryOperators, HtmlAttributes, Assets, \SleepingOwl\Admin\Traits\Renderable; |
19
|
|
|
|
20
|
|
|
protected $view; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Closure|null |
24
|
|
|
*/ |
25
|
|
|
protected $callback; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
protected $columnName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $columnRawName; |
36
|
|
|
|
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
$this->initializePackage(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initialize column filter. |
44
|
|
|
*/ |
45
|
|
|
public function initialize() |
46
|
|
|
{ |
47
|
|
|
$this->includePackage(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return null|string |
52
|
|
|
*/ |
53
|
|
|
public function getColumnName() |
54
|
|
|
{ |
55
|
|
|
return $this->columnName; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param null|string $name |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function setColumnName($name) |
64
|
|
|
{ |
65
|
|
|
$this->columnName = $name; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return null|string |
72
|
|
|
*/ |
73
|
|
|
public function getColumnRawName() |
74
|
|
|
{ |
75
|
|
|
return $this->columnRawName; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param null|string $name |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setColumnRawName($name) |
84
|
|
|
{ |
85
|
|
|
$this->columnRawName = $name; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $value |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function parseValue($value) |
96
|
|
|
{ |
97
|
|
|
return $value; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return \Closure|null |
102
|
|
|
* @deprecated |
103
|
|
|
*/ |
104
|
|
|
public function getCallback() |
105
|
|
|
{ |
106
|
|
|
return $this->callback; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \Closure $callback |
111
|
|
|
* @return $this |
112
|
|
|
* @deprecated |
113
|
|
|
*/ |
114
|
|
|
public function setCallback(Closure $callback) |
115
|
|
|
{ |
116
|
|
|
$this->callback = $callback; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param ColumnInterface $column |
123
|
|
|
* @param Builder $query |
124
|
|
|
* @param string $queryString |
125
|
|
|
* @param array|string $queryParams |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function apply(ColumnInterface $column, Builder $query, $queryString, $queryParams) |
130
|
|
|
{ |
131
|
|
|
$queryString = $this->parseValue($queryString); |
132
|
|
|
|
133
|
|
|
if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) { |
|
|
|
|
134
|
|
|
if (method_exists($metaInstance, 'onFilterSearch')) { |
135
|
|
|
$metaInstance->onFilterSearch($column, $query, $queryString, $queryParams); |
136
|
|
|
|
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (is_callable($callback = $column->getFilterCallback())) { |
|
|
|
|
142
|
|
|
$callback($column, $query, $queryString, $queryParams); |
143
|
|
|
|
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (is_callable($callback = $this->getCallback())) { |
|
|
|
|
148
|
|
|
$callback($column, $query, $queryString, $queryParams); |
149
|
|
|
|
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (empty($queryString) && strlen($queryString) == 0) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (is_null($name = $this->getColumnRawName())) { |
158
|
|
|
if (is_null($name = $this->getColumnName())) { |
159
|
|
|
$name = $column->getName(); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (strpos($name, '.') !== false && is_null($this->getColumnRawName())) { |
164
|
|
|
$parts = explode('.', $name); |
165
|
|
|
$fieldName = array_pop($parts); |
166
|
|
|
$relationName = implode('.', $parts); |
167
|
|
|
try { |
168
|
|
|
$relation = $query->getModel()->{$relationName}(); |
169
|
|
|
$isMorphTo = $relation instanceof \Illuminate\Database\Eloquent\Relations\MorphTo; |
170
|
|
|
} catch (\Exception $e) { |
171
|
|
|
$isMorphTo = false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($isMorphTo) { |
175
|
|
|
$query->whereHasMorph($relationName, '*', function ($q) use ($queryString, $fieldName) { |
176
|
|
|
$this->buildQuery($q, $fieldName, $queryString); |
177
|
|
|
}); |
178
|
|
|
} else { |
179
|
|
|
$query->whereHas($relationName, function ($q) use ($queryString, $fieldName) { |
180
|
|
|
$this->buildQuery($q, $fieldName, $queryString); |
181
|
|
|
}); |
182
|
|
|
} |
183
|
|
|
unset($relation); |
184
|
|
|
} else { |
185
|
|
|
$this->buildQuery($query, $name, $queryString); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get the instance as an array. |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
public function toArray() |
195
|
|
|
{ |
196
|
|
|
return [ |
197
|
|
|
'attributes' => $this->htmlAttributesToString(), |
198
|
|
|
'attributesArray' => $this->getHtmlAttributes(), |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|