]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
f93271430fc50e45b72e235b4e4fa19c17590dd9
[bookstack] / app / Entities / Repos / BaseRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\TagRepo;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Models\HasCoverImage;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\Facades\Activity;
11 use BookStack\Uploads\ImageRepo;
12 use Illuminate\Http\UploadedFile;
13 use Illuminate\Support\Collection;
14
15 class BaseRepo
16 {
17
18     protected $tagRepo;
19     protected $imageRepo;
20
21
22     public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
23     {
24         $this->tagRepo = $tagRepo;
25         $this->imageRepo = $imageRepo;
26     }
27
28     /**
29      * Create a new entity in the system
30      */
31     public function create(Entity $entity, array $input)
32     {
33         $entity->fill($input);
34         $entity->forceFill([
35             'created_by' => user()->id,
36             'updated_by' => user()->id,
37             'owned_by' => user()->id,
38         ]);
39         $entity->refreshSlug();
40         $entity->save();
41
42         if (isset($input['tags'])) {
43             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
44         }
45
46         $entity->rebuildPermissions();
47         $entity->indexForSearch();
48     }
49
50     /**
51      * Update the given entity.
52      */
53     public function update(Entity $entity, array $input)
54     {
55         $entity->fill($input);
56         $entity->updated_by = user()->id;
57
58         if ($entity->isDirty('name')) {
59             $entity->refreshSlug();
60         }
61
62         $entity->save();
63
64         if (isset($input['tags'])) {
65             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
66         }
67
68         $entity->rebuildPermissions();
69         $entity->indexForSearch();
70     }
71
72     /**
73      * Update the given items' cover image, or clear it.
74      * @throws ImageUploadException
75      * @throws \Exception
76      */
77     public function updateCoverImage(HasCoverImage $entity, ?UploadedFile $coverImage, bool $removeImage = false)
78     {
79         if ($coverImage) {
80             $this->imageRepo->destroyImage($entity->cover);
81             $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
82             $entity->cover()->associate($image);
83             $entity->save();
84         }
85
86         if ($removeImage) {
87             $this->imageRepo->destroyImage($entity->cover);
88             $entity->image_id = 0;
89             $entity->save();
90         }
91     }
92
93     /**
94      * Update the permissions of an entity.
95      */
96     public function updatePermissions(Entity $entity, bool $restricted, Collection $permissions = null)
97     {
98         $entity->restricted = $restricted;
99         $entity->permissions()->delete();
100
101         if (!is_null($permissions)) {
102             $entityPermissionData = $permissions->flatMap(function ($restrictions, $roleId) {
103                 return collect($restrictions)->keys()->map(function ($action) use ($roleId) {
104                     return [
105                         'role_id' => $roleId,
106                         'action' => strtolower($action),
107                     ] ;
108                 });
109             });
110
111             $entity->permissions()->createMany($entityPermissionData);
112         }
113
114         $entity->save();
115         $entity->rebuildPermissions();
116         Activity::addForEntity($entity, ActivityType::PERMISSIONS_UPDATE);
117     }
118 }