]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
Licensing: Added license gen as composer command
[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\Queries\PageQueries;
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         protected PageQueries $pageQueries,
27     ) {
28     }
29
30     /**
31      * Create a new entity in the system.
32      */
33     public function create(Entity $entity, array $input)
34     {
35         $entity->fill($input);
36         $this->updateDescription($entity, $input);
37         $entity->forceFill([
38             'created_by' => user()->id,
39             'updated_by' => user()->id,
40             'owned_by'   => user()->id,
41         ]);
42         $entity->refreshSlug();
43         $entity->save();
44
45         if (isset($input['tags'])) {
46             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
47         }
48
49         $entity->refresh();
50         $entity->rebuildPermissions();
51         $entity->indexForSearch();
52         $this->referenceStore->updateForEntity($entity);
53     }
54
55     /**
56      * Update the given entity.
57      */
58     public function update(Entity $entity, array $input)
59     {
60         $oldUrl = $entity->getUrl();
61
62         $entity->fill($input);
63         $this->updateDescription($entity, $input);
64         $entity->updated_by = user()->id;
65
66         if ($entity->isDirty('name') || empty($entity->slug)) {
67             $entity->refreshSlug();
68         }
69
70         $entity->save();
71
72         if (isset($input['tags'])) {
73             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
74             $entity->touch();
75         }
76
77         $entity->rebuildPermissions();
78         $entity->indexForSearch();
79         $this->referenceStore->updateForEntity($entity);
80
81         if ($oldUrl !== $entity->getUrl()) {
82             $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
83         }
84     }
85
86     /**
87      * Update the given items' cover image, or clear it.
88      *
89      * @param Entity&HasCoverImage $entity
90      *
91      * @throws ImageUploadException
92      * @throws \Exception
93      */
94     public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
95     {
96         if ($coverImage) {
97             $imageType = $entity->coverImageTypeKey();
98             $this->imageRepo->destroyImage($entity->cover()->first());
99             $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
100             $entity->cover()->associate($image);
101             $entity->save();
102         }
103
104         if ($removeImage) {
105             $this->imageRepo->destroyImage($entity->cover()->first());
106             $entity->image_id = 0;
107             $entity->save();
108         }
109     }
110
111     /**
112      * Update the default page template used for this item.
113      * Checks that, if changing, the provided value is a valid template and the user
114      * has visibility of the provided page template id.
115      */
116     public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
117     {
118         $changing = $templateId !== intval($entity->default_template_id);
119         if (!$changing) {
120             return;
121         }
122
123         if ($templateId === 0) {
124             $entity->default_template_id = null;
125             $entity->save();
126             return;
127         }
128
129         $templateExists = $this->pageQueries->visibleTemplates()
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 }