$id, 'sort_order' => $sortOrder]; $groupIds[] = $id; continue; } $bookmarkPairs[] = ['id' => $id, 'sort_order' => $sortOrder]; $bookmarkIds[] = $id; } if ($groupPairs === [] && $bookmarkPairs === []) { return false; } if ($groupIds !== []) { $uniqueGroupIds = array_values(array_unique($groupIds)); $groupCount = DB::selectValue( 'select count(*) from user_bookmark_groups where user_id = ? and id in (???)', (string) $userId, array_map('strval', $uniqueGroupIds) ); if ((int) $groupCount !== count($uniqueGroupIds)) { return false; } } if ($bookmarkIds !== []) { $uniqueBookmarkIds = array_values(array_unique($bookmarkIds)); $bookmarkCount = DB::selectValue( 'select count(*) from user_bookmarks where user_id = ? and group_id is NULL and id in (???)', (string) $userId, array_map('strval', $uniqueBookmarkIds) ); if ((int) $bookmarkCount !== count($uniqueBookmarkIds)) { return false; } } $db = DB::handle(); try { $db->begin_transaction(); foreach ($groupPairs as $pair) { $updated = DB::update( 'update user_bookmark_groups set sort_order = ? where id = ? and user_id = ?', (string) $pair['sort_order'], (string) $pair['id'], (string) $userId ); if ($updated === false) { $db->rollback(); return false; } } foreach ($bookmarkPairs as $pair) { $updated = DB::update( 'update user_bookmarks set sort_order = ? where id = ? and user_id = ? and group_id is NULL', (string) $pair['sort_order'], (string) $pair['id'], (string) $userId ); if ($updated === false) { $db->rollback(); return false; } } $db->commit(); return true; } catch (\Throwable) { try { $db->rollback(); } catch (\Throwable) { // no-op } return false; } } }