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