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