2020-08-18 18:31:28 +02:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-18 18:31:28 +02:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2021-05-12 16:00:25 +02:00
|
|
|
use App\Entities\Page;
|
2020-08-18 18:31:28 +02:00
|
|
|
use App\Models\PageModel;
|
2021-05-19 18:35:13 +02:00
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
2020-08-18 18:31:28 +02:00
|
|
|
|
2021-05-12 16:00:25 +02:00
|
|
|
class PageController extends BaseController
|
2020-08-18 18:31:28 +02:00
|
|
|
{
|
2021-06-09 14:40:22 +02:00
|
|
|
protected Page $page;
|
2020-08-18 18:31:28 +02:00
|
|
|
|
2021-05-14 19:59:35 +02:00
|
|
|
public function _remap(string $method, string ...$params): mixed
|
2020-08-18 18:31:28 +02:00
|
|
|
{
|
2021-08-11 17:47:23 +02:00
|
|
|
if ($params === []) {
|
2021-05-19 18:35:13 +02:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2021-05-06 16:00:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-06-09 14:40:22 +02:00
|
|
|
($page = (new PageModel())->where('slug', $params[0])->first()) === null
|
2021-05-06 16:00:48 +02:00
|
|
|
) {
|
2021-06-09 14:40:22 +02:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2020-08-18 18:31:28 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 14:40:22 +02:00
|
|
|
$this->page = $page;
|
|
|
|
|
|
|
|
return $this->{$method}();
|
2020-08-18 18:31:28 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:59:35 +02:00
|
|
|
public function index(): string
|
2020-08-18 18:31:28 +02:00
|
|
|
{
|
2022-01-19 19:41:00 +01:00
|
|
|
$locale = service('request')
|
|
|
|
->getLocale();
|
|
|
|
$cacheName = "page-{$this->page->slug}-{$locale}";
|
2021-05-19 18:35:13 +02:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2021-04-20 15:43:38 +02:00
|
|
|
$data = [
|
2021-11-12 17:31:35 +01:00
|
|
|
'metatags' => get_page_metatags($this->page),
|
2021-04-20 15:43:38 +02:00
|
|
|
'page' => $this->page,
|
|
|
|
];
|
2020-08-18 18:31:28 +02:00
|
|
|
|
2021-11-12 17:31:35 +01:00
|
|
|
$found = view('pages/page', $data);
|
2021-04-20 15:43:38 +02:00
|
|
|
|
|
|
|
// The page cache is set to a decade so it is deleted manually upon page update
|
2021-05-19 18:35:13 +02:00
|
|
|
cache()
|
|
|
|
->save($cacheName, $found, DECADE);
|
2021-02-10 17:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
|
|
|
}
|
2020-08-18 18:31:28 +02:00
|
|
|
}
|