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 $books = $this->bookRepo->getAll();
154 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books]);
157 public function getSortItem($bookSlug)
159 $book = $this->bookRepo->getBySlug($bookSlug);
160 return view('books/sort-box', ['book' => $book]);
164 * Saves an array of sort mapping to pages and chapters.
166 * @param string $bookSlug
167 * @param Request $request
168 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
170 public function saveSort($bookSlug, Request $request)
172 $this->checkPermission('book-update');
173 $book = $this->bookRepo->getBySlug($bookSlug);
175 // Return if no map sent
176 if (!$request->has('sort-tree')) {
177 return redirect($book->getUrl());
181 // Sort pages and chapters
182 $sortMap = json_decode($request->get('sort-tree'));
183 $defaultBookId = $book->id;
184 foreach ($sortMap as $index => $bookChild) {
185 $id = $bookChild->id;
186 $isPage = $bookChild->type == 'page';
187 $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
188 $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
189 $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
190 $model->priority = $index;
192 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
195 if (!in_array($bookId, $sortedBooks)) {
196 $sortedBooks[] = $bookId;
200 // Add activity for books
201 foreach ($sortedBooks as $bookId) {
202 $updatedBook = $this->bookRepo->getById($bookId);
203 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
206 return redirect($book->getUrl());
210 * Remove the specified book from storage.
215 public function destroy($bookSlug)
217 $this->checkPermission('book-delete');
218 $book = $this->bookRepo->getBySlug($bookSlug);
219 Activity::addMessage('book_delete', 0, $book->name);
220 Activity::removeEntity($book);
221 $this->bookRepo->destroyBySlug($bookSlug);
222 return redirect('/books');