1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Repos\CommentRepo;
4 use BookStack\Repos\EntityRepo;
5 use Illuminate\Database\Eloquent\ModelNotFoundException;
6 use Illuminate\Http\Request;
8 class CommentController extends Controller
10 protected $entityRepo;
11 protected $commentRepo;
14 * CommentController constructor.
15 * @param EntityRepo $entityRepo
16 * @param CommentRepo $commentRepo
18 public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo)
20 $this->entityRepo = $entityRepo;
21 $this->commentRepo = $commentRepo;
22 parent::__construct();
26 * Save a new comment for a Page
27 * @param Request $request
28 * @param integer $pageId
29 * @param null|integer $commentId
30 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
32 public function savePageComment(Request $request, $pageId, $commentId = null)
34 $this->validate($request, [
35 'text' => 'required|string',
36 'html' => 'required|string',
40 $page = $this->entityRepo->getById('page', $pageId, true);
41 } catch (ModelNotFoundException $e) {
42 return response('Not found', 404);
45 $this->checkOwnablePermission('page-view', $page);
47 // Prevent adding comments to draft pages
49 return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
52 // Create a new comment.
53 $this->checkPermission('comment-create-all');
54 $comment = $this->commentRepo->create($page, $request->all());
55 return view('comments/comment', ['comment' => $comment]);
59 * Update an existing comment.
60 * @param Request $request
61 * @param integer $commentId
62 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
64 public function update(Request $request, $commentId)
66 $this->validate($request, [
67 'text' => 'required|string',
68 'html' => 'required|string',
71 $comment = $this->commentRepo->getById($commentId);
72 $this->checkOwnablePermission('page-view', $comment->entity);
73 $this->checkOwnablePermission('comment-update', $comment);
75 $comment = $this->commentRepo->update($comment, $request->all());
76 return view('comments/comment', ['comment' => $comment]);
80 * Delete a comment from the system.
82 * @return \Illuminate\Http\JsonResponse
84 public function destroy($id)
86 $comment = $this->commentRepo->getById($id);
87 $this->checkOwnablePermission('comment-delete', $comment);
88 $this->commentRepo->delete($comment);
89 return response()->json(['message' => trans('entities.comment_deleted')]);