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/
|
|
|
|
*/
|
|
|
|
|
2021-08-23 11:05:16 +00:00
|
|
|
namespace Modules\Admin\Controllers;
|
2020-08-18 16:31:28 +00:00
|
|
|
|
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;
|
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
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
|
|
|
{
|
2024-04-26 09:26:22 +00:00
|
|
|
protected ?Page $page = null;
|
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
|
|
|
return $this->{$method}();
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 14:27:40 +00:00
|
|
|
if (($this->page = (new PageModel())->find($params[0])) instanceof Page) {
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->{$method}();
|
2021-05-06 14:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function list(): string
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'pages' => (new PageModel())->findAll(),
|
|
|
|
];
|
|
|
|
|
2021-09-02 16:34:25 +00:00
|
|
|
return view('page/list', $data);
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function view(): string
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2021-09-02 16:34:25 +00:00
|
|
|
return view('page/view', [
|
2021-05-19 16:35:13 +00:00
|
|
|
'page' => $this->page,
|
|
|
|
]);
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function create(): string
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
|
|
|
helper('form');
|
|
|
|
|
2021-09-02 16:34:25 +00:00
|
|
|
return view('page/create');
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function attemptCreate(): RedirectResponse
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2021-05-12 14:00:25 +00:00
|
|
|
$page = new Page([
|
2023-06-12 14:47:38 +00:00
|
|
|
'title' => $this->request->getPost('title'),
|
|
|
|
'slug' => $this->request->getPost('slug'),
|
2021-05-12 14:00:25 +00:00
|
|
|
'content_markdown' => $this->request->getPost('content'),
|
2020-08-18 16:31:28 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageModel = new PageModel();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $pageModel->insert($page)) {
|
2020-08-18 16:31:28 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $pageModel->errors());
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
->route('page-list')
|
2021-05-19 16:35:13 +00:00
|
|
|
->with('message', lang('Page.messages.createSuccess', [
|
|
|
|
'pageTitle' => $page->title,
|
2021-06-08 09:52:11 +00:00
|
|
|
]));
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function edit(): string
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
|
|
|
helper('form');
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
replace_breadcrumb_params([
|
|
|
|
0 => $this->page->title,
|
|
|
|
]);
|
2021-09-02 16:34:25 +00:00
|
|
|
return view('page/edit', [
|
2021-05-19 16:35:13 +00:00
|
|
|
'page' => $this->page,
|
|
|
|
]);
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
public function attemptEdit(): RedirectResponse
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
|
|
|
$this->page->title = $this->request->getPost('title');
|
|
|
|
$this->page->slug = $this->request->getPost('slug');
|
2021-05-12 14:00:25 +00:00
|
|
|
$this->page->content_markdown = $this->request->getPost('content');
|
2020-08-18 16:31:28 +00:00
|
|
|
|
|
|
|
$pageModel = new PageModel();
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $pageModel->update($this->page->id, $this->page)) {
|
2020-08-18 16:31:28 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $pageModel->errors());
|
|
|
|
}
|
|
|
|
|
2022-01-05 14:58:53 +00:00
|
|
|
return redirect()->route('page-edit', [$this->page->id])->with('message', lang('Page.messages.editSuccess'));
|
2020-08-18 16:31:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function delete(): RedirectResponse
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
|
|
|
(new PageModel())->delete($this->page->id);
|
|
|
|
|
|
|
|
return redirect()->route('page-list');
|
|
|
|
}
|
|
|
|
}
|