Skip to content

Commit 779f09b

Browse files
committed
Merge branch 'chapter-templates' into development
2 parents 16af833 + 43a72fb commit 779f09b

23 files changed

+500
-258
lines changed

app/Api/ListingResponseBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function toResponse(): JsonResponse
6161
}
6262
});
6363

64+
dd($data->first());
65+
6466
return response()->json([
6567
'data' => $data,
6668
'total' => $total,

app/Entities/Controllers/ChapterApiController.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ class ChapterApiController extends ApiController
1515
{
1616
protected $rules = [
1717
'create' => [
18-
'book_id' => ['required', 'integer'],
19-
'name' => ['required', 'string', 'max:255'],
20-
'description' => ['string', 'max:1900'],
21-
'description_html' => ['string', 'max:2000'],
22-
'tags' => ['array'],
23-
'priority' => ['integer'],
18+
'book_id' => ['required', 'integer'],
19+
'name' => ['required', 'string', 'max:255'],
20+
'description' => ['string', 'max:1900'],
21+
'description_html' => ['string', 'max:2000'],
22+
'tags' => ['array'],
23+
'priority' => ['integer'],
24+
'default_template_id' => ['nullable', 'integer'],
2425
],
2526
'update' => [
26-
'book_id' => ['integer'],
27-
'name' => ['string', 'min:1', 'max:255'],
28-
'description' => ['string', 'max:1900'],
29-
'description_html' => ['string', 'max:2000'],
30-
'tags' => ['array'],
31-
'priority' => ['integer'],
27+
'book_id' => ['integer'],
28+
'name' => ['string', 'min:1', 'max:255'],
29+
'description' => ['string', 'max:1900'],
30+
'description_html' => ['string', 'max:2000'],
31+
'tags' => ['array'],
32+
'priority' => ['integer'],
33+
'default_template_id' => ['nullable', 'integer'],
3234
],
3335
];
3436

app/Entities/Controllers/ChapterController.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ public function create(string $bookSlug)
4949
public function store(Request $request, string $bookSlug)
5050
{
5151
$validated = $this->validate($request, [
52-
'name' => ['required', 'string', 'max:255'],
53-
'description_html' => ['string', 'max:2000'],
54-
'tags' => ['array'],
52+
'name' => ['required', 'string', 'max:255'],
53+
'description_html' => ['string', 'max:2000'],
54+
'tags' => ['array'],
55+
'default_template_id' => ['nullable', 'integer'],
5556
]);
5657

5758
$book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
@@ -111,9 +112,10 @@ public function edit(string $bookSlug, string $chapterSlug)
111112
public function update(Request $request, string $bookSlug, string $chapterSlug)
112113
{
113114
$validated = $this->validate($request, [
114-
'name' => ['required', 'string', 'max:255'],
115-
'description_html' => ['string', 'max:2000'],
116-
'tags' => ['array'],
115+
'name' => ['required', 'string', 'max:255'],
116+
'description_html' => ['string', 'max:2000'],
117+
'tags' => ['array'],
118+
'default_template_id' => ['nullable', 'integer'],
117119
]);
118120

119121
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);

app/Entities/Controllers/PageController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Activity\Tools\CommentTree;
77
use BookStack\Activity\Tools\UserEntityWatchOptions;
88
use BookStack\Entities\Models\Book;
9+
use BookStack\Entities\Models\Chapter;
910
use BookStack\Entities\Models\Page;
1011
use BookStack\Entities\Repos\PageRepo;
1112
use BookStack\Entities\Tools\BookContents;
@@ -259,7 +260,9 @@ public function showDelete(string $bookSlug, string $pageSlug)
259260
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
260261
$this->checkOwnablePermission('page-delete', $page);
261262
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
262-
$usedAsTemplate = Book::query()->where('default_template_id', '=', $page->id)->count() > 0;
263+
$usedAsTemplate =
264+
Book::query()->where('default_template_id', '=', $page->id)->count() > 0 ||
265+
Chapter::query()->where('default_template_id', '=', $page->id)->count() > 0;
263266

264267
return view('pages.delete', [
265268
'book' => $page->book,
@@ -279,7 +282,9 @@ public function showDeleteDraft(string $bookSlug, int $pageId)
279282
$page = $this->pageRepo->getById($pageId);
280283
$this->checkOwnablePermission('page-update', $page);
281284
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
282-
$usedAsTemplate = Book::query()->where('default_template_id', '=', $page->id)->count() > 0;
285+
$usedAsTemplate =
286+
Book::query()->where('default_template_id', '=', $page->id)->count() > 0 ||
287+
Chapter::query()->where('default_template_id', '=', $page->id)->count() > 0;
283288

284289
return view('pages.delete', [
285290
'book' => $page->book,

app/Entities/Models/Chapter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BookStack\Entities\Models;
44

5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Relations\HasMany;
78
use Illuminate\Support\Collection;
@@ -11,6 +12,8 @@
1112
*
1213
* @property Collection<Page> $pages
1314
* @property string $description
15+
* @property ?int $default_template_id
16+
* @property ?Page $defaultTemplate
1417
*/
1518
class Chapter extends BookChild
1619
{
@@ -48,6 +51,14 @@ public function getUrl(string $path = ''): string
4851
return url('/' . implode('/', $parts));
4952
}
5053

54+
/**
55+
* Get the Page that is used as default template for newly created pages within this Chapter.
56+
*/
57+
public function defaultTemplate(): BelongsTo
58+
{
59+
return $this->belongsTo(Page::class, 'default_template_id');
60+
}
61+
5162
/**
5263
* Get the visible pages in this chapter.
5364
*/

app/Entities/Repos/BaseRepo.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace BookStack\Entities\Repos;
44

55
use BookStack\Activity\TagRepo;
6+
use BookStack\Entities\Models\Book;
7+
use BookStack\Entities\Models\Chapter;
68
use BookStack\Entities\Models\Entity;
79
use BookStack\Entities\Models\HasCoverImage;
810
use BookStack\Entities\Models\HasHtmlDescription;
11+
use BookStack\Entities\Models\Page;
912
use BookStack\Exceptions\ImageUploadException;
1013
use BookStack\References\ReferenceStore;
1114
use BookStack\References\ReferenceUpdater;
@@ -104,6 +107,33 @@ public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $remov
104107
}
105108
}
106109

110+
/**
111+
* Update the default page template used for this item.
112+
* Checks that, if changing, the provided value is a valid template and the user
113+
* has visibility of the provided page template id.
114+
*/
115+
public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): void
116+
{
117+
$changing = $templateId !== intval($entity->default_template_id);
118+
if (!$changing) {
119+
return;
120+
}
121+
122+
if ($templateId === 0) {
123+
$entity->default_template_id = null;
124+
$entity->save();
125+
return;
126+
}
127+
128+
$templateExists = Page::query()->visible()
129+
->where('template', '=', true)
130+
->where('id', '=', $templateId)
131+
->exists();
132+
133+
$entity->default_template_id = $templateExists ? $templateId : null;
134+
$entity->save();
135+
}
136+
107137
protected function updateDescription(Entity $entity, array $input): void
108138
{
109139
if (!in_array(HasHtmlDescription::class, class_uses($entity))) {

app/Entities/Repos/BookRepo.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function create(array $input): Book
8686
$book = new Book();
8787
$this->baseRepo->create($book, $input);
8888
$this->baseRepo->updateCoverImage($book, $input['image'] ?? null);
89-
$this->updateBookDefaultTemplate($book, intval($input['default_template_id'] ?? null));
89+
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id'] ?? null));
9090
Activity::add(ActivityType::BOOK_CREATE, $book);
9191

9292
return $book;
@@ -100,7 +100,7 @@ public function update(Book $book, array $input): Book
100100
$this->baseRepo->update($book, $input);
101101

102102
if (array_key_exists('default_template_id', $input)) {
103-
$this->updateBookDefaultTemplate($book, intval($input['default_template_id']));
103+
$this->baseRepo->updateDefaultTemplate($book, intval($input['default_template_id']));
104104
}
105105

106106
if (array_key_exists('image', $input)) {
@@ -112,33 +112,6 @@ public function update(Book $book, array $input): Book
112112
return $book;
113113
}
114114

115-
/**
116-
* Update the default page template used for this book.
117-
* Checks that, if changing, the provided value is a valid template and the user
118-
* has visibility of the provided page template id.
119-
*/
120-
protected function updateBookDefaultTemplate(Book $book, int $templateId): void
121-
{
122-
$changing = $templateId !== intval($book->default_template_id);
123-
if (!$changing) {
124-
return;
125-
}
126-
127-
if ($templateId === 0) {
128-
$book->default_template_id = null;
129-
$book->save();
130-
return;
131-
}
132-
133-
$templateExists = Page::query()->visible()
134-
->where('template', '=', true)
135-
->where('id', '=', $templateId)
136-
->exists();
137-
138-
$book->default_template_id = $templateExists ? $templateId : null;
139-
$book->save();
140-
}
141-
142115
/**
143116
* Update the given book's cover image, or clear it.
144117
*

app/Entities/Repos/ChapterRepo.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use BookStack\Activity\ActivityType;
66
use BookStack\Entities\Models\Book;
7+
use BookStack\Entities\Models\Page;
78
use BookStack\Entities\Models\Chapter;
8-
use BookStack\Entities\Models\Entity;
99
use BookStack\Entities\Tools\BookContents;
1010
use BookStack\Entities\Tools\TrashCan;
1111
use BookStack\Exceptions\MoveOperationException;
@@ -46,6 +46,7 @@ public function create(array $input, Book $parentBook): Chapter
4646
$chapter->book_id = $parentBook->id;
4747
$chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
4848
$this->baseRepo->create($chapter, $input);
49+
$this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
4950
Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
5051

5152
return $chapter;
@@ -57,6 +58,11 @@ public function create(array $input, Book $parentBook): Chapter
5758
public function update(Chapter $chapter, array $input): Chapter
5859
{
5960
$this->baseRepo->update($chapter, $input);
61+
62+
if (array_key_exists('default_template_id', $input)) {
63+
$this->baseRepo->updateDefaultTemplate($chapter, intval($input['default_template_id']));
64+
}
65+
6066
Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
6167

6268
return $chapter;

app/Entities/Repos/PageRepo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function getNewDraftPage(Entity $parent)
136136
$page->book_id = $parent->id;
137137
}
138138

139-
$defaultTemplate = $page->book->defaultTemplate;
139+
$defaultTemplate = $page->chapter->defaultTemplate ?? $page->book->defaultTemplate;
140140
if ($defaultTemplate && userCan('view', $defaultTemplate)) {
141141
$page->forceFill([
142142
'html' => $defaultTemplate->html,

app/Entities/Tools/TrashCan.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ protected function destroyPage(Page $page): int
206206
Book::query()->where('default_template_id', '=', $page->id)
207207
->update(['default_template_id' => null]);
208208

209+
// Remove chapter template usages
210+
Chapter::query()->where('default_template_id', '=', $page->id)
211+
->update(['default_template_id' => null]);
212+
209213
$page->forceDelete();
210214

211215
return 1;

0 commit comments

Comments
 (0)