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