1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Repos\CommentRepo;
4 use BookStack\Repos\EntityRepo;
5 use Illuminate\Http\Request;
8 // delete -checkOwnablePermission \
9 class CommentController extends Controller
11 protected $entityRepo;
13 public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo)
15 $this->entityRepo = $entityRepo;
16 $this->commentRepo = $commentRepo;
17 parent::__construct();
20 public function save(Request $request, $pageId, $commentId)
22 $this->validate($request, [
23 'text' => 'required|string',
24 'html' => 'required|string',
28 $page = $this->entityRepo->getById('page', $pageId, true);
29 } catch (ModelNotFoundException $e) {
30 return response('Not found', 404);
34 // cannot add comments to drafts.
35 return response()->json([
37 'message' => trans('errors.cannot_add_comment_to_draft'),
41 $this->checkOwnablePermission('page-view', $page);
42 if (empty($commentId)) {
43 // create a new comment.
44 $this->checkPermission('comment-create-all');
45 $comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id']));
46 $respMsg = trans('entities.comment_created');
48 // update existing comment
49 // get comment by ID and check if this user has permission to update.
50 $comment = $this->comment->findOrFail($commentId);
51 $this->checkOwnablePermission('comment-update', $comment);
52 $this->commentRepo->update($comment, $request->all());
53 $respMsg = trans('entities.comment_updated');
56 return response()->json([
57 'status' => 'success',
63 public function destroy($id) {
64 $comment = $this->comment->findOrFail($id);
65 $this->checkOwnablePermission('comment-delete', $comment);
70 public function getComments($pageId, $commentId = null) {
72 $page = $this->entityRepo->getById('page', $pageId, true);
73 } catch (ModelNotFoundException $e) {
74 return response('Not found', 404);
78 // cannot add comments to drafts.
79 return response()->json([
81 'message' => trans('errors.no_comments_for_draft'),
85 $this->checkOwnablePermission('page-view', $page);
87 $comments = $this->commentRepo->getCommentsForPage($pageId, $commentId);
89 return response()->json(array('success' => true, 'comments'=> $comments));