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(10, 0) : false;
46 return view('books/index', ['books' => $books, 'recents' => $recents]);
50 * Show the form for creating a new book.
54 public function create()
56 $this->checkPermission('book-create');
57 return view('books/create');
61 * Store a newly created book in storage.
63 * @param Request $request
66 public function store(Request $request)
68 $this->checkPermission('book-create');
69 $this->validate($request, [
70 'name' => 'required|string|max:255',
71 'description' => 'string|max:1000'
73 $book = $this->bookRepo->newFromInput($request->all());
74 $book->slug = $this->bookRepo->findSuitableSlug($book->name);
75 $book->created_by = Auth::user()->id;
76 $book->updated_by = Auth::user()->id;
78 Activity::add($book, 'book_create', $book->id);
79 return redirect($book->getUrl());
83 * Display the specified book.
88 public function show($slug)
90 $book = $this->bookRepo->getBySlug($slug);
92 return view('books/show', ['book' => $book, 'current' => $book]);
96 * Show the form for editing the specified book.
101 public function edit($slug)
103 $this->checkPermission('book-update');
104 $book = $this->bookRepo->getBySlug($slug);
105 return view('books/edit', ['book' => $book, 'current' => $book]);
109 * Update the specified book in storage.
111 * @param Request $request
115 public function update(Request $request, $slug)
117 $this->checkPermission('book-update');
118 $book = $this->bookRepo->getBySlug($slug);
119 $this->validate($request, [
120 'name' => 'required|string|max:255',
121 'description' => 'string|max:1000'
123 $book->fill($request->all());
124 $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
125 $book->updated_by = Auth::user()->id;
127 Activity::add($book, 'book_update', $book->id);
128 return redirect($book->getUrl());
132 * Shows the page to confirm deletion
134 * @return \Illuminate\View\View
136 public function showDelete($bookSlug)
138 $this->checkPermission('book-delete');
139 $book = $this->bookRepo->getBySlug($bookSlug);
140 return view('books/delete', ['book' => $book, 'current' => $book]);
144 * Shows the view which allows pages to be re-ordered and sorted.
145 * @param string $bookSlug
146 * @return \Illuminate\View\View
148 public function sort($bookSlug)
150 $this->checkPermission('book-update');
151 $book = $this->bookRepo->getBySlug($bookSlug);
152 $books = $this->bookRepo->getAll();
153 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books]);
156 public function getSortItem($bookSlug)
158 $book = $this->bookRepo->getBySlug($bookSlug);
159 return view('books/sort-box', ['book' => $book]);
163 * Saves an array of sort mapping to pages and chapters.
165 * @param string $bookSlug
166 * @param Request $request
167 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
169 public function saveSort($bookSlug, Request $request)
171 $this->checkPermission('book-update');
172 $book = $this->bookRepo->getBySlug($bookSlug);
174 // Return if no map sent
175 if (!$request->has('sort-tree')) {
176 return redirect($book->getUrl());
180 // Sort pages and chapters
181 $sortMap = json_decode($request->get('sort-tree'));
182 $defaultBookId = $book->id;
183 foreach ($sortMap as $index => $bookChild) {
184 $id = $bookChild->id;
185 $isPage = $bookChild->type == 'page';
186 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
187 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
188 $isPage ? $this->pageRepo->setBookId($bookId, $model) : $this->chapterRepo->setBookId($bookId, $model);
189 $model->priority = $index;
191 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
194 if (!in_array($bookId, $sortedBooks)) {
195 $sortedBooks[] = $bookId;
199 // Add activity for books
200 foreach ($sortedBooks as $bookId) {
201 $updatedBook = $this->bookRepo->getById($bookId);
202 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
205 return redirect($book->getUrl());
209 * Remove the specified book from storage.
214 public function destroy($bookSlug)
216 $this->checkPermission('book-delete');
217 $book = $this->bookRepo->getBySlug($bookSlug);
218 Activity::addMessage('book_delete', 0, $book->name);
219 Activity::removeEntity($book);
220 $this->bookRepo->destroyBySlug($bookSlug);
221 return redirect('/books');