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\ReferenceUpdater;
11 use BookStack\Uploads\ImageRepo;
12 use Illuminate\Http\UploadedFile;
16 public function __construct(
17 protected TagRepo $tagRepo,
18 protected ImageRepo $imageRepo,
19 protected ReferenceUpdater $referenceUpdater
24 * Create a new entity in the system.
26 public function create(Entity $entity, array $input)
28 $entity->fill($input);
29 $this->updateDescription($entity, $input);
31 'created_by' => user()->id,
32 'updated_by' => user()->id,
33 'owned_by' => user()->id,
35 $entity->refreshSlug();
38 if (isset($input['tags'])) {
39 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
43 $entity->rebuildPermissions();
44 $entity->indexForSearch();
48 * Update the given entity.
50 public function update(Entity $entity, array $input)
52 $oldUrl = $entity->getUrl();
54 $entity->fill($input);
55 $this->updateDescription($entity, $input);
56 $entity->updated_by = user()->id;
58 if ($entity->isDirty('name') || empty($entity->slug)) {
59 $entity->refreshSlug();
64 if (isset($input['tags'])) {
65 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
69 $entity->rebuildPermissions();
70 $entity->indexForSearch();
72 if ($oldUrl !== $entity->getUrl()) {
73 $this->referenceUpdater->updateEntityPageReferences($entity, $oldUrl);
78 * Update the given items' cover image, or clear it.
80 * @param Entity&HasCoverImage $entity
82 * @throws ImageUploadException
85 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
88 $imageType = $entity->coverImageTypeKey();
89 $this->imageRepo->destroyImage($entity->cover()->first());
90 $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
91 $entity->cover()->associate($image);
96 $this->imageRepo->destroyImage($entity->cover()->first());
97 $entity->image_id = 0;
102 protected function updateDescription(Entity $entity, array $input): void
104 if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
108 /** @var HasHtmlDescription $entity */
109 if (isset($input['description_html'])) {
110 $entity->description_html = $input['description_html'];
111 $entity->description = html_entity_decode(strip_tags($input['description_html']));
112 } else if (isset($input['description'])) {
113 $entity->description = $input['description'];
114 $entity->description_html = $entity->descriptionHtml();