Skip to content

Commit

Permalink
Merge pull request #8 from jonasholfeld/sortable-structure-bugfix
Browse files Browse the repository at this point in the history
Sortable structure bugfix
  • Loading branch information
jonasholfeld authored Oct 27, 2021
2 parents 1db553d + 1df4d0d commit 5fd1f8b
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@
}

// Filtering deleted keys
$oldForeignKeys = $oldPage->$relation()->toStructure();
$newForeignKeys = $newPage->$relation()->toStructure();

$oldForeignKeys = $oldPage->$relation()->toStructure()->toArray();
$newForeignKeys = $newPage->$relation()->toStructure()->toArray();
$oldForeignKeys = array_map('unSetID', $oldForeignKeys);
$newForeignKeys = array_map('unSetID', $newForeignKeys);
$deletedForeignKeys = [];
foreach($oldForeignKeys->toArray() as $oldForeignKey) {
if (!in_array($oldForeignKey, $newForeignKeys->toArray())) {
foreach($oldForeignKeys as $oldForeignKey) {
if (!in_array($oldForeignKey, $newForeignKeys)) {
array_push($deletedForeignKeys, $oldForeignKey);
}
}
Expand Down Expand Up @@ -161,13 +162,33 @@ function deleteRelation($page, $value, $relationField)
}
}

function relationIsChanged($newPage, $oldPage, $relation)
function unSetID($value)
{
//Constructing safer strings for comparison
$oldRelations = str_replace(["\n", "\r"], '', $oldPage->$relation()->toString());
$newRelations = str_replace(["\n", "\r"], '', $newPage->$relation()->toString());
$newValue = $value;
$newValue['id'] = 0;
return $newValue;
}

return $newRelations != $oldRelations;
function relationIsChanged($newPage, $oldPage, $relation)
{

$change = false;
$oldRelationsArray = $oldPage->$relation()->toStructure()->toArray();
$oldRelationsArray = array_map('unSetID', $oldRelationsArray);
$newRelationsArray = $newPage->$relation()->toStructure()->toArray();
$newRelationsArray = array_map('unSetID', $newRelationsArray);

foreach($oldRelationsArray as $oldRelation) {
if(!in_array($oldRelation, $newRelationsArray)) {
$change = true;
}
}
foreach($newRelationsArray as $newRelation) {
if(!in_array($newRelation, $oldRelationsArray)) {
$change = true;
}
}
return $change;
}

function addRelation($page, $value, $relationField)
Expand Down Expand Up @@ -201,4 +222,4 @@ function addRelation($page, $value, $relationField)
return $e->getMessage();
}
}
}
}

0 comments on commit 5fd1f8b

Please sign in to comment.