Aligned item creation wording and updated shelf-book-add logic

This commit is contained in:
Dan Brown 2019-04-15 20:43:25 +01:00
parent 84419005e7
commit 7f3f6e65b9
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
9 changed files with 55 additions and 35 deletions

View File

@ -26,7 +26,9 @@ class Bookshelf extends Entity
*/
public function books()
{
return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')->orderBy('order', 'asc');
return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')
->withPivot('order')
->orderBy('order', 'asc');
}
/**

View File

@ -577,6 +577,21 @@ class EntityRepo
$shelf->books()->sync($syncData);
}
/**
* Append a Book to a BookShelf.
* @param Bookshelf $shelf
* @param Book $book
*/
public function appendBookToShelf(Bookshelf $shelf, Book $book)
{
if ($shelf->contains($book)) {
return;
}
$maxOrder = $shelf->books()->max('order');
$shelf->books()->attach($book->id, ['order' => $maxOrder + 1]);
}
/**
* Change the book that an entity belongs to.
* @param string $type

View File

@ -75,15 +75,16 @@ class BookController extends Controller
/**
* Show the form for creating a new book.
* @param string $shelfSlug
* @return Response
* @throws \BookStack\Exceptions\NotFoundException
*/
public function create($shelfSlug = null)
public function create(string $shelfSlug = null)
{
$bookshelf = null;
if ($shelfSlug !== null) {
$bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
} else {
$bookshelf = null;
}
$this->checkPermission('book-create-all');
@ -97,39 +98,30 @@ class BookController extends Controller
* Store a newly created book in storage.
*
* @param Request $request
* @param $shelfSlug
* @param string $shelfSlug
* @return Response
* @throws \BookStack\Exceptions\NotFoundException
*/
public function store(Request $request, $shelfSlug = null)
public function store(Request $request, string $shelfSlug = null)
{
if ($shelfSlug !== null) {
$bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
$shelfBooks = $this->entityRepo->getBookshelfChildren($bookshelf);
$shelfBookIds = $shelfBooks->pluck('id');
} else {
$bookshelf = null;
}
$this->checkPermission('book-create-all');
$this->validate($request, [
'name' => 'required|string|max:255',
'description' => 'string|max:1000'
]);
$bookshelf = null;
if ($shelfSlug !== null) {
$bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
}
$book = $this->entityRepo->createFromInput('book', $request->all());
Activity::add($book, 'book_create', $book->id);
if ($bookshelf) {
$shelfBookIds = $shelfBookIds->toArray();
array_unshift($shelfBookIds, $book->id);
$shelfBookIds = implode(',', $shelfBookIds);
$this->entityRepo->updateShelfBooks($bookshelf, $shelfBookIds);
$this->entityRepo->appendBookToShelf($bookshelf, $book);
Activity::add($bookshelf, 'bookshelf_update');
return redirect($bookshelf->getUrl());
}
return redirect($book->getUrl());

View File

@ -74,6 +74,7 @@ return [
'shelves_create' => 'Create New Shelf',
'shelves_popular' => 'Popular Shelves',
'shelves_new' => 'New Shelves',
'shelves_new_action' => 'New Shelf',
'shelves_popular_empty' => 'The most popular shelves will appear here.',
'shelves_new_empty' => 'The most recently created shelves will appear here.',
'shelves_save' => 'Save Shelf',
@ -104,6 +105,7 @@ return [
'books_popular' => 'Popular Books',
'books_recent' => 'Recent Books',
'books_new' => 'New Books',
'books_new_action' => 'New Book',
'books_popular_empty' => 'The most popular books will appear here.',
'books_new_empty' => 'The most recently created books will appear here.',
'books_create' => 'Create New Book',

View File

@ -12,7 +12,7 @@
@if($currentUser->can('bookshelf-create-all'))
<a href="{{ baseUrl("/create-shelf") }}" class="icon-list-item">
<span>@icon('add')</span>
<span>{{ trans('entities.shelves_create') }}</span>
<span>{{ trans('entities.shelves_new_action') }}</span>
</a>
@endif
@include('partials.view-toggle', ['view' => $view, 'type' => 'shelf'])

View File

@ -23,8 +23,8 @@
<hr>
<p class="text-muted italic mt-xl mb-m">{{ trans('entities.shelves_empty_contents') }}</p>
<div class="icon-list inline block">
@if($currentUser->can('book-create-all'))
<a href="{{ $shelf->getUrl('/create-book') }}" class="icon-list-item">
@if(userCan('book-create-all') && userCan('bookshelf-update', $shelf))
<a href="{{ $shelf->getUrl('/create-book') }}" class="icon-list-item text-book">
<span class="icon">@icon('add')</span>
<span>{{ trans('entities.books_create') }}</span>
</a>
@ -80,13 +80,15 @@
<h5>{{ trans('common.actions') }}</h5>
<div class="icon-list text-primary">
@if($currentUser->can('book-create-all'))
@if(userCan('book-create-all') && userCan('bookshelf-update', $shelf))
<a href="{{ $shelf->getUrl('/create-book') }}" class="icon-list-item">
<span class="icon">@icon('add')</span>
<span>{{ trans('entities.books_create') }}</span>
<span>{{ trans('entities.books_new_action') }}</span>
</a>
@endif
<hr class="primary-background">
@if(userCan('bookshelf-update', $shelf))
<a href="{{ $shelf->getUrl('/edit') }}" class="icon-list-item">
<span>@icon('edit')</span>

View File

@ -26,8 +26,9 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('/{slug}/permissions', 'BookshelfController@showPermissions');
Route::put('/{slug}/permissions', 'BookshelfController@permissions');
Route::post('/{slug}/copy-permissions', 'BookshelfController@copyPermissions');
Route::get('/{slug}/create-book', 'BookController@create');
Route::post('/{slug}/create-book', 'BookController@store');
Route::get('/{shelfSlug}/create-book', 'BookController@create');
Route::post('/{shelfSlug}/create-book', 'BookController@store');
});
Route::get('/create-book', 'BookController@create');

View File

@ -48,7 +48,7 @@ class BookShelfTest extends TestCase
public function test_shelves_page_contains_create_link()
{
$resp = $this->asEditor()->get('/shelves');
$resp->assertElementContains('a', 'Create New Shelf');
$resp->assertElementContains('a', 'New Shelf');
}
public function test_shelves_create()
@ -103,7 +103,7 @@ class BookShelfTest extends TestCase
$resp->assertSee($shelf->getUrl('/edit'));
$resp->assertSee($shelf->getUrl('/permissions'));
$resp->assertSee($shelf->getUrl('/delete'));
$resp->assertElementContains('a', 'Create New Book');
$resp->assertElementContains('a', 'New Book');
$resp->assertElementContains('a', 'Edit');
$resp->assertElementContains('a', 'Permissions');
$resp->assertElementContains('a', 'Delete');
@ -164,9 +164,15 @@ class BookShelfTest extends TestCase
'name' => $testName,
'description' => 'Book in shelf description'
]);
$createBookResp->assertRedirect();
$newBook = Book::query()->orderBy('id', 'desc')->first();
$this->assertDatabaseHas('bookshelves_books', [
'bookshelf_id' => $shelf->id,
'book_id' => $newBook->id,
]);
$resp = $this->asEditor()->get($shelf->getUrl());
$resp->assertSee($testName);
}

View File

@ -215,7 +215,7 @@ class RolesTest extends BrowserKitTest
$this->checkAccessPermission('bookshelf-create-all', [
'/create-shelf'
], [
'/shelves' => 'Create New Shelf'
'/shelves' => 'New Shelf'
]);
$this->visit('/create-shelf')