Improvements on tags

This commit is contained in:
dignajar 2015-08-29 20:26:46 -03:00
parent 22c75b9d09
commit 25633d0dbc
7 changed files with 58 additions and 35 deletions

View File

@ -22,6 +22,8 @@ function editPost($args)
if( $dbPosts->edit($args) )
{
// Regenerate the database tags
$dbPosts->removeUnpublished();
$dbPosts->sortByDate();
$dbTags->reindexPosts( $dbPosts->db );
Alert::set($Language->g('The changes have been saved'));

View File

@ -26,6 +26,8 @@ function addPost($args)
if( $dbPosts->add($args) )
{
// Regenerate the database tags
$dbPosts->removeUnpublished();
$dbPosts->sortByDate();
$dbTags->reindexPosts( $dbPosts->db );
Alert::set($Language->g('Post added successfully'));

View File

@ -66,22 +66,28 @@ function buildPost($key)
return $Post;
}
function build_posts_per_page($pageNumber=0, $amount=5, $draftPosts=false)
function buildPostForPage($pageNumber=0, $amount=5, $removeUnpublished=false, $tagKey=false)
{
global $dbPosts;
global $dbTags;
global $posts;
global $Url;
$list = $dbPosts->getPage($pageNumber, $amount, $draftPosts);
if($tagKey) {
$list = $dbTags->getList($pageNumber, $amount, $tagKey);
}
else {
$list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished);
}
// There are not post for the pageNumber then true Not found page
// There are not post for the pageNumber then set the page notfound
if(empty($list) && $pageNumber>0) {
$Url->setNotFound(true);
}
foreach($list as $slug=>$db)
foreach($list as $postKey=>$values)
{
$Post = buildPost($slug);
$Post = buildPost($postKey);
if($Post!==false) {
array_push($posts, $Post);
@ -118,18 +124,18 @@ if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) )
}
elseif( ($Url->whereAmI()==='tag') && ($Url->notFound()===false) )
{
buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), true, $Url->slug());
}
// Build post per page
else
{
if($Url->whereAmI()==='admin') {
// Build post for admin area with drafts
build_posts_per_page($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, true);
// Build post for admin area with drafts+schedulers
buildPostForPage($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, true);
}
else
{
// Build post for the site, without the drafts posts
build_posts_per_page($Url->pageNumber(), $Site->postsPerPage(), false);
// Build post for the site, without the drafts and scheduleres posts
buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), false);
}
}

View File

@ -320,7 +320,7 @@ class dbPosts extends dbJSON
return $this->db!=$db;
}
*/
public function getPage($pageNumber, $postPerPage, $draftPosts=false)
public function getList($pageNumber, $postPerPage, $draftPosts=false)
{
// DEBUG: Ver una mejor manera de eliminar draft post antes de ordenarlos
// DEBUG: Se eliminan antes de ordenarlos porque sino los draft cuentan como publicados en el PostPerPage.
@ -334,9 +334,10 @@ class dbPosts extends dbJSON
$outrange = $init<0 ? true : $init>$end;
// Sort posts
$tmp = $this->sortByDate();
$this->sortByDate();
if(!$outrange) {
$tmp = $this->db;
return array_slice($tmp, $init, $postPerPage, true);
}
@ -417,7 +418,7 @@ class dbPosts extends dbJSON
else
uasort($tmp, 'high_to_low');
return $tmp;
return true;
}
}

View File

@ -18,13 +18,28 @@ class dbTags extends dbJSON
parent::__construct(PATH_DATABASES.'tags.php');
}
public function getList($pageNumber, $postPerPage, $tagKey)
{
$init = (int) $postPerPage * $pageNumber;
$end = (int) min( ($init + $postPerPage - 1), $this->countPostsByTag($tagKey) - 1 );
$outrange = $init<0 ? true : $init > $end;
if(!$outrange) {
$list = $this->db['postsIndex'][$tagKey]['posts'];
$tmp = array_flip($list);
return array_slice($tmp, $init, $postPerPage, true);
}
return array();
}
public function countPostsByTag($tagKey)
{
if( isset($this->db['postsIndex'][$tagKey]) ) {
return count($this->db['postsIndex'][$tagKey]['posts']);
}
else {
return false;
return 0;
}
}
@ -35,8 +50,6 @@ class dbTags extends dbJSON
// Foreach post
foreach($db as $postKey=>$values)
{
if( ($values['status']==='published') && ($values['date']<=$currentDate) )
{
$explode = explode(',', $values['tags']);
@ -55,7 +68,6 @@ class dbTags extends dbJSON
}
}
}
}
$this->db['postsIndex'] = $tagsIndex;
if( $this->save() === false ) {

View File

@ -27,12 +27,12 @@ class pluginTags extends Plugin {
global $dbTags;
global $Url;
$db = $dbTags->db['postsIndex'];
$filter = trim($Url->filters('tag'), '/');
$html = '<div class="plugin plugin-tags">';
$html .= '<h2>'.$this->getDbField('label').'</h2>';
$html .= '<div class="plugin-content">';
$db = $dbTags->db['postsIndex'];
$html .= '<ul>';
foreach($db as $tagKey=>$fields)
@ -40,7 +40,7 @@ class pluginTags extends Plugin {
$count = $dbTags->countPostsByTag($tagKey);
// Print the parent
$html .= '<li><a href="'.HTML_PATH_ROOT.$Url->filters('tag').$tagKey.'">'.$fields['name'].' ('.$count.')</a></li>';
$html .= '<li><a href="'.HTML_PATH_ROOT.$filter.'/'.$tagKey.'">'.$fields['name'].' ('.$count.')</a></li>';
}
$html .= '</ul>';

View File

@ -24,7 +24,7 @@
<!-- Content -->
<?php
if($Url->whereAmI()=='home')
if( ($Url->whereAmI()=='home') || ($Url->whereAmI()=='tag') )
{
include('php/home.php');
}