1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Actions\ActivityType;
5 use BookStack\Actions\CommentRepo;
6 use BookStack\Entities\Models\Page;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
10 class CommentController extends Controller
12 protected $commentRepo;
14 public function __construct(CommentRepo $commentRepo)
16 $this->commentRepo = $commentRepo;
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 return view('comments.comment', ['comment' => $comment]);
47 * Update an existing comment.
48 * @throws ValidationException
50 public function update(Request $request, int $commentId)
52 $this->validate($request, [
53 'text' => 'required|string',
56 $comment = $this->commentRepo->getById($commentId);
57 $this->checkOwnablePermission('page-view', $comment->entity);
58 $this->checkOwnablePermission('comment-update', $comment);
60 $comment = $this->commentRepo->update($comment, $request->get('text'));
61 return view('comments.comment', ['comment' => $comment]);
65 * Delete a comment from the system.
67 public function destroy(int $id)
69 $comment = $this->commentRepo->getById($id);
70 $this->checkOwnablePermission('comment-delete', $comment);
72 $this->commentRepo->delete($comment);
73 return response()->json(['message' => trans('entities.comment_deleted')]);