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 Illuminate\Http\UploadedFile;
17 public function __construct(
18 protected TagRepo $tagRepo,
19 protected ImageRepo $imageRepo,
20 protected ReferenceUpdater $referenceUpdater,
21 protected ReferenceStore $referenceStore,
26 * Create a new entity in the system.
28 public function create(Entity $entity, array $input)
30 $entity->fill($input);
31 $this->updateDescription($entity, $input);
33 'created_by' => user()->id,
34 'updated_by' => user()->id,
35 'owned_by' => user()->id,
37 $entity->refreshSlug();
40 if (isset($input['tags'])) {
41 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
45 $entity->rebuildPermissions();
46 $entity->indexForSearch();
47 $this->referenceStore->updateForEntity($entity);
51 * Update the given entity.
53 public function update(Entity $entity, array $input)
55 $oldUrl = $entity->getUrl();
57 $entity->fill($input);
58 $this->updateDescription($entity, $input);
59 $entity->updated_by = user()->id;
61 if ($entity->isDirty('name') || empty($entity->slug)) {
62 $entity->refreshSlug();
67 if (isset($input['tags'])) {
68 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
72 $entity->rebuildPermissions();
73 $entity->indexForSearch();
74 $this->referenceStore->updateForEntity($entity);
76 if ($oldUrl !== $entity->getUrl()) {
77 $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
82 * Update the given items' cover image, or clear it.
84 * @param Entity&HasCoverImage $entity
86 * @throws ImageUploadException
89 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
92 $imageType = $entity->coverImageTypeKey();
93 $this->imageRepo->destroyImage($entity->cover()->first());
94 $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
95 $entity->cover()->associate($image);
100 $this->imageRepo->destroyImage($entity->cover()->first());
101 $entity->image_id = 0;
106 protected function updateDescription(Entity $entity, array $input): void
108 if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
112 /** @var HasHtmlDescription $entity */
113 if (isset($input['description_html'])) {
114 $entity->description_html = $input['description_html'];
115 $entity->description = html_entity_decode(strip_tags($input['description_html']));
116 } else if (isset($input['description'])) {
117 $entity->description = $input['description'];
118 $entity->description_html = $entity->descriptionHtml();