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