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\Pagination\LengthAwarePaginator; |
9
|
|
|
use SleepingOwl\Admin\Display\Extension\Columns; |
10
|
|
|
use SleepingOwl\Admin\Display\Extension\ColumnsTotal; |
11
|
|
|
use SleepingOwl\Admin\Display\Extension\ColumnFilters; |
12
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnInterface; |
13
|
|
|
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface; |
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
|
|
|
$this->extend('columns_total', new ColumnsTotal()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initialize display. |
70
|
|
|
*/ |
71
|
|
|
public function initialize() |
72
|
|
|
{ |
73
|
|
|
parent::initialize(); |
74
|
|
|
|
75
|
|
|
if ($this->getModelConfiguration()->isRestorableModel()) { |
76
|
|
|
$this->setApply(function (Builder $q) { |
77
|
|
|
return $q->withTrashed(); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->setHtmlAttribute('class', 'table table-striped'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array|\Illuminate\Contracts\Translation\Translator|null|string |
86
|
|
|
*/ |
87
|
|
|
public function getNewEntryButtonText() |
88
|
|
|
{ |
89
|
|
|
if (is_null($this->newEntryButtonText)) { |
90
|
|
|
$this->newEntryButtonText = trans('sleeping_owl::lang.table.new-entry'); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->newEntryButtonText; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $newEntryButtonText |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setNewEntryButtonText($newEntryButtonText) |
102
|
|
|
{ |
103
|
|
|
$this->newEntryButtonText = $newEntryButtonText; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getParameters() |
112
|
|
|
{ |
113
|
|
|
return $this->parameters; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $parameters |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function setParameters($parameters) |
122
|
|
|
{ |
123
|
|
|
$this->parameters = $parameters; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $key |
130
|
|
|
* @param mixed $value |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function setParameter($key, $value) |
135
|
|
|
{ |
136
|
|
|
$this->parameters[$key] = $value; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $perPage |
143
|
|
|
* @param string $pageName |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function paginate($perPage = 25, $pageName = 'page') |
148
|
|
|
{ |
149
|
|
|
$this->paginate = (int) $perPage; |
150
|
|
|
$this->pageName = $pageName; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function disablePagination() |
159
|
|
|
{ |
160
|
|
|
$this->paginate = 0; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function usePagination() |
169
|
|
|
{ |
170
|
|
|
return $this->paginate > 0; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return array |
175
|
|
|
* @throws \Exception |
176
|
|
|
*/ |
177
|
|
|
public function toArray() |
178
|
|
|
{ |
179
|
|
|
$model = $this->getModelConfiguration(); |
180
|
|
|
|
181
|
|
|
$params = parent::toArray(); |
182
|
|
|
|
183
|
|
|
$params['creatable'] = $model->isCreatable(); |
184
|
|
|
$params['createUrl'] = $model->getCreateUrl($this->getParameters() + Request::all()); |
185
|
|
|
$params['collection'] = $this->getCollection(); |
186
|
|
|
|
187
|
|
|
$params['extensions'] = $this->getExtensions()->renderable()->sortByOrder(); |
188
|
|
|
$params['newEntryButtonText'] = $this->getNewEntryButtonText(); |
189
|
|
|
|
190
|
|
|
return $params; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* $collection Collection|LengthAwarePaginator|Builder. |
195
|
|
|
*/ |
196
|
|
|
public function setCollection($collection) |
197
|
|
|
{ |
198
|
|
|
$this->collection = $collection; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return Collection|LengthAwarePaginator|Builder |
203
|
|
|
* @throws \Exception |
204
|
|
|
*/ |
205
|
|
|
public function getCollection() |
206
|
|
|
{ |
207
|
|
|
if (! $this->isInitialized()) { |
208
|
|
|
throw new \Exception('Display is not initialized'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (! is_null($this->collection)) { |
212
|
|
|
return $this->collection; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$query = $this->getRepository()->getQuery(); |
216
|
|
|
|
217
|
|
|
$this->modifyQuery($query); |
218
|
|
|
|
219
|
|
|
return $this->collection = $this->usePagination() |
|
|
|
|
220
|
|
|
? $query->paginate($this->paginate, ['*'], $this->pageName)->appends(request()->except($this->pageName)) |
221
|
|
|
: $query->get(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|Builder $query |
226
|
|
|
*/ |
227
|
|
|
protected function modifyQuery(\Illuminate\Database\Eloquent\Builder $query) |
228
|
|
|
{ |
229
|
|
|
$this->extensions->modifyQuery($query); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.