Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Auth/Permissions/PermissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ public function checkUserHasPermissionOnAnything(string $permission, string $ent
$query2->where('has_permission_own', '=', 1)
->where('created_by', '=', $userId);
});
}) ;
});

if (!is_null($entityClass)) {
$entityInstance = app()->make($entityClass);
Expand Down
41 changes: 37 additions & 4 deletions app/Http/Controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,61 @@ public function index()
* Show the form for creating a new book.
* @return Response
*/
public function create()
public function create($shelfSlug = null)
{
if ($shelfSlug !== null) {
$bookshelf = $this->entityRepo->getBySlug('bookshelf', $shelfSlug);
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
} else {
$bookshelf = null;
}

$this->checkPermission('book-create-all');
$this->setPageTitle(trans('entities.books_create'));
return view('books.create');
return view('books.create', [
'bookshelf' => $bookshelf
]);
}

/**
* Store a newly created book in storage.
*
* @param Request $request
* @param Request $request
* @param $shelfSlug
* @return Response
*/
public function store(Request $request)
public function store(Request $request, $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'
]);
$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);
Activity::add($bookshelf, 'bookshelf_update');

return redirect($bookshelf->getUrl());
}

return redirect($book->getUrl());
}

Expand Down
32 changes: 21 additions & 11 deletions resources/views/books/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@
@section('body')
<div class="container small">
<div class="my-s">
@include('partials.breadcrumbs', ['crumbs' => [
'/books' => [
'text' => trans('entities.books'),
'icon' => 'book'
],
'/create-book' => [
'text' => trans('entities.books_create'),
'icon' => 'add'
]
]])
@if (isset($bookshelf))
@include('partials.breadcrumbs', ['crumbs' => [
$bookshelf,
$bookshelf->getUrl('/create-book') => [
'text' => trans('entities.books_create'),
'icon' => 'add'
]
]])
@else
@include('partials.breadcrumbs', ['crumbs' => [
'/books' => [
'text' => trans('entities.books'),
'icon' => 'book'
],
'/create-book' => [
'text' => trans('entities.books_create'),
'icon' => 'add'
]
]])
@endif
</div>

<div class="content-wrap card">
<h1 class="list-heading">{{ trans('entities.books_create') }}</h1>
<form action="{{ baseUrl("/books") }}" method="POST" enctype="multipart/form-data">
<form action="{{ isset($bookshelf) ? $bookshelf->getUrl('/create-book') : baseUrl('/books') }}" method="POST" enctype="multipart/form-data">
@include('books.form')
</form>
</div>
Expand Down
13 changes: 13 additions & 0 deletions resources/views/shelves/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<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">
<span class="icon">@icon('add')</span>
<span>{{ trans('entities.books_create') }}</span>
</a>
@endif
@if(userCan('bookshelf-update', $shelf))
<a href="{{ $shelf->getUrl('/edit') }}" class="icon-list-item text-bookshelf">
<span class="icon">@icon('edit')</span>
Expand Down Expand Up @@ -74,6 +80,13 @@
<h5>{{ trans('common.actions') }}</h5>
<div class="icon-list text-primary">

@if($currentUser->can('book-create-all'))
<a href="{{ $shelf->getUrl('/create-book') }}" class="icon-list-item">
<span class="icon">@icon('add')</span>
<span>{{ trans('entities.books_create') }}</span>
</a>
@endif

@if(userCan('bookshelf-update', $shelf))
<a href="{{ $shelf->getUrl('/edit') }}" class="icon-list-item">
<span>@icon('edit')</span>
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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('/create-book', 'BookController@create');
Expand Down
22 changes: 22 additions & 0 deletions tests/Entity/BookShelfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ public function test_shelf_view_shows_action_buttons()
{
$shelf = Bookshelf::first();
$resp = $this->asAdmin()->get($shelf->getUrl());
$resp->assertSee($shelf->getUrl('/create-book'));
$resp->assertSee($shelf->getUrl('/edit'));
$resp->assertSee($shelf->getUrl('/permissions'));
$resp->assertSee($shelf->getUrl('/delete'));
$resp->assertElementContains('a', 'Create New Book');
$resp->assertElementContains('a', 'Edit');
$resp->assertElementContains('a', 'Permissions');
$resp->assertElementContains('a', 'Delete');
Expand Down Expand Up @@ -148,6 +150,26 @@ public function test_shelf_edit()
$this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
}

public function test_shelf_create_new_book()
{
$shelf = Bookshelf::first();
$resp = $this->asEditor()->get($shelf->getUrl('/create-book'));

$resp->assertSee('Create New Book');
$resp->assertSee($shelf->getShortName());

$testName = 'Test Book in Shelf Name';

$createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
'name' => $testName,
'description' => 'Book in shelf description'
]);

$resp = $this->asEditor()->get($shelf->getUrl());

$resp->assertSee($testName);
}

public function test_shelf_delete()
{
$shelf = Bookshelf::first();
Expand Down