2017-04-19 03:51:45 +08:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2017-01-14 00:15:48 +08:00
|
|
|
|
2017-09-10 00:06:30 +08:00
|
|
|
use Activity;
|
2017-04-19 03:51:45 +08:00
|
|
|
use BookStack\Repos\CommentRepo;
|
|
|
|
use BookStack\Repos\EntityRepo;
|
2017-09-03 23:37:51 +08:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2017-01-14 00:15:48 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2017-04-19 03:51:45 +08:00
|
|
|
protected $entityRepo;
|
2017-09-03 23:37:51 +08:00
|
|
|
protected $commentRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CommentController constructor.
|
|
|
|
* @param EntityRepo $entityRepo
|
|
|
|
* @param CommentRepo $commentRepo
|
|
|
|
*/
|
|
|
|
public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo)
|
2017-04-19 03:51:45 +08:00
|
|
|
{
|
|
|
|
$this->entityRepo = $entityRepo;
|
|
|
|
$this->commentRepo = $commentRepo;
|
|
|
|
parent::__construct();
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
2017-04-19 03:51:45 +08:00
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
/**
|
|
|
|
* Save a new comment for a Page
|
|
|
|
* @param Request $request
|
|
|
|
* @param integer $pageId
|
|
|
|
* @param null|integer $commentId
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
|
|
|
|
*/
|
|
|
|
public function savePageComment(Request $request, $pageId, $commentId = null)
|
2017-04-19 03:51:45 +08: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2017-09-03 23:37:51 +08:00
|
|
|
|
|
|
|
// Prevent adding comments to draft pages
|
|
|
|
if ($page->draft) {
|
|
|
|
return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
|
2017-04-19 03:51:45 +08:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
// Create a new comment.
|
|
|
|
$this->checkPermission('comment-create-all');
|
2017-09-10 00:06:30 +08:00
|
|
|
$comment = $this->commentRepo->create($page, $request->only(['html', 'text', 'parent_id']));
|
|
|
|
Activity::add($page, 'commented_on', $page->book->id);
|
2017-09-03 23:37:51 +08:00
|
|
|
return view('comments/comment', ['comment' => $comment]);
|
|
|
|
}
|
2017-05-30 11:32:47 +08:00
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
/**
|
|
|
|
* Update an existing comment.
|
|
|
|
* @param Request $request
|
|
|
|
* @param integer $commentId
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $commentId)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'text' => 'required|string',
|
|
|
|
'html' => 'required|string',
|
2017-04-19 03:51:45 +08:00
|
|
|
]);
|
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
$comment = $this->commentRepo->getById($commentId);
|
|
|
|
$this->checkOwnablePermission('page-view', $comment->entity);
|
|
|
|
$this->checkOwnablePermission('comment-update', $comment);
|
|
|
|
|
2017-09-10 00:06:30 +08:00
|
|
|
$comment = $this->commentRepo->update($comment, $request->only(['html', 'text']));
|
2017-09-03 23:37:51 +08:00
|
|
|
return view('comments/comment', ['comment' => $comment]);
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
2017-05-16 03:10:14 +08:00
|
|
|
|
2017-09-03 23:37:51 +08:00
|
|
|
/**
|
|
|
|
* Delete a comment from the system.
|
|
|
|
* @param integer $id
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$comment = $this->commentRepo->getById($id);
|
2017-04-19 03:51:45 +08:00
|
|
|
$this->checkOwnablePermission('comment-delete', $comment);
|
2017-06-04 21:22:44 +08:00
|
|
|
$this->commentRepo->delete($comment);
|
2017-09-03 23:37:51 +08:00
|
|
|
return response()->json(['message' => trans('entities.comment_deleted')]);
|
2017-01-14 00:15:48 +08:00
|
|
|
}
|
|
|
|
}
|