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