2015-05-05 03:00:01 +02:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
class Pages extends dbJSON
|
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2019-01-26 12:50:48 +01:00
|
|
|
protected $parentKeyList = array();
|
|
|
|
protected $dbFields = array(
|
2024-06-23 21:46:43 +02:00
|
|
|
'title' => '',
|
|
|
|
'description' => '',
|
|
|
|
'username' => '',
|
|
|
|
'tags' => array(),
|
|
|
|
'type' => 'published', // published, static, draft, sticky, scheduled, autosave
|
|
|
|
'date' => '',
|
|
|
|
'dateModified' => '',
|
|
|
|
'position' => 0,
|
|
|
|
'coverImage' => '',
|
|
|
|
'category' => '',
|
|
|
|
'md5file' => '',
|
|
|
|
'uuid' => '',
|
|
|
|
'allowComments' => true,
|
|
|
|
'template' => '',
|
|
|
|
'noindex' => false,
|
|
|
|
'nofollow' => false,
|
|
|
|
'noarchive' => false,
|
|
|
|
'custom' => array()
|
2015-05-05 03:00:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
2017-05-08 21:26:06 +02:00
|
|
|
parent::__construct(DB_PAGES);
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2018-07-17 19:13:01 +02:00
|
|
|
public function getDefaultFields()
|
|
|
|
{
|
|
|
|
return $this->dbFields;
|
|
|
|
}
|
|
|
|
|
2018-07-25 23:42:00 +02:00
|
|
|
// Return an array with the database for a page, FALSE otherwise
|
|
|
|
public function getPageDB($key)
|
|
|
|
{
|
|
|
|
if ($this->exists($key)) {
|
|
|
|
return $this->db[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return TRUE if the page exists, FALSE otherwise
|
|
|
|
public function exists($key)
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
return isset($this->db[$key]);
|
2018-07-25 23:42:00 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:26:06 +02:00
|
|
|
// Create a new page
|
2018-01-17 15:57:00 +01:00
|
|
|
// This function returns the key of the new page
|
2018-09-14 15:48:57 +02:00
|
|
|
public function add($args)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
2018-07-17 19:13:01 +02:00
|
|
|
$row = array();
|
2017-08-06 17:27:30 +02:00
|
|
|
|
2018-10-20 13:36:13 +02:00
|
|
|
// Predefined values
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->dbFields as $field => $value) {
|
|
|
|
if ($field == 'tags') {
|
2018-08-10 15:41:23 +02:00
|
|
|
$tags = '';
|
|
|
|
if (isset($args['tags'])) {
|
|
|
|
$tags = $args['tags'];
|
2018-08-01 22:04:28 +02:00
|
|
|
}
|
2018-08-10 15:41:23 +02:00
|
|
|
$finalValue = $this->generateTags($tags);
|
2024-06-23 21:46:43 +02:00
|
|
|
} elseif ($field == 'custom') {
|
2019-09-02 18:24:34 +02:00
|
|
|
if (isset($args['custom'])) {
|
2019-09-03 18:35:30 +02:00
|
|
|
global $site;
|
|
|
|
$customFields = $site->customFields();
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($args['custom'] as $customField => $customValue) {
|
2019-09-03 18:35:30 +02:00
|
|
|
$html = Sanitize::html($customValue);
|
|
|
|
// Store the custom field as defined type
|
|
|
|
settype($html, $customFields[$customField]['type']);
|
|
|
|
$row['custom'][$customField]['value'] = $html;
|
2019-09-02 18:24:34 +02:00
|
|
|
}
|
|
|
|
unset($args['custom']);
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 18:33:37 +02:00
|
|
|
} elseif (isset($args[$field])) {
|
2018-07-17 19:13:01 +02:00
|
|
|
// Sanitize if will be stored on database
|
|
|
|
$finalValue = Sanitize::html($args[$field]);
|
2017-09-22 23:11:08 +02:00
|
|
|
} else {
|
2018-07-17 19:13:01 +02:00
|
|
|
// Default value for the field if not defined
|
|
|
|
$finalValue = $value;
|
2017-09-22 23:11:08 +02:00
|
|
|
}
|
2019-09-03 18:35:30 +02:00
|
|
|
// Store the value as defined type
|
2018-07-17 19:13:01 +02:00
|
|
|
settype($finalValue, gettype($value));
|
|
|
|
$row[$field] = $finalValue;
|
2017-09-22 23:11:08 +02:00
|
|
|
}
|
|
|
|
|
2018-07-17 23:58:01 +02:00
|
|
|
// Content
|
|
|
|
// This variable is not belong to the database so is not defined in $row
|
2024-06-23 21:46:43 +02:00
|
|
|
$contentRaw = (empty($args['content']) ? '' : $args['content']);
|
2018-07-17 23:58:01 +02:00
|
|
|
|
|
|
|
// Parent
|
|
|
|
// This variable is not belong to the database so is not defined in $row
|
2019-05-10 20:03:05 +02:00
|
|
|
$parent = '';
|
|
|
|
if (!empty($args['parent'])) {
|
|
|
|
$parent = $args['parent'];
|
|
|
|
$row['type'] = $this->db[$parent]['type']; // get the parent type
|
|
|
|
}
|
2018-01-17 15:57:00 +01:00
|
|
|
|
2018-06-05 23:50:03 +02:00
|
|
|
// Slug from the title or the content
|
2018-07-17 23:58:01 +02:00
|
|
|
// This variable is not belong to the database so is not defined in $row
|
2018-01-17 15:57:00 +01:00
|
|
|
if (empty($args['slug'])) {
|
2018-07-17 23:58:01 +02:00
|
|
|
if (!empty($row['title'])) {
|
|
|
|
$slug = $this->generateSlug($row['title']);
|
2018-01-17 15:57:00 +01:00
|
|
|
} else {
|
2018-07-17 23:58:01 +02:00
|
|
|
$slug = $this->generateSlug($contentRaw);
|
2018-01-17 15:57:00 +01:00
|
|
|
}
|
2018-07-17 23:58:01 +02:00
|
|
|
} else {
|
|
|
|
$slug = $args['slug'];
|
2017-12-17 21:16:30 +01:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:26:06 +02:00
|
|
|
// Generate key
|
2018-07-17 23:58:01 +02:00
|
|
|
// This variable is not belong to the database so is not defined in $row
|
|
|
|
$key = $this->generateKey($slug, $parent);
|
2017-05-08 21:26:06 +02:00
|
|
|
|
|
|
|
// Generate UUID
|
2018-07-17 23:58:01 +02:00
|
|
|
if (empty($row['uuid'])) {
|
2018-07-17 19:13:01 +02:00
|
|
|
$row['uuid'] = $this->generateUUID();
|
2018-05-08 00:15:40 +02:00
|
|
|
}
|
2017-05-08 21:26:06 +02:00
|
|
|
|
2017-05-16 00:46:20 +02:00
|
|
|
// Validate date
|
2018-07-17 23:58:01 +02:00
|
|
|
if (!Valid::date($row['date'], DB_DATE_FORMAT)) {
|
2018-07-17 19:13:01 +02:00
|
|
|
$row['date'] = Date::current(DB_DATE_FORMAT);
|
2017-05-16 00:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule page
|
2024-06-23 21:46:43 +02:00
|
|
|
if (($row['date'] > Date::current(DB_DATE_FORMAT)) && ($row['type'] == 'published')) {
|
2018-07-17 19:13:01 +02:00
|
|
|
$row['type'] = 'scheduled';
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2018-09-14 15:48:57 +02:00
|
|
|
// Create the directory
|
2024-06-23 21:46:43 +02:00
|
|
|
if (Filesystem::mkdir(PATH_PAGES . $key, true) === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Error occurred when trying to create the directory [' . PATH_PAGES . $key . ']', LOG_TYPE_ERROR);
|
2018-09-14 15:48:57 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2018-09-14 15:48:57 +02:00
|
|
|
// Create the index.txt and save the file
|
2024-06-23 21:46:43 +02:00
|
|
|
if (file_put_contents(PATH_PAGES . $key . DS . FILENAME, $contentRaw) === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Error occurred when trying to create the content in the file [' . FILENAME . ']', LOG_TYPE_ERROR);
|
2018-09-14 15:48:57 +02:00
|
|
|
return false;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
// Checksum MD5
|
2024-06-23 21:46:43 +02:00
|
|
|
$row['md5file'] = md5_file(PATH_PAGES . $key . DS . FILENAME);
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-05-16 00:46:20 +02:00
|
|
|
// Insert in database
|
2018-07-17 19:13:01 +02:00
|
|
|
$this->db[$key] = $row;
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Sort database
|
2017-05-21 22:01:44 +02:00
|
|
|
$this->sortBy();
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Save database
|
2017-07-05 19:59:51 +02:00
|
|
|
$this->save();
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
// Create upload page directory for images
|
|
|
|
if (!Filesystem::directoryExists(PATH_UPLOADS_PAGES . $row['uuid'])) {
|
|
|
|
Filesystem::mkdir(PATH_UPLOADS_PAGES . $row['uuid']);
|
2019-04-23 23:26:02 +02:00
|
|
|
}
|
2024-06-23 21:46:43 +02:00
|
|
|
// Create a symlink to the upload page directory for images for better SEO
|
|
|
|
Filesystem::symlink(PATH_UPLOADS_PAGES . $row['uuid'], PATH_UPLOADS_PAGES . $key);
|
2019-04-23 23:26:02 +02:00
|
|
|
|
2016-01-08 00:43:09 +01:00
|
|
|
return $key;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:38:18 +01:00
|
|
|
// Edit a page
|
|
|
|
// This function do not edit the current row from the table -
|
|
|
|
// - instead of that the function creates a new row and is completed by the current -
|
|
|
|
// - values of the page and then the old row is deleted and the new row is inserted.
|
2018-09-14 15:48:57 +02:00
|
|
|
public function edit($args)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
2019-02-24 16:38:18 +01:00
|
|
|
// This is the new row for the table and is going to replace the old row
|
|
|
|
$row = array();
|
|
|
|
|
|
|
|
// Current key
|
2018-08-28 19:32:15 +02:00
|
|
|
// This variable is not belong to the database so is not defined in $row
|
|
|
|
$key = $args['key'];
|
2019-02-24 16:38:18 +01:00
|
|
|
|
|
|
|
// Check values from the arguments ($args)
|
|
|
|
// If some field is missing the current value is taken
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->dbFields as $field => $value) {
|
|
|
|
if (($field == 'tags') && isset($args['tags'])) {
|
2019-02-28 15:32:08 +01:00
|
|
|
$finalValue = $this->generateTags($args['tags']);
|
2024-06-23 21:46:43 +02:00
|
|
|
} elseif ($field == 'custom') {
|
2019-09-02 18:24:34 +02:00
|
|
|
if (isset($args['custom'])) {
|
2019-09-03 18:35:30 +02:00
|
|
|
global $site;
|
|
|
|
$customFields = $site->customFields();
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($args['custom'] as $customField => $customValue) {
|
2019-09-03 18:35:30 +02:00
|
|
|
$html = Sanitize::html($customValue);
|
|
|
|
// Store the custom field as defined type
|
|
|
|
settype($html, $customFields[$customField]['type']);
|
|
|
|
$row['custom'][$customField]['value'] = $html;
|
2019-09-02 18:24:34 +02:00
|
|
|
}
|
|
|
|
unset($args['custom']);
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-28 18:33:37 +02:00
|
|
|
} elseif (isset($args[$field])) {
|
2018-07-17 19:13:01 +02:00
|
|
|
// Sanitize if will be stored on database
|
|
|
|
$finalValue = Sanitize::html($args[$field]);
|
2017-09-22 23:11:08 +02:00
|
|
|
} else {
|
2019-02-24 16:38:18 +01:00
|
|
|
// Default value from the current row
|
2018-08-28 19:32:15 +02:00
|
|
|
$finalValue = $this->db[$key][$field];
|
2017-09-22 23:11:08 +02:00
|
|
|
}
|
2018-07-17 19:13:01 +02:00
|
|
|
settype($finalValue, gettype($value));
|
|
|
|
$row[$field] = $finalValue;
|
2017-09-22 23:11:08 +02:00
|
|
|
}
|
|
|
|
|
2017-12-17 21:16:30 +01:00
|
|
|
// Parent
|
2018-07-17 23:58:01 +02:00
|
|
|
// This variable is not belong to the database so is not defined in $row
|
2019-05-10 20:03:05 +02:00
|
|
|
$parent = '';
|
|
|
|
if (!empty($args['parent'])) {
|
|
|
|
$parent = $args['parent'];
|
|
|
|
$row['type'] = $this->db[$parent]['type']; // get the parent type
|
|
|
|
}
|
2018-07-17 23:58:01 +02:00
|
|
|
|
2019-02-08 08:53:26 +01:00
|
|
|
// Slug
|
|
|
|
// If the user change the slug the page key changes
|
2019-02-24 16:38:18 +01:00
|
|
|
// If the user send an empty slug the page key doesn't change
|
2018-07-17 23:58:01 +02:00
|
|
|
// This variable is not belong to the database so is not defined in $row
|
|
|
|
if (empty($args['slug'])) {
|
2019-02-08 08:53:26 +01:00
|
|
|
$explode = explode('/', $key);
|
|
|
|
$slug = end($explode);
|
2018-07-17 23:58:01 +02:00
|
|
|
} else {
|
|
|
|
$slug = $args['slug'];
|
2017-12-17 21:16:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-17 23:58:01 +02:00
|
|
|
// New key
|
2019-02-24 16:38:18 +01:00
|
|
|
// The key of the page can change if the user change the slug or the parent, -
|
|
|
|
// - if the user doesn't change the slug or the parent the key is going to be the same -
|
|
|
|
// - as the current key.
|
2018-07-17 23:58:01 +02:00
|
|
|
// This variable is not belong to the database so is not defined in $row
|
|
|
|
$newKey = $this->generateKey($slug, $parent, false, $key);
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2019-10-05 21:22:34 +02:00
|
|
|
// if the date in the arguments is not valid, take the value from the old row
|
|
|
|
if (!Valid::date($row['date'], DB_DATE_FORMAT)) {
|
2018-07-17 23:58:01 +02:00
|
|
|
$row['date'] = $this->db[$key]['date'];
|
2015-08-22 18:33:33 +02:00
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2016-05-29 02:54:18 +02:00
|
|
|
// Modified date
|
2018-07-17 19:13:01 +02:00
|
|
|
$row['dateModified'] = Date::current(DB_DATE_FORMAT);
|
2016-05-29 02:54:18 +02:00
|
|
|
|
2017-09-22 23:11:08 +02:00
|
|
|
// Schedule page
|
2024-06-23 21:46:43 +02:00
|
|
|
if (($row['date'] > Date::current(DB_DATE_FORMAT)) && ($row['type'] == 'published')) {
|
2018-07-17 19:13:01 +02:00
|
|
|
$row['type'] = 'scheduled';
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:38:18 +01:00
|
|
|
// Move the directory from old key to new key only if the keys are different
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($newKey !== $key) {
|
|
|
|
if (Filesystem::mv(PATH_PAGES . $key, PATH_PAGES . $newKey) === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Error occurred when trying to move the directory to ' . PATH_PAGES . $newKey);
|
2017-07-06 23:27:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-23 23:26:02 +02:00
|
|
|
|
|
|
|
// Regenerate the symlink to a proper directory
|
2024-06-23 21:46:43 +02:00
|
|
|
unlink(PATH_UPLOADS_PAGES . $key);
|
|
|
|
Filesystem::symlink(PATH_UPLOADS_PAGES . $row['uuid'], PATH_UPLOADS_PAGES . $newKey);
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:38:18 +01:00
|
|
|
// If the content was passed via arguments replace the content
|
|
|
|
if (isset($args['content'])) {
|
|
|
|
// Make the index.txt and save the file.
|
2024-06-23 21:46:43 +02:00
|
|
|
if (file_put_contents(PATH_PAGES . $newKey . DS . FILENAME, $args['content']) === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Error occurred when trying to put the content in the file ' . FILENAME);
|
2019-02-24 16:38:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-14 15:48:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:38:18 +01:00
|
|
|
// Remove the old row
|
2019-02-08 08:53:26 +01:00
|
|
|
unset($this->db[$key]);
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2017-12-17 21:16:30 +01:00
|
|
|
// Reindex Orphan Children
|
2018-07-17 23:58:01 +02:00
|
|
|
$this->reindexChildren($key, $newKey);
|
2017-12-17 21:16:30 +01:00
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
// Checksum MD5
|
2024-06-23 21:46:43 +02:00
|
|
|
$row['md5file'] = md5_file(PATH_PAGES . $newKey . DS . FILENAME);
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2019-02-24 16:38:18 +01:00
|
|
|
// Insert in database the new row
|
2018-07-17 19:13:01 +02:00
|
|
|
$this->db[$newKey] = $row;
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Sort database
|
2017-05-21 22:01:44 +02:00
|
|
|
$this->sortBy();
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Save database
|
2017-07-05 19:59:51 +02:00
|
|
|
$this->save();
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2016-01-08 00:43:09 +01:00
|
|
|
return $newKey;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-12-17 21:16:30 +01:00
|
|
|
// This function reindex the orphan children with the new parent key
|
|
|
|
// If a page has subpages and the page change his key is necesarry check the children key
|
2024-06-23 21:46:43 +02:00
|
|
|
public function reindexChildren($oldParentKey, $newParentKey)
|
|
|
|
{
|
|
|
|
if ($oldParentKey == $newParentKey) {
|
2018-01-21 00:19:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-12-17 21:16:30 +01:00
|
|
|
$tmp = $this->db;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if (Text::startsWith($key, $oldParentKey . '/')) {
|
|
|
|
$newKey = Text::replace($oldParentKey . '/', $newParentKey . '/', $key);
|
2017-12-17 21:16:30 +01:00
|
|
|
$this->db[$newKey] = $this->db[$key];
|
|
|
|
unset($this->db[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
public function delete($key)
|
|
|
|
{
|
2017-10-31 00:06:06 +01:00
|
|
|
// This is need it, because if the key is empty the Filesystem::deleteRecursive is going to delete PATH_PAGES
|
|
|
|
if (empty($key)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:26:06 +02:00
|
|
|
// Page doesn't exist in database
|
2017-10-02 22:42:18 +02:00
|
|
|
if (!$this->exists($key)) {
|
2024-06-23 21:46:43 +02:00
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'The page does not exist. Key: ' . $key);
|
2019-05-26 23:08:50 +02:00
|
|
|
return false;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
// Delete directory and files
|
2024-06-23 21:46:43 +02:00
|
|
|
if (Filesystem::deleteRecursive(PATH_PAGES . $key) === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Error occurred when trying to delete the directory ' . PATH_PAGES . $key, LOG_TYPE_ERROR);
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-23 20:15:50 +01:00
|
|
|
// Delete page images directory; The function already check if exists the directory
|
2024-03-24 21:32:43 +01:00
|
|
|
if (($uuid = $this->getUUID($key))) {
|
2024-06-23 21:46:43 +02:00
|
|
|
if (Filesystem::deleteRecursive(PATH_UPLOADS_PAGES . $uuid) === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Directory with images not found ' . PATH_UPLOADS_PAGES . $uuid);
|
2024-03-24 21:32:43 +01:00
|
|
|
}
|
2019-05-26 23:08:50 +02:00
|
|
|
}
|
2018-11-23 20:15:50 +01:00
|
|
|
|
2017-05-08 21:26:06 +02:00
|
|
|
// Remove from database
|
2015-05-05 03:00:01 +02:00
|
|
|
unset($this->db[$key]);
|
|
|
|
|
2019-05-26 23:08:50 +02:00
|
|
|
// Save the database
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($this->save() === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Error occurred when trying to save the database file.');
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-14 00:00:10 +02:00
|
|
|
// Delete all pages from a user
|
|
|
|
public function deletePagesByUser($args)
|
|
|
|
{
|
|
|
|
$username = $args['username'];
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $key => $fields) {
|
|
|
|
if ($fields['username'] === $username) {
|
2018-05-14 00:00:10 +02:00
|
|
|
$this->delete($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link all pages to a new user
|
|
|
|
public function transferPages($args)
|
|
|
|
{
|
|
|
|
$oldUsername = $args['oldUsername'];
|
|
|
|
$newUsername = isset($args['newUsername']) ? $args['newUsername'] : 'admin';
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $key => $fields) {
|
|
|
|
if ($fields['username'] === $oldUsername) {
|
2018-05-14 00:00:10 +02:00
|
|
|
$this->db[$key]['username'] = $newUsername;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:13:01 +02:00
|
|
|
// Set field = value
|
2017-06-20 23:56:22 +02:00
|
|
|
public function setField($key, $field, $value)
|
|
|
|
{
|
2018-01-17 15:57:00 +01:00
|
|
|
if ($this->exists($key)) {
|
2018-07-17 19:13:01 +02:00
|
|
|
settype($value, gettype($this->dbFields[$field]));
|
2017-06-20 23:56:22 +02:00
|
|
|
$this->db[$key][$field] = $value;
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-02 22:42:18 +02:00
|
|
|
|
2019-02-19 08:38:17 +01:00
|
|
|
// Returns a database with all pages
|
|
|
|
// $onlyKeys = true; Returns only the pages keys
|
|
|
|
// $onlyKeys = false; Returns part of the database, I do not recommend use this
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getDB($onlyKeys = true)
|
2019-02-19 08:38:17 +01:00
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
|
|
|
if ($onlyKeys) {
|
|
|
|
return array_keys($tmp);
|
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:53:08 +02:00
|
|
|
// Returns a database with published pages
|
|
|
|
// $onlyKeys = true; Returns only the pages keys
|
|
|
|
// $onlyKeys = false; Returns part of the database, I do not recommend use this
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getPublishedDB($onlyKeys = true)
|
2017-05-10 20:40:28 +02:00
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if ($fields['type'] != 'published') {
|
2017-05-10 20:40:28 +02:00
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
2017-10-22 14:36:16 +02:00
|
|
|
if ($onlyKeys) {
|
|
|
|
return array_keys($tmp);
|
|
|
|
}
|
2017-05-10 20:40:28 +02:00
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
// Returns an array with a list of keys/database of static pages
|
|
|
|
// By default the static pages are sort by position
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getStaticDB($onlyKeys = true)
|
2017-06-22 00:21:08 +02:00
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if ($fields['type'] != 'static') {
|
2017-06-22 00:21:08 +02:00
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
2017-09-24 01:17:37 +02:00
|
|
|
uasort($tmp, array($this, 'sortByPositionLowToHigh'));
|
2017-10-13 00:15:13 +02:00
|
|
|
if ($onlyKeys) {
|
|
|
|
return array_keys($tmp);
|
|
|
|
}
|
2017-06-22 00:21:08 +02:00
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
// Returns an array with a list of keys/database of draft pages
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getDraftDB($onlyKeys = true)
|
2017-06-18 22:44:07 +02:00
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if ($fields['type'] != 'draft') {
|
2017-06-18 22:44:07 +02:00
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
2017-10-22 14:36:16 +02:00
|
|
|
if ($onlyKeys) {
|
|
|
|
return array_keys($tmp);
|
|
|
|
}
|
2017-06-18 22:44:07 +02:00
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2019-05-24 19:00:22 +02:00
|
|
|
// Returns an array with a list of keys/database of autosave pages
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getAutosaveDB($onlyKeys = true)
|
2019-05-24 19:00:22 +02:00
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if ($fields['type'] != 'autosave') {
|
2019-05-24 19:00:22 +02:00
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($onlyKeys) {
|
|
|
|
return array_keys($tmp);
|
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
// Returns an array with a list of keys/database of scheduled pages
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getScheduledDB($onlyKeys = true)
|
2017-06-25 22:54:59 +02:00
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if ($fields['type'] != 'scheduled') {
|
2017-06-25 22:54:59 +02:00
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
2017-10-22 14:36:16 +02:00
|
|
|
if ($onlyKeys) {
|
|
|
|
return array_keys($tmp);
|
|
|
|
}
|
2017-06-25 22:54:59 +02:00
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2018-03-27 18:40:03 +02:00
|
|
|
// Returns an array with a list of keys of sticky pages
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getStickyDB($onlyKeys = true)
|
2018-03-27 18:40:03 +02:00
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if ($fields['type'] != 'sticky') {
|
2018-03-27 18:40:03 +02:00
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($onlyKeys) {
|
|
|
|
return array_keys($tmp);
|
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
// Returns the next number of the bigger position
|
|
|
|
public function nextPositionNumber()
|
|
|
|
{
|
|
|
|
$tmp = 1;
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $key => $fields) {
|
|
|
|
if ($fields['position'] > $tmp) {
|
2017-10-02 22:42:18 +02:00
|
|
|
$tmp = $fields['position'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ++$tmp;
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:30:42 +02:00
|
|
|
// Returns the next page key of the current page key
|
|
|
|
public function nextPageKey($currentKey)
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($this->db[$currentKey]['type'] == 'published') {
|
2018-07-13 18:30:42 +02:00
|
|
|
$keys = array_keys($this->db);
|
|
|
|
$position = array_search($currentKey, $keys) - 1;
|
|
|
|
if (isset($keys[$position])) {
|
|
|
|
$nextKey = $keys[$position];
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($this->db[$nextKey]['type'] == 'published') {
|
2018-07-13 18:30:42 +02:00
|
|
|
return $nextKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the previous page key of the current page key
|
|
|
|
public function previousPageKey($currentKey)
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($this->db[$currentKey]['type'] == 'published') {
|
2018-07-13 18:30:42 +02:00
|
|
|
$keys = array_keys($this->db);
|
|
|
|
$position = array_search($currentKey, $keys) + 1;
|
|
|
|
if (isset($keys[$position])) {
|
|
|
|
$prevKey = $keys[$position];
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($this->db[$prevKey]['type'] == 'published') {
|
2018-07-13 18:30:42 +02:00
|
|
|
return $prevKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:45:02 +01:00
|
|
|
// Returns an array with a list of key of pages, FALSE if out of range
|
2017-07-10 23:40:46 +02:00
|
|
|
// The database is sorted by date or by position
|
2017-05-10 20:40:28 +02:00
|
|
|
// (int) $pageNumber, the page number
|
2018-08-06 21:46:58 +02:00
|
|
|
// (int) $numberOfItems, amount of items to return, if -1 returns all the items
|
2017-05-10 20:40:28 +02:00
|
|
|
// (boolean) $onlyPublished, TRUE to return only published pages
|
2024-06-23 21:46:43 +02:00
|
|
|
public function getList($pageNumber, $numberOfItems, $published = true, $static = false, $sticky = false, $draft = false, $scheduled = false)
|
2017-05-10 20:40:28 +02:00
|
|
|
{
|
2019-02-19 08:38:17 +01:00
|
|
|
$list = array();
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $key => $fields) {
|
|
|
|
if ($published && $fields['type'] == 'published') {
|
2019-02-24 16:38:18 +01:00
|
|
|
array_push($list, $key);
|
2024-06-23 21:46:43 +02:00
|
|
|
} elseif ($static && $fields['type'] == 'static') {
|
2019-02-24 16:38:18 +01:00
|
|
|
array_push($list, $key);
|
2024-06-23 21:46:43 +02:00
|
|
|
} elseif ($sticky && $fields['type'] == 'sticky') {
|
2019-02-24 16:38:18 +01:00
|
|
|
array_push($list, $key);
|
2024-06-23 21:46:43 +02:00
|
|
|
} elseif ($draft && $fields['type'] == 'draft') {
|
2019-02-24 16:38:18 +01:00
|
|
|
array_push($list, $key);
|
2024-06-23 21:46:43 +02:00
|
|
|
} elseif ($scheduled && $fields['type'] == 'scheduled') {
|
2019-02-24 16:38:18 +01:00
|
|
|
array_push($list, $key);
|
|
|
|
}
|
2017-05-10 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($numberOfItems == -1) {
|
2019-02-19 08:38:17 +01:00
|
|
|
return $list;
|
2017-07-15 15:47:37 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 00:48:29 +02:00
|
|
|
// The first page number is 1, so the real is 0
|
|
|
|
$realPageNumber = $pageNumber - 1;
|
|
|
|
|
2019-02-19 08:38:17 +01:00
|
|
|
$total = count($list);
|
2018-08-06 21:46:58 +02:00
|
|
|
$init = (int) $numberOfItems * $realPageNumber;
|
2024-06-23 21:46:43 +02:00
|
|
|
$end = (int) min(($init + $numberOfItems - 1), $total);
|
|
|
|
$outrange = $init < 0 ? true : $init > $end;
|
2017-10-02 22:42:18 +02:00
|
|
|
if (!$outrange) {
|
2019-02-19 08:38:17 +01:00
|
|
|
return array_slice($list, $init, $numberOfItems, true);
|
2017-05-10 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
2017-07-26 01:17:13 +02:00
|
|
|
return false;
|
2017-05-10 20:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the amount of pages
|
2019-06-17 22:07:01 +02:00
|
|
|
// (boolean) $onlyPublished, TRUE returns the total of published pages (without draft and scheduled)
|
|
|
|
// (boolean) $onlyPublished, FALSE returns the total of pages
|
2024-06-23 21:46:43 +02:00
|
|
|
public function count($onlyPublished = true)
|
2017-05-10 20:40:28 +02:00
|
|
|
{
|
2017-09-22 23:11:08 +02:00
|
|
|
if ($onlyPublished) {
|
2017-12-26 17:45:02 +01:00
|
|
|
$db = $this->getPublishedDB(false);
|
2017-05-10 20:40:28 +02:00
|
|
|
return count($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($this->db);
|
|
|
|
}
|
|
|
|
|
2018-01-21 23:23:22 +01:00
|
|
|
// Returns an array with all parents pages key. A parent page is not a child
|
2017-07-05 22:55:03 +02:00
|
|
|
public function getParents()
|
2017-05-16 00:46:20 +02:00
|
|
|
{
|
2018-01-19 19:37:06 +01:00
|
|
|
$db = $this->getPublishedDB();
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($db as $key => $pageKey) {
|
2017-07-05 22:55:03 +02:00
|
|
|
// if the key has slash then is a child
|
2017-12-26 17:45:02 +01:00
|
|
|
if (Text::stringContains($pageKey, '/')) {
|
2018-01-19 19:37:06 +01:00
|
|
|
unset($db[$key]);
|
2017-05-16 00:46:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
2018-01-21 23:23:22 +01:00
|
|
|
public function getChildren($parentKey)
|
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
|
|
|
$list = array();
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($tmp as $key => $fields) {
|
|
|
|
if (Text::startsWith($key, $parentKey . '/')) {
|
2018-01-21 23:23:22 +01:00
|
|
|
array_push($list, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2017-05-21 22:01:44 +02:00
|
|
|
public function sortBy()
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
if (ORDER_BY == 'date') {
|
2017-05-21 22:01:44 +02:00
|
|
|
return $this->sortByDate(true);
|
|
|
|
}
|
2017-10-02 22:42:18 +02:00
|
|
|
return $this->sortByPosition(false);
|
2017-05-21 22:01:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort pages by position
|
2024-06-23 21:46:43 +02:00
|
|
|
public function sortByPosition($HighToLow = false)
|
2017-05-21 22:01:44 +02:00
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($HighToLow) {
|
2017-05-21 22:01:44 +02:00
|
|
|
uasort($this->db, array($this, 'sortByPositionHighToLow'));
|
2017-10-02 22:42:18 +02:00
|
|
|
} else {
|
2017-05-21 22:01:44 +02:00
|
|
|
uasort($this->db, array($this, 'sortByPositionLowToHigh'));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
private function sortByPositionLowToHigh($a, $b)
|
|
|
|
{
|
2024-08-25 10:11:57 +02:00
|
|
|
if ($a['position'] < $b['position']) {
|
|
|
|
return -1;
|
|
|
|
} elseif ($a['position'] > $b['position']) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-21 22:01:44 +02:00
|
|
|
}
|
2024-08-25 10:11:57 +02:00
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
private function sortByPositionHighToLow($a, $b)
|
|
|
|
{
|
2024-08-25 10:11:57 +02:00
|
|
|
if ($a['position'] > $b['position']) {
|
|
|
|
return -1;
|
|
|
|
} elseif ($a['position'] < $b['position']) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-05-21 22:01:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-16 00:46:20 +02:00
|
|
|
// Sort pages by date
|
2024-06-23 21:46:43 +02:00
|
|
|
public function sortByDate($HighToLow = true)
|
2017-05-16 00:46:20 +02:00
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($HighToLow) {
|
2017-05-21 22:01:44 +02:00
|
|
|
uasort($this->db, array($this, 'sortByDateHighToLow'));
|
2017-10-02 22:42:18 +02:00
|
|
|
} else {
|
2017-05-21 22:01:44 +02:00
|
|
|
uasort($this->db, array($this, 'sortByDateLowToHigh'));
|
2017-05-16 00:46:20 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:42:18 +02:00
|
|
|
private function sortByDateLowToHigh($a, $b)
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
return $a['date'] > $b['date'];
|
2017-05-16 00:46:20 +02:00
|
|
|
}
|
2017-10-02 22:42:18 +02:00
|
|
|
private function sortByDateHighToLow($a, $b)
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
return $a['date'] < $b['date'];
|
2017-05-16 00:46:20 +02:00
|
|
|
}
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
function generateUUID()
|
|
|
|
{
|
|
|
|
return md5(uniqid() . time());
|
2017-06-23 00:41:00 +02:00
|
|
|
}
|
|
|
|
|
2018-05-08 00:15:40 +02:00
|
|
|
// Returns the UUID of a page, by the page key
|
|
|
|
function getUUID($key)
|
|
|
|
{
|
|
|
|
if ($this->exists($key)) {
|
|
|
|
return $this->db[$key]['uuid'];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the page key by the uuid
|
2019-02-24 16:38:18 +01:00
|
|
|
// if the UUID doesn't exits returns FALSE
|
2018-05-08 00:15:40 +02:00
|
|
|
function getByUUID($uuid)
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $key => $value) {
|
|
|
|
if ($value['uuid'] == $uuid) {
|
2018-05-08 00:15:40 +02:00
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-17 15:57:00 +01:00
|
|
|
// Returns string without HTML tags and truncated
|
2024-06-23 21:46:43 +02:00
|
|
|
private function generateSlug($text, $truncateLength = 60)
|
2018-09-03 18:46:21 +02:00
|
|
|
{
|
2018-01-17 15:57:00 +01:00
|
|
|
$tmpslug = Text::removeHTMLTags($text);
|
2018-08-26 13:44:18 +02:00
|
|
|
$tmpslug = Text::removeLineBreaks($tmpslug);
|
2018-09-03 18:46:21 +02:00
|
|
|
$tmpslug = Text::truncate($tmpslug, $truncateLength, '');
|
|
|
|
return $tmpslug;
|
2018-01-17 15:57:00 +01:00
|
|
|
}
|
|
|
|
|
2017-06-23 00:41:00 +02:00
|
|
|
// Returns TRUE if there are new pages published, FALSE otherwise
|
|
|
|
public function scheduler()
|
|
|
|
{
|
|
|
|
// Get current date
|
|
|
|
$currentDate = Date::current(DB_DATE_FORMAT);
|
|
|
|
$saveDatabase = false;
|
|
|
|
|
|
|
|
// The database need to be sorted by date
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $pageKey => $fields) {
|
|
|
|
if ($fields['type'] == 'scheduled') {
|
|
|
|
if ($fields['date'] <= $currentDate) {
|
2018-07-17 19:13:01 +02:00
|
|
|
$this->db[$pageKey]['type'] = 'published';
|
2017-06-23 00:41:00 +02:00
|
|
|
$saveDatabase = true;
|
|
|
|
}
|
2024-06-23 21:46:43 +02:00
|
|
|
} elseif (($fields['type'] == 'published') && (ORDER_BY == 'date')) {
|
2017-06-23 00:41:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($saveDatabase) {
|
|
|
|
if ($this->save() === false) {
|
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'Error occurred when trying to save the database file.');
|
2017-06-23 00:41:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
Log::set(__METHOD__ . LOG_SEP . 'New pages published from the scheduler.');
|
2017-06-23 00:41:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
// Generate a valid Key/Slug
|
2024-06-23 21:46:43 +02:00
|
|
|
public function generateKey($text, $parent = false, $returnSlug = false, $oldKey = '')
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
2018-08-20 22:32:14 +02:00
|
|
|
global $L;
|
2018-08-27 22:19:42 +02:00
|
|
|
global $site;
|
2018-08-20 22:32:14 +02:00
|
|
|
|
2017-10-04 00:00:54 +02:00
|
|
|
if (Text::isEmpty($text)) {
|
2018-08-21 20:11:31 +02:00
|
|
|
$text = $L->g('empty');
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 00:00:54 +02:00
|
|
|
if (Text::isEmpty($parent)) {
|
2015-05-31 03:06:55 +02:00
|
|
|
$newKey = Text::cleanUrl($text);
|
2017-10-04 00:00:54 +02:00
|
|
|
} else {
|
2024-06-23 21:46:43 +02:00
|
|
|
$newKey = Text::cleanUrl($parent) . '/' . Text::cleanUrl($text);
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-31 00:06:06 +01:00
|
|
|
// cleanURL can return empty string
|
|
|
|
if (Text::isEmpty($newKey)) {
|
2018-08-21 20:11:31 +02:00
|
|
|
$newKey = $L->g('empty');
|
2017-10-31 00:06:06 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 21:46:43 +02:00
|
|
|
if ($newKey !== $oldKey) {
|
2017-05-08 22:01:16 +02:00
|
|
|
// Verify if the key is already been used
|
2019-02-08 08:53:26 +01:00
|
|
|
if (isset($this->db[$newKey])) {
|
|
|
|
$i = 0;
|
2024-06-23 21:46:43 +02:00
|
|
|
while (isset($this->db[$newKey . '-' . $i])) {
|
2019-02-08 08:53:26 +01:00
|
|
|
$i++;
|
2015-05-19 01:22:05 +02:00
|
|
|
}
|
2024-06-23 21:46:43 +02:00
|
|
|
$newKey = $newKey . '-' . $i;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:00:54 +02:00
|
|
|
if ($returnSlug) {
|
2015-05-05 03:00:01 +02:00
|
|
|
$explode = explode('/', $newKey);
|
2018-08-21 20:57:24 +02:00
|
|
|
if (isset($explode[1])) {
|
2015-05-05 03:00:01 +02:00
|
|
|
return $explode[1];
|
|
|
|
}
|
|
|
|
return $explode[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $newKey;
|
|
|
|
}
|
|
|
|
|
2015-09-18 05:24:10 +02:00
|
|
|
// Returns an Array, array('tagSlug'=>'tagName')
|
2018-08-01 14:46:12 +02:00
|
|
|
// (string) $tags, tag list separated by comma.
|
2015-09-18 05:24:10 +02:00
|
|
|
public function generateTags($tags)
|
|
|
|
{
|
|
|
|
$tmp = array();
|
|
|
|
$tags = trim($tags);
|
2018-07-28 18:33:37 +02:00
|
|
|
if (empty($tags)) {
|
2015-09-18 05:24:10 +02:00
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tags = explode(',', $tags);
|
2018-07-28 18:33:37 +02:00
|
|
|
foreach ($tags as $tag) {
|
2015-09-18 05:24:10 +02:00
|
|
|
$tag = trim($tag);
|
|
|
|
$tagKey = Text::cleanUrl($tag);
|
|
|
|
$tmp[$tagKey] = $tag;
|
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2018-04-22 17:45:31 +02:00
|
|
|
// Change all pages with the old category key to the new category key
|
2017-04-17 12:49:03 +02:00
|
|
|
public function changeCategory($oldCategoryKey, $newCategoryKey)
|
|
|
|
{
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $key => $value) {
|
|
|
|
if ($value['category'] === $oldCategoryKey) {
|
2017-04-17 12:49:03 +02:00
|
|
|
$this->db[$key]['category'] = $newCategoryKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
2019-09-02 18:24:34 +02:00
|
|
|
// Insert custom fields to all the pages in the database
|
|
|
|
// The structure for the custom fields need to be a valid JSON format
|
|
|
|
// The custom fields are incremental, this means the custom fields are never deleted
|
|
|
|
// The pages only store the value of the custom field, the structure of the custom fields are in the database site.php
|
|
|
|
public function setCustomFields($fields)
|
|
|
|
{
|
|
|
|
$customFields = json_decode($fields, true);
|
|
|
|
if (json_last_error() != JSON_ERROR_NONE) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-06-23 21:46:43 +02:00
|
|
|
foreach ($this->db as $pageKey => $pageFields) {
|
|
|
|
foreach ($customFields as $customField => $customValues) {
|
2019-09-02 18:24:34 +02:00
|
|
|
if (!isset($pageFields['custom'][$customField])) {
|
|
|
|
$defaultValue = '';
|
|
|
|
if (isset($customValues['default'])) {
|
|
|
|
$defaultValue = $customValues['default'];
|
|
|
|
}
|
|
|
|
$this->db[$pageKey]['custom'][$customField]['value'] = $defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->save();
|
|
|
|
}
|
2018-08-01 14:46:12 +02:00
|
|
|
}
|