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\Entities\Queries\PageQueries;
13 use BookStack\Exceptions\ImageUploadException;
14 use BookStack\References\ReferenceStore;
15 use BookStack\References\ReferenceUpdater;
16 use BookStack\Uploads\ImageRepo;
17 use BookStack\Util\HtmlDescriptionFilter;
18 use Illuminate\Http\UploadedFile;
22 public function __construct(
23 protected TagRepo $tagRepo,
24 protected ImageRepo $imageRepo,
25 protected ReferenceUpdater $referenceUpdater,
26 protected ReferenceStore $referenceStore,
27 protected PageQueries $pageQueries,
32 * Create a new entity in the system.
34 public function create(Entity $entity, array $input)
36 $entity->fill($input);
37 $this->updateDescription($entity, $input);
39 'created_by' => user()->id,
40 'updated_by' => user()->id,
41 'owned_by' => user()->id,
43 $entity->refreshSlug();
46 if (isset($input['tags'])) {
47 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
51 $entity->rebuildPermissions();
52 $entity->indexForSearch();
53 $this->referenceStore->updateForEntity($entity);
57 * Update the given entity.
59 public function update(Entity $entity, array $input)
61 $oldUrl = $entity->getUrl();
63 $entity->fill($input);
64 $this->updateDescription($entity, $input);
65 $entity->updated_by = user()->id;
67 if ($entity->isDirty('name') || empty($entity->slug)) {
68 $entity->refreshSlug();
73 if (isset($input['tags'])) {
74 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
78 $entity->rebuildPermissions();
79 $entity->indexForSearch();
80 $this->referenceStore->updateForEntity($entity);
82 if ($oldUrl !== $entity->getUrl()) {
83 $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
88 * Update the given items' cover image, or clear it.
90 * @param Entity&HasCoverImage $entity
92 * @throws ImageUploadException
95 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
98 $imageType = $entity->coverImageTypeKey();
99 $this->imageRepo->destroyImage($entity->cover()->first());
100 $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
101 $entity->cover()->associate($image);
106 $this->imageRepo->destroyImage($entity->cover()->first());
107 $entity->image_id = 0;
113 * Update the default page template used for this item.
114 * Checks that, if changing, the provided value is a valid template and the user
115 * has visibility of the provided page template id.
117 public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
119 $changing = $templateId !== intval($entity->default_template_id);
124 if ($templateId === 0) {
125 $entity->default_template_id = null;
130 $templateExists = $this->pageQueries->visibleTemplates()
131 ->where('id', '=', $templateId)
134 $entity->default_template_id = $templateExists ? $templateId : null;
138 protected function updateDescription(Entity $entity, array $input): void
140 if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
144 /** @var HasHtmlDescription $entity */
145 if (isset($input['description_html'])) {
146 $entity->description_html = HtmlDescriptionFilter::filterFromString($input['description_html']);
147 $entity->description = html_entity_decode(strip_tags($input['description_html']));
148 } else if (isset($input['description'])) {
149 $entity->description = $input['description'];
150 $entity->description_html = '';
151 $entity->description_html = $entity->descriptionHtml();