2018-08-27 09:18:09 -04:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
|
|
|
|
|
|
|
use Activity;
|
2019-10-05 07:55:01 -04:00
|
|
|
use BookStack\Entities\Book;
|
|
|
|
use BookStack\Entities\Managers\EntityContext;
|
|
|
|
use BookStack\Entities\Repos\BookshelfRepo;
|
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2019-04-27 09:55:23 -04:00
|
|
|
use BookStack\Uploads\ImageRepo;
|
2019-10-05 07:55:01 -04:00
|
|
|
use Exception;
|
2018-08-27 09:18:09 -04:00
|
|
|
use Illuminate\Http\Request;
|
2019-10-05 07:55:01 -04:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2018-08-27 09:18:09 -04:00
|
|
|
use Views;
|
|
|
|
|
|
|
|
class BookshelfController extends Controller
|
|
|
|
{
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
protected $bookshelfRepo;
|
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-10-05 07:55:01 -04:00
|
|
|
public function __construct(BookshelfRepo $bookshelfRepo, EntityContext $entityContextManager, ImageRepo $imageRepo)
|
2018-08-27 09:18:09 -04:00
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->bookshelfRepo = $bookshelfRepo;
|
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.
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2019-09-19 19:18:28 -04: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 08:45:45 -05:00
|
|
|
$sortOptions = [
|
|
|
|
'name' => trans('common.sort_name'),
|
|
|
|
'created_at' => trans('common.sort_created_at'),
|
|
|
|
'updated_at' => trans('common.sort_updated_at'),
|
|
|
|
];
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelves = $this->bookshelfRepo->getAllPaginated(18, $sort, $order);
|
|
|
|
$recents = $this->isSignedIn() ? $this->bookshelfRepo->getRecentlyViewed(4) : false;
|
|
|
|
$popular = $this->bookshelfRepo->getPopular(4);
|
|
|
|
$new = $this->bookshelfRepo->getRecentlyCreated(4);
|
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.
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$this->checkPermission('bookshelf-create-all');
|
2019-10-05 07:55:01 -04:00
|
|
|
$books = Book::hasPermission('update')->get();
|
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-10-05 07:55:01 -04:00
|
|
|
* @throws ValidationException
|
|
|
|
* @throws 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',
|
2020-03-03 18:05:47 -05:00
|
|
|
'image' => 'nullable|' . $this->getImageValidationRules(),
|
2018-08-27 09:18:09 -04:00
|
|
|
]);
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$bookIds = explode(',', $request->get('books', ''));
|
|
|
|
$shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
|
2020-02-14 14:33:07 -05:00
|
|
|
$this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
|
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
|
|
|
/**
|
2019-10-05 07:55:01 -04:00
|
|
|
* Display the bookshelf of the given slug.
|
|
|
|
* @throws NotFoundException
|
2018-09-16 14:34:09 -04:00
|
|
|
*/
|
|
|
|
public function show(string $slug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2019-04-27 09:55:23 -04:00
|
|
|
$this->checkOwnablePermission('book-view', $shelf);
|
2018-09-16 14:34:09 -04:00
|
|
|
|
2019-04-27 09:55:23 -04:00
|
|
|
Views::add($shelf);
|
|
|
|
$this->entityContextManager->setShelfContext($shelf->id);
|
2020-04-10 07:49:16 -04:00
|
|
|
$view = setting()->getForCurrentUser('bookshelf_view_type', config('app.views.books'));
|
2018-09-16 14:34:09 -04:00
|
|
|
|
2019-04-27 09:55:23 -04:00
|
|
|
$this->setPageTitle($shelf->getShortName());
|
2019-02-02 06:41:41 -05:00
|
|
|
return view('shelves.show', [
|
2019-04-27 09:55:23 -04:00
|
|
|
'shelf' => $shelf,
|
2019-10-30 06:23:42 -04:00
|
|
|
'view' => $view,
|
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.
|
|
|
|
*/
|
|
|
|
public function edit(string $slug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2019-04-27 09:55:23 -04:00
|
|
|
$this->checkOwnablePermission('bookshelf-update', $shelf);
|
2018-09-16 14:34:09 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
|
|
|
|
$books = Book::hasPermission('update')->whereNotIn('id', $shelfBookIds)->get();
|
2018-09-16 14:34:09 -04:00
|
|
|
|
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,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified bookshelf in storage.
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws ValidationException
|
|
|
|
* @throws ImageUploadException
|
|
|
|
* @throws NotFoundException
|
2018-09-16 14:34:09 -04:00
|
|
|
*/
|
|
|
|
public function update(Request $request, string $slug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2018-09-16 14:34:09 -04:00
|
|
|
$this->checkOwnablePermission('bookshelf-update', $shelf);
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
'description' => 'string|max:1000',
|
2020-03-03 18:05:47 -05:00
|
|
|
'image' => 'nullable|' . $this->getImageValidationRules(),
|
2018-09-16 14:34:09 -04:00
|
|
|
]);
|
|
|
|
|
2019-04-27 09:55:23 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$bookIds = explode(',', $request->get('books', ''));
|
|
|
|
$shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
|
|
|
|
$resetCover = $request->has('image_reset');
|
|
|
|
$this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
|
|
|
|
Activity::add($shelf, 'bookshelf_update');
|
2018-09-16 14:34:09 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return redirect($shelf->getUrl());
|
2018-09-16 14:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the page to confirm deletion
|
|
|
|
*/
|
|
|
|
public function showDelete(string $slug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2019-04-27 09:55:23 -04:00
|
|
|
$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.
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws Exception
|
2018-09-16 14:34:09 -04:00
|
|
|
*/
|
|
|
|
public function destroy(string $slug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2019-04-27 09:55:23 -04:00
|
|
|
$this->checkOwnablePermission('bookshelf-delete', $shelf);
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
Activity::addMessage('bookshelf_delete', $shelf->name);
|
|
|
|
$this->bookshelfRepo->destroy($shelf);
|
2019-04-27 09:55:23 -04:00
|
|
|
|
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.
|
2018-09-20 14:16:11 -04:00
|
|
|
*/
|
2019-02-03 08:45:45 -05:00
|
|
|
public function showPermissions(string $slug)
|
2018-09-20 14:16:11 -04:00
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2019-04-27 09:55:23 -04:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $shelf);
|
2018-09-20 14:16:11 -04:00
|
|
|
|
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
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-03 08:45:45 -05:00
|
|
|
* Set the permissions for this bookshelf.
|
2018-09-20 14:16:11 -04:00
|
|
|
*/
|
2019-09-15 13:53:30 -04:00
|
|
|
public function permissions(Request $request, string $slug)
|
2018-09-20 14:16:11 -04:00
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2019-04-27 09:55:23 -04:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $shelf);
|
2018-09-20 14:16:11 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$restricted = $request->get('restricted') === 'true';
|
|
|
|
$permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
|
|
|
|
$this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
|
|
|
|
|
|
|
|
$this->showSuccessNotification(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.
|
|
|
|
*/
|
|
|
|
public function copyPermissions(string $slug)
|
|
|
|
{
|
2019-10-05 07:55:01 -04:00
|
|
|
$shelf = $this->bookshelfRepo->getBySlug($slug);
|
2019-04-27 09:55:23 -04:00
|
|
|
$this->checkOwnablePermission('restrictions-manage', $shelf);
|
2018-09-20 14:16:11 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$updateCount = $this->bookshelfRepo->copyDownPermissions($shelf);
|
|
|
|
$this->showSuccessNotification(trans('entities.shelves_copy_permission_success', ['count' => $updateCount]));
|
2019-04-27 09:55:23 -04:00
|
|
|
return redirect($shelf->getUrl());
|
|
|
|
}
|
2018-08-27 09:18:09 -04:00
|
|
|
}
|