]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Started work towards adding role view permissions
[bookstack] / app / Http / Controllers / ChapterController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request;
6 use BookStack\Http\Requests;
7 use BookStack\Repos\BookRepo;
8 use BookStack\Repos\ChapterRepo;
9 use Views;
10
11 class ChapterController extends Controller
12 {
13
14     protected $bookRepo;
15     protected $chapterRepo;
16     protected $userRepo;
17
18     /**
19      * ChapterController constructor.
20      * @param BookRepo $bookRepo
21      * @param ChapterRepo $chapterRepo
22      * @param UserRepo $userRepo
23      */
24     public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
25     {
26         $this->bookRepo = $bookRepo;
27         $this->chapterRepo = $chapterRepo;
28         $this->userRepo = $userRepo;
29         parent::__construct();
30     }
31
32     /**
33      * Show the form for creating a new chapter.
34      * @param $bookSlug
35      * @return Response
36      */
37     public function create($bookSlug)
38     {
39         $book = $this->bookRepo->getBySlug($bookSlug);
40         $this->checkOwnablePermission('chapter-create', $book);
41         $this->setPageTitle('Create New Chapter');
42         return view('chapters/create', ['book' => $book, 'current' => $book]);
43     }
44
45     /**
46      * Store a newly created chapter in storage.
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         $this->checkOwnablePermission('chapter-create', $book);
59
60         $chapter = $this->chapterRepo->newFromInput($request->all());
61         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
62         $chapter->priority = $this->bookRepo->getNewPriority($book);
63         $chapter->created_by = auth()->user()->id;
64         $chapter->updated_by = auth()->user()->id;
65         $book->chapters()->save($chapter);
66         Activity::add($chapter, 'chapter_create', $book->id);
67         return redirect($chapter->getUrl());
68     }
69
70     /**
71      * Display the specified chapter.
72      * @param $bookSlug
73      * @param $chapterSlug
74      * @return Response
75      */
76     public function show($bookSlug, $chapterSlug)
77     {
78         $book = $this->bookRepo->getBySlug($bookSlug);
79         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
80         $this->checkOwnablePermission('chapter-view', $chapter);
81         $sidebarTree = $this->bookRepo->getChildren($book);
82         Views::add($chapter);
83         $this->setPageTitle($chapter->getShortName());
84         $pages = $this->chapterRepo->getChildren($chapter);
85         return view('chapters/show', [
86             'book' => $book,
87             'chapter' => $chapter,
88             'current' => $chapter,
89             'sidebarTree' => $sidebarTree,
90             'pages' => $pages
91         ]);
92     }
93
94     /**
95      * Show the form for editing the specified chapter.
96      * @param $bookSlug
97      * @param $chapterSlug
98      * @return Response
99      */
100     public function edit($bookSlug, $chapterSlug)
101     {
102         $book = $this->bookRepo->getBySlug($bookSlug);
103         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
104         $this->checkOwnablePermission('chapter-update', $chapter);
105         $this->setPageTitle('Edit Chapter' . $chapter->getShortName());
106         return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
107     }
108
109     /**
110      * Update the specified chapter in storage.
111      * @param  Request $request
112      * @param          $bookSlug
113      * @param          $chapterSlug
114      * @return Response
115      */
116     public function update(Request $request, $bookSlug, $chapterSlug)
117     {
118         $book = $this->bookRepo->getBySlug($bookSlug);
119         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
120         $this->checkOwnablePermission('chapter-update', $chapter);
121         $chapter->fill($request->all());
122         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
123         $chapter->updated_by = auth()->user()->id;
124         $chapter->save();
125         Activity::add($chapter, 'chapter_update', $book->id);
126         return redirect($chapter->getUrl());
127     }
128
129     /**
130      * Shows the page to confirm deletion of this chapter.
131      * @param $bookSlug
132      * @param $chapterSlug
133      * @return \Illuminate\View\View
134      */
135     public function showDelete($bookSlug, $chapterSlug)
136     {
137         $book = $this->bookRepo->getBySlug($bookSlug);
138         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
139         $this->checkOwnablePermission('chapter-delete', $chapter);
140         $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
141         return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
142     }
143
144     /**
145      * Remove the specified chapter from storage.
146      * @param $bookSlug
147      * @param $chapterSlug
148      * @return Response
149      */
150     public function destroy($bookSlug, $chapterSlug)
151     {
152         $book = $this->bookRepo->getBySlug($bookSlug);
153         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
154         $this->checkOwnablePermission('chapter-delete', $chapter);
155         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
156         $this->chapterRepo->destroy($chapter);
157         return redirect($book->getUrl());
158     }
159
160     /**
161      * Show the Restrictions view.
162      * @param $bookSlug
163      * @param $chapterSlug
164      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
165      */
166     public function showRestrict($bookSlug, $chapterSlug)
167     {
168         $book = $this->bookRepo->getBySlug($bookSlug);
169         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
170         $this->checkOwnablePermission('restrictions-manage', $chapter);
171         $roles = $this->userRepo->getRestrictableRoles();
172         return view('chapters/restrictions', [
173             'chapter' => $chapter,
174             'roles' => $roles
175         ]);
176     }
177
178     /**
179      * Set the restrictions for this chapter.
180      * @param $bookSlug
181      * @param $chapterSlug
182      * @param Request $request
183      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
184      */
185     public function restrict($bookSlug, $chapterSlug, Request $request)
186     {
187         $book = $this->bookRepo->getBySlug($bookSlug);
188         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
189         $this->checkOwnablePermission('restrictions-manage', $chapter);
190         $this->chapterRepo->updateRestrictionsFromRequest($request, $chapter);
191         session()->flash('success', 'Chapter Restrictions Updated');
192         return redirect($chapter->getUrl());
193     }
194 }