3 namespace BookStack\Entities\Repos;
5 use BookStack\Actions\TagRepo;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\HasCoverImage;
8 use BookStack\Exceptions\ImageUploadException;
9 use BookStack\Uploads\ImageRepo;
10 use Illuminate\Http\UploadedFile;
14 protected TagRepo $tagRepo;
15 protected ImageRepo $imageRepo;
17 public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
19 $this->tagRepo = $tagRepo;
20 $this->imageRepo = $imageRepo;
24 * Create a new entity in the system.
26 public function create(Entity $entity, array $input)
28 $entity->fill($input);
30 'created_by' => user()->id,
31 'updated_by' => user()->id,
32 'owned_by' => user()->id,
34 $entity->refreshSlug();
37 if (isset($input['tags'])) {
38 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
41 $entity->rebuildPermissions();
42 $entity->indexForSearch();
46 * Update the given entity.
48 public function update(Entity $entity, array $input)
50 $entity->fill($input);
51 $entity->updated_by = user()->id;
53 if ($entity->isDirty('name')) {
54 $entity->refreshSlug();
59 if (isset($input['tags'])) {
60 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
64 $entity->rebuildPermissions();
65 $entity->indexForSearch();
69 * Update the given items' cover image, or clear it.
71 * @param Entity&HasCoverImage $entity
73 * @throws ImageUploadException
76 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
79 $this->imageRepo->destroyImage($entity->cover);
80 $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
81 $entity->cover()->associate($image);
86 $this->imageRepo->destroyImage($entity->cover);
87 $entity->image_id = 0;