]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageController.php
Added created_by and updated_by to entities. Fixes #3
[bookstack] / app / Http / Controllers / PageController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers;
4
5 use Illuminate\Http\Request;
6
7 use Illuminate\Support\Facades\Auth;
8 use Illuminate\Support\Str;
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     /**
36      * Display a listing of the resource.
37      *
38      * @return Response
39      */
40     public function index()
41     {
42         //
43     }
44
45     /**
46      * Show the form for creating a new resource.
47      *
48      * @param $bookSlug
49      * @param bool $chapterSlug
50      * @return Response
51      * @internal param bool $pageSlug
52      */
53     public function create($bookSlug, $chapterSlug = false)
54     {
55         $book = $this->bookRepo->getBySlug($bookSlug);
56         $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : false;
57         return view('pages/create', ['book' => $book, 'chapter' => $chapter]);
58     }
59
60     /**
61      * Store a newly created resource in storage.
62      *
63      * @param  Request $request
64      * @param $bookSlug
65      * @return Response
66      */
67     public function store(Request $request, $bookSlug)
68     {
69         $this->validate($request, [
70             'name' => 'required|string|max:255',
71             'html' => 'required|string',
72             'parent' => 'integer|exists:pages,id'
73         ]);
74         $book = $this->bookRepo->getBySlug($bookSlug);
75         $page = $this->pageRepo->newFromInput($request->all());
76
77         $page->slug = $this->pageRepo->findSuitableSlug($page->name, $book->id);
78         $page->priority = $this->bookRepo->getNewPriority($book);
79
80         if($request->has('chapter') && $this->chapterRepo->idExists($request->get('chapter'))) {
81             $page->chapter_id = $request->get('chapter');
82         }
83
84         $page->book_id = $book->id;
85         $page->text = strip_tags($page->html);
86         $page->created_by = Auth::user()->id;
87         $page->updated_by = Auth::user()->id;
88         $page->save();
89         return redirect($page->getUrl());
90     }
91
92     /**
93      * Display the specified resource.
94      *
95      * @param $bookSlug
96      * @param $pageSlug
97      * @return Response
98      */
99     public function show($bookSlug, $pageSlug)
100     {
101         $book = $this->bookRepo->getBySlug($bookSlug);
102         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
103         //dd($sidebarBookTree);
104         return view('pages/show', ['page' => $page, 'book' => $book]);
105     }
106
107     /**
108      * Show the form for editing the specified resource.
109      *
110      * @param $bookSlug
111      * @param $pageSlug
112      * @return Response
113      */
114     public function edit($bookSlug, $pageSlug)
115     {
116         $book = $this->bookRepo->getBySlug($bookSlug);
117         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
118         return view('pages/edit', ['page' => $page, 'book' => $book]);
119     }
120
121     /**
122      * Update the specified resource in storage.
123      *
124      * @param  Request $request
125      * @param $bookSlug
126      * @param $pageSlug
127      * @return Response
128      */
129     public function update(Request $request, $bookSlug, $pageSlug)
130     {
131         $book = $this->bookRepo->getBySlug($bookSlug);
132         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
133         $page->fill($request->all());
134         $page->slug = $this->pageRepo->findSuitableSlug($page->name, $book->id, $page->id);
135         $page->text = strip_tags($page->html);
136         $page->updated_by = Auth::user()->id;
137         $page->save();
138         return redirect($page->getUrl());
139     }
140
141     /**
142      * Redirect from a special link url which
143      * uses the page id rather than the name.
144      * @param $pageId
145      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
146      */
147     public function redirectFromLink($pageId)
148     {
149         $page = $this->pageRepo->getById($pageId);
150         return redirect($page->getUrl());
151     }
152
153     /**
154      * Search all available pages, Across all books.
155      * @param Request $request
156      * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
157      */
158     public function searchAll(Request $request)
159     {
160         $searchTerm = $request->get('term');
161         if(empty($searchTerm)) return redirect()->back();
162
163         $pages = $this->pageRepo->getBySearch($searchTerm);
164         return view('pages/search-results', ['pages' => $pages, 'searchTerm' => $searchTerm]);
165     }
166
167     /**
168      * Shows the view which allows pages to be re-ordered and sorted.
169      * @param $bookSlug
170      * @return \Illuminate\View\View
171      */
172     public function sortPages($bookSlug)
173     {
174         $book = $this->bookRepo->getBySlug($bookSlug);
175         return view('pages/sort', ['book' => $book]);
176     }
177
178     /**
179      * Saves an array of sort mapping to pages and chapters.
180      *
181      * @param $bookSlug
182      * @param Request $request
183      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
184      */
185     public function savePageSort($bookSlug, Request $request)
186     {
187         $book = $this->bookRepo->getBySlug($bookSlug);
188         // Return if no map sent
189         if(!$request->has('sort-tree')) {
190             return redirect($book->getUrl());
191         }
192
193         // Sort pages and chapters
194         $sortMap = json_decode($request->get('sort-tree'));
195         foreach($sortMap as $index => $bookChild) {
196             $id = $bookChild->id;
197             $isPage = $bookChild->type == 'page';
198             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
199             $model->priority = $index;
200             if($isPage) {
201                 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
202             }
203             $model->save();
204         }
205         return redirect($book->getUrl());
206     }
207
208     public function showDelete($bookSlug, $pageSlug)
209     {
210         $book = $this->bookRepo->getBySlug($bookSlug);
211         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
212         return view('pages/delete', ['book' => $book, 'page' => $page]);
213     }
214
215     /**
216      * Remove the specified resource from storage.
217      *
218      * @param $bookSlug
219      * @param $pageSlug
220      * @return Response
221      * @internal param int $id
222      */
223     public function destroy($bookSlug, $pageSlug)
224     {
225         $book = $this->bookRepo->getBySlug($bookSlug);
226         $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
227         $page->delete();
228         return redirect($book->getUrl());
229     }
230 }