]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Added further tests, Fixed speed_update issues, improved search result query count
[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         $bookChildren = $this->bookRepo->getChildren($book);
93         return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
94     }
95
96     /**
97      * Show the form for editing the specified book.
98      *
99      * @param $slug
100      * @return Response
101      */
102     public function edit($slug)
103     {
104         $this->checkPermission('book-update');
105         $book = $this->bookRepo->getBySlug($slug);
106         return view('books/edit', ['book' => $book, 'current' => $book]);
107     }
108
109     /**
110      * Update the specified book in storage.
111      *
112      * @param  Request $request
113      * @param          $slug
114      * @return Response
115      */
116     public function update(Request $request, $slug)
117     {
118         $this->checkPermission('book-update');
119         $book = $this->bookRepo->getBySlug($slug);
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         $this->checkPermission('book-delete');
140         $book = $this->bookRepo->getBySlug($bookSlug);
141         return view('books/delete', ['book' => $book, 'current' => $book]);
142     }
143
144     /**
145      * Shows the view which allows pages to be re-ordered and sorted.
146      * @param string $bookSlug
147      * @return \Illuminate\View\View
148      */
149     public function sort($bookSlug)
150     {
151         $this->checkPermission('book-update');
152         $book = $this->bookRepo->getBySlug($bookSlug);
153         $bookChildren = $this->bookRepo->getChildren($book);
154         $books = $this->bookRepo->getAll();
155         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
156     }
157
158     public function getSortItem($bookSlug)
159     {
160         $book = $this->bookRepo->getBySlug($bookSlug);
161         $bookChildren = $this->bookRepo->getChildren($book);
162         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
163     }
164
165     /**
166      * Saves an array of sort mapping to pages and chapters.
167      *
168      * @param  string $bookSlug
169      * @param Request $request
170      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
171      */
172     public function saveSort($bookSlug, Request $request)
173     {
174         $this->checkPermission('book-update');
175         $book = $this->bookRepo->getBySlug($bookSlug);
176
177         // Return if no map sent
178         if (!$request->has('sort-tree')) {
179             return redirect($book->getUrl());
180         }
181
182         $sortedBooks = [];
183         // Sort pages and chapters
184         $sortMap = json_decode($request->get('sort-tree'));
185         $defaultBookId = $book->id;
186         foreach ($sortMap as $index => $bookChild) {
187             $id = $bookChild->id;
188             $isPage = $bookChild->type == 'page';
189             $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
190             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
191             $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
192             $model->priority = $index;
193             if ($isPage) {
194                 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
195             }
196             $model->save();
197             if (!in_array($bookId, $sortedBooks)) {
198                 $sortedBooks[] = $bookId;
199             }
200         }
201
202         // Add activity for books
203         foreach ($sortedBooks as $bookId) {
204             $updatedBook = $this->bookRepo->getById($bookId);
205             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
206         }
207
208         return redirect($book->getUrl());
209     }
210
211     /**
212      * Remove the specified book from storage.
213      *
214      * @param $bookSlug
215      * @return Response
216      */
217     public function destroy($bookSlug)
218     {
219         $this->checkPermission('book-delete');
220         $book = $this->bookRepo->getBySlug($bookSlug);
221         Activity::addMessage('book_delete', 0, $book->name);
222         Activity::removeEntity($book);
223         $this->bookRepo->destroyBySlug($bookSlug);
224         return redirect('/books');
225     }
226 }