]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookshelfRepo.php
API: Added comment-read endpoint, added api docs section descriptions
[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 = $this->baseRepo->create(new Bookshelf(), $input);
29             $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null);
30             $this->updateBooks($shelf, $bookIds);
31             Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
32             return $shelf;
33         }))->run();
34     }
35
36     /**
37      * Update an existing shelf in the system using the given input.
38      */
39     public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
40     {
41         $shelf = $this->baseRepo->update($shelf, $input);
42
43         if (!is_null($bookIds)) {
44             $this->updateBooks($shelf, $bookIds);
45         }
46
47         if (array_key_exists('image', $input)) {
48             $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
49         }
50
51         Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
52
53         return $shelf;
54     }
55
56     /**
57      * Update which books are assigned to this shelf by syncing the given book ids.
58      * Function ensures the managed books are visible to the current user and existing,
59      * and that the user does not alter the assignment of books that are not visible to them.
60      */
61     protected function updateBooks(Bookshelf $shelf, array $bookIds): void
62     {
63         $numericIDs = collect($bookIds)->map(function ($id) {
64             return intval($id);
65         });
66
67         $existingBookIds = $shelf->books()->pluck('id')->toArray();
68         $visibleExistingBookIds = $this->bookQueries->visibleForList()
69             ->whereIn('id', $existingBookIds)
70             ->pluck('id')
71             ->toArray();
72         $nonVisibleExistingBookIds = array_values(array_diff($existingBookIds, $visibleExistingBookIds));
73
74         $newIdsToAssign = $this->bookQueries->visibleForList()
75             ->whereIn('id', $bookIds)
76             ->pluck('id')
77             ->toArray();
78
79         $maxNewIndex = max($numericIDs->keys()->toArray() ?: [0]);
80
81         $syncData = [];
82         foreach ($newIdsToAssign as $id) {
83             $syncData[$id] = ['order' => $numericIDs->search($id)];
84         }
85
86         foreach ($nonVisibleExistingBookIds as $index => $id) {
87             $syncData[$id] = ['order' => $maxNewIndex + ($index + 1)];
88         }
89
90         $shelf->books()->sync($syncData);
91     }
92
93     /**
94      * Remove a bookshelf from the system.
95      *
96      * @throws Exception
97      */
98     public function destroy(Bookshelf $shelf): void
99     {
100         $this->trashCan->softDestroyShelf($shelf);
101         Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);
102         $this->trashCan->autoClearOld();
103     }
104 }