public function store(Request $request, string $bookSlug)
{
$validated = $this->validate($request, [
- 'name' => ['required', 'string', 'max:255'],
- 'description_html' => ['string', 'max:2000'],
- 'tags' => ['array'],
+ 'name' => ['required', 'string', 'max:255'],
+ 'description_html' => ['string', 'max:2000'],
+ 'tags' => ['array'],
+ 'default_template_id' => ['nullable', 'integer'],
]);
$book = Book::visible()->where('slug', '=', $bookSlug)->firstOrFail();
public function update(Request $request, string $bookSlug, string $chapterSlug)
{
$validated = $this->validate($request, [
- 'name' => ['required', 'string', 'max:255'],
- 'description_html' => ['string', 'max:2000'],
- 'tags' => ['array'],
+ 'name' => ['required', 'string', 'max:255'],
+ 'description_html' => ['string', 'max:2000'],
+ 'tags' => ['array'],
+ 'default_template_id' => ['nullable', 'integer'],
]);
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
use BookStack\Activity\Tools\CommentTree;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\BookContents;
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-delete', $page);
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
- $usedAsTemplate = Book::query()->where('default_template_id', '=', $page->id)->count() > 0;
+ $usedAsTemplate =
+ Book::query()->where('default_template_id', '=', $page->id)->count() > 0 ||
+ Chapter::query()->where('default_template_id', '=', $page->id)->count() > 0;
return view('pages.delete', [
'book' => $page->book,
$page = $this->pageRepo->getById($pageId);
$this->checkOwnablePermission('page-update', $page);
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
- $usedAsTemplate = Book::query()->where('default_template_id', '=', $page->id)->count() > 0;
+ $usedAsTemplate =
+ Book::query()->where('default_template_id', '=', $page->id)->count() > 0 ||
+ Chapter::query()->where('default_template_id', '=', $page->id)->count() > 0;
return view('pages.delete', [
'book' => $page->book,
namespace BookStack\Entities\Models;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
*
* @property Collection<Page> $pages
* @property string $description
+ * @property ?int $default_template_id
+ * @property ?Page $defaultTemplate
*/
class Chapter extends BookChild
{
return url('/' . implode('/', $parts));
}
+ /**
+ * Get the Page that is used as default template for newly created pages within this Chapter.
+ */
+ public function defaultTemplate(): BelongsTo
+ {
+ return $this->belongsTo(Page::class, 'default_template_id');
+ }
+
/**
* Get the visible pages in this chapter.
*/
use BookStack\Activity\ActivityType;
use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Page;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Tools\BookContents;
$chapter->book_id = $parentBook->id;
$chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
$this->baseRepo->create($chapter, $input);
+ $this->updateChapterDefaultTemplate($chapter, intval($input['default_template_id'] ?? null));
Activity::add(ActivityType::CHAPTER_CREATE, $chapter);
return $chapter;
public function update(Chapter $chapter, array $input): Chapter
{
$this->baseRepo->update($chapter, $input);
+
+ if (array_key_exists('default_template_id', $input)) {
+ $this->updateChapterDefaultTemplate($chapter, intval($input['default_template_id']));
+ }
+
Activity::add(ActivityType::CHAPTER_UPDATE, $chapter);
return $chapter;
return $parent;
}
+ /**
+ * Update the default page template used for this chapter.
+ * Checks that, if changing, the provided value is a valid template and the user
+ * has visibility of the provided page template id.
+ */
+ protected function updateChapterDefaultTemplate(Chapter $chapter, int $templateId): void
+ {
+ $changing = $templateId !== intval($chapter->default_template_id);
+ if (!$changing) {
+ return;
+ }
+
+ if ($templateId === 0) {
+ $chapter->default_template_id = null;
+ $chapter->save();
+ return;
+ }
+
+ $templateExists = Page::query()->visible()
+ ->where('template', '=', true)
+ ->where('id', '=', $templateId)
+ ->exists();
+
+ $chapter->default_template_id = $templateExists ? $templateId : null;
+ $chapter->save();
+ }
+
/**
* Find a page parent entity via an identifier string in the format:
* {type}:{id}
$page->book_id = $parent->id;
}
- $defaultTemplate = $page->book->defaultTemplate;
+ // check for chapter
+ if ($page->chapter_id) {
+ $defaultTemplate = $page->chapter->defaultTemplate;
+ } else {
+ $defaultTemplate = $page->book->defaultTemplate;
+ }
+
if ($defaultTemplate && userCan('view', $defaultTemplate)) {
$page->forceFill([
'html' => $defaultTemplate->html,
$page->forceDelete();
+ // Remove chapter template usages
+ Chapter::query()->where('default_template_id', '=', $page->id)
+ ->update(['default_template_id' => null]);
+
+ $page->forceDelete();
+
return 1;
}
--- /dev/null
+<?php\r
+\r
+use Illuminate\Database\Migrations\Migration;\r
+use Illuminate\Database\Schema\Blueprint;\r
+use Illuminate\Support\Facades\Schema;\r
+\r
+class AddDefaultTemplateToChapters extends Migration\r
+{\r
+ /**\r
+ * Run the migrations.\r
+ *\r
+ * @return void\r
+ */\r
+ public function up()\r
+ {\r
+ Schema::table('chapters', function (Blueprint $table) {\r
+ $table->integer('default_template_id')->nullable()->default(null);\r
+ });\r
+ }\r
+\r
+ /**\r
+ * Reverse the migrations.\r
+ *\r
+ * @return void\r
+ */\r
+ public function down()\r
+ {\r
+ Schema::table('chapters', function (Blueprint $table) {\r
+ $table->dropColumn('default_template_id');\r
+ });\r
+ }\r
+}\r
'chapters_permissions_success' => 'Chapter Permissions Updated',
'chapters_search_this' => 'Search this chapter',
'chapter_sort_book' => 'Sort Book',
+ 'chapter_default_template' => 'Default Page Template',
+ 'chapter_default_template_explain' => 'Assign a page template that will be used as the default content for all new pages in this chapter. Keep in mind this will only be used if the page creator has view access to those chosen template page.',
+ 'chapter_default_template_select' => 'Select a template page',
// Pages
'page' => 'Page',
</div>
</div>
+<div class="form-group collapsible" component="collapsible" id="template-control">
+ <button refs="collapsible@trigger" type="button" class="collapse-title text-link" aria-expanded="false">
+ <label for="template-manager">{{ trans('entities.chapter_default_template') }}</label>
+ </button>
+ <div refs="collapsible@content" class="collapse-content">
+ <div class="flex-container-row gap-l justify-space-between pb-xs wrap">
+ <p class="text-muted small my-none min-width-xs flex">
+ {{ trans('entities.chapter_default_template_explain') }}
+ </p>
+
+ <div class="min-width-m">
+ @include('form.page-picker', [
+ 'name' => 'default_template_id',
+ 'placeholder' => trans('entities.chapter_default_template_select'),
+ 'value' => $chapter->default_template_id ?? null,
+ 'selectorEndpoint' => '/search/entity-selector-templates',
+ ])
+ </div>
+ </div>
+
+ </div>
+</div>
+
<div class="form-group text-right">
<a href="{{ isset($chapter) ? $chapter->getUrl() : $book->getUrl() }}" class="button outline">{{ trans('common.cancel') }}</a>
<button type="submit" class="button">{{ trans('entities.chapters_save') }}</button>