3 namespace BookStack\Http\Controllers;
6 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\Auth;
9 use Illuminate\Support\Str;
10 use BookStack\Http\Requests;
11 use BookStack\Repos\BookRepo;
12 use BookStack\Repos\ChapterRepo;
13 use BookStack\Repos\PageRepo;
16 class BookController extends Controller
21 protected $chapterRepo;
24 * BookController constructor.
25 * @param BookRepo $bookRepo
26 * @param PageRepo $pageRepo
27 * @param ChapterRepo $chapterRepo
29 public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo)
31 $this->bookRepo = $bookRepo;
32 $this->pageRepo = $pageRepo;
33 $this->chapterRepo = $chapterRepo;
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 $bookChildren = $this->bookRepo->getChildren($book);
96 $this->setPageTitle($book->getShortName());
97 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
101 * Show the form for editing the specified book.
106 public function edit($slug)
108 $book = $this->bookRepo->getBySlug($slug);
109 $this->checkOwnablePermission('book-update', $book);
110 $this->setPageTitle('Edit Book ' . $book->getShortName());
111 return view('books/edit', ['book' => $book, 'current' => $book]);
115 * Update the specified book in storage.
117 * @param Request $request
121 public function update(Request $request, $slug)
123 $book = $this->bookRepo->getBySlug($slug);
124 $this->checkOwnablePermission('book-update', $book);
125 $this->validate($request, [
126 'name' => 'required|string|max:255',
127 'description' => 'string|max:1000'
129 $book->fill($request->all());
130 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
131 $book->updated_by = Auth::user()->id;
133 Activity::add($book, 'book_update', $book->id);
134 return redirect($book->getUrl());
138 * Shows the page to confirm deletion
140 * @return \Illuminate\View\View
142 public function showDelete($bookSlug)
144 $book = $this->bookRepo->getBySlug($bookSlug);
145 $this->checkOwnablePermission('book-delete', $book);
146 $this->setPageTitle('Delete Book ' . $book->getShortName());
147 return view('books/delete', ['book' => $book, 'current' => $book]);
151 * Shows the view which allows pages to be re-ordered and sorted.
152 * @param string $bookSlug
153 * @return \Illuminate\View\View
155 public function sort($bookSlug)
157 $book = $this->bookRepo->getBySlug($bookSlug);
158 $this->checkOwnablePermission('book-update', $book);
159 $bookChildren = $this->bookRepo->getChildren($book);
160 $books = $this->bookRepo->getAll(false);
161 $this->setPageTitle('Sort Book ' . $book->getShortName());
162 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
166 * Shows the sort box for a single book.
167 * Used via AJAX when loading in extra books to a sort.
169 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
171 public function getSortItem($bookSlug)
173 $book = $this->bookRepo->getBySlug($bookSlug);
174 $bookChildren = $this->bookRepo->getChildren($book);
175 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
179 * 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.
230 public function destroy($bookSlug)
232 $book = $this->bookRepo->getBySlug($bookSlug);
233 $this->checkOwnablePermission('book-delete', $book);
234 Activity::addMessage('book_delete', 0, $book->name);
235 Activity::removeEntity($book);
236 $this->bookRepo->destroyBySlug($bookSlug);
237 return redirect('/books');