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 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
51 * Show the form for creating a new book.
55 public function create()
57 $this->checkPermission('book-create');
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');
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.
89 public function show($slug)
91 $book = $this->bookRepo->getBySlug($slug);
93 $bookChildren = $this->bookRepo->getChildren($book);
94 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
98 * Show the form for editing the specified book.
103 public function edit($slug)
105 $this->checkPermission('book-update');
106 $book = $this->bookRepo->getBySlug($slug);
107 return view('books/edit', ['book' => $book, 'current' => $book]);
111 * Update the specified book in storage.
113 * @param Request $request
117 public function update(Request $request, $slug)
119 $this->checkPermission('book-update');
120 $book = $this->bookRepo->getBySlug($slug);
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 $this->checkPermission('book-delete');
141 $book = $this->bookRepo->getBySlug($bookSlug);
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 $this->checkPermission('book-update');
153 $book = $this->bookRepo->getBySlug($bookSlug);
154 $bookChildren = $this->bookRepo->getChildren($book);
155 $books = $this->bookRepo->getAll();
156 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
159 public function getSortItem($bookSlug)
161 $book = $this->bookRepo->getBySlug($bookSlug);
162 $bookChildren = $this->bookRepo->getChildren($book);
163 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
167 * Saves an array of sort mapping to pages and chapters.
169 * @param string $bookSlug
170 * @param Request $request
171 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
173 public function saveSort($bookSlug, Request $request)
175 $this->checkPermission('book-update');
176 $book = $this->bookRepo->getBySlug($bookSlug);
178 // Return if no map sent
179 if (!$request->has('sort-tree')) {
180 return redirect($book->getUrl());
184 // Sort pages and chapters
185 $sortMap = json_decode($request->get('sort-tree'));
186 $defaultBookId = $book->id;
187 foreach ($sortMap as $index => $bookChild) {
188 $id = $bookChild->id;
189 $isPage = $bookChild->type == 'page';
190 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
191 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
192 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
193 $model->priority = $index;
195 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
198 if (!in_array($bookId, $sortedBooks)) {
199 $sortedBooks[] = $bookId;
203 // Add activity for books
204 foreach ($sortedBooks as $bookId) {
205 $updatedBook = $this->bookRepo->getById($bookId);
206 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
209 return redirect($book->getUrl());
213 * Remove the specified book from storage.
218 public function destroy($bookSlug)
220 $this->checkPermission('book-delete');
221 $book = $this->bookRepo->getBySlug($bookSlug);
222 Activity::addMessage('book_delete', 0, $book->name);
223 Activity::removeEntity($book);
224 $this->bookRepo->destroyBySlug($bookSlug);
225 return redirect('/books');