1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request;
6 use Illuminate\Support\Facades\Auth;
7 use BookStack\Http\Requests;
8 use BookStack\Repos\BookRepo;
9 use BookStack\Repos\ChapterRepo;
10 use BookStack\Repos\PageRepo;
13 class BookController extends Controller
18 protected $chapterRepo;
22 * BookController constructor.
23 * @param BookRepo $bookRepo
24 * @param PageRepo $pageRepo
25 * @param ChapterRepo $chapterRepo
26 * @param UserRepo $userRepo
28 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
30 $this->bookRepo = $bookRepo;
31 $this->pageRepo = $pageRepo;
32 $this->chapterRepo = $chapterRepo;
33 $this->userRepo = $userRepo;
34 parent::__construct();
38 * Display a listing of the book.
42 public function index()
44 $books = $this->bookRepo->getAllPaginated(10);
45 $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
46 $popular = $this->bookRepo->getPopular(4, 0);
47 $this->setPageTitle('Books');
48 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
52 * Show the form for creating a new book.
56 public function create()
58 $this->checkPermission('book-create-all');
59 $this->setPageTitle('Create New Book');
60 return view('books/create');
64 * Store a newly created book in storage.
66 * @param Request $request
69 public function store(Request $request)
71 $this->checkPermission('book-create-all');
72 $this->validate($request, [
73 'name' => 'required|string|max:255',
74 'description' => 'string|max:1000'
76 $book = $this->bookRepo->newFromInput($request->all());
77 $book->slug = $this->bookRepo->findSuitableSlug($book->name);
78 $book->created_by = Auth::user()->id;
79 $book->updated_by = Auth::user()->id;
81 Activity::add($book, 'book_create', $book->id);
82 return redirect($book->getUrl());
86 * Display the specified book.
91 public function show($slug)
93 $book = $this->bookRepo->getBySlug($slug);
94 $this->checkOwnablePermission('book-view', $book);
95 $bookChildren = $this->bookRepo->getChildren($book);
97 $this->setPageTitle($book->getShortName());
98 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
102 * Show the form for editing the specified book.
107 public function edit($slug)
109 $book = $this->bookRepo->getBySlug($slug);
110 $this->checkOwnablePermission('book-update', $book);
111 $this->setPageTitle('Edit Book ' . $book->getShortName());
112 return view('books/edit', ['book' => $book, 'current' => $book]);
116 * Update the specified book in storage.
118 * @param Request $request
122 public function update(Request $request, $slug)
124 $book = $this->bookRepo->getBySlug($slug);
125 $this->checkOwnablePermission('book-update', $book);
126 $this->validate($request, [
127 'name' => 'required|string|max:255',
128 'description' => 'string|max:1000'
130 $book->fill($request->all());
131 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
132 $book->updated_by = Auth::user()->id;
134 Activity::add($book, 'book_update', $book->id);
135 return redirect($book->getUrl());
139 * Shows the page to confirm deletion
141 * @return \Illuminate\View\View
143 public function showDelete($bookSlug)
145 $book = $this->bookRepo->getBySlug($bookSlug);
146 $this->checkOwnablePermission('book-delete', $book);
147 $this->setPageTitle('Delete Book ' . $book->getShortName());
148 return view('books/delete', ['book' => $book, 'current' => $book]);
152 * Shows the view which allows pages to be re-ordered and sorted.
153 * @param string $bookSlug
154 * @return \Illuminate\View\View
156 public function sort($bookSlug)
158 $book = $this->bookRepo->getBySlug($bookSlug);
159 $this->checkOwnablePermission('book-update', $book);
160 $bookChildren = $this->bookRepo->getChildren($book);
161 $books = $this->bookRepo->getAll(false);
162 $this->setPageTitle('Sort Book ' . $book->getShortName());
163 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
167 * Shows the sort box for a single book.
168 * Used via AJAX when loading in extra books to a sort.
170 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
172 public function getSortItem($bookSlug)
174 $book = $this->bookRepo->getBySlug($bookSlug);
175 $bookChildren = $this->bookRepo->getChildren($book);
176 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
180 * Saves an array of sort mapping to pages and chapters.
181 * @param string $bookSlug
182 * @param Request $request
183 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
185 public function saveSort($bookSlug, Request $request)
187 $book = $this->bookRepo->getBySlug($bookSlug);
188 $this->checkOwnablePermission('book-update', $book);
190 // Return if no map sent
191 if (!$request->has('sort-tree')) {
192 return redirect($book->getUrl());
196 // Sort pages and chapters
197 $sortMap = json_decode($request->get('sort-tree'));
198 $defaultBookId = $book->id;
199 foreach ($sortMap as $index => $bookChild) {
200 $id = $bookChild->id;
201 $isPage = $bookChild->type == 'page';
202 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
203 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
204 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
205 $model->priority = $index;
207 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
210 if (!in_array($bookId, $sortedBooks)) {
211 $sortedBooks[] = $bookId;
215 // Add activity for books
216 foreach ($sortedBooks as $bookId) {
217 $updatedBook = $this->bookRepo->getById($bookId);
218 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
221 return redirect($book->getUrl());
225 * Remove the specified book from storage.
229 public function destroy($bookSlug)
231 $book = $this->bookRepo->getBySlug($bookSlug);
232 $this->checkOwnablePermission('book-delete', $book);
233 Activity::addMessage('book_delete', 0, $book->name);
234 Activity::removeEntity($book);
235 $this->bookRepo->destroyBySlug($bookSlug);
236 return redirect('/books');
240 * Show the Restrictions view.
242 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
244 public function showRestrict($bookSlug)
246 $book = $this->bookRepo->getBySlug($bookSlug);
247 $this->checkOwnablePermission('restrictions-manage', $book);
248 $roles = $this->userRepo->getRestrictableRoles();
249 return view('books/restrictions', [
256 * Set the restrictions for this book.
259 * @param Request $request
260 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
262 public function restrict($bookSlug, Request $request)
264 $book = $this->bookRepo->getBySlug($bookSlug);
265 $this->checkOwnablePermission('restrictions-manage', $book);
266 $this->bookRepo->updateRestrictionsFromRequest($request, $book);
267 session()->flash('success', 'Book Restrictions Updated');
268 return redirect($book->getUrl());