Skip to content

Commit bf09b42

Browse files
committed
Entities: Been through repos to update for new format
1 parent c60052b commit bf09b42

File tree

9 files changed

+81
-57
lines changed

9 files changed

+81
-57
lines changed

app/Entities/Models/EntityContainerData.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ public function cover(): HasOne
2727
return $this->hasOne(Image::class, 'image_id');
2828
}
2929

30+
/**
31+
* Check if this data supports having a default template assigned.
32+
*/
33+
public function supportsDefaultTemplate(): bool
34+
{
35+
return in_array($this->entity_type, ['book', 'chapter']);
36+
}
37+
38+
/**
39+
* Check this data supports having a cover image assigned.
40+
*/
41+
public function supportsCoverImage(): bool
42+
{
43+
return in_array($this->entity_type, ['book', 'bookshelf']);
44+
}
45+
3046
/**
3147
* Get the description as a cleaned/handled HTML string.
3248
*/

app/Entities/Models/Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/**
1616
* Class Page.
17-
*
17+
* @property EntityPageData $pageData
1818
* @property int $chapter_id
1919
* @property string $html
2020
* @property string $markdown

app/Entities/Repos/BaseRepo.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public function update(Entity $entity, array $input): void
101101
*/
102102
public function updateCoverImage(EntityContainerData $containerData, ?UploadedFile $coverImage, bool $removeImage = false): void
103103
{
104+
if (!$containerData->supportsCoverImage()) {
105+
return;
106+
}
107+
104108
if ($coverImage) {
105109
$imageType = 'cover_' . $containerData->entity_type;
106110
$this->imageRepo->destroyImage($containerData->cover()->first());
@@ -121,25 +125,25 @@ public function updateCoverImage(EntityContainerData $containerData, ?UploadedFi
121125
* Checks that, if changing, the provided value is a valid template and the user
122126
* has visibility of the provided page template id.
123127
*/
124-
public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
128+
public function updateDefaultTemplate(EntityContainerData $containerData, int $templateId): void
125129
{
126-
$changing = $templateId !== intval($entity->default_template_id);
127-
if (!$changing) {
130+
$changing = $templateId !== intval($containerData->default_template_id);
131+
if (!$changing || !$containerData->supportsDefaultTemplate()) {
128132
return;
129133
}
130134

131135
if ($templateId === 0) {
132-
$entity->default_template_id = null;
133-
$entity->save();
136+
$containerData->default_template_id = null;
137+
$containerData->save();
134138
return;
135139
}
136140

137141
$templateExists = $this->pageQueries->visibleTemplates()
138142
->where('id', '=', $templateId)
139143
->exists();
140144

141-
$entity->default_template_id = $templateExists ? $templateId : null;
142-
$entity->save();
145+
$containerData->default_template_id = $templateExists ? $templateId : null;
146+
$containerData->save();
143147
}
144148

145149
/**

app/Entities/Repos/BookRepo.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function create(array $input): Book
3333
$book = new Book();
3434

3535
$this->baseRepo->create($book, $input);
36-
$this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
37-
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
36+
$this->baseRepo->updateCoverImage($book->containerData, $input['image'] ?? null);
37+
$this->baseRepo->updateDefaultTemplate($book->containerData, intval($input['default_template_id'] ?? null));
3838
Activity::add(ActivityType::BOOK_CREATE, $book);
3939

4040
$defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
4141
if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
42-
$book->sort_rule_id = $defaultBookSortSetting;
43-
$book->save();
42+
$book->containerData->sort_rule_id = $defaultBookSortSetting;
43+
$book->containerData->save();
4444
}
4545

4646
return $book;
@@ -55,11 +55,11 @@ public function update(Book $book, array $input): Book
5555
$this->baseRepo->update($book, $input);
5656

5757
if (array_key_exists('default_template_id', $input)) {
58-
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
58+
$this->baseRepo->updateDefaultTemplate($book->containerData, intval($input['default_template_id']));
5959
}
6060

6161
if (array_key_exists('image', $input)) {
62-
$this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
62+
$this->baseRepo->updateCoverImage($book->containerData, $input['image'], $input['image'] === null);
6363
}
6464

6565
Activity::add(ActivityType::BOOK_UPDATE, $book);
@@ -68,22 +68,22 @@ public function update(Book $book, array $input): Book
6868
}
6969

7070
/**
71-
* Update the given book's cover image, or clear it.
71+
* Update the given book's cover image or clear it.
7272
*
7373
* @throws ImageUploadException
7474
* @throws Exception
7575
*/
76-
public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false)
76+
public function updateCoverImage(Book $book, ?UploadedFile $coverImage, bool $removeImage = false): void
7777
{
78-
$this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
78+
$this->baseRepo->updateCoverImage($book->containerData, $coverImage, $removeImage);
7979
}
8080

8181
/**
8282
* Remove a book from the system.
8383
*
8484
* @throws Exception
8585
*/
86-
public function destroy(Book $book)
86+
public function destroy(Book $book): void
8787
{
8888
$this->trashCan->softDestroyBook($book);
8989
Activity::add(ActivityType::BOOK_DELETE, $book);

app/Entities/Repos/BookshelfRepo.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function create(array $input, array $bookIds): Bookshelf
2727
return (new DatabaseTransaction(function () use ($input, $bookIds) {
2828
$shelf = new Bookshelf();
2929
$this->baseRepo->create($shelf, $input);
30-
$this->baseRepo->updateCoverImage($shelf, $input['image'] ?? null);
30+
$this->baseRepo->updateCoverImage($shelf->containerData, $input['image'] ?? null);
3131
$this->updateBooks($shelf, $bookIds);
3232
Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
3333
return $shelf;
@@ -46,7 +46,7 @@ public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshe
4646
}
4747

4848
if (array_key_exists('image', $input)) {
49-
$this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
49+
$this->baseRepo->updateCoverImage($shelf->containerData, $input['image'], $input['image'] === null);
5050
}
5151

5252
Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
@@ -96,7 +96,7 @@ protected function updateBooks(Bookshelf $shelf, array $bookIds): void
9696
*
9797
* @throws Exception
9898
*/
99-
public function destroy(Bookshelf $shelf)
99+
public function destroy(Bookshelf $shelf): void
100100
{
101101
$this->trashCan->softDestroyShelf($shelf);
102102
Activity::add(ActivityType::BOOKSHELF_DELETE, $shelf);

app/Entities/Repos/ChapterRepo.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function create(array $input, Book $parentBook): Chapter
3434
$chapter->book_id = $parentBook->id;
3535
$chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
3636
$this->baseRepo->create($chapter, $input);
37-
$this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
37+
$this->baseRepo->updateDefaultTemplate($chapter->containerData, intval($input['default_template_id'] ?? null));
3838
Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
3939

4040
$this->baseRepo->sortParent($chapter);
@@ -51,7 +51,7 @@ public function update(Chapter $chapter, array $input): Chapter
5151
$this->baseRepo->update($chapter, $input);
5252

5353
if (array_key_exists('default_template_id', $input)) {
54-
$this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id']));
54+
$this->baseRepo->updateDefaultTemplate($chapter->containerData, intval($input['default_template_id']));
5555
}
5656

5757
Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
@@ -66,7 +66,7 @@ public function update(Chapter $chapter, array $input): Chapter
6666
*
6767
* @throws Exception
6868
*/
69-
public function destroy(Chapter $chapter)
69+
public function destroy(Chapter $chapter): void
7070
{
7171
$this->trashCan->softDestroyChapter($chapter);
7272
Activity::add(ActivityType::CHAPTER_DELETE, $chapter);

app/Entities/Repos/DeletionRepo.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99

1010
class DeletionRepo
1111
{
12-
private TrashCan $trashCan;
13-
14-
public function __construct(TrashCan $trashCan)
15-
{
16-
$this->trashCan = $trashCan;
12+
public function __construct(
13+
protected TrashCan $trashCan
14+
) {
1715
}
1816

1917
public function restore(int $id): int

app/Entities/Repos/PageRepo.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Entities\Models\Book;
77
use BookStack\Entities\Models\Chapter;
88
use BookStack\Entities\Models\Entity;
9+
use BookStack\Entities\Models\EntityPageData;
910
use BookStack\Entities\Models\Page;
1011
use BookStack\Entities\Models\PageRevision;
1112
use BookStack\Entities\Queries\EntityQueries;
@@ -37,34 +38,37 @@ public function __construct(
3738
/**
3839
* Get a new draft page belonging to the given parent entity.
3940
*/
40-
public function getNewDraftPage(Entity $parent)
41+
public function getNewDraftPage(Entity $parent): Page
4142
{
4243
$page = (new Page())->forceFill([
4344
'name' => trans('entities.pages_initial_name'),
4445
'created_by' => user()->id,
4546
'owned_by' => user()->id,
4647
'updated_by' => user()->id,
48+
]);
49+
$pageData = (new EntityPageData())->forceFill([
4750
'draft' => true,
4851
'editor' => PageEditorType::getSystemDefault()->value,
4952
]);
5053

5154
if ($parent instanceof Chapter) {
52-
$page->chapter_id = $parent->id;
55+
$pageData->chapter_id = $parent->id;
5356
$page->book_id = $parent->book_id;
5457
} else {
5558
$page->book_id = $parent->id;
5659
}
5760

58-
$defaultTemplate = $page->chapter->defaultTemplate ?? $page->book->defaultTemplate;
61+
$defaultTemplate = $page->chapter->containerData->defaultTemplate ?? $page->book->containerData->defaultTemplate;
5962
if ($defaultTemplate && userCan(Permission::PageView, $defaultTemplate)) {
60-
$page->forceFill([
63+
$pageData->forceFill([
6164
'html' => $defaultTemplate->html,
6265
'markdown' => $defaultTemplate->markdown,
6366
]);
6467
}
6568

66-
(new DatabaseTransaction(function () use ($page) {
69+
(new DatabaseTransaction(function () use ($page, $pageData) {
6770
$page->save();
71+
$page->pageData()->save($pageData);
6872
$page->refresh()->rebuildPermissions();
6973
}))->run();
7074

@@ -77,9 +81,9 @@ public function getNewDraftPage(Entity $parent)
7781
public function publishDraft(Page $draft, array $input): Page
7882
{
7983
return (new DatabaseTransaction(function () use ($draft, $input) {
80-
$draft->draft = false;
81-
$draft->revision_count = 1;
82-
$draft->priority = $this->getNewPriority($draft);
84+
$draft->pageData->draft = false;
85+
$draft->pageData->revision_count = 1;
86+
$draft->pageData->priority = $this->getNewPriority($draft);
8387
$this->updateTemplateStatusAndContentFromInput($draft, $input);
8488
$this->baseRepo->update($draft, $input);
8589
$draft->rebuildPermissions();
@@ -112,15 +116,16 @@ public function setContentFromInput(Page $page, array $input): void
112116
public function update(Page $page, array $input): Page
113117
{
114118
// Hold the old details to compare later
115-
$oldHtml = $page->html;
116119
$oldName = $page->name;
117-
$oldMarkdown = $page->markdown;
120+
$oldHtml = $page->pageData->html;
121+
$oldMarkdown = $page->pageData->markdown;
118122

119123
$this->updateTemplateStatusAndContentFromInput($page, $input);
120124
$this->baseRepo->update($page, $input);
121125

122126
// Update with new details
123-
$page->revision_count++;
127+
$page->pageData->revision_count++;
128+
$page->pageData->save();
124129
$page->save();
125130

126131
// Remove all update drafts for this user and page.
@@ -144,7 +149,7 @@ public function update(Page $page, array $input): Page
144149
protected function updateTemplateStatusAndContentFromInput(Page $page, array $input): void
145150
{
146151
if (isset($input['template']) && userCan(Permission::TemplatesManage)) {
147-
$page->template = ($input['template'] === 'true');
152+
$page->pageData->template = ($input['template'] === 'true');
148153
}
149154

150155
$pageContent = new PageContent($page);
@@ -166,22 +171,22 @@ protected function updateTemplateStatusAndContentFromInput(Page $page, array $in
166171
$pageContent->setNewHTML($input['html'], user());
167172
}
168173

169-
if (($newEditor !== $currentEditor || empty($page->editor)) && userCan(Permission::EditorChange)) {
170-
$page->editor = $newEditor->value;
171-
} elseif (empty($page->editor)) {
172-
$page->editor = $defaultEditor->value;
174+
if (($newEditor !== $currentEditor || empty($page->pageData->editor)) && userCan(Permission::EditorChange)) {
175+
$page->pageData->editor = $newEditor->value;
176+
} elseif (empty($page->pageData->editor)) {
177+
$page->pageData->editor = $defaultEditor->value;
173178
}
174179
}
175180

176181
/**
177182
* Save a page update draft.
178183
*/
179-
public function updatePageDraft(Page $page, array $input)
184+
public function updatePageDraft(Page $page, array $input): Page|PageRevision
180185
{
181-
// If the page itself is a draft simply update that
186+
// If the page itself is a draft, simply update that
182187
if ($page->draft) {
183188
$this->updateTemplateStatusAndContentFromInput($page, $input);
184-
$page->fill($input);
189+
$page->forceFill(array_intersect_key($input, array_flip(['name'])))->save();
185190
$page->save();
186191

187192
return $page;
@@ -209,7 +214,7 @@ public function updatePageDraft(Page $page, array $input)
209214
*
210215
* @throws Exception
211216
*/
212-
public function destroy(Page $page)
217+
public function destroy(Page $page): void
213218
{
214219
$this->trashCan->softDestroyPage($page);
215220
Activity::add(ActivityType::PAGE_DELETE, $page);
@@ -222,7 +227,7 @@ public function destroy(Page $page)
222227
public function restoreRevision(Page $page, int $revisionId): Page
223228
{
224229
$oldUrl = $page->getUrl();
225-
$page->revision_count++;
230+
$page->pageData->revision_count++;
226231

227232
/** @var PageRevision $revision */
228233
$revision = $page->revisions()->where('id', '=', $revisionId)->first();
@@ -238,6 +243,7 @@ public function restoreRevision(Page $page, int $revisionId): Page
238243

239244
$page->updated_by = user()->id;
240245
$page->refreshSlug();
246+
$page->pageData->save();
241247
$page->save();
242248
$page->indexForSearch();
243249
$this->referenceStore->updateForEntity($page);
@@ -277,7 +283,7 @@ public function move(Page $page, string $parentIdentifier): Entity
277283
}
278284

279285
return (new DatabaseTransaction(function () use ($page, $parent) {
280-
$page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null;
286+
$page->pageData->chapter_id = ($parent instanceof Chapter) ? $parent->id : null;
281287
$newBookId = ($parent instanceof Chapter) ? $parent->book->id : $parent->id;
282288
$page->changeBook($newBookId);
283289
$page->rebuildPermissions();

0 commit comments

Comments
 (0)