Prevent content to be empty if not passed as argument

This commit is contained in:
Diego Najar 2019-02-24 16:38:18 +01:00
parent 60f1b37441
commit 0f867720a5
1 changed files with 40 additions and 34 deletions

View File

@ -141,13 +141,21 @@ class Pages extends dbJSON {
return $key; return $key;
} }
// 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.
public function edit($args) public function edit($args)
{ {
// Old key // This is the new row for the table and is going to replace the old row
$row = array();
// Current key
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$key = $args['key']; $key = $args['key'];
$row = array();
// Check values on args or set default values // Check values from the arguments ($args)
// If some field is missing the current value is taken
foreach ($this->dbFields as $field=>$value) { foreach ($this->dbFields as $field=>$value) {
if ($field=='tags') { if ($field=='tags') {
$tags = ''; $tags = '';
@ -159,24 +167,20 @@ class Pages extends dbJSON {
// Sanitize if will be stored on database // Sanitize if will be stored on database
$finalValue = Sanitize::html($args[$field]); $finalValue = Sanitize::html($args[$field]);
} else { } else {
// Default value from the current // Default value from the current row
$finalValue = $this->db[$key][$field]; $finalValue = $this->db[$key][$field];
} }
settype($finalValue, gettype($value)); settype($finalValue, gettype($value));
$row[$field] = $finalValue; $row[$field] = $finalValue;
} }
// Content
// This variable is not belong to the database so is not defined in $row
$contentRaw = (empty($args['content'])?'':$args['content']);
// Parent // Parent
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$parent = (empty($args['parent'])?'':$args['parent']); $parent = (empty($args['parent'])?'':$args['parent']);
// Slug // Slug
// If the user change the slug the page key changes // If the user change the slug the page key changes
// If the user send an empty slug the page key doesn't need to change // If the user send an empty slug the page key doesn't change
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
if (empty($args['slug'])) { if (empty($args['slug'])) {
$explode = explode('/', $key); $explode = explode('/', $key);
@ -186,6 +190,9 @@ class Pages extends dbJSON {
} }
// New key // New key
// 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.
// This variable is not belong to the database so is not defined in $row // This variable is not belong to the database so is not defined in $row
$newKey = $this->generateKey($slug, $parent, false, $key); $newKey = $this->generateKey($slug, $parent, false, $key);
@ -193,6 +200,7 @@ class Pages extends dbJSON {
if ($row['type']=='draft') { if ($row['type']=='draft') {
$row['date'] = Date::current(DB_DATE_FORMAT); $row['date'] = Date::current(DB_DATE_FORMAT);
} elseif (!Valid::date($row['date'], DB_DATE_FORMAT)) { } elseif (!Valid::date($row['date'], DB_DATE_FORMAT)) {
// if the date in the arguments is not valid, take the value from the old row
$row['date'] = $this->db[$key]['date']; $row['date'] = $this->db[$key]['date'];
} }
@ -204,7 +212,7 @@ class Pages extends dbJSON {
$row['type'] = 'scheduled'; $row['type'] = 'scheduled';
} }
// Move the directory from old key to new key. // Move the directory from old key to new key only if the keys are different
if ($newKey!==$key) { if ($newKey!==$key) {
if (Filesystem::mv(PATH_PAGES.$key, PATH_PAGES.$newKey) === false) { 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); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey);
@ -212,13 +220,16 @@ class Pages extends dbJSON {
} }
} }
// Make the index.txt and save the file. // If the content was passed via arguments replace the content
if (file_put_contents(PATH_PAGES.$newKey.DS.FILENAME, $contentRaw)===false) { if (isset($args['content'])) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME); // Make the index.txt and save the file.
return false; 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);
return false;
}
} }
// Remove the old key // Remove the old row
unset($this->db[$key]); unset($this->db[$key]);
// Reindex Orphan Children // Reindex Orphan Children
@ -227,7 +238,7 @@ class Pages extends dbJSON {
// Checksum MD5 // Checksum MD5
$row['md5file'] = md5_file(PATH_PAGES.$newKey.DS.FILENAME); $row['md5file'] = md5_file(PATH_PAGES.$newKey.DS.FILENAME);
// Insert in database // Insert in database the new row
$this->db[$newKey] = $row; $this->db[$newKey] = $row;
// Sort database // Sort database
@ -469,24 +480,18 @@ class Pages extends dbJSON {
public function getList($pageNumber, $numberOfItems, $published=true, $static=false, $sticky=false, $draft=false, $scheduled=false) public function getList($pageNumber, $numberOfItems, $published=true, $static=false, $sticky=false, $draft=false, $scheduled=false)
{ {
$list = array(); $list = array();
if ($published) { foreach ($this->db as $key=>$fields) {
$list += $this->getPublishedDB(); if ($published && $fields['type']=='published') {
} array_push($list, $key);
} elseif ($static && $fields['type']=='static') {
if ($static) { array_push($list, $key);
$list += $this->getStaticDB(); } elseif ($sticky && $fields['type']=='sticky') {
} array_push($list, $key);
} elseif ($draft && $fields['type']=='draft') {
if ($sticky) { array_push($list, $key);
$list += $this->getStickyDB(); } elseif ($scheduled && $fields['type']=='scheduled') {
} array_push($list, $key);
}
if ($draft) {
$list += $this->getDraftDB();
}
if ($scheduled) {
$list += $this->getScheduledDB();
} }
if ($numberOfItems==-1) { if ($numberOfItems==-1) {
@ -609,6 +614,7 @@ class Pages extends dbJSON {
} }
// Returns the page key by the uuid // Returns the page key by the uuid
// if the UUID doesn't exits returns FALSE
function getByUUID($uuid) function getByUUID($uuid)
{ {
foreach ($this->db as $key=>$value) { foreach ($this->db as $key=>$value) {