1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Actions\CommentRepo;
5 use BookStack\Entities\Page;
6 use Illuminate\Http\Request;
7 use Illuminate\Validation\ValidationException;
9 class CommentController extends Controller
11 protected $commentRepo;
13 public function __construct(CommentRepo $commentRepo)
15 $this->commentRepo = $commentRepo;
16 parent::__construct();
20 * Save a new comment for a Page
21 * @throws ValidationException
23 public function savePageComment(Request $request, int $pageId)
25 $this->validate($request, [
26 'text' => 'required|string',
27 'parent_id' => 'nullable|integer',
30 $page = Page::visible()->find($pageId);
32 return response('Not found', 404);
35 // Prevent adding comments to draft pages
37 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
40 // Create a new comment.
41 $this->checkPermission('comment-create-all');
42 $comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
43 Activity::add($page, 'commented_on', $page->book->id);
44 return view('comments.comment', ['comment' => $comment]);
48 * Update an existing comment.
49 * @throws ValidationException
51 public function update(Request $request, int $commentId)
53 $this->validate($request, [
54 'text' => 'required|string',
57 $comment = $this->commentRepo->getById($commentId);
58 $this->checkOwnablePermission('page-view', $comment->entity);
59 $this->checkOwnablePermission('comment-update', $comment);
61 $comment = $this->commentRepo->update($comment, $request->get('text'));
62 return view('comments.comment', ['comment' => $comment]);
66 * Delete a comment from the system.
68 public function destroy(int $id)
70 $comment = $this->commentRepo->getById($id);
71 $this->checkOwnablePermission('comment-delete', $comment);
73 $this->commentRepo->delete($comment);
74 return response()->json(['message' => trans('entities.comment_deleted')]);