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