]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BookshelfRepo.php
Added examples, updated docs for image gallery api endpoints
[bookstack] / app / Entities / Repos / BookshelfRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Facades\Activity;
11 use Exception;
12 use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13 use Illuminate\Support\Collection;
14
15 class BookshelfRepo
16 {
17     protected $baseRepo;
18
19     /**
20      * BookshelfRepo constructor.
21      */
22     public function __construct(BaseRepo $baseRepo)
23     {
24         $this->baseRepo = $baseRepo;
25     }
26
27     /**
28      * Get all bookshelves in a paginated format.
29      */
30     public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
31     {
32         return Bookshelf::visible()
33             ->with(['visibleBooks', 'cover'])
34             ->orderBy($sort, $order)
35             ->paginate($count);
36     }
37
38     /**
39      * Get the bookshelves that were most recently viewed by this user.
40      */
41     public function getRecentlyViewed(int $count = 20): Collection
42     {
43         return Bookshelf::visible()->withLastView()
44             ->having('last_viewed_at', '>', 0)
45             ->orderBy('last_viewed_at', 'desc')
46             ->take($count)->get();
47     }
48
49     /**
50      * Get the most popular bookshelves in the system.
51      */
52     public function getPopular(int $count = 20): Collection
53     {
54         return Bookshelf::visible()->withViewCount()
55             ->having('view_count', '>', 0)
56             ->orderBy('view_count', 'desc')
57             ->take($count)->get();
58     }
59
60     /**
61      * Get the most recently created bookshelves from the system.
62      */
63     public function getRecentlyCreated(int $count = 20): Collection
64     {
65         return Bookshelf::visible()->orderBy('created_at', 'desc')
66             ->take($count)->get();
67     }
68
69     /**
70      * Get a shelf by its slug.
71      */
72     public function getBySlug(string $slug): Bookshelf
73     {
74         $shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
75
76         if ($shelf === null) {
77             throw new NotFoundException(trans('errors.bookshelf_not_found'));
78         }
79
80         return $shelf;
81     }
82
83     /**
84      * Create a new shelf in the system.
85      */
86     public function create(array $input, array $bookIds): Bookshelf
87     {
88         $shelf = new Bookshelf();
89         $this->baseRepo->create($shelf, $input);
90         $this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null);
91         $this->updateBooks($shelf, $bookIds);
92         Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
93
94         return $shelf;
95     }
96
97     /**
98      * Update an existing shelf in the system using the given input.
99      */
100     public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
101     {
102         $this->baseRepo->update($shelf, $input);
103
104         if (!is_null($bookIds)) {
105             $this->updateBooks($shelf, $bookIds);
106         }
107
108         if (array_key_exists('image', $input)) {
109             $this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
110         }
111
112         Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
113
114         return $shelf;
115     }
116
117     /**
118      * Update which books are assigned to this shelf by syncing the given book ids.
119      * Function ensures the books are visible to the current user and existing.
120      */
121     protected function updateBooks(Bookshelf $shelf, array $bookIds)
122     {
123         $numericIDs = collect($bookIds)->map(function ($id) {
124             return intval($id);
125         });
126
127         $syncData = Book::visible()
128             ->whereIn('id', $bookIds)
129             ->pluck('id')
130             ->mapWithKeys(function ($bookId) use ($numericIDs) {
131                 return [$bookId => ['order' => $numericIDs->search($bookId)]];
132             });
133
134         $shelf->books()->sync($syncData);
135     }
136
137     /**
138      * Remove a bookshelf from the system.
139      *
140      * @throws Exception
141      */
142     public function destroy(Bookshelf $shelf)
143     {
144         $trashCan = new TrashCan();
145         $trashCan->softDestroyShelf($shelf);
146         Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);
147         $trashCan->autoClearOld();
148     }
149 }