From 25633d0dbc17cf43cdf54b6d0011c051f92d7ecc Mon Sep 17 00:00:00 2001 From: dignajar Date: Sat, 29 Aug 2015 20:26:46 -0300 Subject: [PATCH] Improvements on tags --- admin/controllers/edit-post.php | 2 ++ admin/controllers/new-post.php | 2 ++ kernel/boot/rules/70.build_posts.php | 26 +++++++++------- kernel/dbposts.class.php | 9 +++--- kernel/dbtags.class.php | 44 ++++++++++++++++++---------- plugins/tags/plugin.php | 8 ++--- themes/pure/index.php | 2 +- 7 files changed, 58 insertions(+), 35 deletions(-) diff --git a/admin/controllers/edit-post.php b/admin/controllers/edit-post.php index a52ee912..df7dbb04 100644 --- a/admin/controllers/edit-post.php +++ b/admin/controllers/edit-post.php @@ -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')); diff --git a/admin/controllers/new-post.php b/admin/controllers/new-post.php index 92131b52..bffb24be 100644 --- a/admin/controllers/new-post.php +++ b/admin/controllers/new-post.php @@ -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')); diff --git a/kernel/boot/rules/70.build_posts.php b/kernel/boot/rules/70.build_posts.php index bfdd20dd..525a251a 100644 --- a/kernel/boot/rules/70.build_posts.php +++ b/kernel/boot/rules/70.build_posts.php @@ -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); } } diff --git a/kernel/dbposts.class.php b/kernel/dbposts.class.php index 97aad227..a0c20729 100644 --- a/kernel/dbposts.class.php +++ b/kernel/dbposts.class.php @@ -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. @@ -331,12 +331,13 @@ class dbPosts extends dbJSON $init = (int) $postPerPage * $pageNumber; $end = (int) min( ($init + $postPerPage - 1), count($this->db) - 1 ); - $outrange = $init<0 ? true : $init > $end; + $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; } } diff --git a/kernel/dbtags.class.php b/kernel/dbtags.class.php index 4c3776d6..22f51ced 100644 --- a/kernel/dbtags.class.php +++ b/kernel/dbtags.class.php @@ -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; } } @@ -36,23 +51,20 @@ class dbTags extends dbJSON // Foreach post foreach($db as $postKey=>$values) { - if( ($values['status']==='published') && ($values['date']<=$currentDate) ) + $explode = explode(',', $values['tags']); + + // Foreach tag from post + foreach($explode as $tagName) { - $explode = explode(',', $values['tags']); + $tagName = trim($tagName); + $tagKey = Text::cleanUrl($tagName); - // Foreach tag from post - foreach($explode as $tagName) - { - $tagName = trim($tagName); - $tagKey = Text::cleanUrl($tagName); - - if( isset($tagsIndex[$tagKey]) ) { - array_push($tagsIndex[$tagKey]['posts'], $postKey); - } - else { - $tagsIndex[$tagKey]['name'] = $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); } } } diff --git a/plugins/tags/plugin.php b/plugins/tags/plugin.php index 52902cc4..ea12d601 100755 --- a/plugins/tags/plugin.php +++ b/plugins/tags/plugin.php @@ -27,12 +27,12 @@ class pluginTags extends Plugin { global $dbTags; global $Url; + $db = $dbTags->db['postsIndex']; + $filter = trim($Url->filters('tag'), '/'); + $html = '
'; $html .= '

'.$this->getDbField('label').'

'; $html .= '
'; - - $db = $dbTags->db['postsIndex']; - $html .= ''; diff --git a/themes/pure/index.php b/themes/pure/index.php index 0e38280b..027a6b57 100644 --- a/themes/pure/index.php +++ b/themes/pure/index.php @@ -24,7 +24,7 @@ whereAmI()=='home') + if( ($Url->whereAmI()=='home') || ($Url->whereAmI()=='tag') ) { include('php/home.php'); }