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