]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/BookController.php
Tweaked some styles and started automated testing. Fixes #11.
[bookstack] / app / Http / Controllers / BookController.php
1 <?php
2
3 namespace Oxbow\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 Oxbow\Http\Requests;
11 use Oxbow\Repos\BookRepo;
12 use Oxbow\Repos\PageRepo;
13
14 class BookController extends Controller
15 {
16
17     protected $bookRepo;
18     protected $pageRepo;
19
20     /**
21      * BookController constructor.
22      * @param BookRepo $bookRepo
23      * @param PageRepo $pageRepo
24      */
25     public function __construct(BookRepo $bookRepo, PageRepo $pageRepo)
26     {
27         $this->bookRepo = $bookRepo;
28         $this->pageRepo = $pageRepo;
29         parent::__construct();
30     }
31
32     /**
33      * Display a listing of the book.
34      *
35      * @return Response
36      */
37     public function index()
38     {
39         $books = $this->bookRepo->getAll();
40         return view('books/index', ['books' => $books]);
41     }
42
43     /**
44      * Show the form for creating a new book.
45      *
46      * @return Response
47      */
48     public function create()
49     {
50         $this->checkPermission('book-create');
51         return view('books/create');
52     }
53
54     /**
55      * Store a newly created book in storage.
56      *
57      * @param  Request $request
58      * @return Response
59      */
60     public function store(Request $request)
61     {
62         $this->checkPermission('book-create');
63         $this->validate($request, [
64             'name'        => 'required|string|max:255',
65             'description' => 'string|max:1000'
66         ]);
67         $book = $this->bookRepo->newFromInput($request->all());
68         $book->slug = $this->bookRepo->findSuitableSlug($book->name);
69         $book->created_by = Auth::user()->id;
70         $book->updated_by = Auth::user()->id;
71         $book->save();
72         Activity::add($book, 'book_create', $book->id);
73         return redirect($book->getUrl());
74     }
75
76     /**
77      * Display the specified book.
78      *
79      * @param $slug
80      * @return Response
81      */
82     public function show($slug)
83     {
84         $book = $this->bookRepo->getBySlug($slug);
85         return view('books/show', ['book' => $book, 'current' => $book]);
86     }
87
88     /**
89      * Show the form for editing the specified book.
90      *
91      * @param $slug
92      * @return Response
93      */
94     public function edit($slug)
95     {
96         $this->checkPermission('book-update');
97         $book = $this->bookRepo->getBySlug($slug);
98         return view('books/edit', ['book' => $book, 'current' => $book]);
99     }
100
101     /**
102      * Update the specified book in storage.
103      *
104      * @param  Request $request
105      * @param          $slug
106      * @return Response
107      */
108     public function update(Request $request, $slug)
109     {
110         $this->checkPermission('book-update');
111         $book = $this->bookRepo->getBySlug($slug);
112         $this->validate($request, [
113             'name'        => 'required|string|max:255',
114             'description' => 'string|max:1000'
115         ]);
116         $book->fill($request->all());
117         $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
118         $book->updated_by = Auth::user()->id;
119         $book->save();
120         Activity::add($book, 'book_update', $book->id);
121         return redirect($book->getUrl());
122     }
123
124     /**
125      * Shows the page to confirm deletion
126      * @param $bookSlug
127      * @return \Illuminate\View\View
128      */
129     public function showDelete($bookSlug)
130     {
131         $this->checkPermission('book-delete');
132         $book = $this->bookRepo->getBySlug($bookSlug);
133         return view('books/delete', ['book' => $book, 'current' => $book]);
134     }
135
136     /**
137      * Remove the specified book from storage.
138      *
139      * @param $bookSlug
140      * @return Response
141      */
142     public function destroy($bookSlug)
143     {
144         $this->checkPermission('book-delete');
145         $book = $this->bookRepo->getBySlug($bookSlug);
146         Activity::addMessage('book_delete', 0, $book->name);
147         $this->bookRepo->destroyBySlug($bookSlug);
148         return redirect('/books');
149     }
150 }