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 $bookChildren = $this->bookRepo->getChildren($book);
93 return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
97 * Show the form for editing the specified book.
102 public function edit($slug)
104 $this->checkPermission('book-update');
105 $book = $this->bookRepo->getBySlug($slug);
106 return view('books/edit', ['book' => $book, 'current' => $book]);
110 * Update the specified book in storage.
112 * @param Request $request
116 public function update(Request $request, $slug)
118 $this->checkPermission('book-update');
119 $book = $this->bookRepo->getBySlug($slug);
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 $this->checkPermission('book-delete');
140 $book = $this->bookRepo->getBySlug($bookSlug);
141 return view('books/delete', ['book' => $book, 'current' => $book]);
145 * Shows the view which allows pages to be re-ordered and sorted.
146 * @param string $bookSlug
147 * @return \Illuminate\View\View
149 public function sort($bookSlug)
151 $this->checkPermission('book-update');
152 $book = $this->bookRepo->getBySlug($bookSlug);
153 $bookChildren = $this->bookRepo->getChildren($book);
154 $books = $this->bookRepo->getAll();
155 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
158 public function getSortItem($bookSlug)
160 $book = $this->bookRepo->getBySlug($bookSlug);
161 $bookChildren = $this->bookRepo->getChildren($book);
162 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
166 * Saves an array of sort mapping to pages and chapters.
168 * @param string $bookSlug
169 * @param Request $request
170 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
172 public function saveSort($bookSlug, Request $request)
174 $this->checkPermission('book-update');
175 $book = $this->bookRepo->getBySlug($bookSlug);
177 // Return if no map sent
178 if (!$request->has('sort-tree')) {
179 return redirect($book->getUrl());
183 // Sort pages and chapters
184 $sortMap = json_decode($request->get('sort-tree'));
185 $defaultBookId = $book->id;
186 foreach ($sortMap as $index => $bookChild) {
187 $id = $bookChild->id;
188 $isPage = $bookChild->type == 'page';
189 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
190 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
191 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
192 $model->priority = $index;
194 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
197 if (!in_array($bookId, $sortedBooks)) {
198 $sortedBooks[] = $bookId;
202 // Add activity for books
203 foreach ($sortedBooks as $bookId) {
204 $updatedBook = $this->bookRepo->getById($bookId);
205 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
208 return redirect($book->getUrl());
212 * Remove the specified book from storage.
217 public function destroy($bookSlug)
219 $this->checkPermission('book-delete');
220 $book = $this->bookRepo->getBySlug($bookSlug);
221 Activity::addMessage('book_delete', 0, $book->name);
222 Activity::removeEntity($book);
223 $this->bookRepo->destroyBySlug($bookSlug);
224 return redirect('/books');