Tags improves

This commit is contained in:
dignajar 2015-09-18 00:24:10 -03:00
parent 696fd4475b
commit f7a1fde3fc
6 changed files with 111 additions and 48 deletions

View File

@ -226,7 +226,7 @@ function install($adminPassword, $email)
'error'=>array( 'error'=>array(
'description'=>'Error page', 'description'=>'Error page',
'username'=>'admin', 'username'=>'admin',
'tags'=>'', 'tags'=>array(),
'status'=>'published', 'status'=>'published',
'date'=>$currentDate, 'date'=>$currentDate,
'position'=>0 'position'=>0
@ -241,7 +241,7 @@ function install($adminPassword, $email)
'description'=>'Welcome to Bludit', 'description'=>'Welcome to Bludit',
'username'=>'admin', 'username'=>'admin',
'status'=>'published', 'status'=>'published',
'tags'=>'bludit,cms,flat-file', 'tags'=>array('bludit'=>'Bludit','cms'=>'CMS','flat-files'=>'Flat files'),
'allowComments'=>false, 'allowComments'=>false,
'date'=>$currentDate 'date'=>$currentDate
) )
@ -305,7 +305,8 @@ function install($adminPassword, $email)
array( array(
'postsIndex'=>array( 'postsIndex'=>array(
'bludit'=>array('name'=>'Bludit', 'posts'=>array('first-post')), 'bludit'=>array('name'=>'Bludit', 'posts'=>array('first-post')),
'cms'=>array('name'=>'cms', 'posts'=>array('first-post')) 'cms'=>array('name'=>'CMS', 'posts'=>array('first-post')),
'flat-files'=>array('name'=>'Flat files', 'posts'=>array('first-post'))
), ),
'pagesIndex'=>array() 'pagesIndex'=>array()
), ),

View File

@ -9,7 +9,7 @@ class dbPages extends dbJSON
'content'=> array('inFile'=>true, 'value'=>''), 'content'=> array('inFile'=>true, 'value'=>''),
'description'=> array('inFile'=>false, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''), 'username'=> array('inFile'=>false, 'value'=>''),
'tags'=> array('inFile'=>false, 'value'=>''), 'tags'=> array('inFile'=>false, 'value'=>array()),
'status'=> array('inFile'=>false, 'value'=>'draft'), 'status'=> array('inFile'=>false, 'value'=>'draft'),
'date'=> array('inFile'=>false, 'value'=>0), 'date'=> array('inFile'=>false, 'value'=>0),
'position'=> array('inFile'=>false, 'value'=>0) 'position'=> array('inFile'=>false, 'value'=>0)
@ -43,12 +43,17 @@ class dbPages extends dbJSON
{ {
if( isset($args[$field]) ) if( isset($args[$field]) )
{ {
// Sanitize if will be saved on database. if($field=='tags') {
if( !$options['inFile'] ) { $tmpValue = $this->generateTags($args['tags']);
$tmpValue = Sanitize::html($args[$field]);
} }
else { else {
$tmpValue = $args[$field]; // Sanitize if will be saved on database.
if( !$options['inFile'] ) {
$tmpValue = Sanitize::html($args[$field]);
}
else {
$tmpValue = $args[$field];
}
} }
} }
// Default value for the field. // Default value for the field.
@ -120,12 +125,17 @@ class dbPages extends dbJSON
{ {
if( isset($args[$field]) ) if( isset($args[$field]) )
{ {
// Sanitize if will be saved on database. if($field=='tags') {
if( !$options['inFile'] ) { $tmpValue = $this->generateTags($args['tags']);
$tmpValue = Sanitize::html($args[$field]);
} }
else { else {
$tmpValue = $args[$field]; // Sanitize if will be saved on database.
if( !$options['inFile'] ) {
$tmpValue = Sanitize::html($args[$field]);
}
else {
$tmpValue = $args[$field];
}
} }
} }
// Default value for the field. // Default value for the field.
@ -285,6 +295,31 @@ class dbPages extends dbJSON
return $this->db; return $this->db;
} }
// Returns an Array, array('tagSlug'=>'tagName')
// (string) $tags, tag list separeted by comma.
public function generateTags($tags)
{
$tmp = array();
$tags = trim($tags);
if(empty($tags)) {
return $tmp;
}
// Make array
$tags = explode(',', $tags);
foreach($tags as $tag)
{
$tag = trim($tag);
$tagKey = Text::cleanUrl($tag);
$tmp[$tagKey] = $tag;
}
return $tmp;
}
public function regenerateCli() public function regenerateCli()
{ {
$db = $this->db; $db = $this->db;

View File

@ -8,7 +8,7 @@ class dbPosts extends dbJSON
'description'=> array('inFile'=>false, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''), 'username'=> array('inFile'=>false, 'value'=>''),
'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled 'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled
'tags'=> array('inFile'=>false, 'value'=>''), 'tags'=> array('inFile'=>false, 'value'=>array()),
'allowComments'=> array('inFile'=>false, 'value'=>false), 'allowComments'=> array('inFile'=>false, 'value'=>false),
'date'=> array('inFile'=>false, 'value'=>'') 'date'=> array('inFile'=>false, 'value'=>'')
); );
@ -112,26 +112,26 @@ class dbPosts extends dbJSON
$args['status'] = 'scheduled'; $args['status'] = 'scheduled';
} }
// Tags
if(Text::isNotEmpty($args['tags'])) {
$cleanTags = array_map('trim', explode(',', $args['tags']));
$args['tags'] = implode(',', $cleanTags);
}
// Verify arguments with the database fields. // Verify arguments with the database fields.
foreach($this->dbFields as $field=>$options) foreach($this->dbFields as $field=>$options)
{ {
// If the field is in the arguments.
if( isset($args[$field]) ) if( isset($args[$field]) )
{ {
// Sanitize if will be saved on database. if($field=='tags') {
if( !$options['inFile'] ) { $tmpValue = $this->generateTags($args['tags']);
$tmpValue = Sanitize::html($args[$field]);
} }
else { else {
$tmpValue = $args[$field]; // Sanitize if will be saved on database.
if( !$options['inFile'] ) {
$tmpValue = Sanitize::html($args[$field]);
}
else {
$tmpValue = $args[$field];
}
} }
} }
// Default value for the field. // Default value if not in the arguments.
else else
{ {
$tmpValue = $options['value']; $tmpValue = $options['value'];
@ -332,6 +332,31 @@ class dbPosts extends dbJSON
return false; return false;
} }
// Returns an Array, array('tagSlug'=>'tagName')
// (string) $tags, tag list separeted by comma.
public function generateTags($tags)
{
$tmp = array();
$tags = trim($tags);
if(empty($tags)) {
return $tmp;
}
// Make array
$tags = explode(',', $tags);
foreach($tags as $tag)
{
$tag = trim($tag);
$tagKey = Text::cleanUrl($tag);
$tmp[$tagKey] = $tag;
}
return $tmp;
}
// Sort posts by date. // Sort posts by date.
public function sortByDate($HighToLow=true) public function sortByDate($HighToLow=true)
{ {

View File

@ -59,25 +59,17 @@ class dbTags extends dbJSON
// Foreach post // Foreach post
foreach($db as $postKey=>$values) foreach($db as $postKey=>$values)
{ {
$explode = explode(',', $values['tags']); $tags = $values['tags'];
// Foreach tag from post // Foreach tag from post
foreach($explode as $tagName) foreach($tags as $tagKey=>$tagName)
{ {
$tagName = trim($tagName); if( isset($tagsIndex[$tagKey]) ) {
$tagKey = $tagName; array_push($tagsIndex[$tagKey]['posts'], $postKey);
//$tagKey = Text::cleanUrl($tagName); }
else {
// If the tag is not empty. $tagsIndex[$tagKey]['name'] = $tagName;
if(Text::isNotEmpty($tagName)) $tagsIndex[$tagKey]['posts'] = array($postKey);
{
if( isset($tagsIndex[$tagKey]) ) {
array_push($tagsIndex[$tagKey]['posts'], $postKey);
}
else {
$tagsIndex[$tagKey]['name'] = $tagName;
$tagsIndex[$tagKey]['posts'] = array($postKey);
}
} }
} }
} }

View File

@ -49,15 +49,24 @@ class Page extends fileContent
return $this->getField('description'); return $this->getField('description');
} }
public function tags() public function tags($returnsArray=false)
{ {
return $this->getField('tags'); global $Url;
}
public function tagsArray()
{
$tags = $this->getField('tags'); $tags = $this->getField('tags');
return explode(',', $tags);
if($returnsArray) {
if($tags==false) {
return array();
}
return $tags;
}
else {
// Return string with tags separeted by comma.
return implode(', ', $tags);
}
} }
public function position() public function position()

View File

@ -123,10 +123,11 @@ class Post extends fileContent
return array(); return array();
} }
return explode(',', $tags); return $tags;
} }
else { else {
return $tags; // Return string with tags separeted by comma.
return implode(', ', $tags);
} }
} }