1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Request; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
8
|
|
|
use SleepingOwl\Admin\Contracts\ColumnInterface; |
9
|
|
|
use SleepingOwl\Admin\Display\Extension\Columns; |
10
|
|
|
use SleepingOwl\Admin\Display\Extension\ColumnFilters; |
11
|
|
|
use SleepingOwl\Admin\Contracts\ColumnFilterInterface; |
12
|
|
|
use SleepingOwl\Admin\Contracts\Display\DisplayExtensionInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DisplayTable. |
16
|
|
|
|
17
|
|
|
* @method Columns getColumns() |
18
|
|
|
* @method $this setColumns(ColumnInterface $column, ... $columns) |
19
|
|
|
* |
20
|
|
|
* @method ColumnFilters getColumnFilters() |
21
|
|
|
* @method $this setColumnFilters(ColumnFilterInterface $filters = null, ...$filters) |
22
|
|
|
*/ |
23
|
|
|
class DisplayTable extends Display |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $view = 'display.table'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $parameters = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int|null |
37
|
|
|
*/ |
38
|
|
|
protected $paginate = 25; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $pageName = 'page'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Collection |
47
|
|
|
*/ |
48
|
|
|
protected $collection; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string|null |
52
|
|
|
*/ |
53
|
|
|
protected $newEntryButtonText; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Display constructor. |
57
|
|
|
*/ |
58
|
|
|
public function __construct() |
59
|
|
|
{ |
60
|
|
|
parent::__construct(); |
61
|
|
|
|
62
|
|
|
$this->extend('columns', new Columns()); |
63
|
|
|
$this->extend('column_filters', new ColumnFilters()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialize display. |
68
|
|
|
*/ |
69
|
|
|
public function initialize() |
70
|
|
|
{ |
71
|
|
|
parent::initialize(); |
72
|
|
|
|
73
|
|
|
if ($this->getModelConfiguration()->isRestorableModel()) { |
74
|
|
|
$this->setApply(function ($q) { |
|
|
|
|
75
|
|
|
return $q->withTrashed(); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->setHtmlAttribute('class', 'table table-striped'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return null|string |
84
|
|
|
*/ |
85
|
|
|
public function getNewEntryButtonText() |
86
|
|
|
{ |
87
|
|
|
if (is_null($this->newEntryButtonText)) { |
88
|
|
|
$this->newEntryButtonText = trans('sleeping_owl::lang.table.new-entry'); |
|
|
|
|
89
|
|
|
} |
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
|
|
|
|
216
|
|
|
return $this->collection = $this->usePagination() |
|
|
|
|
217
|
|
|
? $query->paginate($this->paginate, ['*'], $this->pageName) |
218
|
|
|
: $query->get(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|Builder $query |
223
|
|
|
*/ |
224
|
|
|
protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query) |
225
|
|
|
{ |
226
|
|
|
$this->extensions->each(function (DisplayExtensionInterface $extension) use ($query) { |
227
|
|
|
$extension->modifyQuery($query); |
228
|
|
|
}); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
This check looks for function calls that miss required arguments.