]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
2894a04e36ee6388ea29c41ef133a74466288003
[bookstack] / app / Entities / Repos / BaseRepo.php
1 <?php
2
3 namespace BookStack\Entities\Repos;
4
5 use BookStack\Activity\TagRepo;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\HasCoverImage;
8 use BookStack\Exceptions\ImageUploadException;
9 use BookStack\References\ReferenceUpdater;
10 use BookStack\Uploads\ImageRepo;
11 use Illuminate\Http\UploadedFile;
12
13 class BaseRepo
14 {
15     protected TagRepo $tagRepo;
16     protected ImageRepo $imageRepo;
17     protected ReferenceUpdater $referenceUpdater;
18
19     public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo, ReferenceUpdater $referenceUpdater)
20     {
21         $this->tagRepo = $tagRepo;
22         $this->imageRepo = $imageRepo;
23         $this->referenceUpdater = $referenceUpdater;
24     }
25
26     /**
27      * Create a new entity in the system.
28      */
29     public function create(Entity $entity, array $input)
30     {
31         $entity->fill($input);
32         $entity->forceFill([
33             'created_by' => user()->id,
34             'updated_by' => user()->id,
35             'owned_by'   => user()->id,
36         ]);
37         $entity->refreshSlug();
38         $entity->save();
39
40         if (isset($input['tags'])) {
41             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
42         }
43
44         $entity->refresh();
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         $oldUrl = $entity->getUrl();
55
56         $entity->fill($input);
57         $entity->updated_by = user()->id;
58
59         if ($entity->isDirty('name') || empty($entity->slug)) {
60             $entity->refreshSlug();
61         }
62
63         $entity->save();
64
65         if (isset($input['tags'])) {
66             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
67             $entity->touch();
68         }
69
70         $entity->rebuildPermissions();
71         $entity->indexForSearch();
72
73         if ($oldUrl !== $entity->getUrl()) {
74             $this->referenceUpdater->updateEntityPageReferences($entity, $oldUrl);
75         }
76     }
77
78     /**
79      * Update the given items' cover image, or clear it.
80      *
81      * @param Entity&HasCoverImage $entity
82      *
83      * @throws ImageUploadException
84      * @throws \Exception
85      */
86     public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
87     {
88         if ($coverImage) {
89             $imageType = $entity->coverImageTypeKey();
90             $this->imageRepo->destroyImage($entity->cover()->first());
91             $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
92             $entity->cover()->associate($image);
93             $entity->save();
94         }
95
96         if ($removeImage) {
97             $this->imageRepo->destroyImage($entity->cover()->first());
98             $entity->image_id = 0;
99             $entity->save();
100         }
101     }
102 }