1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-31 15:24:31 +03:00

Default Templates: Started review and updates from PR code

This commit is contained in:
Dan Brown
2023-12-11 12:33:20 +00:00
parent 968bc8cdf3
commit d61f42a377
11 changed files with 39 additions and 51 deletions

View File

@ -7,7 +7,6 @@ use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\View;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\Cloner;
@ -25,15 +24,11 @@ use Throwable;
class BookController extends Controller
{
protected BookRepo $bookRepo;
protected ShelfContext $shelfContext;
protected ReferenceFetcher $referenceFetcher;
public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo, ReferenceFetcher $referenceFetcher)
{
$this->bookRepo = $bookRepo;
$this->shelfContext = $entityContextManager;
$this->referenceFetcher = $referenceFetcher;
public function __construct(
protected ShelfContext $shelfContext,
protected BookRepo $bookRepo,
protected ReferenceFetcher $referenceFetcher
) {
}
/**
@ -82,14 +77,8 @@ class BookController extends Controller
$this->setPageTitle(trans('entities.books_create'));
$templates = Page::visible()
->where('template', '=', true)
->orderBy('name', 'asc')
->get();
return view('books.create', [
'bookshelf' => $bookshelf,
'templates' => $templates,
]);
}
@ -103,11 +92,11 @@ class BookController extends Controller
{
$this->checkPermission('book-create-all');
$validated = $this->validate($request, [
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
'tags' => ['array'],
'default_template' => ['nullable', 'exists:pages,id'],
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
'tags' => ['array'],
'default_template' => ['nullable', 'integer'],
]);
$bookshelf = null;
@ -162,12 +151,7 @@ class BookController extends Controller
$this->checkOwnablePermission('book-update', $book);
$this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
$templates = Page::visible()
->where('template', '=', true)
->orderBy('name', 'asc')
->get();
return view('books.edit', ['book' => $book, 'current' => $book, 'templates' => $templates]);
return view('books.edit', ['book' => $book, 'current' => $book]);
}
/**
@ -183,11 +167,11 @@ class BookController extends Controller
$this->checkOwnablePermission('book-update', $book);
$validated = $this->validate($request, [
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
'tags' => ['array'],
'default_template' => ['nullable', 'exists:pages,id'],
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'image' => array_merge(['nullable'], $this->getImageValidationRules()),
'tags' => ['array'],
'default_template' => ['nullable', 'integer'],
]);
if ($request->has('image_reset')) {

View File

@ -259,13 +259,13 @@ class PageController extends Controller
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-delete', $page);
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
$times_used_as_template = Book::where('default_template', '=', $page->id)->count();
$usedAsTemplate = Book::query()->where('default_template', '=', $page->id)->count() > 0;
return view('pages.delete', [
'book' => $page->book,
'page' => $page,
'current' => $page,
'times_used_as_template' => $times_used_as_template,
'usedAsTemplate' => $usedAsTemplate,
]);
}

View File

@ -20,6 +20,7 @@ use Illuminate\Support\Collection;
* @property \Illuminate\Database\Eloquent\Collection $pages
* @property \Illuminate\Database\Eloquent\Collection $directPages
* @property \Illuminate\Database\Eloquent\Collection $shelves
* @property ?Page $defaultTemplate
*/
class Book extends Entity implements HasCoverImage
{
@ -27,7 +28,7 @@ class Book extends Entity implements HasCoverImage
public $searchFactor = 1.2;
protected $fillable = ['name', 'description', 'default_template'];
protected $fillable = ['name', 'description'];
protected $hidden = ['pivot', 'image_id', 'deleted_at'];
/**

View File

@ -136,9 +136,11 @@ class PageRepo
$page->book_id = $parent->id;
}
if ($page->book->defaultTemplate) {
$defaultTemplate = $page->book->defaultTemplate;
if ($defaultTemplate && userCan('view', $defaultTemplate)) {
$page->forceFill([
'html' => $page->book->defaultTemplate->html,
'html' => $defaultTemplate->html,
'markdown' => $defaultTemplate->markdown,
]);
}