3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\TagRepo;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\HasCoverImage;
10 use BookStack\Entities\Models\HasHtmlDescription;
11 use BookStack\Entities\Models\Page;
12 use BookStack\Exceptions\ImageUploadException;
13 use BookStack\References\ReferenceStore;
14 use BookStack\References\ReferenceUpdater;
15 use BookStack\Uploads\ImageRepo;
16 use BookStack\Util\HtmlDescriptionFilter;
17 use Illuminate\Http\UploadedFile;
21 public function __construct(
22 protected TagRepo $tagRepo,
23 protected ImageRepo $imageRepo,
24 protected ReferenceUpdater $referenceUpdater,
25 protected ReferenceStore $referenceStore,
30 * Create a new entity in the system.
32 public function create(Entity $entity, array $input)
34 $entity->fill($input);
35 $this->updateDescription($entity, $input);
37 'created_by' => user()->id,
38 'updated_by' => user()->id,
39 'owned_by' => user()->id,
41 $entity->refreshSlug();
44 if (isset($input['tags'])) {
45 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
49 $entity->rebuildPermissions();
50 $entity->indexForSearch();
51 $this->referenceStore->updateForEntity($entity);
55 * Update the given entity.
57 public function update(Entity $entity, array $input)
59 $oldUrl = $entity->getUrl();
61 $entity->fill($input);
62 $this->updateDescription($entity, $input);
63 $entity->updated_by = user()->id;
65 if ($entity->isDirty('name') || empty($entity->slug)) {
66 $entity->refreshSlug();
71 if (isset($input['tags'])) {
72 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
76 $entity->rebuildPermissions();
77 $entity->indexForSearch();
78 $this->referenceStore->updateForEntity($entity);
80 if ($oldUrl !== $entity->getUrl()) {
81 $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
86 * Update the given items' cover image, or clear it.
88 * @param Entity&HasCoverImage $entity
90 * @throws ImageUploadException
93 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
96 $imageType = $entity->coverImageTypeKey();
97 $this->imageRepo->destroyImage($entity->cover()->first());
98 $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
99 $entity->cover()->associate($image);
104 $this->imageRepo->destroyImage($entity->cover()->first());
105 $entity->image_id = 0;
111 * Update the default page template used for this item.
112 * Checks that, if changing, the provided value is a valid template and the user
113 * has visibility of the provided page template id.
115 public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
117 $changing = $templateId !== intval($entity->default_template_id);
122 if ($templateId === 0) {
123 $entity->default_template_id = null;
128 $templateExists = Page::query()->visible()
129 ->where('template', '=', true)
130 ->where('id', '=', $templateId)
133 $entity->default_template_id = $templateExists ? $templateId : null;
137 protected function updateDescription(Entity $entity, array $input): void
139 if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
143 /** @var HasHtmlDescription $entity */
144 if (isset($input['description_html'])) {
145 $entity->description_html = HtmlDescriptionFilter::filterFromString($input['description_html']);
146 $entity->description = html_entity_decode(strip_tags($input['description_html']));
147 } else if (isset($input['description'])) {
148 $entity->description = $input['description'];
149 $entity->description_html = '';
150 $entity->description_html = $entity->descriptionHtml();