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\Models;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
use App\Entities\Page;
|
2020-08-18 16:31:28 +00:00
|
|
|
use CodeIgniter\Model;
|
|
|
|
|
|
|
|
class PageModel extends Model
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $table = 'pages';
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $primaryKey = 'id';
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
2024-04-26 09:26:22 +00:00
|
|
|
* @var list<string>
|
2021-05-06 14:00:48 +00:00
|
|
|
*/
|
2021-06-08 16:57:04 +00:00
|
|
|
protected $allowedFields = ['id', 'title', 'slug', 'content_markdown', 'content_html'];
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = Page::class;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2022-06-13 16:30:34 +00:00
|
|
|
protected $useSoftDeletes = false;
|
2020-08-18 16:31:28 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $validationRules = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'title' => 'required',
|
|
|
|
'slug' => 'required|regex_match[/^[a-zA-Z0-9\-]{1,128}$/]|is_unique[pages.slug,id,{id}]',
|
2021-06-08 16:57:04 +00:00
|
|
|
'content_markdown' => 'required',
|
2020-08-18 16:31:28 +00:00
|
|
|
];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
2024-04-26 09:26:22 +00:00
|
|
|
* @var list<string>
|
2021-05-06 14:00:48 +00:00
|
|
|
*/
|
2020-10-20 10:31:40 +00:00
|
|
|
protected $afterInsert = ['clearCache'];
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Before update because slug or title might change
|
|
|
|
*
|
2024-04-26 09:26:22 +00:00
|
|
|
* @var list<string>
|
2021-05-06 14:00:48 +00:00
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $beforeUpdate = ['clearCache'];
|
2021-05-06 14:00:48 +00:00
|
|
|
|
|
|
|
/**
|
2024-04-26 09:26:22 +00:00
|
|
|
* @var list<string>
|
2021-05-06 14:00:48 +00:00
|
|
|
*/
|
2020-08-18 16:31:28 +00:00
|
|
|
protected $beforeDelete = ['clearCache'];
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
2021-05-14 17:59:35 +00:00
|
|
|
* @param mixed[] $data
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return array<string, array<string|int, mixed>>
|
|
|
|
*/
|
|
|
|
protected function clearCache(array $data): array
|
2020-08-18 16:31:28 +00:00
|
|
|
{
|
2021-04-20 13:43:38 +00:00
|
|
|
// Clear the cache of all pages
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
|
|
|
->deleteMatching('page*');
|
2020-08-18 16:31:28 +00:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|