bludit/bl-kernel/dbtags.class.php

109 lines
2.7 KiB
PHP
Raw Normal View History

2015-08-29 07:02:09 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
class dbTags extends dbJSON
{
/*
$postsIndex['tag1']['name'] = 'Tag 1';
$postsIndex['tag1']['posts'] = array('post1','post2','post3');
$postsIndex['tag2']['name'] = 'Tag 2';
$postsIndex['tag2']['posts'] = array('post1','post5');
*/
public $dbFields = array(
2015-08-29 07:02:09 +02:00
'postsIndex'=>array('inFile'=>false, 'value'=>array()),
'pagesIndex'=>array('inFile'=>false, 'value'=>array())
);
function __construct()
{
parent::__construct(PATH_DATABASES.'tags.php');
}
2016-09-26 05:25:40 +02:00
public function getTag($slug) {
if( isset($this->db['postsIndex'][$slug]['name']) ) {
return $this->db['postsIndex'][$slug]['name'];
}
return '';
}
2015-12-31 19:47:34 +01:00
// Returns an array with all tags names
public function getAll()
{
$tmp = array();
foreach($this->db['postsIndex'] as $tagSlug=>$tagInfo) {
$tmp[$tagSlug] = $tagInfo['name'];
}
2016-02-25 02:50:27 +01:00
// Sort low to high, by value.
natcasesort($tmp);
2015-12-31 19:47:34 +01:00
return $tmp;
}
2015-08-31 03:18:06 +02:00
// Returns an array with a list of posts keys, filtered by a page number and a tag key.
2015-08-30 01:26:46 +02:00
public function getList($pageNumber, $postPerPage, $tagKey)
{
2015-08-31 03:18:06 +02:00
if( !isset($this->db['postsIndex'][$tagKey]) ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying get the posts list by the tag key: '.$tagKey);
return array();
}
2015-08-30 01:26:46 +02:00
$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'];
2015-08-31 03:18:06 +02:00
$tmp = array_flip($list); // Change the posts keys list in the array key.
2015-08-30 01:26:46 +02:00
return array_slice($tmp, $init, $postPerPage, true);
}
2015-08-31 03:18:06 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying get the list of posts, out of range?. Pagenumber: '.$pageNumber);
2015-08-30 01:26:46 +02:00
return array();
}
2015-08-29 07:02:09 +02:00
public function countPostsByTag($tagKey)
{
if( isset($this->db['postsIndex'][$tagKey]) ) {
return count($this->db['postsIndex'][$tagKey]['posts']);
}
2015-08-31 03:18:06 +02:00
return 0;
2015-08-29 07:02:09 +02:00
}
2015-08-31 03:18:06 +02:00
// Regenerate the posts index for each tag.
// (array) $db, the $db must be sorted by date and the posts published only.
2015-08-29 07:02:09 +02:00
public function reindexPosts($db)
{
$tagsIndex = array();
// Foreach post
foreach($db as $postKey=>$values)
{
2015-09-18 05:24:10 +02:00
$tags = $values['tags'];
2015-08-29 07:02:09 +02:00
2015-08-30 01:26:46 +02:00
// Foreach tag from post
2015-09-18 05:24:10 +02:00
foreach($tags as $tagKey=>$tagName)
2015-08-30 01:26:46 +02:00
{
2015-09-18 05:24:10 +02:00
if( isset($tagsIndex[$tagKey]) ) {
array_push($tagsIndex[$tagKey]['posts'], $postKey);
}
else {
$tagsIndex[$tagKey]['name'] = $tagName;
$tagsIndex[$tagKey]['posts'] = array($postKey);
2015-08-29 07:02:09 +02:00
}
}
}
$this->db['postsIndex'] = $tagsIndex;
2015-09-02 02:42:21 +02:00
2015-08-29 07:02:09 +02:00
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
2015-08-31 03:18:06 +02:00
return true;
2015-08-29 07:02:09 +02:00
}
2016-02-26 15:42:28 +01:00
}