- // Check permissions for all target and source books
- $permissionsList = [$book->id];
- foreach ($sortMap as $bookChild) {
- // Check permission for target book
- if (!in_array($bookChild->book, $permissionsList)) {
- $targetBook = $this->entityRepo->getById('book', $bookChild->book);
- if (!empty($targetBook)) {
- $bookId = $targetBook->id;
- $this->checkOwnablePermission('book-update', $targetBook);
- // cache the permission for future use.
- $permissionsList[] = $bookId;
- }
- }
-
- // Check permissions for the source book
- $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);
+ // Load models into map
+ $sortMap->each(function($mapItem) use ($bookIdsInvolved) {
+ $mapItem->type = ($mapItem->type === 'page' ? 'page' : 'chapter');
+ $mapItem->model = $this->entityRepo->getById($mapItem->type, $mapItem->id);
+ // Store source and target books
+ $bookIdsInvolved->push(intval($mapItem->model->book_id));
+ $bookIdsInvolved->push(intval($mapItem->book));
+ });