]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/CommentController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / CommentController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\CommentRepo;
6 use BookStack\Entities\Models\Page;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
9
10 class CommentController extends Controller
11 {
12     protected $commentRepo;
13
14     public function __construct(CommentRepo $commentRepo)
15     {
16         $this->commentRepo = $commentRepo;
17     }
18
19     /**
20      * Save a new comment for a Page.
21      *
22      * @throws ValidationException
23      */
24     public function savePageComment(Request $request, int $pageId)
25     {
26         $this->validate($request, [
27             'text'      => 'required|string',
28             'parent_id' => 'nullable|integer',
29         ]);
30
31         $page = Page::visible()->find($pageId);
32         if ($page === null) {
33             return response('Not found', 404);
34         }
35
36         // Prevent adding comments to draft pages
37         if ($page->draft) {
38             return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
39         }
40
41         // Create a new comment.
42         $this->checkPermission('comment-create-all');
43         $comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
44
45         return view('comments.comment', ['comment' => $comment]);
46     }
47
48     /**
49      * Update an existing comment.
50      *
51      * @throws ValidationException
52      */
53     public function update(Request $request, int $commentId)
54     {
55         $this->validate($request, [
56             'text' => 'required|string',
57         ]);
58
59         $comment = $this->commentRepo->getById($commentId);
60         $this->checkOwnablePermission('page-view', $comment->entity);
61         $this->checkOwnablePermission('comment-update', $comment);
62
63         $comment = $this->commentRepo->update($comment, $request->get('text'));
64
65         return view('comments.comment', ['comment' => $comment]);
66     }
67
68     /**
69      * Delete a comment from the system.
70      */
71     public function destroy(int $id)
72     {
73         $comment = $this->commentRepo->getById($id);
74         $this->checkOwnablePermission('comment-delete', $comment);
75
76         $this->commentRepo->delete($comment);
77
78         return response()->json(['message' => trans('entities.comment_deleted')]);
79     }
80 }