]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ChapterController.php
Added mulit image-type compatability to manager & app and added scaled image selection
[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         $this->setPageTitle('Create New Chapter');
44         return view('chapters/create', ['book' => $book, 'current' => $book]);
45     }
46
47     /**
48      * Store a newly created chapter in storage.
49      * @param          $bookSlug
50      * @param  Request $request
51      * @return Response
52      */
53     public function store($bookSlug, Request $request)
54     {
55         $this->checkPermission('chapter-create');
56         $this->validate($request, [
57             'name' => 'required|string|max:255'
58         ]);
59
60         $book = $this->bookRepo->getBySlug($bookSlug);
61         $chapter = $this->chapterRepo->newFromInput($request->all());
62         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id);
63         $chapter->priority = $this->bookRepo->getNewPriority($book);
64         $chapter->created_by = auth()->user()->id;
65         $chapter->updated_by = auth()->user()->id;
66         $book->chapters()->save($chapter);
67         Activity::add($chapter, 'chapter_create', $book->id);
68         return redirect($chapter->getUrl());
69     }
70
71     /**
72      * Display the specified chapter.
73      * @param $bookSlug
74      * @param $chapterSlug
75      * @return Response
76      */
77     public function show($bookSlug, $chapterSlug)
78     {
79         $book = $this->bookRepo->getBySlug($bookSlug);
80         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
81         $sidebarTree = $this->bookRepo->getChildren($book);
82         Views::add($chapter);
83         $this->setPageTitle($chapter->getShortName());
84         return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]);
85     }
86
87     /**
88      * Show the form for editing the specified chapter.
89      * @param $bookSlug
90      * @param $chapterSlug
91      * @return Response
92      */
93     public function edit($bookSlug, $chapterSlug)
94     {
95         $this->checkPermission('chapter-update');
96         $book = $this->bookRepo->getBySlug($bookSlug);
97         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
98         $this->setPageTitle('Edit Chapter' . $chapter->getShortName());
99         return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
100     }
101
102     /**
103      * Update the specified chapter in storage.
104      * @param  Request $request
105      * @param          $bookSlug
106      * @param          $chapterSlug
107      * @return Response
108      */
109     public function update(Request $request, $bookSlug, $chapterSlug)
110     {
111         $this->checkPermission('chapter-update');
112         $book = $this->bookRepo->getBySlug($bookSlug);
113         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
114         $chapter->fill($request->all());
115         $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id);
116         $chapter->updated_by = auth()->user()->id;
117         $chapter->save();
118         Activity::add($chapter, 'chapter_update', $book->id);
119         return redirect($chapter->getUrl());
120     }
121
122     /**
123      * Shows the page to confirm deletion of this chapter.
124      * @param $bookSlug
125      * @param $chapterSlug
126      * @return \Illuminate\View\View
127      */
128     public function showDelete($bookSlug, $chapterSlug)
129     {
130         $this->checkPermission('chapter-delete');
131         $book = $this->bookRepo->getBySlug($bookSlug);
132         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
133         $this->setPageTitle('Delete Chapter' . $chapter->getShortName());
134         return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]);
135     }
136
137     /**
138      * Remove the specified chapter from storage.
139      * @param $bookSlug
140      * @param $chapterSlug
141      * @return Response
142      */
143     public function destroy($bookSlug, $chapterSlug)
144     {
145         $this->checkPermission('chapter-delete');
146         $book = $this->bookRepo->getBySlug($bookSlug);
147         $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
148         Activity::addMessage('chapter_delete', $book->id, $chapter->name);
149         $this->chapterRepo->destroy($chapter);
150         return redirect($book->getUrl());
151     }
152 }