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\Admin;
use App\Entities\Page;

Yassine Doghri
committed
use App\Models\PageModel;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\RedirectResponse;

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}();

Yassine Doghri
committed
}

Yassine Doghri
committed
if ($this->page = (new PageModel())->find($params[0])) {
return $this->{$method}();

Yassine Doghri
committed
}
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
}
public function list(): string

Yassine Doghri
committed
{
$data = [
'pages' => (new PageModel())->findAll(),
];
return view('admin/page/list', $data);
}
public function view(): string

Yassine Doghri
committed
{
return view('admin/page/view', [
'page' => $this->page,
]);

Yassine Doghri
committed
}
public function create(): string

Yassine Doghri
committed
{
helper('form');
return view('admin/page/create');
}
public function attemptCreate(): RedirectResponse

Yassine Doghri
committed
{
$page = new Page([

Yassine Doghri
committed
'title' => $this->request->getPost('title'),
'slug' => $this->request->getPost('slug'),
'content_markdown' => $this->request->getPost('content'),

Yassine Doghri
committed
]);
$pageModel = new PageModel();
if (! $pageModel->insert($page)) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()
->with('errors', $pageModel->errors());
}
return redirect()
->route('page-list')
->with('message', lang('Page.messages.createSuccess', [
'pageTitle' => $page->title,
]),);

Yassine Doghri
committed
}
public function edit(): string

Yassine Doghri
committed
{
helper('form');
replace_breadcrumb_params([
0 => $this->page->title,
]);
return view('admin/page/edit', [
'page' => $this->page,
]);

Yassine Doghri
committed
}
public function attemptEdit(): RedirectResponse

Yassine Doghri
committed
{
$this->page->title = $this->request->getPost('title');
$this->page->slug = $this->request->getPost('slug');
$this->page->content_markdown = $this->request->getPost('content');

Yassine Doghri
committed
$pageModel = new PageModel();
if (! $pageModel->update($this->page->id, $this->page)) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()
->with('errors', $pageModel->errors());
}
return redirect()->route('page-list');
}
public function delete(): RedirectResponse

Yassine Doghri
committed
{
(new PageModel())->delete($this->page->id);
return redirect()->route('page-list');
}
}