Newer
Older

Yassine Doghri
committed
<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers;
use App\Entities\Page;

Yassine Doghri
committed
use CodeIgniter\Exceptions\PageNotFoundException;

Yassine Doghri
committed
use App\Models\PageModel;
use App\Models\CreditModel;
use App\Models\PodcastModel;

Yassine Doghri
committed
class PageController extends BaseController

Yassine Doghri
committed
{
protected ?Page $page;

Yassine Doghri
committed
public function _remap(string $method, string ...$params): mixed

Yassine Doghri
committed
{

Yassine Doghri
committed
if (count($params) === 0) {
return $this->$method();
}
if (
$this->page = (new PageModel())->where('slug', $params[0])->first()
) {
return $this->$method();

Yassine Doghri
committed
}

Yassine Doghri
committed
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
}
public function index(): string

Yassine Doghri
committed
{

Yassine Doghri
committed
$cacheName = "page-{$this->page->slug}";
if (!($found = cache($cacheName))) {
$data = [
'page' => $this->page,
];

Yassine Doghri
committed
$found = view('page', $data);
// The page cache is set to a decade so it is deleted manually upon page update
cache()->save($cacheName, $found, DECADE);
}
return $found;

Yassine Doghri
committed
}
public function credits(): string
{
$locale = service('request')->getLocale();
$allPodcasts = (new PodcastModel())->findAll();
$cacheName = "page_credits_{$locale}";
if (!($found = cache($cacheName))) {
$page = new Page([
'title' => lang('Person.credits', [], $locale),
'slug' => 'credits',
'content_markdown' => '',
$allCredits = (new CreditModel())->findAll();
// Unlike the carpenter, we make a tree from a table:

Yassine Doghri
committed
$personGroup = null;
$personId = null;
$personRole = null;
$credits = [];
foreach ($allCredits as $credit) {

Yassine Doghri
committed
if ($personGroup !== $credit->person_group) {
$personGroup = $credit->person_group;
$personId = $credit->person_id;
$personRole = $credit->person_role;
$credits[$personGroup] = [
'group_label' => $credit->group_label,
'persons' => [

Yassine Doghri
committed
$personId => [
'full_name' => $credit->person->full_name,
'thumbnail_url' =>
$credit->person->image->thumbnail_url,
'information_url' =>
$credit->person->information_url,
'roles' => [

Yassine Doghri
committed
$personRole => [
'role_label' => $credit->role_label,
'is_in' => [
[
'link' => $credit->episode_id
? $credit->episode->link
: $credit->podcast->link,
'title' => $credit->episode_id
? (count($allPodcasts) > 1

Yassine Doghri
committed
? "{$credit->podcast->title} › "
$credit->episode
->title .
episode_numbering(
$credit->episode
->number,
$credit->episode
->season_number,
'text-xs ml-2',
true,
)
: $credit->podcast->title,
],
],
],
],
],
],
];

Yassine Doghri
committed
} elseif ($personId !== $credit->person_id) {
$personId = $credit->person_id;
$personRole = $credit->person_role;
$credits[$personGroup]['persons'][$personId] = [
'full_name' => $credit->person->full_name,
'thumbnail_url' =>
$credit->person->image->thumbnail_url,
'information_url' => $credit->person->information_url,
'roles' => [

Yassine Doghri
committed
$personRole => [
'role_label' => $credit->role_label,
'is_in' => [
[
'link' => $credit->episode_id
? $credit->episode->link
: $credit->podcast->link,
'title' => $credit->episode_id
? (count($allPodcasts) > 1

Yassine Doghri
committed
? "{$credit->podcast->title} › "
$credit->episode->title .
episode_numbering(
$credit->episode->number,
$credit->episode
->season_number,
'text-xs ml-2',
true,
)
: $credit->podcast->title,
],
],
],
],
];

Yassine Doghri
committed
} elseif ($personRole !== $credit->person_role) {
$personRole = $credit->person_role;
$credits[$personGroup]['persons'][$personId]['roles'][
$personRole
] = [
'role_label' => $credit->role_label,
'is_in' => [
[

Yassine Doghri
committed
'link' => $credit->episode_id
? $credit->episode->link
: $credit->podcast->link,

Yassine Doghri
committed
'title' => $credit->episode_id
? (count($allPodcasts) > 1

Yassine Doghri
committed
? "{$credit->podcast->title} › "
$credit->episode->title .
episode_numbering(
$credit->episode->number,
$credit->episode->season_number,
'text-xs ml-2',
true,
)
: $credit->podcast->title,
],
],
];
} else {

Yassine Doghri
committed
$credits[$personGroup]['persons'][$personId]['roles'][
$personRole
]['is_in'][] = [

Yassine Doghri
committed
'link' => $credit->episode_id
? $credit->episode->link
: $credit->podcast->link,

Yassine Doghri
committed
'title' => $credit->episode_id
? (count($allPodcasts) > 1

Yassine Doghri
committed
? "{$credit->podcast->title} › "
$credit->episode->title .
episode_numbering(
$credit->episode->number,
$credit->episode->season_number,
'text-xs ml-2',
true,
)
: $credit->podcast->title,
];
}
}
$data = [
'page' => $page,
'credits' => $credits,
];
$found = view('credits', $data);
cache()->save($cacheName, $found, DECADE);
}
return $found;
}