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.
41 public function index()
43 $books = $this->bookRepo->getAllPaginated(10);
44 $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
45 $popular = $this->bookRepo->getPopular(4, 0);
46 $this->setPageTitle('Books');
47 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
51 * Show the form for creating a new book.
54 public function create()
56 $this->checkPermission('book-create-all');
57 $this->setPageTitle('Create New Book');
58 return view('books/create');
62 * Store a newly created book in storage.
64 * @param Request $request
67 public function store(Request $request)
69 $this->checkPermission('book-create-all');
70 $this->validate($request, [
71 'name' => 'required|string|max:255',
72 'description' => 'string|max:1000'
74 $book = $this->bookRepo->createFromInput($request->all());
75 Activity::add($book, 'book_create', $book->id);
76 return redirect($book->getUrl());
80 * Display the specified book.
84 public function show($slug)
86 $book = $this->bookRepo->getBySlug($slug);
87 $this->checkOwnablePermission('book-view', $book);
88 $bookChildren = $this->bookRepo->getChildren($book);
90 $this->setPageTitle($book->getShortName());
91 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
95 * Show the form for editing the specified book.
99 public function edit($slug)
101 $book = $this->bookRepo->getBySlug($slug);
102 $this->checkOwnablePermission('book-update', $book);
103 $this->setPageTitle('Edit Book ' . $book->getShortName());
104 return view('books/edit', ['book' => $book, 'current' => $book]);
108 * Update the specified book in storage.
109 * @param Request $request
113 public function update(Request $request, $slug)
115 $book = $this->bookRepo->getBySlug($slug);
116 $this->checkOwnablePermission('book-update', $book);
117 $this->validate($request, [
118 'name' => 'required|string|max:255',
119 'description' => 'string|max:1000'
121 $book = $this->bookRepo->updateFromInput($book, $request->all());
122 Activity::add($book, 'book_update', $book->id);
123 return redirect($book->getUrl());
127 * Shows the page to confirm deletion
129 * @return \Illuminate\View\View
131 public function showDelete($bookSlug)
133 $book = $this->bookRepo->getBySlug($bookSlug);
134 $this->checkOwnablePermission('book-delete', $book);
135 $this->setPageTitle('Delete Book ' . $book->getShortName());
136 return view('books/delete', ['book' => $book, 'current' => $book]);
140 * Shows the view which allows pages to be re-ordered and sorted.
141 * @param string $bookSlug
142 * @return \Illuminate\View\View
144 public function sort($bookSlug)
146 $book = $this->bookRepo->getBySlug($bookSlug);
147 $this->checkOwnablePermission('book-update', $book);
148 $bookChildren = $this->bookRepo->getChildren($book, true);
149 $books = $this->bookRepo->getAll(false);
150 $this->setPageTitle('Sort Book ' . $book->getShortName());
151 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
155 * Shows the sort box for a single book.
156 * Used via AJAX when loading in extra books to a sort.
158 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
160 public function getSortItem($bookSlug)
162 $book = $this->bookRepo->getBySlug($bookSlug);
163 $bookChildren = $this->bookRepo->getChildren($book);
164 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
168 * Saves an array of sort mapping to pages and chapters.
169 * @param string $bookSlug
170 * @param Request $request
171 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
173 public function saveSort($bookSlug, Request $request)
175 $book = $this->bookRepo->getBySlug($bookSlug);
176 $this->checkOwnablePermission('book-update', $book);
178 // Return if no map sent
179 if (!$request->has('sort-tree')) {
180 return redirect($book->getUrl());
184 // Sort pages and chapters
185 $sortMap = json_decode($request->get('sort-tree'));
186 $defaultBookId = $book->id;
187 foreach ($sortMap as $index => $bookChild) {
188 $id = $bookChild->id;
189 $isPage = $bookChild->type == 'page';
190 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
191 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
192 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
193 $model->priority = $index;
195 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
198 if (!in_array($bookId, $sortedBooks)) {
199 $sortedBooks[] = $bookId;
203 // Add activity for books
204 foreach ($sortedBooks as $bookId) {
205 $updatedBook = $this->bookRepo->getById($bookId);
206 $this->bookRepo->updateBookPermissions($updatedBook);
207 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
210 return redirect($book->getUrl());
214 * Remove the specified book from storage.
218 public function destroy($bookSlug)
220 $book = $this->bookRepo->getBySlug($bookSlug);
221 $this->checkOwnablePermission('book-delete', $book);
222 Activity::addMessage('book_delete', 0, $book->name);
223 Activity::removeEntity($book);
224 $this->bookRepo->destroy($book);
225 return redirect('/books');
229 * Show the Restrictions view.
231 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
233 public function showRestrict($bookSlug)
235 $book = $this->bookRepo->getBySlug($bookSlug);
236 $this->checkOwnablePermission('restrictions-manage', $book);
237 $roles = $this->userRepo->getRestrictableRoles();
238 return view('books/restrictions', [
245 * Set the restrictions for this book.
248 * @param Request $request
249 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
251 public function restrict($bookSlug, Request $request)
253 $book = $this->bookRepo->getBySlug($bookSlug);
254 $this->checkOwnablePermission('restrictions-manage', $book);
255 $this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
256 session()->flash('success', 'Book Restrictions Updated');
257 return redirect($book->getUrl());