return $this->hasMany(Chapter::class);
}
+ /**
+ * Get the shelves this book is contained within.
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
+ */
+ public function shelves()
+ {
+ return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
+ }
+
/**
* Get an excerpt of this book's description to the specified length or less.
* @param int $length
$recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
$recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 12);
+ $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
+ $homepageOption = setting('app-homepage-type', 'default');
+ if (!in_array($homepageOption, $homepageOptions)) {
+ $homepageOption = 'default';
+ }
- $customHomepage = false;
- $books = false;
- $booksViewType = false;
+ $commonData = [
+ 'activity' => $activity,
+ 'recents' => $recents,
+ 'recentlyUpdatedPages' => $recentlyUpdatedPages,
+ 'draftPages' => $draftPages,
+ ];
+
+ if ($homepageOption === 'bookshelves') {
+ $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18);
+ $shelvesViewType = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
+ $data = array_merge($commonData, ['shelves' => $shelves, 'shelvesViewType' => $shelvesViewType]);
+ return view('common.home-shelves', $data);
+ }
- // Check book homepage
- $bookHomepageSetting = setting('app-book-homepage');
- if ($bookHomepageSetting) {
+ if ($homepageOption === 'books') {
$books = $this->entityRepo->getAllPaginated('book', 18);
$booksViewType = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books', 'list'));
- } else {
- // Check custom homepage
- $homepageSetting = setting('app-homepage');
- if ($homepageSetting) {
- $id = intval(explode(':', $homepageSetting)[0]);
- $customHomepage = $this->entityRepo->getById('page', $id, false, true);
- $this->entityRepo->renderPage($customHomepage, true);
- }
+ $data = array_merge($commonData, ['books' => $books, 'booksViewType' => $booksViewType]);
+ return view('common.home-book', $data);
}
- $view = 'home';
- if ($bookHomepageSetting) {
- $view = 'home-book';
- } else if ($customHomepage) {
- $view = 'home-custom';
+ if ($homepageOption === 'page') {
+ $homepageSetting = setting('app-homepage', '0:');
+ $id = intval(explode(':', $homepageSetting)[0]);
+ $customHomepage = $this->entityRepo->getById('page', $id, false, true);
+ $this->entityRepo->renderPage($customHomepage, true);
+ return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
}
- return view('common/' . $view, [
- 'activity' => $activity,
- 'recents' => $recents,
- 'recentlyUpdatedPages' => $recentlyUpdatedPages,
- 'draftPages' => $draftPages,
- 'customHomepage' => $customHomepage,
- 'books' => $books,
- 'booksViewType' => $booksViewType
- ]);
+ return view('common.home', $commonData);
}
/**
*/
public function destroyPage(Page $page)
{
- $this->destroyEntityCommonRelations($page);
-
// Check if set as custom homepage
$customHome = setting('app-homepage', '0:');
if (intval($page->id) === intval(explode(':', $customHome)[0])) {
throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
}
+ $this->destroyEntityCommonRelations($page);
+
// Delete Attached Files
$attachmentService = app(AttachmentService::class);
foreach ($page->attachments as $attachment) {
<?php namespace BookStack\Services;
use BookStack\Book;
+use BookStack\Bookshelf;
use BookStack\Chapter;
use BookStack\Entity;
use BookStack\EntityPermission;
public $book;
public $chapter;
public $page;
+ public $bookshelf;
protected $db;
* PermissionService constructor.
* @param JointPermission $jointPermission
* @param EntityPermission $entityPermission
+ * @param Role $role
* @param Connection $db
+ * @param Bookshelf $bookshelf
* @param Book $book
* @param Chapter $chapter
* @param Page $page
- * @param Role $role
*/
- public function __construct(JointPermission $jointPermission, EntityPermission $entityPermission, Connection $db, Book $book, Chapter $chapter, Page $page, Role $role)
+ public function __construct(
+ JointPermission $jointPermission, EntityPermission $entityPermission, Role $role, Connection $db,
+ Bookshelf $bookshelf, Book $book, Chapter $chapter, Page $page
+ )
{
$this->db = $db;
$this->jointPermission = $jointPermission;
$this->entityPermission = $entityPermission;
$this->role = $role;
+ $this->bookshelf = $bookshelf;
$this->book = $book;
$this->chapter = $chapter;
$this->page = $page;
$this->bookFetchQuery()->chunk(5, function ($books) use ($roles) {
$this->buildJointPermissionsForBooks($books, $roles);
});
+
+ // Chunk through all bookshelves
+ $this->bookshelf->newQuery()->select(['id', 'restricted', 'created_by'])
+ ->chunk(50, function ($shelves) use ($roles) {
+ $this->buildJointPermissionsForShelves($shelves, $roles);
+ });
}
/**
}]);
}
+ /**
+ * @param Collection $shelves
+ * @param array $roles
+ * @param bool $deleteOld
+ * @throws \Throwable
+ */
+ protected function buildJointPermissionsForShelves($shelves, $roles, $deleteOld = false)
+ {
+ if ($deleteOld) {
+ $this->deleteManyJointPermissionsForEntities($shelves->all());
+ }
+ $this->createManyJointPermissions($shelves, $roles);
+ }
+
/**
* Build joint permissions for an array of books
* @param Collection $books
$this->bookFetchQuery()->chunk(20, function ($books) use ($roles) {
$this->buildJointPermissionsForBooks($books, $roles);
});
+
+ // Chunk through all bookshelves
+ $this->bookshelf->newQuery()->select(['id', 'restricted', 'created_by'])
+ ->chunk(50, function ($shelves) use ($roles) {
+ $this->buildJointPermissionsForShelves($shelves, $roles);
+ });
}
/**
];
});
+$factory->define(BookStack\Bookshelf::class, function ($faker) {
+ return [
+ 'name' => $faker->sentence,
+ 'slug' => str_random(10),
+ 'description' => $faker->paragraph
+ ];
+});
+
$factory->define(BookStack\Book::class, function ($faker) {
return [
'name' => $faker->sentence,
$role = \BookStack\Role::getRole('viewer');
$viewerUser->attachRole($role);
- factory(\BookStack\Book::class, 5)->create(['created_by' => $editorUser->id, 'updated_by' => $editorUser->id])
- ->each(function($book) use ($editorUser) {
- $chapters = factory(\BookStack\Chapter::class, 3)->create(['created_by' => $editorUser->id, 'updated_by' => $editorUser->id])
- ->each(function($chapter) use ($editorUser, $book){
- $pages = factory(\BookStack\Page::class, 3)->make(['created_by' => $editorUser->id, 'updated_by' => $editorUser->id, 'book_id' => $book->id]);
+ $byData = ['created_by' => $editorUser->id, 'updated_by' => $editorUser->id];
+
+ factory(\BookStack\Book::class, 5)->create($byData)
+ ->each(function($book) use ($editorUser, $byData) {
+ $chapters = factory(\BookStack\Chapter::class, 3)->create($byData)
+ ->each(function($chapter) use ($editorUser, $book, $byData){
+ $pages = factory(\BookStack\Page::class, 3)->make(array_merge($byData, ['book_id' => $book->id]));
$chapter->pages()->saveMany($pages);
});
- $pages = factory(\BookStack\Page::class, 3)->make(['created_by' => $editorUser->id, 'updated_by' => $editorUser->id]);
+ $pages = factory(\BookStack\Page::class, 3)->make($byData);
$book->chapters()->saveMany($chapters);
$book->pages()->saveMany($pages);
});
- $largeBook = factory(\BookStack\Book::class)->create(['name' => 'Large book' . str_random(10), 'created_by' => $editorUser->id, 'updated_by' => $editorUser->id]);
- $pages = factory(\BookStack\Page::class, 200)->make(['created_by' => $editorUser->id, 'updated_by' => $editorUser->id]);
- $chapters = factory(\BookStack\Chapter::class, 50)->make(['created_by' => $editorUser->id, 'updated_by' => $editorUser->id]);
+ $largeBook = factory(\BookStack\Book::class)->create(array_merge($byData, ['name' => 'Large book' . str_random(10)]));
+ $pages = factory(\BookStack\Page::class, 200)->make($byData);
+ $chapters = factory(\BookStack\Chapter::class, 50)->make($byData);
$largeBook->pages()->saveMany($pages);
$largeBook->chapters()->saveMany($chapters);
+
+ $shelves = factory(\BookStack\Bookshelf::class, 10)->create($byData);
+ $largeBook->shelves()->attach($shelves->pluck('id'));
+
app(\BookStack\Services\PermissionService::class)->buildJointPermissions();
app(\BookStack\Services\SearchService::class)->indexAllEntities();
}
--- /dev/null
+
+class HomepageControl {
+
+ constructor(elem) {
+ this.elem = elem;
+ this.typeControl = elem.querySelector('[name="setting-app-homepage-type"]');
+ this.pagePickerContainer = elem.querySelector('[page-picker-container]');
+
+ this.typeControl.addEventListener('change', this.controlPagePickerVisibility.bind(this));
+ this.controlPagePickerVisibility();
+ }
+
+ controlPagePickerVisibility() {
+ const showPagePicker = this.typeControl.value === 'page';
+ this.pagePickerContainer.style.display = (showPagePicker ? 'block' : 'none');
+ }
+
+
+
+}
+
+module.exports = HomepageControl;
\ No newline at end of file
'toggle-switch': require('./toggle-switch'),
'page-display': require('./page-display'),
'shelf-sort': require('./shelf-sort'),
+ 'homepage-control': require('./homepage-control'),
};
window.components = {};
}
setupListeners() {
- // Select click
- this.selectButton.addEventListener('click', event => {
- window.EntitySelectorPopup.show(entity => {
- this.setValue(entity.id, entity.name);
- });
- });
+ this.selectButton.addEventListener('click', this.showPopup.bind(this));
+ this.display.parentElement.addEventListener('click', this.showPopup.bind(this));
this.resetButton.addEventListener('click', event => {
this.setValue('', '');
});
}
+ showPopup() {
+ window.EntitySelectorPopup.show(entity => {
+ this.setValue(entity.id, entity.name);
+ });
+ }
+
setValue(value, name) {
this.value = value;
this.input.value = value;
'details' => 'Details',
'grid_view' => 'Grid View',
'list_view' => 'List View',
+ 'default' => 'Default',
/**
* Header
'message' => ':start :time. Take care not to overwrite each other\'s updates!',
],
'pages_draft_discarded' => 'Draft discarded, The editor has been updated with the current page content',
+ 'pages_specific' => 'Specific Page',
/**
* Editor sidebar
'app_primary_color' => 'Application primary color',
'app_primary_color_desc' => 'This should be a hex value. <br>Leave empty to reset to the default color.',
'app_homepage' => 'Application Homepage',
- 'app_homepage_desc' => 'Select a page to show on the homepage instead of the default view. Page permissions are ignored for selected pages.',
- 'app_homepage_default' => 'Default homepage view chosen',
- 'app_homepage_books' => 'Or select the books page as your homepage. This will override any page selected as your homepage.',
+ 'app_homepage_desc' => 'Select a view to show on the homepage instead of the default view. Page permissions are ignored for selected pages.',
+ 'app_homepage_select' => 'Select a page',
'app_disable_comments' => 'Disable comments',
'app_disable_comments_desc' => 'Disable comments across all pages in the application. Existing comments are not shown.',
--- /dev/null
+@extends('sidebar-layout')
+
+@section('toolbar')
+ <div class="col-sm-6 faded">
+ <div class="action-buttons text-left">
+ <a expand-toggle=".entity-list.compact .entity-item-snippet" class="text-primary text-button">@icon('expand-text'){{ trans('common.toggle_details') }}</a>
+ @include('shelves/view-toggle', ['shelvesViewType' => $shelvesViewType])
+ </div>
+ </div>
+@stop
+
+@section('sidebar')
+ @include('common/home-sidebar')
+@stop
+
+@section('body')
+ @include('shelves/list', ['shelves' => $shelves, 'shelvesViewType' => $shelvesViewType])
+@stop
\ No newline at end of file
@section('body')
- <div class="container">
+ <div class="container" id="home-default">
<div class="row">
<div class="col-sm-4">
<input type="text" value="{{ setting('app-color') }}" name="setting-app-color" id="setting-app-color" placeholder="#0288D1">
<input type="hidden" value="{{ setting('app-color-light') }}" name="setting-app-color-light" id="setting-app-color-light">
</div>
- <div class="form-group" id="homepage-control">
+ <div homepage-control class="form-group" id="homepage-control">
<label for="setting-app-homepage">{{ trans('settings.app_homepage') }}</label>
<p class="small">{{ trans('settings.app_homepage_desc') }}</p>
- @include('components.page-picker', ['name' => 'setting-app-homepage', 'placeholder' => trans('settings.app_homepage_default'), 'value' => setting('app-homepage')])
- <p class="small">{{ trans('settings.app_homepage_books') }}</p>
- @include('components.toggle-switch', ['name' => 'setting-app-book-homepage', 'value' => setting('app-book-homepage')])
+
+ <select name="setting-app-homepage-type" id="setting-app-homepage-type">
+ <option @if(setting('app-homepage-type') === 'default') selected @endif value="default">{{ trans('common.default') }}</option>
+ <option @if(setting('app-homepage-type') === 'books') selected @endif value="books">{{ trans('entities.books') }}</option>
+ <option @if(setting('app-homepage-type') === 'bookshelves') selected @endif value="bookshelves">{{ trans('entities.shelves') }}</option>
+ <option @if(setting('app-homepage-type') === 'page') selected @endif value="page">{{ trans('entities.pages_specific') }}</option>
+ </select>
+
+ <br><br>
+
+ <div page-picker-container style="display: none;">
+ @include('components.page-picker', ['name' => 'setting-app-homepage', 'placeholder' => trans('settings.app_homepage_select'), 'value' => setting('app-homepage')])
+ </div>
</div>
</div>
$homeVisit->assertSee('My Recently Viewed');
$homeVisit->assertSee('Recently Updated Pages');
$homeVisit->assertSee('Recent Activity');
+ $homeVisit->assertSee('home-default');
}
public function test_custom_homepage()
{
$this->asEditor();
$name = 'My custom homepage';
- $content = 'This is the body content of my custom homepage.';
+ $content = str_repeat('This is the body content of my custom homepage.', 20);
$customPage = $this->newPage(['name' => $name, 'html' => $content]);
$this->setSettings(['app-homepage' => $customPage->id]);
+ $this->setSettings(['app-homepage-type' => 'page']);
$homeVisit = $this->get('/');
$homeVisit->assertSee($name);
{
$this->asEditor();
$name = 'My custom homepage';
- $content = 'This is the body content of my custom homepage.';
+ $content = str_repeat('This is the body content of my custom homepage.', 20);
$customPage = $this->newPage(['name' => $name, 'html' => $content]);
$this->setSettings(['app-homepage' => $customPage->id]);
$editor = $this->getEditor();
setting()->putUser($editor, 'books_view_type', 'grid');
- $this->setSettings(['app-book-homepage' => true]);
+ $this->setSettings(['app-homepage-type' => 'books']);
$this->asEditor();
$homeVisit = $this->get('/');
$homeVisit->assertSee('grid-card-footer');
$homeVisit->assertSee('featured-image-container');
- $this->setSettings(['app-book-homepage' => false]);
+ $this->setSettings(['app-homepage-type' => false]);
+ $this->test_default_homepage_visible();
+ }
+
+ public function test_set_bookshelves_homepage()
+ {
+ $editor = $this->getEditor();
+ setting()->putUser($editor, 'bookshelves_view_type', 'grid');
+
+ $this->setSettings(['app-homepage-type' => 'bookshelves']);
+
+ $this->asEditor();
+ $homeVisit = $this->get('/');
+ $homeVisit->assertSee('Shelves');
+ $homeVisit->assertSee('bookshelf-grid-item grid-card');
+ $homeVisit->assertSee('grid-card-content');
+ $homeVisit->assertSee('grid-card-footer');
+ $homeVisit->assertSee('featured-image-container');
+
+ $this->setSettings(['app-homepage-type' => false]);
$this->test_default_homepage_visible();
}
}