]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
Update PWA manifest orientation to any
[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\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\HasCoverImage;
10 use BookStack\Entities\Models\HasHtmlDescription;
11 use BookStack\Entities\Models\Page;
12 use BookStack\Exceptions\ImageUploadException;
13 use BookStack\References\ReferenceStore;
14 use BookStack\References\ReferenceUpdater;
15 use BookStack\Uploads\ImageRepo;
16 use BookStack\Util\HtmlDescriptionFilter;
17 use Illuminate\Http\UploadedFile;
18
19 class BaseRepo
20 {
21     public function __construct(
22         protected TagRepo $tagRepo,
23         protected ImageRepo $imageRepo,
24         protected ReferenceUpdater $referenceUpdater,
25         protected ReferenceStore $referenceStore,
26     ) {
27     }
28
29     /**
30      * Create a new entity in the system.
31      */
32     public function create(Entity $entity, array $input)
33     {
34         $entity->fill($input);
35         $this->updateDescription($entity, $input);
36         $entity->forceFill([
37             'created_by' => user()->id,
38             'updated_by' => user()->id,
39             'owned_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->refresh();
49         $entity->rebuildPermissions();
50         $entity->indexForSearch();
51         $this->referenceStore->updateForEntity($entity);
52     }
53
54     /**
55      * Update the given entity.
56      */
57     public function update(Entity $entity, array $input)
58     {
59         $oldUrl = $entity->getUrl();
60
61         $entity->fill($input);
62         $this->updateDescription($entity, $input);
63         $entity->updated_by = user()->id;
64
65         if ($entity->isDirty('name') || empty($entity->slug)) {
66             $entity->refreshSlug();
67         }
68
69         $entity->save();
70
71         if (isset($input['tags'])) {
72             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
73             $entity->touch();
74         }
75
76         $entity->rebuildPermissions();
77         $entity->indexForSearch();
78         $this->referenceStore->updateForEntity($entity);
79
80         if ($oldUrl !== $entity->getUrl()) {
81             $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
82         }
83     }
84
85     /**
86      * Update the given items' cover image, or clear it.
87      *
88      * @param Entity&HasCoverImage $entity
89      *
90      * @throws ImageUploadException
91      * @throws \Exception
92      */
93     public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
94     {
95         if ($coverImage) {
96             $imageType = $entity->coverImageTypeKey();
97             $this->imageRepo->destroyImage($entity->cover()->first());
98             $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
99             $entity->cover()->associate($image);
100             $entity->save();
101         }
102
103         if ($removeImage) {
104             $this->imageRepo->destroyImage($entity->cover()->first());
105             $entity->image_id = 0;
106             $entity->save();
107         }
108     }
109
110     /**
111      * Update the default page template used for this item.
112      * Checks that, if changing, the provided value is a valid template and the user
113      * has visibility of the provided page template id.
114      */
115     public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
116     {
117         $changing = $templateId !== intval($entity->default_template_id);
118         if (!$changing) {
119             return;
120         }
121
122         if ($templateId === 0) {
123             $entity->default_template_id = null;
124             $entity->save();
125             return;
126         }
127
128         $templateExists = Page::query()->visible()
129             ->where('template', '=', true)
130             ->where('id', '=', $templateId)
131             ->exists();
132
133         $entity->default_template_id = $templateExists ? $templateId : null;
134         $entity->save();
135     }
136
137     protected function updateDescription(Entity $entity, array $input): void
138     {
139         if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
140             return;
141         }
142
143         /** @var HasHtmlDescription $entity */
144         if (isset($input['description_html'])) {
145             $entity->description_html = HtmlDescriptionFilter::filterFromString($input['description_html']);
146             $entity->description = html_entity_decode(strip_tags($input['description_html']));
147         } else if (isset($input['description'])) {
148             $entity->description = $input['description'];
149             $entity->description_html = '';
150             $entity->description_html = $entity->descriptionHtml();
151         }
152     }
153 }