]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Merge branch 'master' into nwalke-update_site_color
[bookstack] / app / Http / Controllers / BookController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use Activity;
6 use BookStack\Repos\UserRepo;
7 use Illuminate\Http\Request;
8
9 use Illuminate\Support\Facades\Auth;
10 use Illuminate\Support\Str;
11 use BookStack\Http\Requests;
12 use BookStack\Repos\BookRepo;
13 use BookStack\Repos\ChapterRepo;
14 use BookStack\Repos\PageRepo;
15 use Views;
16
17 class BookController extends Controller
18 {
19
20     protected $bookRepo;
21     protected $pageRepo;
22     protected $chapterRepo;
23     protected $userRepo;
24
25     /**
26      * BookController constructor.
27      * @param BookRepo $bookRepo
28      * @param PageRepo $pageRepo
29      * @param ChapterRepo $chapterRepo
30      * @param UserRepo $userRepo
31      */
32     public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
33     {
34         $this->bookRepo = $bookRepo;
35         $this->pageRepo = $pageRepo;
36         $this->chapterRepo = $chapterRepo;
37         $this->userRepo = $userRepo;
38         parent::__construct();
39     }
40
41     /**
42      * Display a listing of the book.
43      *
44      * @return Response
45      */
46     public function index()
47     {
48         $books = $this->bookRepo->getAllPaginated(10);
49         $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
50         $popular = $this->bookRepo->getPopular(4, 0);
51         $this->setPageTitle('Books');
52         return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
53     }
54
55     /**
56      * Show the form for creating a new book.
57      *
58      * @return Response
59      */
60     public function create()
61     {
62         $this->checkPermission('book-create-all');
63         $this->setPageTitle('Create New Book');
64         return view('books/create');
65     }
66
67     /**
68      * Store a newly created book in storage.
69      *
70      * @param  Request $request
71      * @return Response
72      */
73     public function store(Request $request)
74     {
75         $this->checkPermission('book-create-all');
76         $this->validate($request, [
77             'name' => 'required|string|max:255',
78             'description' => 'string|max:1000'
79         ]);
80         $book = $this->bookRepo->newFromInput($request->all());
81         $book->slug = $this->bookRepo->findSuitableSlug($book->name);
82         $book->created_by = Auth::user()->id;
83         $book->updated_by = Auth::user()->id;
84         $book->save();
85         Activity::add($book, 'book_create', $book->id);
86         return redirect($book->getUrl());
87     }
88
89     /**
90      * Display the specified book.
91      *
92      * @param $slug
93      * @return Response
94      */
95     public function show($slug)
96     {
97         $book = $this->bookRepo->getBySlug($slug);
98         $bookChildren = $this->bookRepo->getChildren($book);
99         Views::add($book);
100         $this->setPageTitle($book->getShortName());
101         return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
102     }
103
104     /**
105      * Show the form for editing the specified book.
106      *
107      * @param $slug
108      * @return Response
109      */
110     public function edit($slug)
111     {
112         $book = $this->bookRepo->getBySlug($slug);
113         $this->checkOwnablePermission('book-update', $book);
114         $this->setPageTitle('Edit Book ' . $book->getShortName());
115         return view('books/edit', ['book' => $book, 'current' => $book]);
116     }
117
118     /**
119      * Update the specified book in storage.
120      *
121      * @param  Request $request
122      * @param          $slug
123      * @return Response
124      */
125     public function update(Request $request, $slug)
126     {
127         $book = $this->bookRepo->getBySlug($slug);
128         $this->checkOwnablePermission('book-update', $book);
129         $this->validate($request, [
130             'name' => 'required|string|max:255',
131             'description' => 'string|max:1000'
132         ]);
133         $book->fill($request->all());
134         $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
135         $book->updated_by = Auth::user()->id;
136         $book->save();
137         Activity::add($book, 'book_update', $book->id);
138         return redirect($book->getUrl());
139     }
140
141     /**
142      * Shows the page to confirm deletion
143      * @param $bookSlug
144      * @return \Illuminate\View\View
145      */
146     public function showDelete($bookSlug)
147     {
148         $book = $this->bookRepo->getBySlug($bookSlug);
149         $this->checkOwnablePermission('book-delete', $book);
150         $this->setPageTitle('Delete Book ' . $book->getShortName());
151         return view('books/delete', ['book' => $book, 'current' => $book]);
152     }
153
154     /**
155      * Shows the view which allows pages to be re-ordered and sorted.
156      * @param string $bookSlug
157      * @return \Illuminate\View\View
158      */
159     public function sort($bookSlug)
160     {
161         $book = $this->bookRepo->getBySlug($bookSlug);
162         $this->checkOwnablePermission('book-update', $book);
163         $bookChildren = $this->bookRepo->getChildren($book);
164         $books = $this->bookRepo->getAll(false);
165         $this->setPageTitle('Sort Book ' . $book->getShortName());
166         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
167     }
168
169     /**
170      * Shows the sort box for a single book.
171      * Used via AJAX when loading in extra books to a sort.
172      * @param $bookSlug
173      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
174      */
175     public function getSortItem($bookSlug)
176     {
177         $book = $this->bookRepo->getBySlug($bookSlug);
178         $bookChildren = $this->bookRepo->getChildren($book);
179         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
180     }
181
182     /**
183      * Saves an array of sort mapping to pages and chapters.
184      * @param  string $bookSlug
185      * @param Request $request
186      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
187      */
188     public function saveSort($bookSlug, Request $request)
189     {
190         $book = $this->bookRepo->getBySlug($bookSlug);
191         $this->checkOwnablePermission('book-update', $book);
192
193         // Return if no map sent
194         if (!$request->has('sort-tree')) {
195             return redirect($book->getUrl());
196         }
197
198         $sortedBooks = [];
199         // Sort pages and chapters
200         $sortMap = json_decode($request->get('sort-tree'));
201         $defaultBookId = $book->id;
202         foreach ($sortMap as $index => $bookChild) {
203             $id = $bookChild->id;
204             $isPage = $bookChild->type == 'page';
205             $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
206             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
207             $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
208             $model->priority = $index;
209             if ($isPage) {
210                 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
211             }
212             $model->save();
213             if (!in_array($bookId, $sortedBooks)) {
214                 $sortedBooks[] = $bookId;
215             }
216         }
217
218         // Add activity for books
219         foreach ($sortedBooks as $bookId) {
220             $updatedBook = $this->bookRepo->getById($bookId);
221             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
222         }
223
224         return redirect($book->getUrl());
225     }
226
227     /**
228      * Remove the specified book from storage.
229      * @param $bookSlug
230      * @return Response
231      */
232     public function destroy($bookSlug)
233     {
234         $book = $this->bookRepo->getBySlug($bookSlug);
235         $this->checkOwnablePermission('book-delete', $book);
236         Activity::addMessage('book_delete', 0, $book->name);
237         Activity::removeEntity($book);
238         $this->bookRepo->destroyBySlug($bookSlug);
239         return redirect('/books');
240     }
241
242     /**
243      * Show the Restrictions view.
244      * @param $bookSlug
245      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
246      */
247     public function showRestrict($bookSlug)
248     {
249         $book = $this->bookRepo->getBySlug($bookSlug);
250         $this->checkOwnablePermission('restrictions-manage', $book);
251         $roles = $this->userRepo->getRestrictableRoles();
252         return view('books/restrictions', [
253             'book' => $book,
254             'roles' => $roles
255         ]);
256     }
257
258     /**
259      * Set the restrictions for this book.
260      * @param $bookSlug
261      * @param $bookSlug
262      * @param Request $request
263      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
264      */
265     public function restrict($bookSlug, Request $request)
266     {
267         $book = $this->bookRepo->getBySlug($bookSlug);
268         $this->checkOwnablePermission('restrictions-manage', $book);
269         $this->bookRepo->updateRestrictionsFromRequest($request, $book);
270         session()->flash('success', 'Page Restrictions Updated');
271         return redirect($book->getUrl());
272     }
273 }