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