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;
14 * CommentController constructor.
16 public function __construct(CommentRepo $commentRepo)
18 $this->commentRepo = $commentRepo;
19 parent::__construct();
23 * Save a new comment for a Page
24 * @throws ValidationException
26 public function savePageComment(Request $request, int $pageId, int $commentId = null)
28 $this->validate($request, [
29 'text' => 'required|string',
30 'html' => 'required|string',
33 $page = Page::visible()->find($pageId);
35 return response('Not found', 404);
38 $this->checkOwnablePermission('page-view', $page);
40 // Prevent adding comments to draft pages
42 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
45 // Create a new comment.
46 $this->checkPermission('comment-create-all');
47 $comment = $this->commentRepo->create($page, $request->only(['html', 'text', 'parent_id']));
48 Activity::add($page, 'commented_on', $page->book->id);
49 return view('comments.comment', ['comment' => $comment]);
53 * Update an existing comment.
54 * @throws ValidationException
56 public function update(Request $request, int $commentId)
58 $this->validate($request, [
59 'text' => 'required|string',
60 'html' => 'required|string',
63 $comment = $this->commentRepo->getById($commentId);
64 $this->checkOwnablePermission('page-view', $comment->entity);
65 $this->checkOwnablePermission('comment-update', $comment);
67 $comment = $this->commentRepo->update($comment, $request->only(['html', 'text']));
68 return view('comments.comment', ['comment' => $comment]);
72 * Delete a comment from the system.
74 public function destroy(int $id)
76 $comment = $this->commentRepo->getById($id);
77 $this->checkOwnablePermission('comment-delete', $comment);
79 $this->commentRepo->delete($comment);
80 return response()->json(['message' => trans('entities.comment_deleted')]);