2018-08-27 15:18:09 +02:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
|
|
|
|
|
|
|
use Activity;
|
2018-09-25 17:58:03 +02:00
|
|
|
use BookStack\Auth\UserRepo;
|
2018-09-25 13:30:50 +02:00
|
|
|
use BookStack\Entities\Bookshelf;
|
2019-04-07 19:28:11 +02:00
|
|
|
use BookStack\Entities\EntityContextManager;
|
2018-10-13 12:27:55 +02:00
|
|
|
use BookStack\Entities\Repos\EntityRepo;
|
2019-04-27 15:55:23 +02:00
|
|
|
use BookStack\Uploads\ImageRepo;
|
2018-08-27 15:18:09 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Views;
|
|
|
|
|
|
|
|
class BookshelfController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $entityRepo;
|
|
|
|
protected $userRepo;
|
2019-04-07 19:28:11 +02:00
|
|
|
protected $entityContextManager;
|
2019-04-27 15:55:23 +02:00
|
|
|
protected $imageRepo;
|
2018-08-27 15:18:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* BookController constructor.
|
2019-04-07 10:57:48 +02:00
|
|
|
* @param EntityRepo $entityRepo
|
2018-08-27 15:18:09 +02:00
|
|
|
* @param UserRepo $userRepo
|
2019-04-07 19:28:11 +02:00
|
|
|
* @param EntityContextManager $entityContextManager
|
2019-04-27 15:55:23 +02:00
|
|
|
* @param ImageRepo $imageRepo
|
2018-08-27 15:18:09 +02:00
|
|
|
*/
|
2019-04-27 15:55:23 +02:00
|
|
|
public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, EntityContextManager $entityContextManager, ImageRepo $imageRepo)
|
2018-08-27 15:18:09 +02:00
|
|
|
{
|
|
|
|
$this->entityRepo = $entityRepo;
|
|
|
|
$this->userRepo = $userRepo;
|
2019-04-07 19:28:11 +02:00
|
|
|
$this->entityContextManager = $entityContextManager;
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->imageRepo = $imageRepo;
|
2018-08-27 15:18:09 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the book.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2019-02-03 14:45:45 +01:00
|
|
|
$view = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
|
|
|
|
$sort = setting()->getUser($this->currentUser, 'bookshelves_sort', 'name');
|
|
|
|
$order = setting()->getUser($this->currentUser, 'bookshelves_sort_order', 'asc');
|
|
|
|
$sortOptions = [
|
|
|
|
'name' => trans('common.sort_name'),
|
|
|
|
'created_at' => trans('common.sort_created_at'),
|
|
|
|
'updated_at' => trans('common.sort_updated_at'),
|
|
|
|
];
|
|
|
|
|
2019-04-07 19:28:11 +02:00
|
|
|
$shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $sort, $order);
|
|
|
|
foreach ($shelves as $shelf) {
|
|
|
|
$shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
|
|
|
|
}
|
|
|
|
|
2018-08-27 15:18:09 +02:00
|
|
|
$recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('bookshelf', 4, 0) : false;
|
|
|
|
$popular = $this->entityRepo->getPopular('bookshelf', 4, 0);
|
|
|
|
$new = $this->entityRepo->getRecentlyCreated('bookshelf', 4, 0);
|
2019-02-03 14:45:45 +01:00
|
|
|
|
2019-04-07 19:28:11 +02:00
|
|
|
$this->entityContextManager->clearShelfContext();
|
2018-08-27 15:18:09 +02:00
|
|
|
$this->setPageTitle(trans('entities.shelves'));
|
2019-02-02 12:41:41 +01:00
|
|
|
return view('shelves.index', [
|
2018-08-27 15:18:09 +02:00
|
|
|
'shelves' => $shelves,
|
|
|
|
'recents' => $recents,
|
|
|
|
'popular' => $popular,
|
|
|
|
'new' => $new,
|
2019-02-03 14:45:45 +01:00
|
|
|
'view' => $view,
|
|
|
|
'sort' => $sort,
|
|
|
|
'order' => $order,
|
|
|
|
'sortOptions' => $sortOptions,
|
2018-08-27 15:18:09 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new bookshelf.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$this->checkPermission('bookshelf-create-all');
|
|
|
|
$books = $this->entityRepo->getAll('book', false, 'update');
|
2018-09-16 20:34:09 +02:00
|
|
|
$this->setPageTitle(trans('entities.shelves_create'));
|
2019-02-02 12:41:41 +01:00
|
|
|
return view('shelves.create', ['books' => $books]);
|
2018-08-27 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-16 20:34:09 +02:00
|
|
|
* Store a newly created bookshelf in storage.
|
2019-05-04 16:48:15 +02:00
|
|
|
* @param Request $request
|
2018-08-27 15:18:09 +02:00
|
|
|
* @return Response
|
2019-05-04 16:48:15 +02:00
|
|
|
* @throws \BookStack\Exceptions\ImageUploadException
|
2018-08-27 15:18:09 +02:00
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$this->checkPermission('bookshelf-create-all');
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
'description' => 'string|max:1000',
|
2019-04-27 15:55:23 +02:00
|
|
|
'image' => $this->imageRepo->getImageValidationRules(),
|
2018-08-27 15:18:09 +02:00
|
|
|
]);
|
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$shelf = $this->entityRepo->createFromInput('bookshelf', $request->all());
|
|
|
|
$this->shelfUpdateActions($shelf, $request);
|
2018-08-27 15:18:09 +02:00
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
Activity::add($shelf, 'bookshelf_create');
|
|
|
|
return redirect($shelf->getUrl());
|
2018-08-27 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2018-09-16 20:34:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified bookshelf.
|
|
|
|
* @param String $slug
|
|
|
|
* @return Response
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
|
|
|
*/
|
|
|
|
public function show(string $slug)
|
|
|
|
{
|
2019-04-27 15:55:23 +02:00
|
|
|
/** @var Bookshelf $shelf */
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->checkOwnablePermission('book-view', $shelf);
|
2018-09-16 20:34:09 +02:00
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$books = $this->entityRepo->getBookshelfChildren($shelf);
|
|
|
|
Views::add($shelf);
|
|
|
|
$this->entityContextManager->setShelfContext($shelf->id);
|
2018-09-16 20:34:09 +02:00
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->setPageTitle($shelf->getShortName());
|
2019-05-05 16:54:22 +02:00
|
|
|
|
2019-02-02 12:41:41 +01:00
|
|
|
return view('shelves.show', [
|
2019-04-27 15:55:23 +02:00
|
|
|
'shelf' => $shelf,
|
2018-09-16 20:34:09 +02:00
|
|
|
'books' => $books,
|
2019-04-27 15:55:23 +02:00
|
|
|
'activity' => Activity::entityActivity($shelf, 20, 1)
|
2018-09-16 20:34:09 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified bookshelf.
|
|
|
|
* @param $slug
|
|
|
|
* @return Response
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
|
|
|
*/
|
|
|
|
public function edit(string $slug)
|
|
|
|
{
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->checkOwnablePermission('bookshelf-update', $shelf);
|
2018-09-16 20:34:09 +02:00
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$shelfBooks = $this->entityRepo->getBookshelfChildren($shelf);
|
2018-09-16 20:34:09 +02:00
|
|
|
$shelfBookIds = $shelfBooks->pluck('id');
|
|
|
|
$books = $this->entityRepo->getAll('book', false, 'update');
|
|
|
|
$books = $books->filter(function ($book) use ($shelfBookIds) {
|
|
|
|
return !$shelfBookIds->contains($book->id);
|
|
|
|
});
|
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
|
2019-02-02 12:41:41 +01:00
|
|
|
return view('shelves.edit', [
|
2019-04-27 15:55:23 +02:00
|
|
|
'shelf' => $shelf,
|
2018-09-16 20:34:09 +02:00
|
|
|
'books' => $books,
|
|
|
|
'shelfBooks' => $shelfBooks,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified bookshelf in storage.
|
2019-04-27 15:55:23 +02:00
|
|
|
* @param Request $request
|
2018-09-16 20:34:09 +02:00
|
|
|
* @param string $slug
|
|
|
|
* @return Response
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
2019-04-27 15:55:23 +02:00
|
|
|
* @throws \BookStack\Exceptions\ImageUploadException
|
2018-09-16 20:34:09 +02:00
|
|
|
*/
|
|
|
|
public function update(Request $request, string $slug)
|
|
|
|
{
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $bookshelf Bookshelf */
|
2018-09-16 20:34:09 +02:00
|
|
|
$this->checkOwnablePermission('bookshelf-update', $shelf);
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
'description' => 'string|max:1000',
|
2019-04-27 15:55:23 +02:00
|
|
|
'image' => $this->imageRepo->getImageValidationRules(),
|
2018-09-16 20:34:09 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$shelf = $this->entityRepo->updateFromInput('bookshelf', $shelf, $request->all());
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->shelfUpdateActions($shelf, $request);
|
|
|
|
|
2018-09-16 20:34:09 +02:00
|
|
|
Activity::add($shelf, 'bookshelf_update');
|
|
|
|
|
|
|
|
return redirect($shelf->getUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the page to confirm deletion
|
|
|
|
* @param $slug
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
|
|
|
*/
|
|
|
|
public function showDelete(string $slug)
|
|
|
|
{
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->checkOwnablePermission('bookshelf-delete', $shelf);
|
2018-09-16 20:34:09 +02:00
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
|
|
|
|
return view('shelves.delete', ['shelf' => $shelf]);
|
2018-09-16 20:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified bookshelf from storage.
|
|
|
|
* @param string $slug
|
|
|
|
* @return Response
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function destroy(string $slug)
|
|
|
|
{
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->checkOwnablePermission('bookshelf-delete', $shelf);
|
|
|
|
Activity::addMessage('bookshelf_delete', 0, $shelf->name);
|
|
|
|
|
|
|
|
if ($shelf->cover) {
|
|
|
|
$this->imageRepo->destroyImage($shelf->cover);
|
|
|
|
}
|
|
|
|
$this->entityRepo->destroyBookshelf($shelf);
|
|
|
|
|
2018-09-16 20:34:09 +02:00
|
|
|
return redirect('/shelves');
|
|
|
|
}
|
2018-09-20 20:16:11 +02:00
|
|
|
|
|
|
|
/**
|
2019-02-03 14:45:45 +01:00
|
|
|
* Show the permissions view.
|
|
|
|
* @param string $slug
|
2018-09-20 20:16:11 +02:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
|
|
|
*/
|
2019-02-03 14:45:45 +01:00
|
|
|
public function showPermissions(string $slug)
|
2018-09-20 20:16:11 +02:00
|
|
|
{
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $shelf);
|
2018-09-20 20:16:11 +02:00
|
|
|
|
|
|
|
$roles = $this->userRepo->getRestrictableRoles();
|
2019-02-03 14:45:45 +01:00
|
|
|
return view('shelves.permissions', [
|
2019-04-27 15:55:23 +02:00
|
|
|
'shelf' => $shelf,
|
2018-09-20 20:16:11 +02:00
|
|
|
'roles' => $roles
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 14:45:45 +01:00
|
|
|
* Set the permissions for this bookshelf.
|
2018-09-20 20:16:11 +02:00
|
|
|
* @param Request $request
|
2019-09-15 19:53:30 +02:00
|
|
|
* @param string $slug
|
2018-09-20 20:16:11 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
2019-02-03 14:45:45 +01:00
|
|
|
* @throws \Throwable
|
2018-09-20 20:16:11 +02:00
|
|
|
*/
|
2019-09-15 19:53:30 +02:00
|
|
|
public function permissions(Request $request, string $slug)
|
2018-09-20 20:16:11 +02:00
|
|
|
{
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $shelf);
|
2018-09-20 20:16:11 +02:00
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->entityRepo->updateEntityPermissionsFromRequest($request, $shelf);
|
2019-09-19 16:12:10 +02:00
|
|
|
$this->showSuccessNotification( trans('entities.shelves_permissions_updated'));
|
2019-04-27 15:55:23 +02:00
|
|
|
return redirect($shelf->getUrl());
|
2018-09-20 20:16:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the permissions of a bookshelf to the child books.
|
|
|
|
* @param string $slug
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws \BookStack\Exceptions\NotFoundException
|
|
|
|
*/
|
|
|
|
public function copyPermissions(string $slug)
|
|
|
|
{
|
2019-09-16 00:28:23 +02:00
|
|
|
$shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug);
|
2019-04-27 15:55:23 +02:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $shelf);
|
2018-09-20 20:16:11 +02:00
|
|
|
|
2019-04-27 15:55:23 +02:00
|
|
|
$updateCount = $this->entityRepo->copyBookshelfPermissions($shelf);
|
2019-09-19 16:12:10 +02:00
|
|
|
$this->showSuccessNotification( trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
|
2019-04-27 15:55:23 +02:00
|
|
|
return redirect($shelf->getUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common actions to run on bookshelf update.
|
|
|
|
* @param Bookshelf $shelf
|
|
|
|
* @param Request $request
|
|
|
|
* @throws \BookStack\Exceptions\ImageUploadException
|
|
|
|
*/
|
|
|
|
protected function shelfUpdateActions(Bookshelf $shelf, Request $request)
|
|
|
|
{
|
|
|
|
// Update the books that the shelf references
|
|
|
|
$this->entityRepo->updateShelfBooks($shelf, $request->get('books', ''));
|
|
|
|
|
|
|
|
// Update the cover image if in request
|
2019-05-04 16:48:15 +02:00
|
|
|
if ($request->has('image')) {
|
|
|
|
$newImage = $request->file('image');
|
2019-05-05 16:54:22 +02:00
|
|
|
$this->imageRepo->destroyImage($shelf->cover);
|
2019-05-04 16:48:15 +02:00
|
|
|
$image = $this->imageRepo->saveNew($newImage, 'cover_shelf', $shelf->id, 512, 512, true);
|
2019-04-27 15:55:23 +02:00
|
|
|
$shelf->image_id = $image->id;
|
|
|
|
$shelf->save();
|
|
|
|
}
|
2019-05-04 16:48:15 +02:00
|
|
|
|
|
|
|
if ($request->has('image_reset')) {
|
|
|
|
$this->imageRepo->destroyImage($shelf->cover);
|
|
|
|
$shelf->image_id = 0;
|
|
|
|
$shelf->save();
|
|
|
|
}
|
2018-09-20 20:16:11 +02:00
|
|
|
}
|
2018-08-27 15:18:09 +02:00
|
|
|
}
|