$book = new Book();
$this->baseRepo->create($book, $input);
$this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
- $this->updateBookDefaultTemplate($book, intval($input['default_template_id'] ?? null));
+ $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
Activity::add(ActivityType::BOOK_CREATE, $book);
return $book;
$this->baseRepo->update($book, $input);
if (array_key_exists('default_template_id', $input)) {
- $this->updateBookDefaultTemplate($book, intval($input['default_template_id']));
+ $this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
}
if (array_key_exists('image', $input)) {
return $book;
}
- /**
- * Update the default page template used for this book.
- * Checks that, if changing, the provided value is a valid template and the user
- * has visibility of the provided page template id.
- */
- protected function updateBookDefaultTemplate(Book $book, int $templateId): void
- {
- $changing = $templateId !== intval($book->default_template_id);
- if (!$changing) {
- return;
- }
-
- if ($templateId === 0) {
- $book->default_template_id = null;
- $book->save();
- return;
- }
-
- $templateExists = Page::query()->visible()
- ->where('template', '=', true)
- ->where('id', '=', $templateId)
- ->exists();
-
- $book->default_template_id = $templateExists ? $templateId : null;
- $book->save();
- }
-
/**
* Update the given book's cover image, or clear it.
*