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