]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookshelfRepo.php
b870ec37747b1cce6e92a784db43f485eca93ff5
[bookstack] / app / Entities / Repos / BookshelfRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Queries\BookQueries;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Facades\Activity;
10 use BookStack\Util\DatabaseTransaction;
11 use Exception;
12
13 class BookshelfRepo
14 {
15     public function __construct(
16         protected BaseRepo $baseRepo,
17         protected BookQueries $bookQueries,
18         protected TrashCan $trashCan,
19     ) {
20     }
21
22     /**
23      * Create a new shelf in the system.
24      */
25     public function create(array $input, array $bookIds): Bookshelf
26     {
27         return (new DatabaseTransaction(function () use ($input, $bookIds) {
28             $shelf = new Bookshelf();
29             $this->baseRepo->create($shelf, $input);
30             $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null);
31             $this->updateBooks($shelf, $bookIds);
32             Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
33             return $shelf;
34         }))->run();
35     }
36
37     /**
38      * Update an existing shelf in the system using the given input.
39      */
40     public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
41     {
42         $this->baseRepo->update($shelf, $input);
43
44         if (!is_null($bookIds)) {
45             $this->updateBooks($shelf, $bookIds);
46         }
47
48         if (array_key_exists('image', $input)) {
49             $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
50         }
51
52         Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
53
54         return $shelf;
55     }
56
57     /**
58      * Update which books are assigned to this shelf by syncing the given book ids.
59      * Function ensures the managed books are visible to the current user and existing,
60      * and that the user does not alter the assignment of books that are not visible to them.
61      */
62     protected function updateBooks(Bookshelf $shelf, array $bookIds): void
63     {
64         $numericIDs = collect($bookIds)->map(function ($id) {
65             return intval($id);
66         });
67
68         $existingBookIds = $shelf->books()->pluck('id')->toArray();
69         $visibleExistingBookIds = $this->bookQueries->visibleForList()
70             ->whereIn('id', $existingBookIds)
71             ->pluck('id')
72             ->toArray();
73         $nonVisibleExistingBookIds = array_values(array_diff($existingBookIds, $visibleExistingBookIds));
74
75         $newIdsToAssign = $this->bookQueries->visibleForList()
76             ->whereIn('id', $bookIds)
77             ->pluck('id')
78             ->toArray();
79
80         $maxNewIndex = max($numericIDs->keys()->toArray() ?: [0]);
81
82         $syncData = [];
83         foreach ($newIdsToAssign as $id) {
84             $syncData[$id] = ['order' => $numericIDs->search($id)];
85         }
86
87         foreach ($nonVisibleExistingBookIds as $index => $id) {
88             $syncData[$id] = ['order' => $maxNewIndex + ($index + 1)];
89         }
90
91         $shelf->books()->sync($syncData);
92     }
93
94     /**
95      * Remove a bookshelf from the system.
96      *
97      * @throws Exception
98      */
99     public function destroy(Bookshelf $shelf)
100     {
101         $this->trashCan->softDestroyShelf($shelf);
102         Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);
103         $this->trashCan->autoClearOld();
104     }
105 }