3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Queries\PageQueries;
10 class EntityDefaultTemplate
12 public function __construct(
13 protected Book|Chapter $entity,
18 * Set the default template ID for this entity.
20 public function setFromId(int $templateId): void
22 $changing = $templateId !== intval($this->entity->default_template_id);
27 if ($templateId === 0) {
28 $this->entity->default_template_id = null;
32 $pageQueries = app()->make(PageQueries::class);
33 $templateExists = $pageQueries->visibleTemplates()
34 ->where('id', '=', $templateId)
37 $this->entity->default_template_id = $templateExists ? $templateId : null;
41 * Get the default template for this entity (if visible).
43 public function get(): Page|null
45 if (!$this->entity->default_template_id) {
49 $pageQueries = app()->make(PageQueries::class);
50 $page = $pageQueries->visibleTemplates(true)
51 ->where('id', '=', $this->entity->default_template_id)
54 if ($page instanceof Page) {