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->newFromInput($request->all());
75 $book->slug = $this->bookRepo->findSuitableSlug($book->name);
76 $book->created_by = Auth::user()->id;
77 $book->updated_by = Auth::user()->id;
79 Activity::add($book, 'book_create', $book->id);
80 return redirect($book->getUrl());
84 * Display the specified book.
88 public function show($slug)
90 $book = $this->bookRepo->getBySlug($slug);
91 $this->checkOwnablePermission('book-view', $book);
92 $bookChildren = $this->bookRepo->getChildren($book);
94 $this->setPageTitle($book->getShortName());
95 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
99 * Show the form for editing the specified book.
103 public function edit($slug)
105 $book = $this->bookRepo->getBySlug($slug);
106 $this->checkOwnablePermission('book-update', $book);
107 $this->setPageTitle('Edit Book ' . $book->getShortName());
108 return view('books/edit', ['book' => $book, 'current' => $book]);
112 * Update the specified book in storage.
113 * @param Request $request
117 public function update(Request $request, $slug)
119 $book = $this->bookRepo->getBySlug($slug);
120 $this->checkOwnablePermission('book-update', $book);
121 $this->validate($request, [
122 'name' => 'required|string|max:255',
123 'description' => 'string|max:1000'
125 $book->fill($request->all());
126 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
127 $book->updated_by = Auth::user()->id;
129 Activity::add($book, 'book_update', $book->id);
130 return redirect($book->getUrl());
134 * Shows the page to confirm deletion
136 * @return \Illuminate\View\View
138 public function showDelete($bookSlug)
140 $book = $this->bookRepo->getBySlug($bookSlug);
141 $this->checkOwnablePermission('book-delete', $book);
142 $this->setPageTitle('Delete Book ' . $book->getShortName());
143 return view('books/delete', ['book' => $book, 'current' => $book]);
147 * Shows the view which allows pages to be re-ordered and sorted.
148 * @param string $bookSlug
149 * @return \Illuminate\View\View
151 public function sort($bookSlug)
153 $book = $this->bookRepo->getBySlug($bookSlug);
154 $this->checkOwnablePermission('book-update', $book);
155 $bookChildren = $this->bookRepo->getChildren($book, true);
156 $books = $this->bookRepo->getAll(false);
157 $this->setPageTitle('Sort Book ' . $book->getShortName());
158 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
162 * Shows the sort box for a single book.
163 * Used via AJAX when loading in extra books to a sort.
165 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
167 public function getSortItem($bookSlug)
169 $book = $this->bookRepo->getBySlug($bookSlug);
170 $bookChildren = $this->bookRepo->getChildren($book);
171 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
175 * Saves an array of sort mapping to pages and chapters.
176 * @param string $bookSlug
177 * @param Request $request
178 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
180 public function saveSort($bookSlug, Request $request)
182 $book = $this->bookRepo->getBySlug($bookSlug);
183 $this->checkOwnablePermission('book-update', $book);
185 // Return if no map sent
186 if (!$request->has('sort-tree')) {
187 return redirect($book->getUrl());
191 // Sort pages and chapters
192 $sortMap = json_decode($request->get('sort-tree'));
193 $defaultBookId = $book->id;
194 foreach ($sortMap as $index => $bookChild) {
195 $id = $bookChild->id;
196 $isPage = $bookChild->type == 'page';
197 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
198 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
199 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
200 $model->priority = $index;
202 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
205 if (!in_array($bookId, $sortedBooks)) {
206 $sortedBooks[] = $bookId;
210 // Add activity for books
211 foreach ($sortedBooks as $bookId) {
212 $updatedBook = $this->bookRepo->getById($bookId);
213 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
216 return redirect($book->getUrl());
220 * Remove the specified book from storage.
224 public function destroy($bookSlug)
226 $book = $this->bookRepo->getBySlug($bookSlug);
227 $this->checkOwnablePermission('book-delete', $book);
228 Activity::addMessage('book_delete', 0, $book->name);
229 Activity::removeEntity($book);
230 $this->bookRepo->destroyBySlug($bookSlug);
231 return redirect('/books');
235 * Show the Restrictions view.
237 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
239 public function showRestrict($bookSlug)
241 $book = $this->bookRepo->getBySlug($bookSlug);
242 $this->checkOwnablePermission('restrictions-manage', $book);
243 $roles = $this->userRepo->getRestrictableRoles();
244 return view('books/restrictions', [
251 * Set the restrictions for this book.
254 * @param Request $request
255 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
257 public function restrict($bookSlug, Request $request)
259 $book = $this->bookRepo->getBySlug($bookSlug);
260 $this->checkOwnablePermission('restrictions-manage', $book);
261 $this->bookRepo->updateRestrictionsFromRequest($request, $book);
262 session()->flash('success', 'Book Restrictions Updated');
263 return redirect($book->getUrl());