1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EloquentSortable; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope; |
9
|
|
|
|
10
|
|
|
trait SortableTrait |
11
|
|
|
{ |
12
|
|
|
public static function bootSortableTrait() |
13
|
|
|
{ |
14
|
|
|
static::creating(function ($model) { |
|
|
|
|
15
|
|
|
if ($model instanceof Sortable && $model->shouldSortWhenCreating()) { |
16
|
|
|
$model->setHighestOrderNumber(); |
17
|
|
|
} |
18
|
|
|
}); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Modify the order column value. |
23
|
|
|
*/ |
24
|
|
|
public function setHighestOrderNumber() |
25
|
|
|
{ |
26
|
|
|
$orderColumnName = $this->determineOrderColumnName(); |
27
|
|
|
|
28
|
|
|
$this->$orderColumnName = $this->getHighestOrderNumber() + 1; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Determine the order value for the new record. |
33
|
|
|
*/ |
34
|
|
|
public function getHighestOrderNumber(): int |
35
|
|
|
{ |
36
|
|
|
return (int) $this->buildSortQuery()->max($this->determineOrderColumnName()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Let's be nice and provide an ordered scope. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
43
|
|
|
* @param string $direction |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Database\Query\Builder |
46
|
|
|
*/ |
47
|
|
|
public function scopeOrdered(Builder $query, string $direction = 'asc') |
48
|
|
|
{ |
49
|
|
|
return $query->orderBy($this->determineOrderColumnName(), $direction); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* This function reorders the records: the record with the first id in the array |
54
|
|
|
* will get order 1, the record with the second it will get order 2, ... |
55
|
|
|
* |
56
|
|
|
* A starting order number can be optionally supplied (defaults to 1). |
57
|
|
|
* |
58
|
|
|
* @param array|\ArrayAccess $ids |
59
|
|
|
* @param int $startOrder |
60
|
|
|
*/ |
61
|
|
|
public static function setNewOrder($ids, int $startOrder = 1) |
62
|
|
|
{ |
63
|
|
|
if (! is_array($ids) && ! $ids instanceof ArrayAccess) { |
64
|
|
|
throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$model = new static; |
68
|
|
|
|
69
|
|
|
$orderColumnName = $model->determineOrderColumnName(); |
70
|
|
|
$primaryKeyColumn = $model->getKeyName(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
foreach ($ids as $id) { |
73
|
|
|
static::withoutGlobalScope(SoftDeletingScope::class) |
74
|
|
|
->where($primaryKeyColumn, $id) |
75
|
|
|
->update([$orderColumnName => $startOrder++]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* |
80
|
|
|
* Determine the column name of the order column. |
81
|
|
|
*/ |
82
|
|
|
protected function determineOrderColumnName(): string |
83
|
|
|
{ |
84
|
|
|
if ( |
85
|
|
|
isset($this->sortable['order_column_name']) && |
86
|
|
|
! empty($this->sortable['order_column_name']) |
87
|
|
|
) { |
88
|
|
|
return $this->sortable['order_column_name']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return 'order_column'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/* |
95
|
|
|
* Determine the column name of the group column. |
96
|
|
|
*/ |
97
|
|
|
protected function determineGroupColumnName(): string |
98
|
|
|
{ |
99
|
|
|
if ( |
100
|
|
|
isset($this->sortable['group_column_name']) && |
101
|
|
|
! empty($this->sortable['group_column_name']) |
102
|
|
|
) { |
103
|
|
|
return $this->sortable['group_column_name']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return 'group_column'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Determine the order value for the new record. |
111
|
|
|
*/ |
112
|
|
|
public function getGroupColumnValue() |
113
|
|
|
{ |
114
|
|
|
$groupColumnName = $this->determineGroupColumnName(); |
115
|
|
|
return $this->$groupColumnName; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determine if it should be sorted by group column. |
120
|
|
|
*/ |
121
|
|
|
public function shouldSortByGroup(): bool |
122
|
|
|
{ |
123
|
|
|
return $this->sortable['sort_by_group'] ?? false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Determine if the order column should be set when saving a new model instance. |
128
|
|
|
*/ |
129
|
|
|
public function shouldSortWhenCreating(): bool |
130
|
|
|
{ |
131
|
|
|
return $this->sortable['sort_when_creating'] ?? true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Swaps the order of this model with the model 'below' this model. |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
View Code Duplication |
public function moveOrderDown() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$orderColumnName = $this->determineOrderColumnName(); |
142
|
|
|
|
143
|
|
|
$swapWithModel = $this->buildSortQuery()->limit(1) |
144
|
|
|
->ordered() |
145
|
|
|
->where($orderColumnName, '>', $this->$orderColumnName) |
146
|
|
|
->first(); |
147
|
|
|
|
148
|
|
|
if (! $swapWithModel) { |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->swapOrderWithModel($swapWithModel); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Swaps the order of this model with the model 'above' this model. |
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function moveOrderUp() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$orderColumnName = $this->determineOrderColumnName(); |
163
|
|
|
|
164
|
|
|
$swapWithModel = $this->buildSortQuery()->limit(1) |
165
|
|
|
->ordered('desc') |
166
|
|
|
->where($orderColumnName, '<', $this->$orderColumnName) |
167
|
|
|
->first(); |
168
|
|
|
|
169
|
|
|
if (! $swapWithModel) { |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->swapOrderWithModel($swapWithModel); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Swap the order of this model with the order of another model. |
178
|
|
|
* |
179
|
|
|
* @param \Spatie\EloquentSortable\Sortable $otherModel |
180
|
|
|
* |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
|
|
public function swapOrderWithModel(Sortable $otherModel) |
184
|
|
|
{ |
185
|
|
|
$orderColumnName = $this->determineOrderColumnName(); |
186
|
|
|
|
187
|
|
|
$oldOrderOfOtherModel = $otherModel->$orderColumnName; |
188
|
|
|
|
189
|
|
|
$otherModel->$orderColumnName = $this->$orderColumnName; |
190
|
|
|
$otherModel->save(); |
191
|
|
|
|
192
|
|
|
$this->$orderColumnName = $oldOrderOfOtherModel; |
193
|
|
|
$this->save(); |
|
|
|
|
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Swap the order of two models. |
200
|
|
|
* |
201
|
|
|
* @param \Spatie\EloquentSortable\Sortable $model |
202
|
|
|
* @param \Spatie\EloquentSortable\Sortable $otherModel |
203
|
|
|
*/ |
204
|
|
|
public static function swapOrder(Sortable $model, Sortable $otherModel) |
205
|
|
|
{ |
206
|
|
|
$model->swapOrderWithModel($otherModel); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Moves this model to the first position. |
211
|
|
|
* |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
|
|
public function moveToStart() |
215
|
|
|
{ |
216
|
|
|
$firstModel = $this->buildSortQuery()->limit(1) |
217
|
|
|
->ordered() |
218
|
|
|
->first(); |
219
|
|
|
|
220
|
|
|
if ($firstModel->id === $this->id) { |
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$orderColumnName = $this->determineOrderColumnName(); |
225
|
|
|
|
226
|
|
|
$this->$orderColumnName = $firstModel->$orderColumnName; |
227
|
|
|
$this->save(); |
|
|
|
|
228
|
|
|
|
229
|
|
|
$this->buildSortQuery()->where($this->getKeyName(), '!=', $this->id)->increment($orderColumnName); |
|
|
|
|
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Moves this model to the last position. |
236
|
|
|
* |
237
|
|
|
* @return $this |
238
|
|
|
*/ |
239
|
|
|
public function moveToEnd() |
240
|
|
|
{ |
241
|
|
|
$maxOrder = $this->getHighestOrderNumber(); |
242
|
|
|
|
243
|
|
|
$orderColumnName = $this->determineOrderColumnName(); |
244
|
|
|
|
245
|
|
|
if ($this->$orderColumnName === $maxOrder) { |
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$oldOrder = $this->$orderColumnName; |
250
|
|
|
|
251
|
|
|
$this->$orderColumnName = $maxOrder; |
252
|
|
|
$this->save(); |
|
|
|
|
253
|
|
|
|
254
|
|
|
$this->buildSortQuery()->where($this->getKeyName(), '!=', $this->id) |
|
|
|
|
255
|
|
|
->where($orderColumnName, '>', $oldOrder) |
256
|
|
|
->decrement($orderColumnName); |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Build eloquent builder of sortable. |
263
|
|
|
* |
264
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
265
|
|
|
*/ |
266
|
|
|
public function buildSortQuery() |
267
|
|
|
{ |
268
|
|
|
if ($this->shouldSortByGroup() && $groupColumnName = $this->determineGroupColumnName()) { |
269
|
|
|
return static::query()->where($groupColumnName, $this->getGroupColumnValue()); |
270
|
|
|
} else { |
271
|
|
|
return static::query(); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.