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