My models now look like this:
comments.php
:
<?php
class CommentsPage extends Kirby\Cms\Page
{
public function children()
{
$comments = [];
foreach (Db::select('comments') as $comment) {
$comments[] = [
'slug' => $comment->slug(),
'num' => $comment->status() === 'listed' ? 0 : null,
'template' => 'comment',
'model' => 'comment',
'content' => [
'text' => $comment->text(),
'user' => $comment->user(),
'status' => is_null($comment->status()) ? 'draft' : $comment->status()
]
];
}
return Pages::factory($comments, $this);
}
}
comment.php
<?php
class CommentPage extends Kirby\Cms\Page
{
public function isDraft(): bool
{
return in_array($this->content()->status(), ['listed', 'unlisted']) === false;
}
public function changeSlug(string $slug, string $languageCode = null)
{
// always sanitize the slug
$slug = Str::slug($slug);
$data['slug'] = $slug;
if ($comment = Db::first('comments', '*', ['slug' => $this->slug()])) {
if (Db::update('comments', $data, ['slug' => $this->slug()])) {
return $this;
};
}
return $this;
}
protected function changeStatusToDraft()
{
$data['status'] = 'null';
if ($comment = Db::first('comments', '*', ['slug' => $this->slug()])) {
return Db::update('comments', $data, ['slug' => $this->slug()]);
}
return $this;
}
protected function changeStatusToListed(int $position = null)
{
// create a sorting number for the page
$num = $this->createNum($position);
// don't sort if not necessary
if ($this->status() === 'listed' && $num === $this->num()) {
return $this;
}
$data['status'] = 'listed';
if ($comment = Db::first('comments', '*', ['slug' => $this->slug()])) {
return Db::update('comments', $data, ['slug' => $this->slug()]);
}
if ($this->blueprint()->num() === 'default') {
$this->resortSiblingsAfterListing($num);
}
return $this;
}
protected function changeStatusToUnlisted()
{
if ($this->status() === 'unlisted') {
return $this;
}
$data['status'] = 'unlisted';
if ($comment = Db::first('comments', '*', ['slug' => $this->slug()])) {
return Db::update('comments', $data, ['slug' => $this->slug()]);
}
$this->resortSiblingsAfterUnlisting();
return $this;
}
public function changeTitle(string $slug, string $languageCode = null)
{
// always sanitize the slug
$slug = Str::slug($slug);
$data['slug'] = $slug;
if ($comment = Db::first('comments', '*', ['slug' => $this->slug()])) {
if (Db::update('comments', $data, ['slug' => $this->slug()])) {
return $this;
};
}
return $this;
}
public function delete(bool $force = false): bool
{
return Db::delete('comments', ['slug' => $this->slug()]);
}
public function title(): Kirby\Cms\Field
{
return $this->text()->excerpt(100)->or('New comment');
}
public function writeContent(array $data, string $languageCode = null): bool
{
unset($data['title']);
if ($comment = Db::first('comments', '*', ['slug' => $this->slug()])) {
return Db::update('comments', $data, ['slug' => $this->slug()]);
} else {
$data['slug'] = $this->slug();
return Db::insert('comments', $data);
}
}
}
comment.yml
blueprint:
title: Comment
icon: 📢
options:
title: false
status: true
url: true
fields:
user:
label: User
type: text
text:
label: text
type: textarea
# just for control purposes
status:
label: Status
type: text
disabled: true
Let me know if that works for you as well or what issues you encounter with this setup.