2017-04-18 21:51:45 +02:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2017-01-13 17:15:48 +01:00
|
|
|
|
2017-04-18 21:51:45 +02:00
|
|
|
use BookStack\Repos\CommentRepo;
|
|
|
|
use BookStack\Repos\EntityRepo;
|
2017-05-15 21:10:14 +02:00
|
|
|
use BookStack\Comment;
|
2017-01-13 17:15:48 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2017-04-18 21:51:45 +02:00
|
|
|
// delete -checkOwnablePermission \
|
2017-01-13 17:15:48 +01:00
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2017-04-18 21:51:45 +02:00
|
|
|
protected $entityRepo;
|
|
|
|
|
2017-05-15 21:10:14 +02:00
|
|
|
public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo, Comment $comment)
|
2017-04-18 21:51:45 +02:00
|
|
|
{
|
|
|
|
$this->entityRepo = $entityRepo;
|
|
|
|
$this->commentRepo = $commentRepo;
|
2017-05-15 21:10:14 +02:00
|
|
|
$this->comment = $comment;
|
2017-04-18 21:51:45 +02:00
|
|
|
parent::__construct();
|
2017-01-13 17:15:48 +01:00
|
|
|
}
|
2017-04-18 21:51:45 +02:00
|
|
|
|
2017-05-02 23:12:04 +02:00
|
|
|
public function save(Request $request, $pageId, $commentId = null)
|
2017-04-18 21:51:45 +02:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'text' => 'required|string',
|
|
|
|
'html' => 'required|string',
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
return response('Not found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($page->draft) {
|
|
|
|
// cannot add comments to drafts.
|
|
|
|
return response()->json([
|
|
|
|
'status' => 'error',
|
|
|
|
'message' => trans('errors.cannot_add_comment_to_draft'),
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
|
|
|
if (empty($commentId)) {
|
|
|
|
// create a new comment.
|
|
|
|
$this->checkPermission('comment-create-all');
|
|
|
|
$comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id']));
|
2017-05-15 21:10:14 +02:00
|
|
|
$respMsg = trans('entities.comment_created');
|
2017-04-18 21:51:45 +02:00
|
|
|
} else {
|
|
|
|
// update existing comment
|
2017-05-15 21:10:14 +02:00
|
|
|
// get comment by ID and check if this user has permission to update.
|
2017-04-18 21:51:45 +02:00
|
|
|
$comment = $this->comment->findOrFail($commentId);
|
|
|
|
$this->checkOwnablePermission('comment-update', $comment);
|
|
|
|
$this->commentRepo->update($comment, $request->all());
|
|
|
|
$respMsg = trans('entities.comment_updated');
|
|
|
|
}
|
|
|
|
|
2017-05-30 05:32:47 +02:00
|
|
|
$comment = $this->commentRepo->getCommentById($comment->id);
|
|
|
|
|
2017-04-18 21:51:45 +02:00
|
|
|
return response()->json([
|
|
|
|
'status' => 'success',
|
2017-05-30 05:32:47 +02:00
|
|
|
'message' => $respMsg,
|
|
|
|
'comment' => $comment
|
2017-04-18 21:51:45 +02:00
|
|
|
]);
|
|
|
|
|
2017-01-13 17:15:48 +01:00
|
|
|
}
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-01-13 17:15:48 +01:00
|
|
|
public function destroy($id) {
|
2017-04-18 21:51:45 +02:00
|
|
|
$comment = $this->comment->findOrFail($id);
|
|
|
|
$this->checkOwnablePermission('comment-delete', $comment);
|
2017-01-13 17:15:48 +01:00
|
|
|
}
|
2017-04-18 21:51:45 +02:00
|
|
|
|
2017-05-30 05:32:47 +02:00
|
|
|
|
|
|
|
public function getPageComments($pageId) {
|
2017-04-18 21:51:45 +02:00
|
|
|
try {
|
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
return response('Not found', 404);
|
|
|
|
}
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-04-18 21:51:45 +02:00
|
|
|
if($page->draft) {
|
|
|
|
// cannot add comments to drafts.
|
|
|
|
return response()->json([
|
|
|
|
'status' => 'error',
|
|
|
|
'message' => trans('errors.no_comments_for_draft'),
|
|
|
|
], 400);
|
|
|
|
}
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-04-18 21:51:45 +02:00
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-05-30 05:32:47 +02:00
|
|
|
$comments = $this->commentRepo->getPageComments($pageId);
|
|
|
|
return response()->json(['success' => true, 'comments'=> $comments['comments'], 'total' => $comments['total']]);
|
2017-01-13 17:15:48 +01:00
|
|
|
}
|
|
|
|
}
|