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 Illuminate\Contracts\Support\Renderable; |
9
|
|
|
use SleepingOwl\Admin\Contracts\ColumnInterface; |
10
|
|
|
use SleepingOwl\Admin\Display\Extension\Columns; |
11
|
|
|
use SleepingOwl\Admin\Contracts\ColumnFilterInterface; |
12
|
|
|
use SleepingOwl\Admin\Display\Extension\ColumnFilters; |
13
|
|
|
use SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class DisplayTable. |
17
|
|
|
|
18
|
|
|
* @method Columns getColumns() |
19
|
|
|
* @method $this setColumns(ColumnInterface|ColumnInterface[] $column) |
20
|
|
|
* |
21
|
|
|
* @method ColumnFilters getColumnFilters() |
22
|
|
|
* @method $this setColumnFilters(ColumnFilterInterface $filters = null, ...$filters) |
23
|
|
|
*/ |
24
|
|
|
class DisplayTable extends Display |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $view = 'display.table'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $parameters = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int|null |
38
|
|
|
*/ |
39
|
|
|
protected $paginate = 25; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $pageName = 'page'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Collection |
48
|
|
|
*/ |
49
|
|
|
protected $collection; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string|null |
53
|
|
|
*/ |
54
|
|
|
protected $newEntryButtonText; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Display constructor. |
58
|
|
|
*/ |
59
|
|
|
public function __construct() |
60
|
|
|
{ |
61
|
|
|
parent::__construct(); |
62
|
|
|
|
63
|
|
|
$this->extend('columns', new Columns()); |
64
|
|
|
$this->extend('column_filters', new ColumnFilters()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Initialize display. |
69
|
|
|
*/ |
70
|
|
|
public function initialize() |
71
|
|
|
{ |
72
|
|
|
parent::initialize(); |
73
|
|
|
|
74
|
|
|
if ($this->getModelConfiguration()->isRestorableModel()) { |
75
|
|
|
$this->setApply(function ($q) { |
|
|
|
|
76
|
|
|
return $q->withTrashed(); |
77
|
|
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->setHtmlAttribute('class', 'table table-striped'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return null|string |
85
|
|
|
*/ |
86
|
|
|
public function getNewEntryButtonText() |
87
|
|
|
{ |
88
|
|
|
if (is_null($this->newEntryButtonText)) { |
89
|
|
|
$this->newEntryButtonText = trans('sleeping_owl::lang.table.new-entry'); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->newEntryButtonText; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $newEntryButtonText |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setNewEntryButtonText($newEntryButtonText) |
101
|
|
|
{ |
102
|
|
|
$this->newEntryButtonText = $newEntryButtonText; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getParameters() |
111
|
|
|
{ |
112
|
|
|
return $this->parameters; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $parameters |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function setParameters($parameters) |
121
|
|
|
{ |
122
|
|
|
$this->parameters = $parameters; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $key |
129
|
|
|
* @param mixed $value |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function setParameter($key, $value) |
134
|
|
|
{ |
135
|
|
|
$this->parameters[$key] = $value; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param int $perPage |
142
|
|
|
* @param string $pageName |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function paginate($perPage = 25, $pageName = 'page') |
147
|
|
|
{ |
148
|
|
|
$this->paginate = (int) $perPage; |
149
|
|
|
$this->pageName = $pageName; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function disablePagination() |
158
|
|
|
{ |
159
|
|
|
$this->paginate = 0; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function usePagination() |
168
|
|
|
{ |
169
|
|
|
return $this->paginate > 0; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function toArray() |
176
|
|
|
{ |
177
|
|
|
$model = $this->getModelConfiguration(); |
178
|
|
|
|
179
|
|
|
$params = parent::toArray(); |
180
|
|
|
|
181
|
|
|
$params['creatable'] = $model->isCreatable(); |
182
|
|
|
$params['createUrl'] = $model->getCreateUrl($this->getParameters() + Request::all()); |
183
|
|
|
$params['collection'] = $this->getCollection(); |
184
|
|
|
|
185
|
|
|
$params['extensions'] = $this->getExtensions() |
186
|
|
|
->filter(function (DisplayExtensionInterface $ext) { |
187
|
|
|
return $ext instanceof Renderable; |
188
|
|
|
}) |
189
|
|
|
->sortBy(function (DisplayExtensionInterface $extension) { |
190
|
|
|
return $extension->getOrder(); |
191
|
|
|
}); |
192
|
|
|
|
193
|
|
|
$params['newEntryButtonText'] = $this->getNewEntryButtonText(); |
194
|
|
|
|
195
|
|
|
return $params; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return Collection |
200
|
|
|
* @throws \Exception |
201
|
|
|
*/ |
202
|
|
|
public function getCollection() |
203
|
|
|
{ |
204
|
|
|
if (! $this->isInitialized()) { |
205
|
|
|
throw new \Exception('Display is not initialized'); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (! is_null($this->collection)) { |
209
|
|
|
return $this->collection; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$query = $this->getRepository()->getQuery(); |
213
|
|
|
|
214
|
|
|
$this->modifyQuery($query); |
215
|
|
|
$this->applyOrders($query); |
216
|
|
|
|
217
|
|
|
return $this->collection = $this->usePagination() |
|
|
|
|
218
|
|
|
? $query->paginate($this->paginate, ['*'], $this->pageName)->appends(request()->except($this->pageName)) |
219
|
|
|
: $query->get(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Apply orders to the query. |
224
|
|
|
* |
225
|
|
|
* @param $query |
226
|
|
|
*/ |
227
|
|
|
protected function applyOrders(Builder $query) |
228
|
|
|
{ |
229
|
|
|
$orders = Request::input('order', []); |
230
|
|
|
|
231
|
|
|
$columns = $this->getColumns()->all(); |
232
|
|
|
|
233
|
|
|
if (! is_int(key($orders))) { |
234
|
|
|
$orders = [$orders]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
foreach ($orders as $order) { |
238
|
|
|
$columnIndex = array_get($order, 'column'); |
239
|
|
|
$direction = array_get($order, 'dir', 'asc'); |
240
|
|
|
|
241
|
|
|
if (! $columnIndex && $columnIndex !== '0') { |
242
|
|
|
continue; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$column = $columns->get($columnIndex); |
246
|
|
|
|
247
|
|
|
if ($column instanceof ColumnInterface && $column->isOrderable()) { |
248
|
|
|
$column->orderBy($query, $direction); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|Builder $query |
255
|
|
|
*/ |
256
|
|
|
protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query) |
257
|
|
|
{ |
258
|
|
|
$this->extensions->each(function (DisplayExtensionInterface $extension) use ($query) { |
259
|
|
|
$extension->modifyQuery($query); |
260
|
|
|
}); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
This check looks for function calls that miss required arguments.