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