]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
Code cleanup, bug squashing
[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         ]);
38         $entity->refreshSlug();
39         $entity->save();
40
41         if (isset($input['tags'])) {
42             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
43         }
44
45         $entity->rebuildPermissions();
46         $entity->indexForSearch();
47     }
48
49     /**
50      * Update the given entity.
51      */
52     public function update(Entity $entity, array $input)
53     {
54         $entity->fill($input);
55         $entity->updated_by = user()->id;
56
57         if ($entity->isDirty('name')) {
58             $entity->refreshSlug();
59         }
60
61         $entity->save();
62
63         if (isset($input['tags'])) {
64             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
65         }
66
67         $entity->rebuildPermissions();
68         $entity->indexForSearch();
69     }
70
71     /**
72      * Update the given items' cover image, or clear it.
73      * @throws ImageUploadException
74      * @throws \Exception
75      */
76     public function updateCoverImage(HasCoverImage $entity, ?UploadedFile $coverImage, bool $removeImage = false)
77     {
78         if ($coverImage) {
79             $this->imageRepo->destroyImage($entity->cover);
80             $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
81             $entity->cover()->associate($image);
82             $entity->save();
83         }
84
85         if ($removeImage) {
86             $this->imageRepo->destroyImage($entity->cover);
87             $entity->image_id = 0;
88             $entity->save();
89         }
90     }
91
92     /**
93      * Update the permissions of an entity.
94      */
95     public function updatePermissions(Entity $entity, bool $restricted, Collection $permissions = null)
96     {
97         $entity->restricted = $restricted;
98         $entity->permissions()->delete();
99
100         if (!is_null($permissions)) {
101             $entityPermissionData = $permissions->flatMap(function ($restrictions, $roleId) {
102                 return collect($restrictions)->keys()->map(function ($action) use ($roleId) {
103                     return [
104                         'role_id' => $roleId,
105                         'action' => strtolower($action),
106                     ] ;
107                 });
108             });
109
110             $entity->permissions()->createMany($entityPermissionData);
111         }
112
113         $entity->save();
114         $entity->rebuildPermissions();
115         Activity::addForEntity($entity, ActivityType::PERMISSIONS_UPDATE);
116     }
117 }