]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
3d3d16732a6057b89b41084b9ba9f7502555c9f9
[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\Entities\Models\HasHtmlDescription;
9 use BookStack\Exceptions\ImageUploadException;
10 use BookStack\References\ReferenceStore;
11 use BookStack\References\ReferenceUpdater;
12 use BookStack\Uploads\ImageRepo;
13 use Illuminate\Http\UploadedFile;
14
15 class BaseRepo
16 {
17     public function __construct(
18         protected TagRepo $tagRepo,
19         protected ImageRepo $imageRepo,
20         protected ReferenceUpdater $referenceUpdater,
21         protected ReferenceStore $referenceStore,
22     ) {
23     }
24
25     /**
26      * Create a new entity in the system.
27      */
28     public function create(Entity $entity, array $input)
29     {
30         $entity->fill($input);
31         $this->updateDescription($entity, $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         $this->referenceStore->updateForEntity($entity);
48     }
49
50     /**
51      * Update the given entity.
52      */
53     public function update(Entity $entity, array $input)
54     {
55         $oldUrl = $entity->getUrl();
56
57         $entity->fill($input);
58         $this->updateDescription($entity, $input);
59         $entity->updated_by = user()->id;
60
61         if ($entity->isDirty('name') || empty($entity->slug)) {
62             $entity->refreshSlug();
63         }
64
65         $entity->save();
66
67         if (isset($input['tags'])) {
68             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
69             $entity->touch();
70         }
71
72         $entity->rebuildPermissions();
73         $entity->indexForSearch();
74         $this->referenceStore->updateForEntity($entity);
75
76         if ($oldUrl !== $entity->getUrl()) {
77             $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
78         }
79     }
80
81     /**
82      * Update the given items' cover image, or clear it.
83      *
84      * @param Entity&HasCoverImage $entity
85      *
86      * @throws ImageUploadException
87      * @throws \Exception
88      */
89     public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
90     {
91         if ($coverImage) {
92             $imageType = $entity->coverImageTypeKey();
93             $this->imageRepo->destroyImage($entity->cover()->first());
94             $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
95             $entity->cover()->associate($image);
96             $entity->save();
97         }
98
99         if ($removeImage) {
100             $this->imageRepo->destroyImage($entity->cover()->first());
101             $entity->image_id = 0;
102             $entity->save();
103         }
104     }
105
106     protected function updateDescription(Entity $entity, array $input): void
107     {
108         if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
109             return;
110         }
111
112         /** @var HasHtmlDescription $entity */
113         if (isset($input['description_html'])) {
114             $entity->description_html = $input['description_html'];
115             $entity->description = html_entity_decode(strip_tags($input['description_html']));
116         } else if (isset($input['description'])) {
117             $entity->description = $input['description'];
118             $entity->description_html = $entity->descriptionHtml();
119         }
120     }
121 }