function addSortOrder($post, $option, $name) { if ($_POST[$post] == $option) { $query = $name; $query .= checkSortOrder(); return $query; } else { return ""; } }
/** * Recursive function checking the nested set order against the sort_order. * Starting with root node, every child tree is checked. * @param Opus_Db_Collections $collectionsTable * @param int $collectionId ID of root collection */ function checkSortOrder(Opus_Db_Collections $collectionsTable, $collectionId) { $childrenSelect = $collectionsTable->select()->where("parent_id = ?", $collectionId)->order("sort_order ASC"); $unsortedChildren = $sortedChildren = $collectionsTable->fetchAll($childrenSelect)->toArray(); if (!isSortOrderSet($unsortedChildren)) { return true; } foreach ($unsortedChildren as $child) { $result = checkSortOrder($collectionsTable, $child['id']); } if ($result) { usort($sortedChildren, function ($a, $b) { if ($a['left_id'] == $b['left_id']) { return 0; } return $a['left_id'] < $b['left_id'] ? -1 : 1; }); foreach ($sortedChildren as $pos => $child) { if ($unsortedChildren[$pos]['id'] != $child['id']) { return false; } } } return true; }