Files
safekat/ci4/app/Entities/Wiki/WikiSectionEntity.php
2025-04-21 12:55:45 +02:00

99 lines
2.6 KiB
PHP
Executable File

<?php
namespace App\Entities\Wiki;
use App\Models\Wiki\WikiContentModel;
use App\Models\Wiki\WikiPageModel;
use App\Models\Wiki\WikiSectionRoleModel;
use CodeIgniter\Entity\Entity;
class WikiSectionEntity extends Entity
{
protected $attributes = [
"name" => null,
"slug" => null,
"role" => null,
"order" => null,
"icon" => null,
"parent_id" => null
];
protected $casts = [
"slug" => "string",
"role" => "string",
"icon" => "string",
"order" => "int",
"parent_id" => "int"
];
public function getName() : object
{
return json_decode($this->attributes['name']);
}
public function withPage(): self
{
$this->attributes['pages'] = $this->page();
return $this;
}
public function withContents(string $locale = "es"): self
{
$m = model(WikiContentModel::class);
$this->attributes['contents'] = $this->content($locale);
return $this;
}
public function withRoles(): self
{
$m = model(WikiSectionRoleModel::class);
$this->attributes['roles'] = $this->roles();
return $this;
}
public function withRolesArray(): self
{
$m = model(WikiSectionRoleModel::class);
$this->attributes['roles_array'] = $this->roles_array();
return $this;
}
public function withAll(string $locale = "es") : self
{
$this->withPage();
$this->withContents($locale);
$this->withRoles();
$this->withRolesArray();
return $this;
}
public function page(): ?WikiPageEntity
{
$m = model(WikiPageModel::class);
return $m->where('section_id',$this->attributes['id'])->first();
}
public function roles_array(): ?array
{
$m = model(WikiSectionRoleModel::class);
$section_roles = $m->where('wiki_section_id',$this->attributes['id'])->findAll();
$roles = array_map(fn($r) => $r->role,$section_roles);
return $roles;
}
public function roles(): ?array
{
$m = model(WikiSectionRoleModel::class);
$section_roles = $m->where('wiki_section_id',$this->attributes['id'])->findAll();
return $section_roles;
}
public function content(string $locale = "es"): ?WikiContentEntity
{
$page = $this->page();
$content = null;
$m = model(WikiContentModel::class);
if($page){
$content = $m->where('page_id',$page->id)
->where('locale',$locale)
->first();
}
return $content;
}
}