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