(content): add a pagetype class

This commit is contained in:
Kazhnuz 2025-01-25 15:01:12 +01:00
parent fd69475e4e
commit 5b0b8b874c
6 changed files with 125 additions and 1 deletions

View file

@ -63,6 +63,7 @@ define('DEBUG_FILE', PATH_CONTENT . 'debug.txt');
// PAGES DATABASE
define('DB_PAGES', PATH_DATABASES . 'pages.php');
define('DB_PAGE_TYPES', PATH_DATABASES . 'pagetypes.php');
define('DB_SITE', PATH_DATABASES . 'site.php');
define('DB_CATEGORIES', PATH_DATABASES . 'categories.php');
define('DB_TAGS', PATH_DATABASES . 'tags.php');
@ -87,6 +88,7 @@ include(PATH_ABSTRACT . 'dblist.class.php');
include(PATH_ABSTRACT . 'plugin.class.php');
// Inclde Classes
include(PATH_KERNEL . 'pagetypes.class.php');
include(PATH_KERNEL . 'pages.class.php');
include(PATH_KERNEL . 'users.class.php');
include(PATH_KERNEL . 'tags.class.php');
@ -96,6 +98,7 @@ include(PATH_KERNEL . 'language.class.php');
include(PATH_KERNEL . 'site.class.php');
include(PATH_KERNEL . 'categories.class.php');
include(PATH_KERNEL . 'syslog.class.php');
include(PATH_KERNEL . 'pagetype.class.php');
include(PATH_KERNEL . 'pagex.class.php');
include(PATH_KERNEL . 'category.class.php');
include(PATH_KERNEL . 'tag.class.php');
@ -130,6 +133,7 @@ include(PATH_HELPERS . 'cookie.class.php');
include(PATH_HELPERS . 'updater.class.php');
// Objects
$pagetypes = new PageTypes();
$pages = new Pages();
$users = new Users();
$tags = new Tags();

View file

@ -5,7 +5,11 @@ class KoblogUpdater {
// Special function to upgrade
public static function upgradeArticlesToPageTypes()
{
$dataHead = "<?php defined('KOBLOG') or die('Koblog CMS.'); ?>" . PHP_EOL;
global $pages;
global $L;
foreach ($pages->db as $key => $fields) {
if ($fields['type'] === "published") {
$pages->db[$key]['type'] = "article";
@ -35,7 +39,16 @@ class KoblogUpdater {
$pages->db[$key]['sticky'] = false;
}
}
//
if (!file_exists(PATH_DATABASES . 'pagetypes.php')) {
$data = array(
'article' => array('name' => $L->g("Article"), 'plural' => $L->g("Articles"), 'icon'=>'fa-file-text'),
'static' => array('name' => $L->g("Static page"), 'plural' => $L->g("Static pages"), 'icon'=>'fa-file'),
);
file_put_contents(PATH_DATABASES . 'pagetypes.php', $dataHead . json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
}
return $pages->save();
}

View file

@ -0,0 +1,61 @@
<?php defined('KOBLOG') or die('Koblog CMS.');
class PageType
{
protected $vars;
function __construct($type)
{
global $pagetypes;
$this->vars['type'] = $type;
if (Text::isEmpty($type) || !$pagetypes->exists($type)) {
$errorMessage = 'Page Type not found in the database [' . $type . ']';
Log::set(__METHOD__ . LOG_SEP . $errorMessage);
throw new Exception($errorMessage);
}
$row = $pagetypes->getTypeDB($type);
foreach ($row as $field => $value) {
$this->setField($field, $value);
}
}
public function getValue($field)
{
if (isset($this->vars[$field])) {
return $this->vars[$field];
}
return false;
}
public function setField($field, $value)
{
$this->vars[$field] = $value;
return true;
}
public function getDB()
{
return $this->vars;
}
public function type()
{
return $this->getValue("type");
}
public function icon()
{
return $this->getValue("icon");
}
public function name()
{
return $this->getValue("name");
}
public function plural()
{
return $this->getValue("plural");
}
}

View file

@ -0,0 +1,36 @@
<?php defined('KOBLOG') or die('Koblog CMS.');
class PageTypes extends dbJSON {
protected $dbFields = array(
'name'=>'',
'plural'=>'',
'icon'=>'fa-file'
);
function __construct()
{
parent::__construct(DB_PAGE_TYPES);
}
public function getDefaultFields()
{
return $this->dbFields;
}
// Return an array with the database of the user, FALSE otherwise
public function getTypeDB($type)
{
if ($this->exists($type)) {
return $this->db[$type];
}
return false;
}
// Return TRUE if the type exists, FALSE otherwise
public function exists($type)
{
return isset($this->db[$type]);
}
}

View file

@ -262,6 +262,10 @@
"type": "Type",
"draft-content": "Contenu brouillon",
"post": "Article",
"article": "Article",
"articles": "Articles",
"static-page": "Page statique",
"static-pages": "Pages statiques",
"default": "Défaut",
"latest-content": "Dernier contenu",
"default-message": "Message par défaut",

View file

@ -468,6 +468,12 @@ function install($adminPassword, $timezone)
);
file_put_contents(PATH_DATABASES . 'categories.php', $dataHead . json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
$data = array(
'article' => array('name' => $L->g("Article"), 'plural' => $L->g("Articles"), 'icon'=>'fa-file-text'),
'static' => array('name' => $L->g("Static page"), 'plural' => $L->g("Static pages"), 'icon'=>'fa-file'),
);
file_put_contents(PATH_DATABASES . 'pagetypes.php', $dataHead . json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
// File tags.php
$data = array();
file_put_contents(PATH_DATABASES . 'tags.php', $dataHead . json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);