]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Added further tests, Fixed speed_update issues, improved search result query count
[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      * @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      * @param          $bookSlug
49      * @param  Request $request
50      * @return Response
51      */
52     public function store($bookSlug, Request $request)
53     {
54         $this->checkPermission('chapter-create');
55         $this->validate($request, [
56             'name' => 'required|string|max:255'
57         ]);
58
59         $book = $this->bookRepo->getBySlug($bookSlug);
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         return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
83     }
84
85     /**
86      * Show the form for editing the specified chapter.
87      * @param $bookSlug
88      * @param $chapterSlug
89      * @return Response
90      */
91     public function edit($bookSlug, $chapterSlug)
92     {
93         $this->checkPermission('chapter-update');
94         $book = $this->bookRepo->getBySlug($bookSlug);
95         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
96         return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
97     }
98
99     /**
100      * Update the specified chapter in storage.
101      * @param  Request $request
102      * @param          $bookSlug
103      * @param          $chapterSlug
104      * @return Response
105      */
106     public function update(Request $request, $bookSlug, $chapterSlug)
107     {
108         $this->checkPermission('chapter-update');
109         $book = $this->bookRepo->getBySlug($bookSlug);
110         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
111         $chapter->fill($request->all());
112         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
113         $chapter->updated_by = auth()->user()->id;
114         $chapter->save();
115         Activity::add($chapter, 'chapter_update', $book->id);
116         return redirect($chapter->getUrl());
117     }
118
119     /**
120      * Shows the page to confirm deletion of this chapter.
121      * @param $bookSlug
122      * @param $chapterSlug
123      * @return \Illuminate\View\View
124      */
125     public function showDelete($bookSlug, $chapterSlug)
126     {
127         $this->checkPermission('chapter-delete');
128         $book = $this->bookRepo->getBySlug($bookSlug);
129         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
130         return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
131     }
132
133     /**
134      * Remove the specified chapter from storage.
135      * @param $bookSlug
136      * @param $chapterSlug
137      * @return Response
138      */
139     public function destroy($bookSlug, $chapterSlug)
140     {
141         $this->checkPermission('chapter-delete');
142         $book = $this->bookRepo->getBySlug($bookSlug);
143         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
144         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
145         $this->chapterRepo->destroy($chapter);
146         return redirect($book->getUrl());
147     }
148 }