1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Extension; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
6
|
|
|
use Illuminate\Contracts\Support\Renderable; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnInterface; |
10
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnMetaInterface; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
12
|
|
|
use SleepingOwl\Admin\Display\Column\Control; |
13
|
|
|
|
14
|
|
|
class Columns extends Extension implements Initializable, Renderable |
15
|
|
|
{ |
16
|
|
|
use \SleepingOwl\Admin\Traits\Renderable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ColumnInterface[]|Collection |
20
|
|
|
*/ |
21
|
|
|
protected $columns; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $controlActive = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $view = 'display.extensions.columns'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $initialize = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Control |
40
|
|
|
*/ |
41
|
|
|
protected $controlColumn; |
42
|
|
|
|
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->columns = new Collection(); |
46
|
|
|
|
47
|
|
|
$this->setControlColumn(app('sleeping_owl.table.column')->control()); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ColumnInterface $controlColumn |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function setControlColumn(ColumnInterface $controlColumn) |
56
|
|
|
{ |
57
|
|
|
$this->controlColumn = $controlColumn; |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Control |
64
|
|
|
*/ |
65
|
|
|
public function getControlColumn() |
66
|
|
|
{ |
67
|
|
|
return $this->controlColumn; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function isControlActive() |
74
|
|
|
{ |
75
|
|
|
return $this->controlActive; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function enableControls() |
82
|
|
|
{ |
83
|
|
|
$this->controlActive = true; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function disableControls() |
92
|
|
|
{ |
93
|
|
|
$this->controlActive = false; |
94
|
|
|
|
95
|
|
|
if ($this->isInitialize()) { |
96
|
|
|
$this->columns = $this->columns->filter(function ($column) { |
97
|
|
|
$class = get_class($this->getControlColumn()); |
98
|
|
|
|
99
|
|
|
return ! ($column instanceof $class); |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Collection|\SleepingOwl\Admin\Contracts\Display\ColumnInterface[] |
108
|
|
|
*/ |
109
|
|
|
public function all() |
110
|
|
|
{ |
111
|
|
|
return $this->columns; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $columns |
116
|
|
|
* |
117
|
|
|
* @return \SleepingOwl\Admin\Contracts\Display\DisplayInterface |
118
|
|
|
*/ |
119
|
|
|
public function set($columns) |
120
|
|
|
{ |
121
|
|
|
if (! is_array($columns)) { |
122
|
|
|
$columns = func_get_args(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
foreach ($columns as $column) { |
126
|
|
|
$this->push($column); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->getDisplay(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param ColumnInterface $column |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function push(ColumnInterface $column) |
138
|
|
|
{ |
139
|
|
|
$this->columns->push($column); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isInitialize() |
148
|
|
|
{ |
149
|
|
|
return $this->initialize; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the instance as an array. |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function toArray() |
158
|
|
|
{ |
159
|
|
|
return [ |
160
|
|
|
'columns' => $this->all(), |
161
|
|
|
'attributes' => $this->getDisplay()->htmlAttributesToString(), |
|
|
|
|
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function initialize() |
166
|
|
|
{ |
167
|
|
|
$this->all()->each(function (ColumnInterface $column) { |
168
|
|
|
$column->initialize(); |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
if ($this->isControlActive()) { |
172
|
|
|
$this->push($this->getControlColumn()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->initialize = true; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the evaluated contents of the object. |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function render() |
184
|
|
|
{ |
185
|
|
|
$params = $this->toArray(); |
186
|
|
|
$params['collection'] = $this->getDisplay()->getCollection(); |
|
|
|
|
187
|
|
|
$params['pagination'] = null; |
188
|
|
|
|
189
|
|
|
if ($params['collection'] instanceof Paginator) { |
190
|
|
|
if (class_exists('Illuminate\Pagination\BootstrapThreePresenter')) { |
191
|
|
|
$params['pagination'] = (new \Illuminate\Pagination\BootstrapThreePresenter($params['collection']))->render(); |
|
|
|
|
192
|
|
|
} else { |
193
|
|
|
$params['pagination'] = $params['collection']->render(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return app('sleeping_owl.template')->view($this->getView(), $params)->render(); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
202
|
|
|
*/ |
203
|
|
|
public function modifyQuery(\Illuminate\Database\Eloquent\Builder $query) |
204
|
|
|
{ |
205
|
|
|
$orders = app('request')->get('order', []); |
206
|
|
|
|
207
|
|
|
$columns = $this->all(); |
208
|
|
|
|
209
|
|
|
if (! is_int(key($orders))) { |
210
|
|
|
$orders = [$orders]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$_model = $query->getModel(); |
214
|
|
|
|
215
|
|
|
foreach ($orders as $order) { |
216
|
|
|
$columnIndex = Arr::get($order, 'column'); |
217
|
|
|
$direction = Arr::get($order, 'dir', 'asc'); |
218
|
|
|
|
219
|
|
|
if (! $columnIndex && $columnIndex !== '0') { |
220
|
|
|
continue; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$column = $columns->get($columnIndex); |
224
|
|
|
|
225
|
|
|
if ($column instanceof ColumnInterface && $column->isOrderable()) { |
226
|
|
|
if ($column instanceof ColumnInterface) { |
227
|
|
|
if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) { |
|
|
|
|
228
|
|
|
if (method_exists($metaInstance, 'onOrderBy')) { |
229
|
|
|
$metaInstance->onOrderBy($column, $query, $direction); |
230
|
|
|
continue; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if (is_callable($callback = $column->getOrderCallback())) { |
|
|
|
|
235
|
|
|
$callback($column, $query, $direction); |
236
|
|
|
continue; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ($_model->getAttribute($column->getName())) { |
241
|
|
|
continue; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$column->orderBy($query, $direction); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.