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