]> BookStack Code Mirror - bookstack/blob - app/Entities/Repos/BaseRepo.php
Merge pull request #5913 from BookStackApp/slug_history
[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\BookChild;
7 use BookStack\Entities\Models\HasCoverInterface;
8 use BookStack\Entities\Models\HasDescriptionInterface;
9 use BookStack\Entities\Models\Entity;
10 use BookStack\Entities\Queries\PageQueries;
11 use BookStack\Entities\Tools\SlugGenerator;
12 use BookStack\Entities\Tools\SlugHistory;
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         protected SlugGenerator $slugGenerator,
31         protected SlugHistory $slugHistory,
32     ) {
33     }
34
35     /**
36      * Create a new entity in the system.
37      * @template T of Entity
38      * @param T $entity
39      * @return T
40      */
41     public function create(Entity $entity, array $input): Entity
42     {
43         $entity = (clone $entity)->refresh();
44         $entity->fill($input);
45         $entity->forceFill([
46             'created_by' => user()->id,
47             'updated_by' => user()->id,
48             'owned_by'   => user()->id,
49         ]);
50         $this->refreshSlug($entity);
51
52         if ($entity instanceof HasDescriptionInterface) {
53             $this->updateDescription($entity, $input);
54         }
55
56         $entity->save();
57
58         if (isset($input['tags'])) {
59             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
60         }
61
62         $entity->refresh();
63         $entity->rebuildPermissions();
64         $entity->indexForSearch();
65
66         $this->referenceStore->updateForEntity($entity);
67
68         return $entity;
69     }
70
71     /**
72      * Update the given entity.
73      * @template T of Entity
74      * @param T $entity
75      * @return T
76      */
77     public function update(Entity $entity, array $input): Entity
78     {
79         $oldUrl = $entity->getUrl();
80
81         $entity->fill($input);
82         $entity->updated_by = user()->id;
83
84         if ($entity->isDirty('name') || empty($entity->slug)) {
85             $this->refreshSlug($entity);
86         }
87
88         if ($entity instanceof HasDescriptionInterface) {
89             $this->updateDescription($entity, $input);
90         }
91
92         $entity->save();
93
94         if (isset($input['tags'])) {
95             $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
96             $entity->touch();
97         }
98
99         $entity->indexForSearch();
100         $this->referenceStore->updateForEntity($entity);
101
102         if ($oldUrl !== $entity->getUrl()) {
103             $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
104         }
105
106         return $entity;
107     }
108
109     /**
110      * Update the given items' cover image or clear it.
111      *
112      * @throws ImageUploadException
113      * @throws \Exception
114      */
115     public function updateCoverImage(Entity&HasCoverInterface $entity, ?UploadedFile $coverImage, bool $removeImage = false): void
116     {
117         if ($coverImage) {
118             $imageType = 'cover_' . $entity->type;
119             $this->imageRepo->destroyImage($entity->coverInfo()->getImage());
120             $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
121             $entity->coverInfo()->setImage($image);
122             $entity->save();
123         }
124
125         if ($removeImage) {
126             $this->imageRepo->destroyImage($entity->coverInfo()->getImage());
127             $entity->coverInfo()->setImage(null);
128             $entity->save();
129         }
130     }
131
132     /**
133      * Sort the parent of the given entity if any auto sort actions are set for it.
134      * Typically ran during create/update/insert events.
135      */
136     public function sortParent(Entity $entity): void
137     {
138         if ($entity instanceof BookChild) {
139             $book = $entity->book;
140             $this->bookSorter->runBookAutoSort($book);
141         }
142     }
143
144     /**
145      * Update the description of the given entity from input data.
146      */
147     protected function updateDescription(Entity $entity, array $input): void
148     {
149         if (!$entity instanceof HasDescriptionInterface) {
150             return;
151         }
152
153         if (isset($input['description_html'])) {
154             $entity->descriptionInfo()->set(
155                 HtmlDescriptionFilter::filterFromString($input['description_html']),
156                 html_entity_decode(strip_tags($input['description_html']))
157             );
158         } else if (isset($input['description'])) {
159             $entity->descriptionInfo()->set('', $input['description']);
160         }
161     }
162
163     /**
164      * Refresh the slug for the given entity.
165      */
166     public function refreshSlug(Entity $entity): void
167     {
168         $this->slugHistory->recordForEntity($entity);
169         $this->slugGenerator->regenerateForEntity($entity);
170     }
171 }