3 namespace BookStack\Entities\Repos;
5 use BookStack\Actions\TagRepo;
6 use BookStack\Entities\Book;
7 use BookStack\Entities\Entity;
8 use BookStack\Entities\HasCoverImage;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Uploads\ImageRepo;
11 use Illuminate\Http\UploadedFile;
12 use Illuminate\Support\Collection;
22 * BaseRepo constructor.
25 public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
27 $this->tagRepo = $tagRepo;
28 $this->imageRepo = $imageRepo;
32 * Create a new entity in the system
34 public function create(Entity $entity, array $input)
36 $entity->fill($input);
38 'created_by' => user()->id,
39 'updated_by' => user()->id,
41 $entity->refreshSlug();
44 if (isset($input['tags'])) {
45 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
48 $entity->rebuildPermissions();
49 $entity->indexForSearch();
53 * Update the given entity.
55 public function update(Entity $entity, array $input)
57 $entity->fill($input);
58 $entity->updated_by = user()->id;
60 if ($entity->isDirty('name')) {
61 $entity->refreshSlug();
66 if (isset($input['tags'])) {
67 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
70 $entity->rebuildPermissions();
71 $entity->indexForSearch();
75 * Update the given items' cover image, or clear it.
76 * @throws ImageUploadException
79 public function updateCoverImage(HasCoverImage $entity, UploadedFile $coverImage = null, bool $removeImage = false)
82 $this->imageRepo->destroyImage($entity->cover);
83 $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
84 $entity->cover()->associate($image);
88 $this->imageRepo->destroyImage($entity->cover);
89 $entity->image_id = 0;
95 * Update the permissions of an entity.
97 public function updatePermissions(Entity $entity, bool $restricted, Collection $permissions = null)
99 $entity->restricted = $restricted;
100 $entity->permissions()->delete();
102 if (!is_null($permissions)) {
103 $entityPermissionData = $permissions->flatMap(function ($restrictions, $roleId) {
104 return collect($restrictions)->keys()->map(function ($action) use ($roleId) {
106 'role_id' => $roleId,
107 'action' => strtolower($action),
112 $entity->permissions()->createMany($entityPermissionData);
116 $entity->rebuildPermissions();