3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\TagRepo;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\BookChild;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Models\HasCoverImage;
11 use BookStack\Entities\Models\HasHtmlDescription;
12 use BookStack\Entities\Queries\PageQueries;
13 use BookStack\Exceptions\ImageUploadException;
14 use BookStack\References\ReferenceStore;
15 use BookStack\References\ReferenceUpdater;
16 use BookStack\Sorting\BookSorter;
17 use BookStack\Uploads\ImageRepo;
18 use BookStack\Util\HtmlDescriptionFilter;
19 use Illuminate\Http\UploadedFile;
23 public function __construct(
24 protected TagRepo $tagRepo,
25 protected ImageRepo $imageRepo,
26 protected ReferenceUpdater $referenceUpdater,
27 protected ReferenceStore $referenceStore,
28 protected PageQueries $pageQueries,
29 protected BookSorter $bookSorter,
34 * Create a new entity in the system.
36 public function create(Entity $entity, array $input)
38 $entity->fill($input);
39 $this->updateDescription($entity, $input);
41 'created_by' => user()->id,
42 'updated_by' => user()->id,
43 'owned_by' => user()->id,
45 $entity->refreshSlug();
48 if (isset($input['tags'])) {
49 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
53 $entity->rebuildPermissions();
54 $entity->indexForSearch();
55 $this->referenceStore->updateForEntity($entity);
59 * Update the given entity.
61 public function update(Entity $entity, array $input)
63 $oldUrl = $entity->getUrl();
65 $entity->fill($input);
66 $this->updateDescription($entity, $input);
67 $entity->updated_by = user()->id;
69 if ($entity->isDirty('name') || empty($entity->slug)) {
70 $entity->refreshSlug();
75 if (isset($input['tags'])) {
76 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
80 $entity->rebuildPermissions();
81 $entity->indexForSearch();
82 $this->referenceStore->updateForEntity($entity);
84 if ($oldUrl !== $entity->getUrl()) {
85 $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
90 * Update the given items' cover image, or clear it.
92 * @param Entity&HasCoverImage $entity
94 * @throws ImageUploadException
97 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
100 $imageType = $entity->coverImageTypeKey();
101 $this->imageRepo->destroyImage($entity->cover()->first());
102 $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
103 $entity->cover()->associate($image);
108 $this->imageRepo->destroyImage($entity->cover()->first());
109 $entity->image_id = 0;
115 * Update the default page template used for this item.
116 * Checks that, if changing, the provided value is a valid template and the user
117 * has visibility of the provided page template id.
119 public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
121 $changing = $templateId !== intval($entity->default_template_id);
126 if ($templateId === 0) {
127 $entity->default_template_id = null;
132 $templateExists = $this->pageQueries->visibleTemplates()
133 ->where('id', '=', $templateId)
136 $entity->default_template_id = $templateExists ? $templateId : null;
141 * Sort the parent of the given entity, if any auto sort actions are set for it.
142 * Typical ran during create/update/insert events.
144 public function sortParent(Entity $entity): void
146 if ($entity instanceof BookChild) {
147 $book = $entity->book;
148 $this->bookSorter->runBookAutoSort($book);
152 protected function updateDescription(Entity $entity, array $input): void
154 if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
158 /** @var HasHtmlDescription $entity */
159 if (isset($input['description_html'])) {
160 $entity->description_html = HtmlDescriptionFilter::filterFromString($input['description_html']);
161 $entity->description = html_entity_decode(strip_tags($input['description_html']));
162 } else if (isset($input['description'])) {
163 $entity->description = $input['description'];
164 $entity->description_html = '';
165 $entity->description_html = $entity->descriptionHtml();