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