]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Implemented database structure and inital interfaces for entity restrictions
[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         $sidebarTree = $this->bookRepo->getChildren($book);
81         Views::add($chapter);
82         $this->setPageTitle($chapter->getShortName());
83         return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
84     }
85
86     /**
87      * Show the form for editing the specified chapter.
88      * @param $bookSlug
89      * @param $chapterSlug
90      * @return Response
91      */
92     public function edit($bookSlug, $chapterSlug)
93     {
94         $book = $this->bookRepo->getBySlug($bookSlug);
95         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
96         $this->checkOwnablePermission('chapter-update', $chapter);
97         $this->setPageTitle('Edit Chapter' . $chapter->getShortName());
98         return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
99     }
100
101     /**
102      * Update the specified chapter in storage.
103      * @param  Request $request
104      * @param          $bookSlug
105      * @param          $chapterSlug
106      * @return Response
107      */
108     public function update(Request $request, $bookSlug, $chapterSlug)
109     {
110         $book = $this->bookRepo->getBySlug($bookSlug);
111         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
112         $this->checkOwnablePermission('chapter-update', $chapter);
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         $book = $this->bookRepo->getBySlug($bookSlug);
130         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
131         $this->checkOwnablePermission('chapter-delete', $chapter);
132         $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
133         return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
134     }
135
136     /**
137      * Remove the specified chapter from storage.
138      * @param $bookSlug
139      * @param $chapterSlug
140      * @return Response
141      */
142     public function destroy($bookSlug, $chapterSlug)
143     {
144         $book = $this->bookRepo->getBySlug($bookSlug);
145         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
146         $this->checkOwnablePermission('chapter-delete', $chapter);
147         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
148         $this->chapterRepo->destroy($chapter);
149         return redirect($book->getUrl());
150     }
151
152     /**
153      * Show the Restrictions view.
154      * @param $bookSlug
155      * @param $chapterSlug
156      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
157      */
158     public function showRestrict($bookSlug, $chapterSlug)
159     {
160         $book = $this->bookRepo->getBySlug($bookSlug);
161         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
162         $this->checkOwnablePermission('restrictions-manage', $chapter);
163         $roles = $this->userRepo->getRestrictableRoles();
164         return view('chapters/restrictions', [
165             'chapter' => $chapter,
166             'roles' => $roles
167         ]);
168     }
169
170     /**
171      * Set the restrictions for this chapter.
172      * @param $bookSlug
173      * @param $chapterSlug
174      * @param Request $request
175      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
176      */
177     public function restrict($bookSlug, $chapterSlug, Request $request)
178     {
179         $book = $this->bookRepo->getBySlug($bookSlug);
180         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
181         $this->checkOwnablePermission('restrictions-manage', $chapter);
182         $this->chapterRepo->updateRestrictionsFromRequest($request, $chapter);
183         session()->flash('success', 'Page Restrictions Updated');
184         return redirect($chapter->getUrl());
185     }
186 }