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