2017-04-18 15:51:45 -04:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2017-01-13 11:15:48 -05:00
|
|
|
|
2017-09-09 12:06:30 -04:00
|
|
|
use Activity;
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Actions\CommentRepo;
|
2019-10-05 07:55:01 -04:00
|
|
|
use BookStack\Entities\Page;
|
2017-01-13 11:15:48 -05:00
|
|
|
use Illuminate\Http\Request;
|
2019-10-05 07:55:01 -04:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2017-01-13 11:15:48 -05:00
|
|
|
|
|
|
|
class CommentController extends Controller
|
|
|
|
{
|
2017-09-03 11:37:51 -04:00
|
|
|
protected $commentRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CommentController constructor.
|
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function __construct(CommentRepo $commentRepo)
|
2017-04-18 15:51:45 -04:00
|
|
|
{
|
|
|
|
$this->commentRepo = $commentRepo;
|
|
|
|
parent::__construct();
|
2017-01-13 11:15:48 -05:00
|
|
|
}
|
2017-04-18 15:51:45 -04:00
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Save a new comment for a Page
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws ValidationException
|
2017-09-03 11:37:51 -04:00
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function savePageComment(Request $request, int $pageId, int $commentId = null)
|
2017-04-18 15:51:45 -04:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'text' => 'required|string',
|
|
|
|
'html' => 'required|string',
|
|
|
|
]);
|
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
$page = Page::visible()->find($pageId);
|
|
|
|
if ($page === null) {
|
2017-04-18 15:51:45 -04:00
|
|
|
return response('Not found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->checkOwnablePermission('page-view', $page);
|
2017-09-03 11:37:51 -04:00
|
|
|
|
|
|
|
// Prevent adding comments to draft pages
|
|
|
|
if ($page->draft) {
|
|
|
|
return $this->jsonError(trans('errors.cannot_add_comment_to_draft'), 400);
|
2017-04-18 15:51:45 -04:00
|
|
|
}
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
// Create a new comment.
|
|
|
|
$this->checkPermission('comment-create-all');
|
2017-09-09 12:06:30 -04:00
|
|
|
$comment = $this->commentRepo->create($page, $request->only(['html', 'text', 'parent_id']));
|
|
|
|
Activity::add($page, 'commented_on', $page->book->id);
|
2019-04-07 07:00:09 -04:00
|
|
|
return view('comments.comment', ['comment' => $comment]);
|
2017-09-03 11:37:51 -04:00
|
|
|
}
|
2017-05-29 23:32:47 -04:00
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Update an existing comment.
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws ValidationException
|
2017-09-03 11:37:51 -04:00
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function update(Request $request, int $commentId)
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'text' => 'required|string',
|
|
|
|
'html' => 'required|string',
|
2017-04-18 15:51:45 -04:00
|
|
|
]);
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
$comment = $this->commentRepo->getById($commentId);
|
|
|
|
$this->checkOwnablePermission('page-view', $comment->entity);
|
|
|
|
$this->checkOwnablePermission('comment-update', $comment);
|
|
|
|
|
2017-09-09 12:06:30 -04:00
|
|
|
$comment = $this->commentRepo->update($comment, $request->only(['html', 'text']));
|
2019-04-07 07:00:09 -04:00
|
|
|
return view('comments.comment', ['comment' => $comment]);
|
2017-01-13 11:15:48 -05:00
|
|
|
}
|
2017-05-15 15:10:14 -04:00
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Delete a comment from the system.
|
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function destroy(int $id)
|
2017-09-03 11:37:51 -04:00
|
|
|
{
|
|
|
|
$comment = $this->commentRepo->getById($id);
|
2017-04-18 15:51:45 -04:00
|
|
|
$this->checkOwnablePermission('comment-delete', $comment);
|
2019-10-05 07:55:01 -04:00
|
|
|
|
2017-06-04 09:22:44 -04:00
|
|
|
$this->commentRepo->delete($comment);
|
2017-09-03 11:37:51 -04:00
|
|
|
return response()->json(['message' => trans('entities.comment_deleted')]);
|
2017-01-13 11:15:48 -05:00
|
|
|
}
|
|
|
|
}
|