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