1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Table\Repositories; |
4
|
|
|
|
5
|
|
|
use Yaro\Jarboe\Table\CRUD; |
6
|
|
|
|
7
|
|
|
class EloquentModelRepository implements ModelRepositoryInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var CRUD |
11
|
|
|
*/ |
12
|
|
|
private $crud; |
13
|
|
|
private $filter; |
14
|
|
|
private $perPage = 20; |
15
|
|
|
private $defaultOrder = [ |
16
|
|
|
'column' => false, |
17
|
|
|
'direction' => false, |
18
|
|
|
]; |
19
|
|
|
|
20
|
60 |
|
public function setCrud(CRUD $crud): ModelRepositoryInterface |
21
|
|
|
{ |
22
|
60 |
|
$this->crud = $crud; |
23
|
|
|
|
24
|
60 |
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
public function get() |
28
|
|
|
{ |
29
|
2 |
|
$model = $this->crud->getModel(); |
30
|
2 |
|
$model = $model::query(); |
31
|
|
|
|
32
|
2 |
|
$this->applyFilter($model); |
33
|
2 |
|
$this->applySearchFilters($model); |
34
|
|
|
|
35
|
2 |
|
if ($this->crud->isSortableByWeightActive()) { |
36
|
|
|
$this->applySortableOrder($model); |
37
|
|
|
} else { |
38
|
2 |
|
$this->applyOrder($model); |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
$this->applyPaginate($model); |
42
|
|
|
|
43
|
2 |
|
return $model; |
44
|
|
|
} |
45
|
|
|
|
46
|
25 |
|
public function find($id) |
47
|
|
|
{ |
48
|
25 |
|
$model = $this->crud->getModel(); |
49
|
25 |
|
$model = $model::query(); |
50
|
25 |
|
$this->applyFilter($model); |
51
|
|
|
|
52
|
25 |
|
$model = $model->find($id); |
53
|
25 |
|
if (!$model) { |
54
|
|
|
throw new \RuntimeException(sprintf('Not allowed or no record to edit [%s]', $id)); |
55
|
|
|
} |
56
|
|
|
|
57
|
25 |
|
return $model; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function delete($id): bool |
61
|
|
|
{ |
62
|
1 |
|
$model = $this->find($id); |
63
|
|
|
|
64
|
1 |
|
return $model->delete(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function store(array $data) |
68
|
|
|
{ |
69
|
1 |
|
$model = $this->crud->getModel(); |
70
|
|
|
|
71
|
1 |
|
$model = $model::create($data); |
72
|
|
|
|
73
|
1 |
|
return $model; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
public function update($id, array $data) |
77
|
|
|
{ |
78
|
2 |
|
$model = $this->find($id); |
79
|
|
|
|
80
|
2 |
|
$model->update($data); |
81
|
|
|
|
82
|
2 |
|
return $model; |
83
|
|
|
} |
84
|
|
|
|
85
|
58 |
|
public function filter(\Closure $callback) |
86
|
|
|
{ |
87
|
58 |
|
$this->filter = $callback; |
88
|
58 |
|
} |
89
|
|
|
|
90
|
|
|
public function order(string $column, string $direction) |
91
|
|
|
{ |
92
|
|
|
$this->defaultOrder = [ |
93
|
|
|
'column' => $column, |
94
|
|
|
'direction' => $direction, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
public function perPage(int $perPage = null) |
99
|
|
|
{ |
100
|
4 |
|
if (is_null($perPage)) { |
101
|
2 |
|
return $this->perPage; |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
$this->perPage = $perPage; |
105
|
3 |
|
} |
106
|
|
|
|
107
|
27 |
|
private function applyFilter($model) |
108
|
|
|
{ |
109
|
27 |
|
$callback = $this->filter; |
110
|
27 |
|
if ($callback) { |
111
|
27 |
|
$callback($model); |
112
|
|
|
} |
113
|
27 |
|
} |
114
|
|
|
|
115
|
2 |
|
private function applySearchFilters($model) |
116
|
|
|
{ |
117
|
2 |
|
foreach ($this->crud->getAllFieldObjects() as $field) { |
118
|
2 |
|
if ($field->filter()) { |
119
|
|
|
$field->filter()->apply($model, $field->name()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
2 |
|
} |
123
|
|
|
|
124
|
2 |
|
private function applyOrder($model) |
125
|
|
|
{ |
126
|
2 |
|
$shouldApplyDefaultOrder = true; |
127
|
2 |
|
foreach ($this->crud->getAllFieldObjects() as $field) { |
128
|
2 |
|
if ($field->isOrderable()) { |
129
|
|
|
$direction = $this->crud->getOrderFilterParam($field->name()); |
130
|
|
|
if (!is_null($direction)) { |
131
|
|
|
$callback = $field->getOverridedOrderCallback(); |
132
|
|
|
if ($callback) { |
133
|
|
|
$callback($model, $field, $direction, $shouldApplyDefaultOrder); |
134
|
|
|
} else { |
135
|
|
|
$model->orderBy($field->name(), $direction); |
136
|
|
|
$shouldApplyDefaultOrder = false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
if ($shouldApplyDefaultOrder && array_filter($this->defaultOrder)) { |
143
|
|
|
$model->orderBy($this->defaultOrder['column'], $this->defaultOrder['direction']); |
144
|
|
|
} |
145
|
2 |
|
} |
146
|
|
|
|
147
|
2 |
|
private function applyPaginate(&$model) |
148
|
|
|
{ |
149
|
2 |
|
$model = $model->paginate($this->perPage()); |
150
|
2 |
|
} |
151
|
|
|
|
152
|
|
|
private function applySortableOrder($model) |
153
|
|
|
{ |
154
|
|
|
$model->orderBy($this->crud->getSortableWeightFieldName(), 'asc'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function reorder($id, $idPrev, $idNext) |
158
|
|
|
{ |
159
|
|
|
$sort = $this->crud->getSortableWeightFieldName(); |
160
|
|
|
$model = $this->crud->getModel(); |
161
|
|
|
$query = $model::query(); |
162
|
|
|
$key = (new $model)->getKeyName(); |
163
|
|
|
|
164
|
|
|
if ($idPrev) { |
165
|
|
|
$weight = $this->find($idPrev)->$sort + 1; |
166
|
|
|
$this->find($id)->update([ |
167
|
|
|
$sort => $weight, |
168
|
|
|
]); |
169
|
|
|
|
170
|
|
|
$query->where($key, '!=', $id)->where($sort, '>=', $weight)->increment($sort); |
171
|
|
|
} elseif ($idNext) { |
172
|
|
|
$weight = $this->find($idNext)->$sort - 1; |
173
|
|
|
$this->find($id)->update([ |
174
|
|
|
$sort => $weight, |
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
$query->where($key, '!=', $id)->where($sort, '<=', $weight)->decrement($sort); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
2 |
|
public function restore($id) |
182
|
|
|
{ |
183
|
2 |
|
$model = $this->find($id); |
184
|
|
|
|
185
|
2 |
|
return $model->restore(); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
public function forceDelete($id) |
189
|
|
|
{ |
190
|
1 |
|
$model = $this->find($id); |
191
|
|
|
|
192
|
1 |
|
return $model->forceDelete(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|