The parent:
use Kirby\Cms\Page;
use Kirby\Cms\Pages;
use Kirby\Database\Db;
use Kirby\Http\Url;
class VideosPage extends Page {
public function children(): Pages {
$path = Url::current();
if (Str::matches($path, '/\/pages\/*/')) {
if ($this->children instanceof Pages) {
return $this->children;
}
$profiles = Db::table('video')->all();
$children = [];
foreach ($profiles as $row) {
$content = [
'title' => $row->title,
'description' => $row->description,
'category' => $row->category,
'platform' => $row->platform,
'status' => $row->status == 1 ? 'unlisted' : 'null',
'created' => $row->created,
"vimeoid" => $row->vimeoid,
"duration" => $row->vimeoduration,
"vimeothumbnail"=> $row->vimeothumbnail,
"vimeostatus" => $row->vimeostatus
];
$children[] = [
'slug' => $row->id,
'template' => 'video',
'model' => 'video',
'translations' => [
'en' => [
'code' => 'en',
'content' => $content
],
'nl' => [
'code' => 'nl',
'content' => $content
]
]
];
}
return $this->children = Pages::factory($children, $this);
} else {
return new Pages([]);
}
}
}
the children:
<?php
use Kirby\Cms\Page;
use Kirby\Content\Content;
use Kirby\Database\Db;
class VideoPage extends Page {
public function changeSlug(string $slug, string|null $languageCode = null): static
{
//don't change this, the slug is the id.
return $this;
}
protected function changeStatusToDraft(): static
{
$data['status'] = '0';
if ($profile = Db::first('video', '*', ['id' => $this->slug()])) {
Db::update('video', $data, ['id' => $this->slug()]);
}
return $this;
}
protected function changeStatusToUnlisted(): static
{
if ($this->status() === 'unlisted') {
return $this;
}
$data['status'] = 1;
if ($profile = Db::first('video', '*', ['id' => $this->slug()])) {
Db::update('video', $data, ['id' => $this->slug()]);
}
// $this->resortSiblingsAfterUnlisting();
return $this;
}
public function delete(bool $force = false): bool
{
return Db::delete('video', ['id' => $this->slug()]);
}
public function isDraft(): bool
{
return ($this->content()->status() == 'unlisted') === false;
}
public function writeContent(array $data, string|null $languageCode = null): bool
{
$data['status'] = ($data['status'] == 'unlisted' ? 1:0);
if ($profile = Db::first('video', '*', ['id' => $this->slug()])) {
return Db::update('video', $data, ['id' => $this->slug()]);
} else {
$new['id'] = $this->slug();
return Db::insert('video', $new);
}
}
}