|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
|
4
|
|
|
|
|
5
|
|
|
use Request; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use SleepingOwl\Admin\Traits\PanelControl; |
|
9
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
10
|
|
|
use SleepingOwl\Admin\Display\Extension\Columns; |
|
11
|
|
|
use SleepingOwl\Admin\Display\Extension\ColumnsTotal; |
|
12
|
|
|
use SleepingOwl\Admin\Display\Extension\ColumnFilters; |
|
13
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnInterface; |
|
14
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnMetaInterface; |
|
15
|
|
|
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class DisplayTable. |
|
19
|
|
|
* |
|
20
|
|
|
* @method Columns getColumns() |
|
21
|
|
|
* @method $this setColumns(ColumnInterface|ColumnInterface[] $column) |
|
22
|
|
|
* |
|
23
|
|
|
* @method ColumnFilters getColumnFilters() |
|
24
|
|
|
* @method $this setColumnFilters(ColumnFilterInterface|ColumnFilterInterface[] $filters = null, ...$filters) |
|
25
|
|
|
*/ |
|
26
|
|
|
class DisplayTable extends Display |
|
27
|
|
|
{ |
|
28
|
|
|
use PanelControl; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $view = 'display.table'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $parameters = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int|null |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $paginate = 25; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $pageName = 'page'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var Collection |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $collection; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string|null |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $newEntryButtonText; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Display constructor. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct() |
|
64
|
|
|
{ |
|
65
|
|
|
parent::__construct(); |
|
66
|
|
|
|
|
67
|
|
|
$this->extend('columns', new Columns()); |
|
68
|
|
|
$this->extend('column_filters', new ColumnFilters()); |
|
69
|
|
|
$this->extend('columns_total', new ColumnsTotal()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Initialize display. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function initialize() |
|
76
|
|
|
{ |
|
77
|
|
|
parent::initialize(); |
|
78
|
|
|
|
|
79
|
|
|
if ($this->getModelConfiguration()->isRestorableModel()) { |
|
80
|
|
|
$this->setApply(function (Builder $q) { |
|
81
|
|
|
return $q->withTrashed(); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->setHtmlAttribute('class', 'table table-striped'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return array|\Illuminate\Contracts\Translation\Translator|null|string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getNewEntryButtonText() |
|
92
|
|
|
{ |
|
93
|
|
|
if (is_null($this->newEntryButtonText)) { |
|
94
|
|
|
$this->newEntryButtonText = trans('sleeping_owl::lang.table.new-entry'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->newEntryButtonText; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $newEntryButtonText |
|
102
|
|
|
* |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setNewEntryButtonText($newEntryButtonText) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->newEntryButtonText = $newEntryButtonText; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getParameters() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->parameters; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param array $parameters |
|
122
|
|
|
* |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setParameters($parameters) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->parameters = $parameters; |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $key |
|
134
|
|
|
* @param mixed $value |
|
135
|
|
|
* |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setParameter($key, $value) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->parameters[$key] = $value; |
|
141
|
|
|
|
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param int $perPage |
|
147
|
|
|
* @param string $pageName |
|
148
|
|
|
* |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
|
|
public function paginate($perPage = 25, $pageName = 'page') |
|
152
|
|
|
{ |
|
153
|
|
|
$this->paginate = (int) $perPage; |
|
154
|
|
|
$this->pageName = $pageName; |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
|
|
public function disablePagination() |
|
163
|
|
|
{ |
|
164
|
|
|
$this->paginate = 0; |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return bool |
|
171
|
|
|
*/ |
|
172
|
|
|
public function usePagination() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->paginate > 0; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return array |
|
179
|
|
|
* @throws \Exception |
|
180
|
|
|
*/ |
|
181
|
|
|
public function toArray() |
|
182
|
|
|
{ |
|
183
|
|
|
$model = $this->getModelConfiguration(); |
|
184
|
|
|
|
|
185
|
|
|
$params = parent::toArray(); |
|
186
|
|
|
|
|
187
|
|
|
$params['creatable'] = $model->isCreatable(); |
|
188
|
|
|
$params['createUrl'] = $model->getCreateUrl($this->getParameters() + Request::all()); |
|
189
|
|
|
$params['collection'] = $this->getCollection(); |
|
190
|
|
|
|
|
191
|
|
|
$params['extensions'] = $this->getExtensions()->renderable()->sortByOrder(); |
|
192
|
|
|
$params['newEntryButtonText'] = $this->getNewEntryButtonText(); |
|
193
|
|
|
$params['panel_class'] = $this->getPanelClass(); |
|
194
|
|
|
|
|
195
|
|
|
return $params; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* $collection Collection|LengthAwarePaginator|Builder. |
|
200
|
|
|
*/ |
|
201
|
|
|
public function setCollection($collection) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->collection = $collection; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Apply offset and limit to the query. |
|
208
|
|
|
* |
|
209
|
|
|
* @param $query |
|
210
|
|
|
* @param \Illuminate\Http\Request $request |
|
211
|
|
|
*/ |
|
212
|
|
|
public function applyOffset($query, \Illuminate\Http\Request $request) |
|
213
|
|
|
{ |
|
214
|
|
|
$offset = $request->input('start', 0); |
|
215
|
|
|
$limit = $request->input('length', 10); |
|
216
|
|
|
|
|
217
|
|
|
if ($limit == -1) { |
|
218
|
|
|
return; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$query->offset((int) $offset)->limit((int) $limit); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Apply search to the query. |
|
226
|
|
|
* |
|
227
|
|
|
* @param Builder $query |
|
228
|
|
|
* @param \Illuminate\Http\Request $request |
|
229
|
|
|
*/ |
|
230
|
|
|
public function applySearch(Builder $query, \Illuminate\Http\Request $request) |
|
231
|
|
|
{ |
|
232
|
|
|
$search = $request->input('search.value'); |
|
233
|
|
|
if (empty($search)) { |
|
234
|
|
|
return; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$query->where(function (Builder $query) use ($search) { |
|
238
|
|
|
$columns = $this->getColumns()->all(); |
|
239
|
|
|
|
|
240
|
|
|
foreach ($columns as $column) { |
|
241
|
|
|
if ($column->isSearchable()) { |
|
242
|
|
|
if ($column instanceof ColumnInterface) { |
|
243
|
|
|
if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) { |
|
|
|
|
|
|
244
|
|
|
if (method_exists($metaInstance, 'onSearch')) { |
|
245
|
|
|
$metaInstance->onSearch($column, $query, $search); |
|
246
|
|
|
continue; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
if (is_callable($callback = $column->getSearchCallback())) { |
|
|
|
|
|
|
251
|
|
|
$callback($column, $query, $search); |
|
252
|
|
|
continue; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$query->orWhere($column->getName(), 'like', '%'.$search.'%'); |
|
|
|
|
|
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
}); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @return Collection|LengthAwarePaginator|Builder |
|
264
|
|
|
* @throws \Exception |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getCollection() |
|
267
|
|
|
{ |
|
268
|
|
|
if (! $this->isInitialized()) { |
|
269
|
|
|
throw new \Exception('Display is not initialized'); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
if (! is_null($this->collection)) { |
|
273
|
|
|
return $this->collection; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$query = $this->getRepository()->getQuery(); |
|
277
|
|
|
|
|
278
|
|
|
$this->modifyQuery($query); |
|
279
|
|
|
|
|
280
|
|
|
return $this->collection = $this->usePagination() |
|
|
|
|
|
|
281
|
|
|
? $query->paginate($this->paginate, ['*'], $this->pageName)->appends(request()->except($this->pageName)) |
|
282
|
|
|
: $query->get(); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|Builder $query |
|
287
|
|
|
*/ |
|
288
|
|
|
protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query) |
|
289
|
|
|
{ |
|
290
|
|
|
$this->extensions->modifyQuery($query); |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|