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