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