]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
e7ade577e0e3325a35ac93523b59480179c4efe4
[bookstack] / app / Http / Controllers / BookController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use Activity;
6 use Illuminate\Http\Request;
7
8 use Illuminate\Support\Facades\Auth;
9 use Illuminate\Support\Str;
10 use BookStack\Http\Requests;
11 use BookStack\Repos\BookRepo;
12 use BookStack\Repos\ChapterRepo;
13 use BookStack\Repos\PageRepo;
14 use Views;
15
16 class BookController extends Controller
17 {
18
19     protected $bookRepo;
20     protected $pageRepo;
21     protected $chapterRepo;
22
23     /**
24      * BookController constructor.
25      * @param BookRepo    $bookRepo
26      * @param PageRepo    $pageRepo
27      * @param ChapterRepo $chapterRepo
28      */
29     public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo)
30     {
31         $this->bookRepo = $bookRepo;
32         $this->pageRepo = $pageRepo;
33         $this->chapterRepo = $chapterRepo;
34         parent::__construct();
35     }
36
37     /**
38      * Display a listing of the book.
39      *
40      * @return Response
41      */
42     public function index()
43     {
44         $books = $this->bookRepo->getAllPaginated(10);
45         $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(10, 0) : false;
46         return view('books/index', ['books' => $books, 'recents' => $recents]);
47     }
48
49     /**
50      * Show the form for creating a new book.
51      *
52      * @return Response
53      */
54     public function create()
55     {
56         $this->checkPermission('book-create');
57         return view('books/create');
58     }
59
60     /**
61      * Store a newly created book in storage.
62      *
63      * @param  Request $request
64      * @return Response
65      */
66     public function store(Request $request)
67     {
68         $this->checkPermission('book-create');
69         $this->validate($request, [
70             'name'        => 'required|string|max:255',
71             'description' => 'string|max:1000'
72         ]);
73         $book = $this->bookRepo->newFromInput($request->all());
74         $book->slug = $this->bookRepo->findSuitableSlug($book->name);
75         $book->created_by = Auth::user()->id;
76         $book->updated_by = Auth::user()->id;
77         $book->save();
78         Activity::add($book, 'book_create', $book->id);
79         return redirect($book->getUrl());
80     }
81
82     /**
83      * Display the specified book.
84      *
85      * @param $slug
86      * @return Response
87      */
88     public function show($slug)
89     {
90         $book = $this->bookRepo->getBySlug($slug);
91         Views::add($book);
92         return view('books/show', ['book' => $book, 'current' => $book]);
93     }
94
95     /**
96      * Show the form for editing the specified book.
97      *
98      * @param $slug
99      * @return Response
100      */
101     public function edit($slug)
102     {
103         $this->checkPermission('book-update');
104         $book = $this->bookRepo->getBySlug($slug);
105         return view('books/edit', ['book' => $book, 'current' => $book]);
106     }
107
108     /**
109      * Update the specified book in storage.
110      *
111      * @param  Request $request
112      * @param          $slug
113      * @return Response
114      */
115     public function update(Request $request, $slug)
116     {
117         $this->checkPermission('book-update');
118         $book = $this->bookRepo->getBySlug($slug);
119         $this->validate($request, [
120             'name'        => 'required|string|max:255',
121             'description' => 'string|max:1000'
122         ]);
123         $book->fill($request->all());
124         $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
125         $book->updated_by = Auth::user()->id;
126         $book->save();
127         Activity::add($book, 'book_update', $book->id);
128         return redirect($book->getUrl());
129     }
130
131     /**
132      * Shows the page to confirm deletion
133      * @param $bookSlug
134      * @return \Illuminate\View\View
135      */
136     public function showDelete($bookSlug)
137     {
138         $this->checkPermission('book-delete');
139         $book = $this->bookRepo->getBySlug($bookSlug);
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         $this->checkPermission('book-update');
151         $book = $this->bookRepo->getBySlug($bookSlug);
152         $books = $this->bookRepo->getAll();
153         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books]);
154     }
155
156     public function getSortItem($bookSlug)
157     {
158         $book = $this->bookRepo->getBySlug($bookSlug);
159         return view('books/sort-box', ['book' => $book]);
160     }
161
162     /**
163      * Saves an array of sort mapping to pages and chapters.
164      *
165      * @param  string $bookSlug
166      * @param Request $request
167      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
168      */
169     public function saveSort($bookSlug, Request $request)
170     {
171         $this->checkPermission('book-update');
172         $book = $this->bookRepo->getBySlug($bookSlug);
173
174         // Return if no map sent
175         if (!$request->has('sort-tree')) {
176             return redirect($book->getUrl());
177         }
178
179         $sortedBooks = [];
180         // Sort pages and chapters
181         $sortMap = json_decode($request->get('sort-tree'));
182         $defaultBookId = $book->id;
183         foreach ($sortMap as $index => $bookChild) {
184             $id = $bookChild->id;
185             $isPage = $bookChild->type == 'page';
186             $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
187             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
188             $isPage ? $this->pageRepo->setBookId($bookId, $model) : $this->chapterRepo->setBookId($bookId, $model);
189             $model->priority = $index;
190             if ($isPage) {
191                 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
192             }
193             $model->save();
194             if (!in_array($bookId, $sortedBooks)) {
195                 $sortedBooks[] = $bookId;
196             }
197         }
198
199         // Add activity for books
200         foreach ($sortedBooks as $bookId) {
201             $updatedBook = $this->bookRepo->getById($bookId);
202             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
203         }
204
205         return redirect($book->getUrl());
206     }
207
208     /**
209      * Remove the specified book from storage.
210      *
211      * @param $bookSlug
212      * @return Response
213      */
214     public function destroy($bookSlug)
215     {
216         $this->checkPermission('book-delete');
217         $book = $this->bookRepo->getBySlug($bookSlug);
218         Activity::addMessage('book_delete', 0, $book->name);
219         Activity::removeEntity($book);
220         $this->bookRepo->destroyBySlug($bookSlug);
221         return redirect('/books');
222     }
223 }