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\Exceptions\ImageUploadException;
9 use BookStack\References\ReferenceUpdater;
10 use BookStack\Uploads\ImageRepo;
11 use Illuminate\Http\UploadedFile;
15 protected TagRepo $tagRepo;
16 protected ImageRepo $imageRepo;
17 protected ReferenceUpdater $referenceUpdater;
19 public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo, ReferenceUpdater $referenceUpdater)
21 $this->tagRepo = $tagRepo;
22 $this->imageRepo = $imageRepo;
23 $this->referenceUpdater = $referenceUpdater;
27 * Create a new entity in the system.
29 public function create(Entity $entity, array $input)
31 $entity->fill($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();
50 * Update the given entity.
52 public function update(Entity $entity, array $input)
54 $oldUrl = $entity->getUrl();
56 $entity->fill($input);
57 $entity->updated_by = user()->id;
59 if ($entity->isDirty('name') || empty($entity->slug)) {
60 $entity->refreshSlug();
65 if (isset($input['tags'])) {
66 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
70 $entity->rebuildPermissions();
71 $entity->indexForSearch();
73 if ($oldUrl !== $entity->getUrl()) {
74 $this->referenceUpdater->updateEntityPageReferences($entity, $oldUrl);
79 * Update the given items' cover image, or clear it.
81 * @param Entity&HasCoverImage $entity
83 * @throws ImageUploadException
86 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
89 $imageType = $entity->coverImageTypeKey();
90 $this->imageRepo->destroyImage($entity->cover()->first());
91 $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
92 $entity->cover()->associate($image);
97 $this->imageRepo->destroyImage($entity->cover()->first());
98 $entity->image_id = 0;