]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Added Popular books list with relevant tests
[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(4, 0) : false;
46         $popular = $this->bookRepo->getPopular(4, 0);
47         return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
48     }
49
50     /**
51      * Show the form for creating a new book.
52      *
53      * @return Response
54      */
55     public function create()
56     {
57         $this->checkPermission('book-create');
58         return view('books/create');
59     }
60
61     /**
62      * Store a newly created book in storage.
63      *
64      * @param  Request $request
65      * @return Response
66      */
67     public function store(Request $request)
68     {
69         $this->checkPermission('book-create');
70         $this->validate($request, [
71             'name'        => 'required|string|max:255',
72             'description' => 'string|max:1000'
73         ]);
74         $book = $this->bookRepo->newFromInput($request->all());
75         $book->slug = $this->bookRepo->findSuitableSlug($book->name);
76         $book->created_by = Auth::user()->id;
77         $book->updated_by = Auth::user()->id;
78         $book->save();
79         Activity::add($book, 'book_create', $book->id);
80         return redirect($book->getUrl());
81     }
82
83     /**
84      * Display the specified book.
85      *
86      * @param $slug
87      * @return Response
88      */
89     public function show($slug)
90     {
91         $book = $this->bookRepo->getBySlug($slug);
92         Views::add($book);
93         $bookChildren = $this->bookRepo->getChildren($book);
94         return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
95     }
96
97     /**
98      * Show the form for editing the specified book.
99      *
100      * @param $slug
101      * @return Response
102      */
103     public function edit($slug)
104     {
105         $this->checkPermission('book-update');
106         $book = $this->bookRepo->getBySlug($slug);
107         return view('books/edit', ['book' => $book, 'current' => $book]);
108     }
109
110     /**
111      * Update the specified book in storage.
112      *
113      * @param  Request $request
114      * @param          $slug
115      * @return Response
116      */
117     public function update(Request $request, $slug)
118     {
119         $this->checkPermission('book-update');
120         $book = $this->bookRepo->getBySlug($slug);
121         $this->validate($request, [
122             'name'        => 'required|string|max:255',
123             'description' => 'string|max:1000'
124         ]);
125         $book->fill($request->all());
126         $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
127         $book->updated_by = Auth::user()->id;
128         $book->save();
129         Activity::add($book, 'book_update', $book->id);
130         return redirect($book->getUrl());
131     }
132
133     /**
134      * Shows the page to confirm deletion
135      * @param $bookSlug
136      * @return \Illuminate\View\View
137      */
138     public function showDelete($bookSlug)
139     {
140         $this->checkPermission('book-delete');
141         $book = $this->bookRepo->getBySlug($bookSlug);
142         return view('books/delete', ['book' => $book, 'current' => $book]);
143     }
144
145     /**
146      * Shows the view which allows pages to be re-ordered and sorted.
147      * @param string $bookSlug
148      * @return \Illuminate\View\View
149      */
150     public function sort($bookSlug)
151     {
152         $this->checkPermission('book-update');
153         $book = $this->bookRepo->getBySlug($bookSlug);
154         $bookChildren = $this->bookRepo->getChildren($book);
155         $books = $this->bookRepo->getAll();
156         return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
157     }
158
159     public function getSortItem($bookSlug)
160     {
161         $book = $this->bookRepo->getBySlug($bookSlug);
162         $bookChildren = $this->bookRepo->getChildren($book);
163         return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
164     }
165
166     /**
167      * Saves an array of sort mapping to pages and chapters.
168      *
169      * @param  string $bookSlug
170      * @param Request $request
171      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
172      */
173     public function saveSort($bookSlug, Request $request)
174     {
175         $this->checkPermission('book-update');
176         $book = $this->bookRepo->getBySlug($bookSlug);
177
178         // Return if no map sent
179         if (!$request->has('sort-tree')) {
180             return redirect($book->getUrl());
181         }
182
183         $sortedBooks = [];
184         // Sort pages and chapters
185         $sortMap = json_decode($request->get('sort-tree'));
186         $defaultBookId = $book->id;
187         foreach ($sortMap as $index => $bookChild) {
188             $id = $bookChild->id;
189             $isPage = $bookChild->type == 'page';
190             $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId;
191             $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id);
192             $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
193             $model->priority = $index;
194             if ($isPage) {
195                 $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter;
196             }
197             $model->save();
198             if (!in_array($bookId, $sortedBooks)) {
199                 $sortedBooks[] = $bookId;
200             }
201         }
202
203         // Add activity for books
204         foreach ($sortedBooks as $bookId) {
205             $updatedBook = $this->bookRepo->getById($bookId);
206             Activity::add($updatedBook, 'book_sort', $updatedBook->id);
207         }
208
209         return redirect($book->getUrl());
210     }
211
212     /**
213      * Remove the specified book from storage.
214      *
215      * @param $bookSlug
216      * @return Response
217      */
218     public function destroy($bookSlug)
219     {
220         $this->checkPermission('book-delete');
221         $book = $this->bookRepo->getBySlug($bookSlug);
222         Activity::addMessage('book_delete', 0, $book->name);
223         Activity::removeEntity($book);
224         $this->bookRepo->destroyBySlug($bookSlug);
225         return redirect('/books');
226     }
227 }