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
@ -80,11 +89,12 @@ class dbPages extends dbJSON
$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'] = '';
@ -181,11 +195,12 @@ class dbPages extends dbJSON
$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];
@ -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()
{ {