3 namespace BookStack\Entities\Repos;
5 use BookStack\Activity\TagRepo;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\HasCoverImage;
8 use BookStack\Entities\Models\HasHtmlDescription;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\References\ReferenceStore;
11 use BookStack\References\ReferenceUpdater;
12 use BookStack\Uploads\ImageRepo;
13 use BookStack\Util\HtmlDescriptionFilter;
14 use Illuminate\Http\UploadedFile;
18 public function __construct(
19 protected TagRepo $tagRepo,
20 protected ImageRepo $imageRepo,
21 protected ReferenceUpdater $referenceUpdater,
22 protected ReferenceStore $referenceStore,
27 * Create a new entity in the system.
29 public function create(Entity $entity, array $input)
31 $entity->fill($input);
32 $this->updateDescription($entity, $input);
34 'created_by' => user()->id,
35 'updated_by' => user()->id,
36 'owned_by' => user()->id,
38 $entity->refreshSlug();
41 if (isset($input['tags'])) {
42 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
46 $entity->rebuildPermissions();
47 $entity->indexForSearch();
48 $this->referenceStore->updateForEntity($entity);
52 * Update the given entity.
54 public function update(Entity $entity, array $input)
56 $oldUrl = $entity->getUrl();
58 $entity->fill($input);
59 $this->updateDescription($entity, $input);
60 $entity->updated_by = user()->id;
62 if ($entity->isDirty('name') || empty($entity->slug)) {
63 $entity->refreshSlug();
68 if (isset($input['tags'])) {
69 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
73 $entity->rebuildPermissions();
74 $entity->indexForSearch();
75 $this->referenceStore->updateForEntity($entity);
77 if ($oldUrl !== $entity->getUrl()) {
78 $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
83 * Update the given items' cover image, or clear it.
85 * @param Entity&HasCoverImage $entity
87 * @throws ImageUploadException
90 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
93 $imageType = $entity->coverImageTypeKey();
94 $this->imageRepo->destroyImage($entity->cover()->first());
95 $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
96 $entity->cover()->associate($image);
101 $this->imageRepo->destroyImage($entity->cover()->first());
102 $entity->image_id = 0;
107 protected function updateDescription(Entity $entity, array $input): void
109 if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
113 /** @var HasHtmlDescription $entity */
114 if (isset($input['description_html'])) {
115 $entity->description_html = HtmlDescriptionFilter::filterFromString($input['description_html']);
116 $entity->description = html_entity_decode(strip_tags($input['description_html']));
117 } else if (isset($input['description'])) {
118 $entity->description = $input['description'];
119 $entity->description_html = '';
120 $entity->description_html = $entity->descriptionHtml();