2017-04-18 15:51:45 -04:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2017-01-13 11:15:48 -05:00
|
|
|
|
2017-04-18 15:51:45 -04:00
|
|
|
use BookStack\Repos\CommentRepo;
|
|
|
|
use BookStack\Repos\EntityRepo;
|
2017-05-15 15:10:14 -04:00
|
|
|
use BookStack\Comment;
|
2017-01-13 11:15:48 -05:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2017-04-18 15:51:45 -04:00
|
|
|
// delete -checkOwnablePermission \
|
2017-01-13 11:15:48 -05:00
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2017-04-18 15:51:45 -04:00
|
|
|
protected $entityRepo;
|
|
|
|
|
2017-05-15 15:10:14 -04:00
|
|
|
public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo, Comment $comment)
|
2017-04-18 15:51:45 -04:00
|
|
|
{
|
|
|
|
$this->entityRepo = $entityRepo;
|
|
|
|
$this->commentRepo = $commentRepo;
|
2017-05-15 15:10:14 -04:00
|
|
|
$this->comment = $comment;
|
2017-04-18 15:51:45 -04:00
|
|
|
parent::__construct();
|
2017-01-13 11:15:48 -05:00
|
|
|
}
|
2017-04-18 15:51:45 -04:00
|
|
|
|
2017-05-02 17:12:04 -04:00
|
|
|
public function save(Request $request, $pageId, $commentId = null)
|
2017-04-18 15:51:45 -04: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 15:10:14 -04:00
|
|
|
$respMsg = trans('entities.comment_created');
|
2017-04-18 15:51:45 -04:00
|
|
|
} else {
|
|
|
|
// update existing comment
|
2017-05-15 15:10:14 -04:00
|
|
|
// get comment by ID and check if this user has permission to update.
|
2017-04-18 15:51:45 -04:00
|
|
|
$comment = $this->comment->findOrFail($commentId);
|
|
|
|
$this->checkOwnablePermission('comment-update', $comment);
|
|
|
|
$this->commentRepo->update($comment, $request->all());
|
|
|
|
$respMsg = trans('entities.comment_updated');
|
|
|
|
}
|
|
|
|
|
2017-05-29 23:32:47 -04:00
|
|
|
$comment = $this->commentRepo->getCommentById($comment->id);
|
|
|
|
|
2017-04-18 15:51:45 -04:00
|
|
|
return response()->json([
|
|
|
|
'status' => 'success',
|
2017-05-29 23:32:47 -04:00
|
|
|
'message' => $respMsg,
|
|
|
|
'comment' => $comment
|
2017-04-18 15:51:45 -04:00
|
|
|
]);
|
|
|
|
|
2017-01-13 11:15:48 -05:00
|
|
|
}
|
2017-05-15 15:10:14 -04:00
|
|
|
|
2017-01-13 11:15:48 -05:00
|
|
|
public function destroy($id) {
|
2017-04-18 15:51:45 -04:00
|
|
|
$comment = $this->comment->findOrFail($id);
|
|
|
|
$this->checkOwnablePermission('comment-delete', $comment);
|
2017-01-13 11:15:48 -05:00
|
|
|
}
|
2017-04-18 15:51:45 -04:00
|
|
|
|
2017-05-29 23:32:47 -04:00
|
|
|
|
|
|
|
public function getPageComments($pageId) {
|
2017-04-18 15:51:45 -04:00
|
|
|
try {
|
|
|
|
$page = $this->entityRepo->getById('page', $pageId, true);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
return response('Not found', 404);
|
|
|
|
}
|
2017-05-15 15:10:14 -04:00
|
|
|
|
2017-04-18 15:51:45 -04: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 15:10:14 -04:00
|
|
|
|
2017-04-18 15:51:45 -04:00
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2017-05-15 15:10:14 -04:00
|
|
|
|
2017-05-29 23:32:47 -04:00
|
|
|
$comments = $this->commentRepo->getPageComments($pageId);
|
|
|
|
return response()->json(['success' => true, 'comments'=> $comments['comments'], 'total' => $comments['total']]);
|
2017-01-13 11:15:48 -05:00
|
|
|
}
|
|
|
|
}
|