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