]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/CommentController.php
#47 Implements the reply and edit functionality for comments.
[bookstack] / app / Http / Controllers / CommentController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Repos\CommentRepo;
4 use BookStack\Repos\EntityRepo;
5 use BookStack\Comment;
6 use Illuminate\Http\Request;
7
8 // delete  -checkOwnablePermission \
9 class CommentController extends Controller
10 {
11     protected $entityRepo;
12
13     public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo, Comment $comment)
14     {
15         $this->entityRepo = $entityRepo;
16         $this->commentRepo = $commentRepo;
17         $this->comment = $comment;
18         parent::__construct();
19     }
20
21     public function save(Request $request, $pageId, $commentId = null)
22     {
23         $this->validate($request, [
24             'text' => 'required|string',
25             'html' => 'required|string',
26         ]);
27
28         try {
29             $page = $this->entityRepo->getById('page', $pageId, true);
30         } catch (ModelNotFoundException $e) {
31             return response('Not found', 404);
32         }
33
34         if($page->draft) {
35             // cannot add comments to drafts.
36             return response()->json([
37                 'status' => 'error',
38                 'message' => trans('errors.cannot_add_comment_to_draft'),
39             ], 400);
40         }
41
42         $this->checkOwnablePermission('page-view', $page);
43         if (empty($commentId)) {
44             // create a new comment.
45             $this->checkPermission('comment-create-all');
46             $comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id']));
47             $respMsg = trans('entities.comment_created');
48         } else {
49             // update existing comment
50             // get comment by ID and check if this user has permission to update.
51             $comment = $this->comment->findOrFail($commentId);
52             $this->checkOwnablePermission('comment-update', $comment);
53             $this->commentRepo->update($comment, $request->all());
54             $respMsg = trans('entities.comment_updated');
55         }
56
57         return response()->json([
58             'status'    => 'success',
59             'message'   => $respMsg
60         ]);
61
62     }
63
64     public function destroy($id) {
65         $comment = $this->comment->findOrFail($id);
66         $this->checkOwnablePermission('comment-delete', $comment);
67
68         //
69     }
70
71     public function getCommentThread($pageId, $commentId = null) {
72         try {
73             $page = $this->entityRepo->getById('page', $pageId, true);
74         } catch (ModelNotFoundException $e) {
75             return response('Not found', 404);
76         }
77
78         if($page->draft) {
79             // cannot add comments to drafts.
80             return response()->json([
81                 'status' => 'error',
82                 'message' => trans('errors.no_comments_for_draft'),
83             ], 400);
84         }
85
86         $this->checkOwnablePermission('page-view', $page);
87
88         $comments = $this->commentRepo->getCommentsForPage($pageId, $commentId);
89         if (empty($commentId)) {
90             // requesting for parent level comments, send the total count as well.
91             $totalComments = $this->commentRepo->getCommentCount($pageId);
92             return response()->json(['success' => true, 'comments'=> $comments, 'total' => $totalComments]);
93         }
94         return response()->json(['success' => true, 'comments'=> $comments]);
95     }
96 }