2017-01-13 17:15:48 +01:00
|
|
|
<?php namespace BookStack\Repos;
|
|
|
|
|
|
|
|
use BookStack\Comment;
|
2017-09-03 17:37:51 +02:00
|
|
|
use BookStack\Entity;
|
2017-01-13 17:15:48 +01:00
|
|
|
|
|
|
|
/**
|
2017-09-03 17:37:51 +02:00
|
|
|
* Class CommentRepo
|
2017-01-13 17:15:48 +01:00
|
|
|
* @package BookStack\Repos
|
|
|
|
*/
|
2018-01-28 17:58:52 +01:00
|
|
|
class CommentRepo
|
|
|
|
{
|
2017-09-03 17:37:51 +02:00
|
|
|
|
2017-01-13 17:15:48 +01:00
|
|
|
/**
|
2017-05-15 21:10:14 +02:00
|
|
|
* @var Comment $comment
|
2017-01-13 17:15:48 +01:00
|
|
|
*/
|
|
|
|
protected $comment;
|
2017-04-18 21:51:45 +02:00
|
|
|
|
2017-09-03 17:37:51 +02:00
|
|
|
/**
|
|
|
|
* CommentRepo constructor.
|
|
|
|
* @param Comment $comment
|
|
|
|
*/
|
2017-04-18 21:51:45 +02:00
|
|
|
public function __construct(Comment $comment)
|
|
|
|
{
|
|
|
|
$this->comment = $comment;
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:37:51 +02:00
|
|
|
/**
|
|
|
|
* Get a comment by ID.
|
|
|
|
* @param $id
|
|
|
|
* @return Comment|\Illuminate\Database\Eloquent\Model
|
|
|
|
*/
|
|
|
|
public function getById($id)
|
|
|
|
{
|
|
|
|
return $this->comment->newQuery()->findOrFail($id);
|
2017-04-18 21:51:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 17:37:51 +02:00
|
|
|
/**
|
|
|
|
* Create a new comment on an entity.
|
|
|
|
* @param Entity $entity
|
|
|
|
* @param array $data
|
|
|
|
* @return Comment
|
|
|
|
*/
|
2018-01-28 17:58:52 +01:00
|
|
|
public function create(Entity $entity, $data = [])
|
2017-09-03 17:37:51 +02:00
|
|
|
{
|
2017-04-18 21:51:45 +02:00
|
|
|
$userId = user()->id;
|
2017-09-03 17:37:51 +02:00
|
|
|
$comment = $this->comment->newInstance($data);
|
|
|
|
$comment->created_by = $userId;
|
2017-04-18 21:51:45 +02:00
|
|
|
$comment->updated_by = $userId;
|
2017-09-03 17:37:51 +02:00
|
|
|
$comment->local_id = $this->getNextLocalId($entity);
|
|
|
|
$entity->comments()->save($comment);
|
2017-06-04 15:22:44 +02:00
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:37:51 +02:00
|
|
|
/**
|
|
|
|
* Update an existing comment.
|
|
|
|
* @param Comment $comment
|
|
|
|
* @param array $input
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function update($comment, $input)
|
|
|
|
{
|
|
|
|
$comment->updated_by = user()->id;
|
|
|
|
$comment->update($input);
|
2017-04-18 21:51:45 +02:00
|
|
|
return $comment;
|
|
|
|
}
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-09-03 17:37:51 +02:00
|
|
|
/**
|
|
|
|
* Delete a comment from the system.
|
|
|
|
* @param Comment $comment
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function delete($comment)
|
|
|
|
{
|
|
|
|
return $comment->delete();
|
2017-04-26 23:05:29 +02:00
|
|
|
}
|
2017-05-15 21:10:14 +02:00
|
|
|
|
2017-09-03 17:37:51 +02:00
|
|
|
/**
|
|
|
|
* Get the next local ID relative to the linked entity.
|
|
|
|
* @param Entity $entity
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getNextLocalId(Entity $entity)
|
|
|
|
{
|
2017-09-09 16:56:24 +02:00
|
|
|
$comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
|
2018-01-28 17:58:52 +01:00
|
|
|
if ($comments === null) {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-09-03 17:37:51 +02:00
|
|
|
return $comments->local_id + 1;
|
2017-04-18 21:51:45 +02:00
|
|
|
}
|
2018-01-28 17:58:52 +01:00
|
|
|
}
|