2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Repos;
|
2019-09-15 18:28:23 -04:00
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Activity\ActivityType;
|
|
|
|
use BookStack\Activity\TagRepo;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
2020-11-21 18:20:54 -05:00
|
|
|
use BookStack\Entities\Tools\TrashCan;
|
2019-10-05 07:55:01 -04:00
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
2020-11-07 17:37:27 -05:00
|
|
|
use BookStack\Facades\Activity;
|
2019-10-05 07:55:01 -04:00
|
|
|
use BookStack\Uploads\ImageRepo;
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Http\UploadedFile;
|
2019-09-15 18:28:23 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
class BookRepo
|
2019-09-15 18:28:23 -04:00
|
|
|
{
|
2023-12-11 10:55:43 -05:00
|
|
|
public function __construct(
|
|
|
|
protected BaseRepo $baseRepo,
|
|
|
|
protected TagRepo $tagRepo,
|
2024-02-07 11:37:36 -05:00
|
|
|
protected ImageRepo $imageRepo,
|
|
|
|
protected TrashCan $trashCan,
|
2023-12-11 10:55:43 -05:00
|
|
|
) {
|
2019-10-05 07:55:01 -04:00
|
|
|
}
|
|
|
|
|
2019-09-15 18:28:23 -04:00
|
|
|
/**
|
2021-06-26 11:23:15 -04:00
|
|
|
* Create a new book in the system.
|
2019-09-15 18:28:23 -04:00
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function create(array $input): Book
|
2019-09-15 18:28:23 -04:00
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$book = new Book();
|
|
|
|
$this->baseRepo->create($book, $input);
|
2022-06-14 11:42:29 -04:00
|
|
|
$this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
|
2024-02-01 07:51:47 -05:00
|
|
|
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
|
2021-12-11 12:29:33 -05:00
|
|
|
Activity::add(ActivityType::BOOK_CREATE, $book);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return $book;
|
|
|
|
}
|
2019-09-15 18:28:23 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
/**
|
|
|
|
* Update the given book.
|
|
|
|
*/
|
|
|
|
public function update(Book $book, array $input): Book
|
|
|
|
{
|
|
|
|
$this->baseRepo->update($book, $input);
|
2022-06-13 12:20:21 -04:00
|
|
|
|
2023-12-12 07:14:00 -05:00
|
|
|
if (array_key_exists('default_template_id', $input)) {
|
2024-02-01 07:51:47 -05:00
|
|
|
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
|
2023-12-11 10:55:43 -05:00
|
|
|
}
|
|
|
|
|
2022-06-14 11:42:29 -04:00
|
|
|
if (array_key_exists('image', $input)) {
|
2022-06-13 12:20:21 -04:00
|
|
|
$this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
|
|
|
|
}
|
|
|
|
|
2021-12-11 12:29:33 -05:00
|
|
|
Activity::add(ActivityType::BOOK_UPDATE, $book);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return $book;
|
|
|
|
}
|
2019-09-15 18:28:23 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
/**
|
|
|
|
* Update the given book's cover image, or clear it.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws ImageUploadException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2020-02-15 13:38:36 -05:00
|
|
|
public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
|
2019-10-05 07:55:01 -04:00
|
|
|
{
|
|
|
|
$this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
|
2019-09-15 18:28:23 -04:00
|
|
|
}
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
/**
|
|
|
|
* Remove a book from the system.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2020-09-27 18:24:33 -04:00
|
|
|
* @throws Exception
|
2019-10-05 07:55:01 -04:00
|
|
|
*/
|
|
|
|
public function destroy(Book $book)
|
|
|
|
{
|
2024-02-07 11:37:36 -05:00
|
|
|
$this->trashCan->softDestroyBook($book);
|
2021-12-11 12:29:33 -05:00
|
|
|
Activity::add(ActivityType::BOOK_DELETE, $book);
|
2020-11-07 17:37:27 -05:00
|
|
|
|
2024-02-07 11:37:36 -05:00
|
|
|
$this->trashCan->autoClearOld();
|
2019-10-05 07:55:01 -04:00
|
|
|
}
|
|
|
|
}
|