BookStack/app/Entities/Repos/BookRepo.php
Dan Brown b866dee0cf
Some checks are pending
analyse-php / build (push) Waiting to run
lint-php / build (push) Waiting to run
test-migrations / build (8.2) (push) Waiting to run
test-migrations / build (8.3) (push) Waiting to run
test-migrations / build (8.4) (push) Waiting to run
test-php / build (8.2) (push) Waiting to run
test-php / build (8.3) (push) Waiting to run
test-php / build (8.4) (push) Waiting to run
Entities: Updated repos to act on refreshed clones
Changes to core entity models are now done on clones to ensure clean
state before save, and those clones are returned back if changes are
needed after that action.
2025-09-24 18:19:16 +01:00

92 lines
2.7 KiB
PHP

<?php
namespace BookStack\Entities\Repos;
use BookStack\Activity\ActivityType;
use BookStack\Activity\TagRepo;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\ImageUploadException;
use BookStack\Facades\Activity;
use BookStack\Sorting\SortRule;
use BookStack\Uploads\ImageRepo;
use BookStack\Util\DatabaseTransaction;
use Exception;
use Illuminate\Http\UploadedFile;
class BookRepo
{
public function __construct(
protected BaseRepo $baseRepo,
protected TagRepo $tagRepo,
protected ImageRepo $imageRepo,
protected TrashCan $trashCan,
) {
}
/**
* Create a new book in the system.
*/
public function create(array $input): Book
{
return (new DatabaseTransaction(function () use ($input) {
$book = $this->baseRepo->create(new Book(), $input);
$this->baseRepo->updateCoverImage($book->containerData, $input['image'] ?? null);
$this->baseRepo->updateDefaultTemplate($book->containerData, intval($input['default_template_id'] ?? null));
Activity::add(ActivityType::BOOK_CREATE, $book);
$defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
$book->containerData->sort_rule_id = $defaultBookSortSetting;
$book->containerData->save();
}
return $book;
}))->run();
}
/**
* Update the given book.
*/
public function update(Book $book, array $input): Book
{
$book = $this->baseRepo->update($book, $input);
if (array_key_exists('default_template_id', $input)) {
$this->baseRepo->updateDefaultTemplate($book->containerData, intval($input['default_template_id']));
}
if (array_key_exists('image', $input)) {
$this->baseRepo->updateCoverImage($book->containerData, $input['image'], $input['image'] === null);
}
Activity::add(ActivityType::BOOK_UPDATE, $book);
return $book;
}
/**
* Update the given book's cover image or clear it.
*
* @throws ImageUploadException
* @throws Exception
*/
public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false): void
{
$this->baseRepo->updateCoverImage($book->containerData, $coverImage, $removeImage);
}
/**
* Remove a book from the system.
*
* @throws Exception
*/
public function destroy(Book $book): void
{
$this->trashCan->softDestroyBook($book);
Activity::add(ActivityType::BOOK_DELETE, $book);
$this->trashCan->autoClearOld();
}
}