Refactored book sort using collections

This commit is contained in:
Dan Brown 2018-01-13 16:44:47 +00:00
parent a77756a2da
commit 281da59bae
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9

View File

@ -190,71 +190,56 @@ class BookController extends Controller
} }
// Sort pages and chapters // Sort pages and chapters
$sortedBooks = []; $sortMap = collect(json_decode($request->get('sort-tree')));
$updatedModels = collect(); $bookIdsInvolved = collect([$book->id]);
$sortMap = json_decode($request->get('sort-tree'));
$defaultBookId = $book->id;
// Check permissions for all target and source books // Load models into map
$permissionsList = [$book->id]; $sortMap->each(function($mapItem) use ($bookIdsInvolved) {
foreach ($sortMap as $bookChild) { $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
// Check permission for target book $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
if (!in_array($bookChild->book, $permissionsList)) { // Store source and target books
$targetBook = $this->entityRepo->getById('book', $bookChild->book); $bookIdsInvolved->push(intval($mapItem->model->book_id));
if (!empty($targetBook)) { $bookIdsInvolved->push(intval($mapItem->book));
$bookId = $targetBook->id; });
$this->checkOwnablePermission('book-update', $targetBook);
// cache the permission for future use.
$permissionsList[] = $bookId;
}
}
// Check permissions for the source book // Get the books involved in the sort
$id = intval($bookChild->id); $bookIdsInvolved = $bookIdsInvolved->unique()->toArray();
$isPage = $bookChild->type == 'page'; $booksInvolved = $this->entityRepo->book->newQuery()->whereIn('id', $bookIdsInvolved)->get();
$model = $this->entityRepo->getById($isPage?'page':'chapter', $id); // Throw permission error if invalid ids or inaccessible books given.
$sourceBook = $model->book; if (count($bookIdsInvolved) !== count($booksInvolved)) {
if (!in_array($sourceBook->id, $permissionsList)) { $this->showPermissionError();
$this->checkOwnablePermission('book-update', $sourceBook);
// cache the permission for future use.
$permissionsList[] = $sourceBook->id;
}
} }
// Check permissions of involved books
$booksInvolved->each(function(Book $book) {
$this->checkOwnablePermission('book-update', $book);
});
// Loop through contents of provided map and update entities accordingly // Perform the sort
foreach ($sortMap as $bookChild) { $sortMap->each(function($mapItem) {
$priority = $bookChild->sort; $model = $mapItem->model;
$id = intval($bookChild->id);
$isPage = $bookChild->type == 'page';
$bookId = $defaultBookId;
$targetBook = $this->entityRepo->getById('book', $bookChild->book);
$chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter); $priorityChanged = intval($model->priority) !== intval($mapItem->sort);
$model = $this->entityRepo->getById($isPage?'page':'chapter', $id); $bookChanged = intval($model->book_id) !== intval($mapItem->book);
$chapterChanged = ($mapItem->type === 'page') && intval($model->chapter_id) !== $mapItem->parentChapter;
// Update models only if there's a change in parent chain or ordering. if ($bookChanged) {
if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) { $this->entityRepo->changeBook($mapItem->type, $mapItem->book, $model);
$this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model); }
$model->priority = $priority; if ($chapterChanged) {
if ($isPage) $model->chapter_id = $chapterId; $model->chapter_id = intval($mapItem->parentChapter);
$model->save(); $model->save();
$updatedModels->push($model);
} }
if ($priorityChanged) {
// Store involved books to be sorted later $model->priority = intval($mapItem->sort);
if (!in_array($bookId, $sortedBooks)) { $model->save();
$sortedBooks[] = $bookId;
} }
} });
// Add activity for books // Rebuild permissions and add activity for involved books.
foreach ($sortedBooks as $bookId) { $booksInvolved->each(function(Book $book) {
/** @var Book $updatedBook */ $this->entityRepo->buildJointPermissionsForBook($book);
$updatedBook = $this->entityRepo->getById('book', $bookId); Activity::add($book, 'book_sort', $book->id);
$this->entityRepo->buildJointPermissionsForBook($updatedBook); });
Activity::add($updatedBook, 'book_sort', $updatedBook->id);
}
return redirect($book->getUrl()); return redirect($book->getUrl());
} }