Improves on add / edit a page

This commit is contained in:
floppy0 2018-01-17 15:57:00 +01:00
parent 076fe7343c
commit 71fc19efe8
2 changed files with 50 additions and 29 deletions

View File

@ -19,7 +19,8 @@ class dbPages extends dbJSON
'category'=> array('inFile'=>false, 'value'=>''), 'category'=> array('inFile'=>false, 'value'=>''),
'md5file'=> array('inFile'=>false, 'value'=>''), 'md5file'=> array('inFile'=>false, 'value'=>''),
'uuid'=> array('inFile'=>false, 'value'=>''), 'uuid'=> array('inFile'=>false, 'value'=>''),
'allowComments'=> array('inFile'=>false, 'value'=>true) 'allowComments'=> array('inFile'=>false, 'value'=>true),
'template'=> array('inFile'=>false, 'value'=>'')
); );
function __construct() function __construct()
@ -28,22 +29,20 @@ class dbPages extends dbJSON
} }
// Create a new page // Create a new page
// This function returns the key of the new page
public function add($args, $climode=false) public function add($args, $climode=false)
{ {
$dataForDb = array(); // This data will be saved in the database $dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file $dataForFile = array(); // This data will be saved in the file
// Check values on args or set default values
foreach ($this->dbFields as $field=>$options) { foreach ($this->dbFields as $field=>$options) {
if (isset($args[$field])) { if (isset($args[$field])) {
if ($field=='tags') { if ($options['inFile'] || is_array($args[$field])) {
$value = $this->generateTags($args['tags']); $value = $args[$field];
} else { } else {
if( !$options['inFile'] ) { // Sanitize if will be stored on database
// Sanitize if will be stored on database $value = Sanitize::html($args[$field]);
$value = Sanitize::html($args[$field]);
} else {
$value = $args[$field];
}
} }
} else { } else {
// Default value for the field // Default value for the field
@ -53,10 +52,20 @@ class dbPages extends dbJSON
$args[$field] = $value; $args[$field] = $value;
} }
// Generate slug from content if the title is empty // Tags
if (empty($args['title']) || empty($args['slug'])) { if (!empty($args['tags'])) {
$tmpslug = Text::removeHTMLTags($args['content']); $args['tags'] = $this->generateTags($args['tags']);
$args['slug'] = Text::truncate($tmpslug, 60, ''); } else {
$args['tags'] = array();
}
// Slug from title or content
if (empty($args['slug'])) {
if (!empty($args['title'])) {
$args['slug'] = $this->generateSlug($args['title']);
} else {
$args['slug'] = $this->generateSlug($args['content']);
}
} }
// Parent // Parent
@ -71,20 +80,21 @@ class dbPages extends dbJSON
$args['uuid'] = $this->generateUUID(); $args['uuid'] = $this->generateUUID();
// Validate date // Validate date
if ( !Valid::date($args['date'], DB_DATE_FORMAT) ) { if (!Valid::date($args['date'], DB_DATE_FORMAT)) {
$args['date'] = Date::current(DB_DATE_FORMAT); $args['date'] = Date::current(DB_DATE_FORMAT);
} }
// Schedule page // Schedule page
if ( ($args['date']>Date::current(DB_DATE_FORMAT)) && ($args['status']=='published') ) { if (($args['date']>Date::current(DB_DATE_FORMAT)) && ($args['status']=='published')) {
$args['status'] = 'scheduled'; $args['status'] = 'scheduled';
} }
// Set type // Set type of the page
if ($args['status']=='static') { if ($args['status']=='static') {
$args['type'] = 'page'; $args['type'] = 'page';
} }
// Set type to the variables
foreach ($this->dbFields as $field=>$options) { foreach ($this->dbFields as $field=>$options) {
$value = $args[$field]; $value = $args[$field];
@ -135,17 +145,14 @@ class dbPages extends dbJSON
$dataForDb = array(); $dataForDb = array();
$dataForFile = array(); $dataForFile = array();
// Check values on args or set default values
foreach ($this->dbFields as $field=>$options) { foreach ($this->dbFields as $field=>$options) {
if (isset($args[$field])) { if (isset($args[$field])) {
if ($field=='tags') { if ($options['inFile'] || is_array($args[$field])) {
$value = $this->generateTags($args['tags']); $value = $args[$field];
} else { } else {
if( !$options['inFile'] ) { // Sanitize if will be stored on database
// Sanitize if will be stored on database $value = Sanitize::html($args[$field]);
$value = Sanitize::html($args[$field]);
} else {
$value = $args[$field];
}
} }
} else { } else {
// By default is the current value // By default is the current value
@ -159,6 +166,13 @@ class dbPages extends dbJSON
$args[$field] = $value; $args[$field] = $value;
} }
// Tags
if (!empty($args['tags'])) {
$args['tags'] = $this->generateTags($args['tags']);
} else {
$args['tags'] = array();
}
// Parent // Parent
if (!isset($args['parent'])) { if (!isset($args['parent'])) {
$args['parent'] = ''; $args['parent'] = '';
@ -177,15 +191,16 @@ class dbPages extends dbJSON
$args['dateModified'] = Date::current(DB_DATE_FORMAT); $args['dateModified'] = Date::current(DB_DATE_FORMAT);
// Schedule page // Schedule page
if ( ($args['date']>Date::current(DB_DATE_FORMAT)) && ($args['status']=='published') ) { if (($args['date']>Date::current(DB_DATE_FORMAT)) && ($args['status']=='published')) {
$args['status'] = 'scheduled'; $args['status'] = 'scheduled';
} }
// Set type // Set type of the page
if ($args['status']=='static') { if ($args['status']=='static') {
$args['type'] = 'page'; $args['type'] = 'page';
} }
// Set type to the variables
foreach ($this->dbFields as $field=>$options) { foreach ($this->dbFields as $field=>$options) {
$value = $args[$field]; $value = $args[$field];
@ -283,7 +298,7 @@ class dbPages extends dbJSON
// Change a field's value // Change a field's value
public function setField($key, $field, $value) public function setField($key, $field, $value)
{ {
if( $this->exists($key) ) { if ($this->exists($key)) {
settype($value, gettype($this->dbFields[$field]['value'])); settype($value, gettype($this->dbFields[$field]['value']));
$this->db[$key][$field] = $value; $this->db[$key][$field] = $value;
return $this->save(); return $this->save();
@ -343,7 +358,7 @@ class dbPages extends dbJSON
public function getScheduledDB($onlyKeys=true) public function getScheduledDB($onlyKeys=true)
{ {
$tmp = $this->db; $tmp = $this->db;
foreach($tmp as $key=>$fields) { foreach ($tmp as $key=>$fields) {
if($fields['status']!='scheduled') { if($fields['status']!='scheduled') {
unset($tmp[$key]); unset($tmp[$key]);
} }
@ -492,6 +507,12 @@ class dbPages extends dbJSON
return md5( uniqid().time() ); return md5( uniqid().time() );
} }
// Returns string without HTML tags and truncated
private function generateSlug($text, $truncateLength=60) {
$tmpslug = Text::removeHTMLTags($text);
returns Text::truncate($tmpslug, $truncateLength, '');
}
// Returns TRUE if there are new pages published, FALSE otherwise // Returns TRUE if there are new pages published, FALSE otherwise
public function scheduler() public function scheduler()
{ {

View File

@ -9,7 +9,7 @@ class Sanitize {
{ {
$flags = ENT_COMPAT; $flags = ENT_COMPAT;
if(defined('ENT_HTML5')) { if (defined('ENT_HTML5')) {
$flags = ENT_COMPAT|ENT_HTML5; $flags = ENT_COMPAT|ENT_HTML5;
} }