-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
SortableTrait.php
214 lines (163 loc) · 6.12 KB
/
SortableTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Spatie\EloquentSortable;
use ArrayAccess;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Event;
use InvalidArgumentException;
trait SortableTrait
{
public static function bootSortableTrait()
{
static::creating(function ($model) {
if ($model instanceof Sortable && $model->shouldSortWhenCreating()) {
$model->setHighestOrderNumber();
}
});
}
public function setHighestOrderNumber(): void
{
$orderColumnName = $this->determineOrderColumnName();
$this->$orderColumnName = $this->getHighestOrderNumber() + 1;
}
public function getHighestOrderNumber(): int
{
return (int)$this->buildSortQuery()->max($this->determineOrderColumnName());
}
public function getLowestOrderNumber(): int
{
return (int)$this->buildSortQuery()->min($this->determineOrderColumnName());
}
public function scopeOrdered(Builder $query, string $direction = 'asc')
{
return $query->orderBy($this->determineOrderColumnName(), $direction);
}
public static function setNewOrder(
$ids,
int $startOrder = 1,
string $primaryKeyColumn = null,
callable $modifyQuery = null
): void {
if (! is_array($ids) && ! $ids instanceof ArrayAccess) {
throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder');
}
$model = new static();
$orderColumnName = $model->determineOrderColumnName();
if (is_null($primaryKeyColumn)) {
$primaryKeyColumn = $model->getQualifiedKeyName();
}
if (config('eloquent-sortable.ignore_timestamps', false)) {
static::$ignoreTimestampsOn = array_values(array_merge(static::$ignoreTimestampsOn, [static::class]));
}
foreach ($ids as $id) {
static::withoutGlobalScope(SoftDeletingScope::class)
->when(is_callable($modifyQuery), function ($query) use ($modifyQuery) {
return $modifyQuery($query);
})
->where($primaryKeyColumn, $id)
->update([$orderColumnName => $startOrder++]);
}
Event::dispatch(new EloquentModelSortedEvent(static::class));
if (config('eloquent-sortable.ignore_timestamps', false)) {
static::$ignoreTimestampsOn = array_values(array_diff(static::$ignoreTimestampsOn, [static::class]));
}
}
public static function setNewOrderByCustomColumn(string $primaryKeyColumn, $ids, int $startOrder = 1)
{
self::setNewOrder($ids, $startOrder, $primaryKeyColumn);
}
public function determineOrderColumnName(): string
{
return $this->sortable['order_column_name'] ?? config('eloquent-sortable.order_column_name', 'order_column');
}
/**
* Determine if the order column should be set when saving a new model instance.
*/
public function shouldSortWhenCreating(): bool
{
return $this->sortable['sort_when_creating'] ?? config('eloquent-sortable.sort_when_creating', true);
}
public function moveOrderDown(): static
{
$orderColumnName = $this->determineOrderColumnName();
$swapWithModel = $this->buildSortQuery()->limit(1)
->ordered()
->where($orderColumnName, '>', $this->$orderColumnName)
->first();
if (! $swapWithModel) {
return $this;
}
return $this->swapOrderWithModel($swapWithModel);
}
public function moveOrderUp(): static
{
$orderColumnName = $this->determineOrderColumnName();
$swapWithModel = $this->buildSortQuery()->limit(1)
->ordered('desc')
->where($orderColumnName, '<', $this->$orderColumnName)
->first();
if (! $swapWithModel) {
return $this;
}
return $this->swapOrderWithModel($swapWithModel);
}
public function swapOrderWithModel(Sortable $otherModel): static
{
$orderColumnName = $this->determineOrderColumnName();
$oldOrderOfOtherModel = $otherModel->$orderColumnName;
$otherModel->$orderColumnName = $this->$orderColumnName;
$otherModel->save();
$this->$orderColumnName = $oldOrderOfOtherModel;
$this->save();
return $this;
}
public static function swapOrder(Sortable $model, Sortable $otherModel): void
{
$model->swapOrderWithModel($otherModel);
}
public function moveToStart(): static
{
$firstModel = $this->buildSortQuery()->limit(1)
->ordered()
->first();
if ($firstModel->getKey() === $this->getKey()) {
return $this;
}
$orderColumnName = $this->determineOrderColumnName();
$this->$orderColumnName = $firstModel->$orderColumnName;
$this->save();
$this->buildSortQuery()->where($this->getQualifiedKeyName(), '!=', $this->getKey())->increment(
$orderColumnName
);
return $this;
}
public function moveToEnd(): static
{
$maxOrder = $this->getHighestOrderNumber();
$orderColumnName = $this->determineOrderColumnName();
if ($this->$orderColumnName === $maxOrder) {
return $this;
}
$oldOrder = $this->$orderColumnName;
$this->$orderColumnName = $maxOrder;
$this->save();
$this->buildSortQuery()->where($this->getQualifiedKeyName(), '!=', $this->getKey())
->where($orderColumnName, '>', $oldOrder)
->decrement($orderColumnName);
return $this;
}
public function isLastInOrder(): bool
{
$orderColumnName = $this->determineOrderColumnName();
return (int)$this->$orderColumnName === $this->getHighestOrderNumber();
}
public function isFirstInOrder(): bool
{
$orderColumnName = $this->determineOrderColumnName();
return (int)$this->$orderColumnName === $this->getLowestOrderNumber();
}
public function buildSortQuery(): Builder
{
return static::query();
}
}