Skip to content

Commit 5fd1f8b

Browse files
authored
Merge pull request #8 from jonasholfeld/sortable-structure-bugfix
Sortable structure bugfix
2 parents 1db553d + 1df4d0d commit 5fd1f8b

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

index.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@
5454
}
5555

5656
// Filtering deleted keys
57-
$oldForeignKeys = $oldPage->$relation()->toStructure();
58-
$newForeignKeys = $newPage->$relation()->toStructure();
59-
57+
$oldForeignKeys = $oldPage->$relation()->toStructure()->toArray();
58+
$newForeignKeys = $newPage->$relation()->toStructure()->toArray();
59+
$oldForeignKeys = array_map('unSetID', $oldForeignKeys);
60+
$newForeignKeys = array_map('unSetID', $newForeignKeys);
6061
$deletedForeignKeys = [];
61-
foreach($oldForeignKeys->toArray() as $oldForeignKey) {
62-
if (!in_array($oldForeignKey, $newForeignKeys->toArray())) {
62+
foreach($oldForeignKeys as $oldForeignKey) {
63+
if (!in_array($oldForeignKey, $newForeignKeys)) {
6364
array_push($deletedForeignKeys, $oldForeignKey);
6465
}
6566
}
@@ -161,13 +162,33 @@ function deleteRelation($page, $value, $relationField)
161162
}
162163
}
163164

164-
function relationIsChanged($newPage, $oldPage, $relation)
165+
function unSetID($value)
165166
{
166-
//Constructing safer strings for comparison
167-
$oldRelations = str_replace(["\n", "\r"], '', $oldPage->$relation()->toString());
168-
$newRelations = str_replace(["\n", "\r"], '', $newPage->$relation()->toString());
167+
$newValue = $value;
168+
$newValue['id'] = 0;
169+
return $newValue;
170+
}
169171

170-
return $newRelations != $oldRelations;
172+
function relationIsChanged($newPage, $oldPage, $relation)
173+
{
174+
175+
$change = false;
176+
$oldRelationsArray = $oldPage->$relation()->toStructure()->toArray();
177+
$oldRelationsArray = array_map('unSetID', $oldRelationsArray);
178+
$newRelationsArray = $newPage->$relation()->toStructure()->toArray();
179+
$newRelationsArray = array_map('unSetID', $newRelationsArray);
180+
181+
foreach($oldRelationsArray as $oldRelation) {
182+
if(!in_array($oldRelation, $newRelationsArray)) {
183+
$change = true;
184+
}
185+
}
186+
foreach($newRelationsArray as $newRelation) {
187+
if(!in_array($newRelation, $oldRelationsArray)) {
188+
$change = true;
189+
}
190+
}
191+
return $change;
171192
}
172193

173194
function addRelation($page, $value, $relationField)
@@ -201,4 +222,4 @@ function addRelation($page, $value, $relationField)
201222
return $e->getMessage();
202223
}
203224
}
204-
}
225+
}

0 commit comments

Comments
 (0)