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