1 <?php namespace BookStack\Actions;
3 use BookStack\Entities\Entity;
4 use League\CommonMark\CommonMarkConverter;
13 * @var Comment $comment
18 public function __construct(Comment $comment)
20 $this->comment = $comment;
24 * Get a comment by ID.
26 public function getById(int $id): Comment
28 return $this->comment->newQuery()->findOrFail($id);
32 * Create a new comment on an entity.
34 public function create(Entity $entity, string $text, ?int $parent_id): Comment
37 $comment = $this->comment->newInstance();
39 $comment->text = $text;
40 $comment->html = $this->commentToHtml($text);
41 $comment->created_by = $userId;
42 $comment->updated_by = $userId;
43 $comment->local_id = $this->getNextLocalId($entity);
44 $comment->parent_id = $parent_id;
46 $entity->comments()->save($comment);
51 * Update an existing comment.
53 public function update(Comment $comment, string $text): Comment
55 $comment->updated_by = user()->id;
56 $comment->text = $text;
57 $comment->html = $this->commentToHtml($text);
63 * Delete a comment from the system.
65 public function delete(Comment $comment)
71 * Convert the given comment markdown text to HTML.
73 public function commentToHtml(string $commentText): string
75 $converter = new CommonMarkConverter([
76 'html_input' => 'strip',
77 'max_nesting_level' => 10,
78 'allow_unsafe_links' => false,
81 return $converter->convertToHtml($commentText);
85 * Get the next local ID relative to the linked entity.
87 protected function getNextLocalId(Entity $entity): int
89 $comments = $entity->comments(false)->orderBy('local_id', 'desc')->first();
90 return ($comments->local_id ?? 0) + 1;