3 namespace BookStack\Entities\Repos;
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;
21 public function __construct(
22 protected TagRepo $tagRepo,
23 protected ImageRepo $imageRepo,
24 protected ReferenceUpdater $referenceUpdater,
25 protected ReferenceStore $referenceStore,
26 protected PageQueries $pageQueries,
31 * Create a new entity in the system.
33 public function create(Entity $entity, array $input)
35 $entity->fill($input);
36 $this->updateDescription($entity, $input);
38 'created_by' => user()->id,
39 'updated_by' => user()->id,
40 'owned_by' => user()->id,
42 $entity->refreshSlug();
45 if (isset($input['tags'])) {
46 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
50 $entity->rebuildPermissions();
51 $entity->indexForSearch();
52 $this->referenceStore->updateForEntity($entity);
56 * Update the given entity.
58 public function update(Entity $entity, array $input)
60 $oldUrl = $entity->getUrl();
62 $entity->fill($input);
63 $this->updateDescription($entity, $input);
64 $entity->updated_by = user()->id;
66 if ($entity->isDirty('name') || empty($entity->slug)) {
67 $entity->refreshSlug();
72 if (isset($input['tags'])) {
73 $this->tagRepo->saveTagsToEntity($entity, $input['tags']);
77 $entity->rebuildPermissions();
78 $entity->indexForSearch();
79 $this->referenceStore->updateForEntity($entity);
81 if ($oldUrl !== $entity->getUrl()) {
82 $this->referenceUpdater->updateEntityReferences($entity, $oldUrl);
87 * Update the given items' cover image, or clear it.
89 * @param Entity&HasCoverImage $entity
91 * @throws ImageUploadException
94 public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
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);
105 $this->imageRepo->destroyImage($entity->cover()->first());
106 $entity->image_id = 0;
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.
116 public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
118 $changing = $templateId !== intval($entity->default_template_id);
123 if ($templateId === 0) {
124 $entity->default_template_id = null;
129 $templateExists = $this->pageQueries->visibleTemplates()
130 ->where('id', '=', $templateId)
133 $entity->default_template_id = $templateExists ? $templateId : null;
137 protected function updateDescription(Entity $entity, array $input): void
139 if (!in_array(HasHtmlDescription::class, class_uses($entity))) {
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();