3 namespace BookStack\Entities\Repos;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\TagRepo;
7 use BookStack\Auth\User;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\HasCoverImage;
10 use BookStack\Exceptions\ImageUploadException;
11 use BookStack\Facades\Activity;
12 use BookStack\Uploads\ImageRepo;
13 use Illuminate\Http\UploadedFile;
14 use Illuminate\Support\Collection;
23 public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
25 $this->tagRepo = $tagRepo;
26 $this->imageRepo = $imageRepo;
30 * Create a new entity in the system
32 public function create(Entity $entity, array $input)
34 $entity->fill($input);
36 'created_by' => user()->id,
37 'updated_by' => user()->id,
38 'owned_by' => user()->id,
40 $entity->refreshSlug();
43 if (isset($input['tags'])) {
44 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
47 $entity->rebuildPermissions();
48 $entity->indexForSearch();
52 * Update the given entity.
54 public function update(Entity $entity, array $input)
56 $entity->fill($input);
57 $entity->updated_by = user()->id;
59 if ($entity->isDirty('name')) {
60 $entity->refreshSlug();
65 if (isset($input['tags'])) {
66 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
69 $entity->rebuildPermissions();
70 $entity->indexForSearch();
74 * Update the given items' cover image, or clear it.
75 * @throws ImageUploadException
78 public function updateCoverImage(HasCoverImage $entity, ?UploadedFile $coverImage, bool $removeImage = false)
81 $this->imageRepo->destroyImage($entity->cover);
82 $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
83 $entity->cover()->associate($image);
88 $this->imageRepo->destroyImage($entity->cover);
89 $entity->image_id = 0;