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