1 | <?php |
||||||
2 | |||||||
3 | namespace SleepingOwl\Admin\Traits; |
||||||
4 | |||||||
5 | use Illuminate\Database\Eloquent\Builder; |
||||||
6 | use Illuminate\Database\Eloquent\Model; |
||||||
7 | |||||||
8 | /** |
||||||
9 | * Trait OrderableModel. |
||||||
10 | * @method static $this orderModel() |
||||||
11 | * @method Builder findByPosition($position) |
||||||
12 | */ |
||||||
13 | trait OrderableModel |
||||||
14 | { |
||||||
15 | /** |
||||||
16 | * Boot trait. |
||||||
17 | */ |
||||||
18 | protected static function bootOrderableModel() |
||||||
19 | { |
||||||
20 | static::creating(function (Model $row) { |
||||||
21 | $row->updateOrderFieldOnCreate(); |
||||||
22 | }); |
||||||
23 | |||||||
24 | static::deleted(function (Model $row) { |
||||||
25 | $row->updateOrderFieldOnDelete(); |
||||||
26 | }); |
||||||
27 | |||||||
28 | if (in_array("Illuminate\Database\Eloquent\SoftDeletes", trait_uses_recursive(new static()))) { |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
29 | static::restoring(function (Model $row) { |
||||||
30 | $row->updateOrderFieldOnRestore(); |
||||||
31 | }); |
||||||
32 | } |
||||||
33 | } |
||||||
34 | |||||||
35 | /** |
||||||
36 | * Get order value. |
||||||
37 | * @return int |
||||||
38 | */ |
||||||
39 | public function getOrderValue() |
||||||
40 | { |
||||||
41 | return $this->{$this->getOrderField()}; |
||||||
42 | } |
||||||
43 | |||||||
44 | /** |
||||||
45 | * Move model up. |
||||||
46 | */ |
||||||
47 | public function moveUp() |
||||||
48 | { |
||||||
49 | $this->move(1); |
||||||
50 | } |
||||||
51 | |||||||
52 | /** |
||||||
53 | * Move model down. |
||||||
54 | */ |
||||||
55 | public function moveDown() |
||||||
56 | { |
||||||
57 | $this->move(-1); |
||||||
58 | } |
||||||
59 | |||||||
60 | /** |
||||||
61 | * Move model in the $destination. |
||||||
62 | * |
||||||
63 | * @param $destination -1 (move down) or 1 (move up) |
||||||
0 ignored issues
–
show
|
|||||||
64 | */ |
||||||
65 | protected function move($destination) |
||||||
66 | { |
||||||
67 | if ($previousRow = static::orderModel()->findByPosition($this->getOrderValue() - $destination)->first()) { |
||||||
68 | $previousRow->{$this->getOrderField()} += $destination; |
||||||
69 | $previousRow->save(); |
||||||
70 | } |
||||||
71 | |||||||
72 | $this->{$this->getOrderField()} -= $destination; |
||||||
73 | $this->save(); |
||||||
0 ignored issues
–
show
It seems like
save() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
74 | } |
||||||
75 | |||||||
76 | /** |
||||||
77 | * Update order field on create. |
||||||
78 | */ |
||||||
79 | protected function updateOrderFieldOnCreate() |
||||||
80 | { |
||||||
81 | $this->{$this->getOrderField()} = static::orderModel()->count(); |
||||||
0 ignored issues
–
show
It seems like
count() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
82 | } |
||||||
83 | |||||||
84 | /** |
||||||
85 | * Update order field on delete. |
||||||
86 | */ |
||||||
87 | protected function updateOrderFieldOnDelete() |
||||||
88 | { |
||||||
89 | static::orderModel() |
||||||
90 | ->where($this->getOrderField(), '>', $this->getOrderValue()) |
||||||
0 ignored issues
–
show
It seems like
where() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
91 | ->decrement($this->getOrderField()); |
||||||
92 | } |
||||||
93 | |||||||
94 | /** |
||||||
95 | * Update order field on restore. |
||||||
96 | */ |
||||||
97 | protected function updateOrderFieldOnRestore() |
||||||
98 | { |
||||||
99 | static::orderModel() |
||||||
100 | ->where($this->getOrderField(), '>', $this->getOrderValue()) |
||||||
101 | ->increment($this->getOrderField()); |
||||||
102 | } |
||||||
103 | |||||||
104 | /** |
||||||
105 | * Order scope. |
||||||
106 | * |
||||||
107 | * @param $query |
||||||
108 | * |
||||||
109 | * @return mixed |
||||||
110 | */ |
||||||
111 | public function scopeOrderModel($query) |
||||||
112 | { |
||||||
113 | $parentFields = $this->getParentFieldName(); |
||||||
114 | if ($parentFields) { |
||||||
115 | $parentFields = (array) $parentFields; |
||||||
116 | foreach ($parentFields as $parentFieldName) { |
||||||
117 | $query->where($parentFieldName, $this->{$parentFieldName}); |
||||||
118 | } |
||||||
119 | } |
||||||
120 | |||||||
121 | return $query->orderBy($this->getOrderField(), 'ASC'); |
||||||
122 | } |
||||||
123 | |||||||
124 | /** |
||||||
125 | * @param Builder $query |
||||||
126 | * @param int $position |
||||||
127 | * |
||||||
128 | * @return mixed |
||||||
129 | */ |
||||||
130 | public function scopeFindByPosition(Builder $query, $position) |
||||||
131 | { |
||||||
132 | $query->where($this->getOrderField(), $position); |
||||||
133 | } |
||||||
134 | |||||||
135 | /** |
||||||
136 | * Get order field name. |
||||||
137 | * |
||||||
138 | * @return string |
||||||
139 | */ |
||||||
140 | public function getOrderField() |
||||||
141 | { |
||||||
142 | return $this->orderField ?: 'order'; |
||||||
143 | } |
||||||
144 | |||||||
145 | /** |
||||||
146 | * Get parent order field name. |
||||||
147 | * |
||||||
148 | * @return string |
||||||
149 | */ |
||||||
150 | public function getParentFieldName() |
||||||
151 | { |
||||||
152 | return $this->parentFieldName ?: null; |
||||||
153 | } |
||||||
154 | } |
||||||
155 |