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