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 $bookChildren = $this->bookRepo->getChildren($book);
93 $this->setPageTitle($book->getShortName());
94 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
98 * Show the form for editing the specified book.
102 public function edit($slug)
104 $book = $this->bookRepo->getBySlug($slug);
105 $this->checkOwnablePermission('book-update', $book);
106 $this->setPageTitle('Edit Book ' . $book->getShortName());
107 return view('books/edit', ['book' => $book, 'current' => $book]);
111 * Update the specified book in storage.
112 * @param Request $request
116 public function update(Request $request, $slug)
118 $book = $this->bookRepo->getBySlug($slug);
119 $this->checkOwnablePermission('book-update', $book);
120 $this->validate($request, [
121 'name' => 'required|string|max:255',
122 'description' => 'string|max:1000'
124 $book->fill($request->all());
125 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
126 $book->updated_by = Auth::user()->id;
128 Activity::add($book, 'book_update', $book->id);
129 return redirect($book->getUrl());
133 * Shows the page to confirm deletion
135 * @return \Illuminate\View\View
137 public function showDelete($bookSlug)
139 $book = $this->bookRepo->getBySlug($bookSlug);
140 $this->checkOwnablePermission('book-delete', $book);
141 $this->setPageTitle('Delete Book ' . $book->getShortName());
142 return view('books/delete', ['book' => $book, 'current' => $book]);
146 * Shows the view which allows pages to be re-ordered and sorted.
147 * @param string $bookSlug
148 * @return \Illuminate\View\View
150 public function sort($bookSlug)
152 $book = $this->bookRepo->getBySlug($bookSlug);
153 $this->checkOwnablePermission('book-update', $book);
154 $bookChildren = $this->bookRepo->getChildren($book, true);
155 $books = $this->bookRepo->getAll(false);
156 $this->setPageTitle('Sort Book ' . $book->getShortName());
157 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
161 * Shows the sort box for a single book.
162 * Used via AJAX when loading in extra books to a sort.
164 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
166 public function getSortItem($bookSlug)
168 $book = $this->bookRepo->getBySlug($bookSlug);
169 $bookChildren = $this->bookRepo->getChildren($book);
170 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
174 * Saves an array of sort mapping to pages and chapters.
175 * @param string $bookSlug
176 * @param Request $request
177 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
179 public function saveSort($bookSlug, Request $request)
181 $book = $this->bookRepo->getBySlug($bookSlug);
182 $this->checkOwnablePermission('book-update', $book);
184 // Return if no map sent
185 if (!$request->has('sort-tree')) {
186 return redirect($book->getUrl());
190 // Sort pages and chapters
191 $sortMap = json_decode($request->get('sort-tree'));
192 $defaultBookId = $book->id;
193 foreach ($sortMap as $index => $bookChild) {
194 $id = $bookChild->id;
195 $isPage = $bookChild->type == 'page';
196 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
197 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
198 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
199 $model->priority = $index;
201 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
204 if (!in_array($bookId, $sortedBooks)) {
205 $sortedBooks[] = $bookId;
209 // Add activity for books
210 foreach ($sortedBooks as $bookId) {
211 $updatedBook = $this->bookRepo->getById($bookId);
212 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
215 return redirect($book->getUrl());
219 * Remove the specified book from storage.
223 public function destroy($bookSlug)
225 $book = $this->bookRepo->getBySlug($bookSlug);
226 $this->checkOwnablePermission('book-delete', $book);
227 Activity::addMessage('book_delete', 0, $book->name);
228 Activity::removeEntity($book);
229 $this->bookRepo->destroyBySlug($bookSlug);
230 return redirect('/books');
234 * Show the Restrictions view.
236 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
238 public function showRestrict($bookSlug)
240 $book = $this->bookRepo->getBySlug($bookSlug);
241 $this->checkOwnablePermission('restrictions-manage', $book);
242 $roles = $this->userRepo->getRestrictableRoles();
243 return view('books/restrictions', [
250 * Set the restrictions for this book.
253 * @param Request $request
254 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
256 public function restrict($bookSlug, Request $request)
258 $book = $this->bookRepo->getBySlug($bookSlug);
259 $this->checkOwnablePermission('restrictions-manage', $book);
260 $this->bookRepo->updateRestrictionsFromRequest($request, $book);
261 session()->flash('success', 'Book Restrictions Updated');
262 return redirect($book->getUrl());