public function getCommentWithChildren(int $commentId) : array
{
$versionId = $this->db->cell('
SELECT
MAX(versionid)
FROM
hull_blog_comment_versions
WHERE
approved
AND comment = ?
', $commentId);
if (empty($versionId)) {
return [];
}
/**
* Now let's get the actual comment
*/
$comment = $this->db->row('SELECT
c.*,
v.created AS modified,
v.message
FROM
view_hull_blog_comments c
LEFT JOIN
hull_blog_comment_versions v
ON v.comment = c.commentid
WHERE
v.versionid = ?
', $versionId);
if (isset($comment['metadata'])) {
$comment['metadata'] = \json_decode($comment['metadata'], true);
}
/**
* Let's recursively add all of its children:
*/
$comment['children'] = [];
$children = $this->db->run('
SELECT
commentid
FROM
hull_blog_comments
WHERE
replyto = ?', $commentId);
foreach ($children as $child) {
$data = $this->getCommentWithChildren((int) $child['commentid']);
if (!empty($data)) {
$comment['children'][] = $data;
}
}
/**
* Now return:
*/
return $comment;
}