bludit/kernel/dbtags.class.php

79 lines
1.9 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');
*/
private $dbFields = array(
'postsIndex'=>array('inFile'=>false, 'value'=>array()),
'pagesIndex'=>array('inFile'=>false, 'value'=>array())
);
function __construct()
{
parent::__construct(PATH_DATABASES.'tags.php');
}
2015-08-30 01:26:46 +02:00
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();
}
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']);
}
else {
2015-08-30 01:26:46 +02:00
return 0;
2015-08-29 07:02:09 +02:00
}
}
public function reindexPosts($db)
{
$tagsIndex = array();
$currentDate = Date::current(DB_DATE_FORMAT);
// Foreach post
foreach($db as $postKey=>$values)
{
2015-08-30 01:26:46 +02:00
$explode = explode(',', $values['tags']);
2015-08-29 07:02:09 +02:00
2015-08-30 01:26:46 +02:00
// Foreach tag from post
foreach($explode as $tagName)
{
$tagName = trim($tagName);
$tagKey = Text::cleanUrl($tagName);
2015-08-29 07:02:09 +02:00
2015-08-30 01:26:46 +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;
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
}
}