2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Repos;
|
2019-10-05 13:55:01 +02:00
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\Activity\ActivityType;
|
2020-11-22 01:17:45 +01:00
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
2024-02-07 17:37:36 +01:00
|
|
|
use BookStack\Entities\Queries\BookQueries;
|
2020-11-22 00:20:54 +01:00
|
|
|
use BookStack\Entities\Tools\TrashCan;
|
2020-11-07 23:37:27 +01:00
|
|
|
use BookStack\Facades\Activity;
|
2019-10-05 13:55:01 +02:00
|
|
|
use Exception;
|
|
|
|
|
|
|
|
class BookshelfRepo
|
|
|
|
{
|
2024-02-04 20:32:19 +01:00
|
|
|
public function __construct(
|
|
|
|
protected BaseRepo $baseRepo,
|
2024-02-07 17:37:36 +01:00
|
|
|
protected BookQueries $bookQueries,
|
|
|
|
protected TrashCan $trashCan,
|
2024-02-04 20:32:19 +01:00
|
|
|
) {
|
2019-10-05 13:55:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new shelf in the system.
|
|
|
|
*/
|
|
|
|
public function create(array $input, array $bookIds): Bookshelf
|
|
|
|
{
|
|
|
|
$shelf = new Bookshelf();
|
|
|
|
$this->baseRepo->create($shelf, $input);
|
2022-06-15 16:05:08 +02:00
|
|
|
$this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null);
|
2019-10-05 13:55:01 +02:00
|
|
|
$this->updateBooks($shelf, $bookIds);
|
2021-12-11 18:29:33 +01:00
|
|
|
Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2019-10-05 13:55:01 +02:00
|
|
|
return $shelf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-07 23:37:27 +01:00
|
|
|
* Update an existing shelf in the system using the given input.
|
2019-10-05 13:55:01 +02:00
|
|
|
*/
|
2020-04-10 16:19:18 +02:00
|
|
|
public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
|
2019-10-05 13:55:01 +02:00
|
|
|
{
|
|
|
|
$this->baseRepo->update($shelf, $input);
|
2020-04-10 16:19:18 +02:00
|
|
|
|
|
|
|
if (!is_null($bookIds)) {
|
|
|
|
$this->updateBooks($shelf, $bookIds);
|
|
|
|
}
|
|
|
|
|
2022-06-15 16:05:08 +02:00
|
|
|
if (array_key_exists('image', $input)) {
|
2022-06-13 18:20:21 +02:00
|
|
|
$this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
|
|
|
|
}
|
|
|
|
|
2021-12-11 18:29:33 +01:00
|
|
|
Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
|
2021-06-26 17:23:15 +02:00
|
|
|
|
2019-10-05 13:55:01 +02:00
|
|
|
return $shelf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-13 18:20:21 +02:00
|
|
|
* Update which books are assigned to this shelf by syncing the given book ids.
|
2019-10-05 13:55:01 +02:00
|
|
|
* Function ensures the books are visible to the current user and existing.
|
|
|
|
*/
|
|
|
|
protected function updateBooks(Bookshelf $shelf, array $bookIds)
|
|
|
|
{
|
|
|
|
$numericIDs = collect($bookIds)->map(function ($id) {
|
|
|
|
return intval($id);
|
|
|
|
});
|
|
|
|
|
2024-02-07 17:37:36 +01:00
|
|
|
$syncData = $this->bookQueries->visibleForList()
|
2019-10-05 13:55:01 +02:00
|
|
|
->whereIn('id', $bookIds)
|
2021-11-05 17:18:06 +01:00
|
|
|
->pluck('id')
|
|
|
|
->mapWithKeys(function ($bookId) use ($numericIDs) {
|
2019-10-05 13:55:01 +02:00
|
|
|
return [$bookId => ['order' => $numericIDs->search($bookId)]];
|
|
|
|
});
|
|
|
|
|
|
|
|
$shelf->books()->sync($syncData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a bookshelf from the system.
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
2019-10-05 13:55:01 +02:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function destroy(Bookshelf $shelf)
|
|
|
|
{
|
2024-02-07 17:37:36 +01:00
|
|
|
$this->trashCan->softDestroyShelf($shelf);
|
2021-12-11 18:29:33 +01:00
|
|
|
Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);
|
2024-02-07 17:37:36 +01:00
|
|
|
$this->trashCan->autoClearOld();
|
2019-10-05 13:55:01 +02:00
|
|
|
}
|
|
|
|
}
|