3 namespace BookStack\Http\Controllers;
6 use BookStack\Repos\UserRepo;
7 use Illuminate\Http\Request;
9 use Illuminate\Support\Facades\Auth;
10 use Illuminate\Support\Str;
11 use BookStack\Http\Requests;
12 use BookStack\Repos\BookRepo;
13 use BookStack\Repos\ChapterRepo;
14 use BookStack\Repos\PageRepo;
17 class BookController extends Controller
22 protected $chapterRepo;
26 * BookController constructor.
27 * @param BookRepo $bookRepo
28 * @param PageRepo $pageRepo
29 * @param ChapterRepo $chapterRepo
30 * @param UserRepo $userRepo
32 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
34 $this->bookRepo = $bookRepo;
35 $this->pageRepo = $pageRepo;
36 $this->chapterRepo = $chapterRepo;
37 $this->userRepo = $userRepo;
38 parent::__construct();
42 * Display a listing of the book.
46 public function index()
48 $books = $this->bookRepo->getAllPaginated(10);
49 $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
50 $popular = $this->bookRepo->getPopular(4, 0);
51 $this->setPageTitle('Books');
52 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
56 * Show the form for creating a new book.
60 public function create()
62 $this->checkPermission('book-create-all');
63 $this->setPageTitle('Create New Book');
64 return view('books/create');
68 * Store a newly created book in storage.
70 * @param Request $request
73 public function store(Request $request)
75 $this->checkPermission('book-create-all');
76 $this->validate($request, [
77 'name' => 'required|string|max:255',
78 'description' => 'string|max:1000'
80 $book = $this->bookRepo->newFromInput($request->all());
81 $book->slug = $this->bookRepo->findSuitableSlug($book->name);
82 $book->created_by = Auth::user()->id;
83 $book->updated_by = Auth::user()->id;
85 Activity::add($book, 'book_create', $book->id);
86 return redirect($book->getUrl());
90 * Display the specified book.
95 public function show($slug)
97 $book = $this->bookRepo->getBySlug($slug);
98 $bookChildren = $this->bookRepo->getChildren($book);
100 $this->setPageTitle($book->getShortName());
101 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
105 * Show the form for editing the specified book.
110 public function edit($slug)
112 $book = $this->bookRepo->getBySlug($slug);
113 $this->checkOwnablePermission('book-update', $book);
114 $this->setPageTitle('Edit Book ' . $book->getShortName());
115 return view('books/edit', ['book' => $book, 'current' => $book]);
119 * Update the specified book in storage.
121 * @param Request $request
125 public function update(Request $request, $slug)
127 $book = $this->bookRepo->getBySlug($slug);
128 $this->checkOwnablePermission('book-update', $book);
129 $this->validate($request, [
130 'name' => 'required|string|max:255',
131 'description' => 'string|max:1000'
133 $book->fill($request->all());
134 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
135 $book->updated_by = Auth::user()->id;
137 Activity::add($book, 'book_update', $book->id);
138 return redirect($book->getUrl());
142 * Shows the page to confirm deletion
144 * @return \Illuminate\View\View
146 public function showDelete($bookSlug)
148 $book = $this->bookRepo->getBySlug($bookSlug);
149 $this->checkOwnablePermission('book-delete', $book);
150 $this->setPageTitle('Delete Book ' . $book->getShortName());
151 return view('books/delete', ['book' => $book, 'current' => $book]);
155 * Shows the view which allows pages to be re-ordered and sorted.
156 * @param string $bookSlug
157 * @return \Illuminate\View\View
159 public function sort($bookSlug)
161 $book = $this->bookRepo->getBySlug($bookSlug);
162 $this->checkOwnablePermission('book-update', $book);
163 $bookChildren = $this->bookRepo->getChildren($book);
164 $books = $this->bookRepo->getAll(false);
165 $this->setPageTitle('Sort Book ' . $book->getShortName());
166 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
170 * Shows the sort box for a single book.
171 * Used via AJAX when loading in extra books to a sort.
173 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
175 public function getSortItem($bookSlug)
177 $book = $this->bookRepo->getBySlug($bookSlug);
178 $bookChildren = $this->bookRepo->getChildren($book);
179 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
183 * Saves an array of sort mapping to pages and chapters.
184 * @param string $bookSlug
185 * @param Request $request
186 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
188 public function saveSort($bookSlug, Request $request)
190 $book = $this->bookRepo->getBySlug($bookSlug);
191 $this->checkOwnablePermission('book-update', $book);
193 // Return if no map sent
194 if (!$request->has('sort-tree')) {
195 return redirect($book->getUrl());
199 // Sort pages and chapters
200 $sortMap = json_decode($request->get('sort-tree'));
201 $defaultBookId = $book->id;
202 foreach ($sortMap as $index => $bookChild) {
203 $id = $bookChild->id;
204 $isPage = $bookChild->type == 'page';
205 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
206 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
207 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
208 $model->priority = $index;
210 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
213 if (!in_array($bookId, $sortedBooks)) {
214 $sortedBooks[] = $bookId;
218 // Add activity for books
219 foreach ($sortedBooks as $bookId) {
220 $updatedBook = $this->bookRepo->getById($bookId);
221 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
224 return redirect($book->getUrl());
228 * Remove the specified book from storage.
232 public function destroy($bookSlug)
234 $book = $this->bookRepo->getBySlug($bookSlug);
235 $this->checkOwnablePermission('book-delete', $book);
236 Activity::addMessage('book_delete', 0, $book->name);
237 Activity::removeEntity($book);
238 $this->bookRepo->destroyBySlug($bookSlug);
239 return redirect('/books');
243 * Show the Restrictions view.
245 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
247 public function showRestrict($bookSlug)
249 $book = $this->bookRepo->getBySlug($bookSlug);
250 $this->checkOwnablePermission('restrictions-manage', $book);
251 $roles = $this->userRepo->getRestrictableRoles();
252 return view('books/restrictions', [
259 * Set the restrictions for this book.
262 * @param Request $request
263 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
265 public function restrict($bookSlug, Request $request)
267 $book = $this->bookRepo->getBySlug($bookSlug);
268 $this->checkOwnablePermission('restrictions-manage', $book);
269 $this->bookRepo->updateRestrictionsFromRequest($request, $book);
270 session()->flash('success', 'Page Restrictions Updated');
271 return redirect($book->getUrl());