]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Added password reset functionaility. Fixes #27.
[bookstack] / app / Http / Controllers / PageController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers;
4
5 use Activity;
6 use Illuminate\Http\Request;
7
8 use Illuminate\Support\Facades\Auth;
9 use Oxbow\Http\Requests;
10 use Oxbow\Repos\BookRepo;
11 use Oxbow\Repos\ChapterRepo;
12 use Oxbow\Repos\PageRepo;
13
14 class PageController extends Controller
15 {
16
17     protected $pageRepo;
18     protected $bookRepo;
19     protected $chapterRepo;
20
21     /**
22      * PageController constructor.
23      * @param PageRepo $pageRepo
24      * @param BookRepo $bookRepo
25      * @param ChapterRepo $chapterRepo
26      */
27     public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo)
28     {
29         $this->pageRepo = $pageRepo;
30         $this->bookRepo = $bookRepo;
31         $this->chapterRepo = $chapterRepo;
32     }
33
34     /**
35      * Show the form for creating a new page.
36      *
37      * @param $bookSlug
38      * @param bool $chapterSlug
39      * @return Response
40      * @internal param bool $pageSlug
41      */
42     public function create($bookSlug, $chapterSlug = false)
43     {
44         $book = $this->bookRepo->getBySlug($bookSlug);
45         $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : false;
46         return view('pages/create', ['book' => $book, 'chapter' => $chapter]);
47     }
48
49     /**
50      * Store a newly created page in storage.
51      *
52      * @param  Request $request
53      * @param $bookSlug
54      * @return Response
55      */
56     public function store(Request $request, $bookSlug)
57     {
58         $this->validate($request, [
59             'name' => 'required|string|max:255',
60             'html' => 'required|string',
61             'parent' => 'integer|exists:pages,id'
62         ]);
63         $book = $this->bookRepo->getBySlug($bookSlug);
64         $page = $this->pageRepo->newFromInput($request->all());
65
66         $page->slug = $this->pageRepo->findSuitableSlug($page->name, $book->id);
67         $page->priority = $this->bookRepo->getNewPriority($book);
68
69         if($request->has('chapter') && $this->chapterRepo->idExists($request->get('chapter'))) {
70             $page->chapter_id = $request->get('chapter');
71         }
72
73         $page->book_id = $book->id;
74         $page->text = strip_tags($page->html);
75         $page->created_by = Auth::user()->id;
76         $page->updated_by = Auth::user()->id;
77         $page->save();
78         $this->pageRepo->saveRevision($page);
79         Activity::add($page, 'page_create', $book->id);
80         return redirect($page->getUrl());
81     }
82
83     /**
84      * Display the specified page.
85      *
86      * @param $bookSlug
87      * @param $pageSlug
88      * @return Response
89      */
90     public function show($bookSlug, $pageSlug)
91     {
92         $book = $this->bookRepo->getBySlug($bookSlug);
93         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
94         return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page]);
95     }
96
97     /**
98      * Show the form for editing the specified page.
99      *
100      * @param $bookSlug
101      * @param $pageSlug
102      * @return Response
103      */
104     public function edit($bookSlug, $pageSlug)
105     {
106         $book = $this->bookRepo->getBySlug($bookSlug);
107         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
108         return view('pages/edit', ['page' => $page, 'book' => $book, 'current' => $page]);
109     }
110
111     /**
112      * Update the specified page in storage.
113      *
114      * @param  Request $request
115      * @param $bookSlug
116      * @param $pageSlug
117      * @return Response
118      */
119     public function update(Request $request, $bookSlug, $pageSlug)
120     {
121         $book = $this->bookRepo->getBySlug($bookSlug);
122         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
123         $this->pageRepo->updatePage($page, $book->id, $request->all());
124         Activity::add($page, 'page_update', $book->id);
125         return redirect($page->getUrl());
126     }
127
128     /**
129      * Redirect from a special link url which
130      * uses the page id rather than the name.
131      * @param $pageId
132      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
133      */
134     public function redirectFromLink($pageId)
135     {
136         $page = $this->pageRepo->getById($pageId);
137         return redirect($page->getUrl());
138     }
139
140     /**
141      * Search all available pages, Across all books.
142      * @param Request $request
143      * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
144      */
145     public function searchAll(Request $request)
146     {
147         $searchTerm = $request->get('term');
148         if(empty($searchTerm)) return redirect()->back();
149
150         $pages = $this->pageRepo->getBySearch($searchTerm);
151         return view('pages/search-results', ['pages' => $pages, 'searchTerm' => $searchTerm]);
152     }
153
154     /**
155      * Shows the view which allows pages to be re-ordered and sorted.
156      * @param $bookSlug
157      * @return \Illuminate\View\View
158      */
159     public function sortPages($bookSlug)
160     {
161         $book = $this->bookRepo->getBySlug($bookSlug);
162         return view('pages/sort', ['book' => $book, 'current' => $book]);
163     }
164
165     /**
166      * Saves an array of sort mapping to pages and chapters.
167      *
168      * @param $bookSlug
169      * @param Request $request
170      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
171      */
172     public function savePageSort($bookSlug, Request $request)
173     {
174         $book = $this->bookRepo->getBySlug($bookSlug);
175         // Return if no map sent
176         if(!$request->has('sort-tree')) {
177             return redirect($book->getUrl());
178         }
179
180         // Sort pages and chapters
181         $sortMap = json_decode($request->get('sort-tree'));
182         foreach($sortMap as $index => $bookChild) {
183             $id = $bookChild->id;
184             $isPage = $bookChild->type == 'page';
185             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
186             $model->priority = $index;
187             if($isPage) {
188                 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
189             }
190             $model->save();
191         }
192         Activity::add($book, 'book_sort', $book->id);
193         return redirect($book->getUrl());
194     }
195
196     /**
197      * Show the deletion page for the specified page.
198      * @param $bookSlug
199      * @param $pageSlug
200      * @return \Illuminate\View\View
201      */
202     public function showDelete($bookSlug, $pageSlug)
203     {
204         $book = $this->bookRepo->getBySlug($bookSlug);
205         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
206         return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]);
207     }
208
209     /**
210      * Remove the specified page from storage.
211      *
212      * @param $bookSlug
213      * @param $pageSlug
214      * @return Response
215      * @internal param int $id
216      */
217     public function destroy($bookSlug, $pageSlug)
218     {
219         $book = $this->bookRepo->getBySlug($bookSlug);
220         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
221         Activity::addMessage('page_delete', $book->id, $page->name);
222         Activity::removeEntity($page);
223         $page->delete();
224         return redirect($book->getUrl());
225     }
226
227     /**
228      * Shows the last revisions for this page.
229      * @param $bookSlug
230      * @param $pageSlug
231      * @return \Illuminate\View\View
232      */
233     public function showRevisions($bookSlug, $pageSlug)
234     {
235         $book = $this->bookRepo->getBySlug($bookSlug);
236         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
237         return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]);
238     }
239
240     /**
241      * Shows a preview of a single revision
242      * @param $bookSlug
243      * @param $pageSlug
244      * @param $revisionId
245      * @return \Illuminate\View\View
246      */
247     public function showRevision($bookSlug, $pageSlug, $revisionId)
248     {
249         $book = $this->bookRepo->getBySlug($bookSlug);
250         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
251         $revision = $this->pageRepo->getRevisionById($revisionId);
252         $page->fill($revision->toArray());
253         return view('pages/revision', ['page' => $page, 'book' => $book]);
254     }
255
256     public function restoreRevision($bookSlug, $pageSlug, $revisionId)
257     {
258         $book = $this->bookRepo->getBySlug($bookSlug);
259         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
260         $revision = $this->pageRepo->getRevisionById($revisionId);
261         $page = $this->pageRepo->updatePage($page, $book->id, $revision->toArray());
262         Activity::add($page, 'page_restore', $book->id);
263         return redirect($page->getUrl());
264     }
265 }