Categories implementation

This commit is contained in:
Diego 2017-03-26 20:51:32 +02:00
parent 1138633e1f
commit 96d3efaa65
6 changed files with 123 additions and 82 deletions

View File

@ -15,21 +15,24 @@ function addPost($args)
// Add the page, if the $key is FALSE the creation of the post failure.
$key = $dbPosts->add($args);
if($key)
{
if($key) {
// Reindex tags, this function is in 70.posts.php
reIndexTagsPosts();
// Call the plugins after post created.
// Re index categories
reIndexCategoriesPosts();
// Call the plugins after post creation
Theme::plugins('afterPostCreate');
// Alert for the user
Alert::set($Language->g('Post added successfully'));
Redirect::page('admin', 'manage-posts');
}
else
{
else {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the post.');
Log::set(__METHOD__.LOG_SEP.'Cleaning database...');
$dbPosts->delete($key);
}
return false;

View File

@ -51,6 +51,16 @@ echo '<div class="bl-publish-sidebar uk-width-2-10">';
echo '<li><h2 class="sidebar-button" data-view="sidebar-general-view"><i class="uk-icon-angle-down"></i> '.$L->g('General').'</h2></li>';
echo '<li id="sidebar-general-view" class="sidebar-view">';
// Category
HTML::formSelect(array(
'name'=>'category',
'label'=>$L->g('Category'),
'class'=>'uk-width-1-1 uk-form-medium',
'options'=>$dbCategories->getAll(),
'selected'=>'',
'tip'=>''
));
// Description input
HTML::formTextarea(array(
'name'=>'description',

View File

@ -144,6 +144,7 @@ include(PATH_KERNEL.'dbusers.class.php');
include(PATH_KERNEL.'dbtags.class.php');
include(PATH_KERNEL.'dblanguage.class.php');
include(PATH_KERNEL.'dbsite.class.php');
include(PATH_KERNEL.'dbcategories.class.php');
include(PATH_KERNEL.'post.class.php');
include(PATH_KERNEL.'page.class.php');
include(PATH_KERNEL.'user.class.php');
@ -183,6 +184,7 @@ $dbPosts = new dbPosts();
$dbPages = new dbPages();
$dbUsers = new dbUsers();
$dbTags = new dbTags();
$dbCategories = new dbCategories();
$Site = new dbSite();
$Url = new Url();
$Parsedown = new ParsedownExtra();

View File

@ -5,25 +5,15 @@ Database structure
- To re index the list of posts and pages need to be sorted
{
"postsIndex": {
"videos": {
"name": "Videos",
"list": [ "first-post", "second-post" ]
},
"pets": {
"name": "Pets",
"list": [ "second-post", "another-post" ]
}
"videos": {
"name": "Videos",
"posts": [ "first-post", "second-post" ],
"pages": [ "my-page", "second-page" ]
},
"pagesIndex": {
"videos": {
"name": "Videos",
"list": [ "first-post", "second-post" ]
},
"music": {
"name": "Music",
"list": [ "second-post", "another-post" ]
}
"pets": {
"name": "Pets",
"posts": [ "second-post", "bull-terrier" ],
"pages": [ ]
}
}
@ -31,25 +21,22 @@ Database structure
class dbCategories extends dbJSON
{
public $dbFields = array(
'postsIndex'=>array('inFile'=>false, 'value'=>array()),
'pagesIndex'=>array('inFile'=>false, 'value'=>array())
);
public $dbFields = array();
function __construct()
{
parent::__construct(PATH_DATABASES.'categories.php');
}
private function getByCategory($type='postsIndex', $categorySlug, $amountPerPage, $pageNumber)
private function getByCategory($type='posts', $categoryKey, $amountPerPage, $pageNumber)
{
// Check if the category exists
if( !isset($this->db[$type][$categorySlug]) ) {
Log::set(__METHOD__.LOG_SEP.'Error getting '.$type.' by the category: '.$categorySlug);
if( !isset($this->db[$categoryKey]) ) {
Log::set(__METHOD__.LOG_SEP.'Error getting '.$type.' by the category: '.$categoryKey);
return array();
}
$list = $this->db[$type][$categorySlug]['list'];
$list = $this->db[$categoryKey][$type];
$init = (int) $amountPerPage * $pageNumber;
$end = (int) min( ($init + $amountPerPage - 1), count($list) - 1 );
@ -64,60 +51,63 @@ class dbCategories extends dbJSON
return array_slice($tmp, $init, $amountPerPage, true);
}
public function getPagesByCategory($categorySlug, $amountPerPage, $pageNumber)
public function getPagesByCategory($categoryKey, $amountPerPage, $pageNumber)
{
return $this->getByCategory('pagesIndex', $categorySlug, $amountPerPage, $pageNumber);
return $this->getByCategory('pages', $categoryKey, $amountPerPage, $pageNumber);
}
public function getPostsByCategory($categorySlug, $amountPerPage, $pageNumber)
public function getPostsByCategory($categoryKey, $amountPerPage, $pageNumber)
{
return $this->getByCategory('postsIndex', $categorySlug, $amountPerPage, $pageNumber);
return $this->getByCategory('posts', $categoryKey, $amountPerPage, $pageNumber);
}
private function countByCategory($type='postsIndex', $categorySlug)
private function countByCategory($type='posts', $categoryKey)
{
if( isset($this->db[$type][$categorySlug]) ) {
return count($this->db[$type][$categorySlug]['list']);
if( isset($this->db[$categoryKey][$type]) ) {
return count($this->db[$categoryKey][$type]);
}
return 0;
}
public function countPostsByCategory($categorySlug)
public function countPostsByCategory($categoryKey)
{
return $this->countByCategory('postsIndex', $categorySlug);
return $this->countByCategory('posts', $categoryKey);
}
public function countPagesByCategory($categorySlug)
public function countPagesByCategory($categoryKey)
{
return $this->countByCategory('pagesIndex', $categorySlug);
return $this->countByCategory('pages', $categoryKey);
}
// Regenerate the posts index for each tag.
public function getAll()
{
$tmp = array();
foreach($this->db as $key=>$data) {
$tmp[$key] = $data['name'];
}
// Sort low to high, by value.
natcasesort($tmp);
return $tmp;
}
// Re-generate posts index
// (array) $db, the $db must be sorted by date and the posts published only.
public function reindexPosts($db)
public function reIndexPosts($db)
{
$tagsIndex = array();
$index = array();
// Foreach post
foreach($db as $postKey=>$values)
{
$tags = $values['tags'];
// Foreach tag from post
foreach($tags as $tagKey=>$tagName)
{
if( isset($tagsIndex[$tagKey]) ) {
array_push($tagsIndex[$tagKey]['posts'], $postKey);
}
else {
$tagsIndex[$tagKey]['name'] = $tagName;
$tagsIndex[$tagKey]['posts'] = array($postKey);
}
// Foreach post in the database
foreach($db as $postKey=>$postData) {
if(!empty($postData['category'])) {
$categoryKey = $postData['category'];
array_push($index, $postKey);
}
}
$this->db['postsIndex'] = $tagsIndex;
$this->db[$categoryKey]['posts'] = $index;
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
@ -127,4 +117,27 @@ class dbCategories extends dbJSON
return true;
}
// Re-generate pages index
// (array) $db, the $db must be sorted by date and the posts published only.
public function reIndexPages($db)
{
$index = array();
// Foreach post in the database
foreach($db as $pageKey=>$pageData) {
$categoryKey = $pageData['category'];
array_push($index, $pageKey);
}
$this->db[$categoryKey]['pages'] = $index;
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
}

View File

@ -13,7 +13,8 @@ class dbPosts extends dbJSON
'date'=> array('inFile'=>false, 'value'=>''),
'dateModified'=> array('inFile'=>false, 'value'=>''),
'coverImage'=> array('inFile'=>false, 'value'=>''),
'md5file'=> array('inFile'=>false, 'value'=>'')
'md5file'=> array('inFile'=>false, 'value'=>''),
'category'=> array('inFile'=>false, 'value'=>'')
);
function __construct()
@ -21,7 +22,7 @@ class dbPosts extends dbJSON
parent::__construct(PATH_DATABASES.'posts.php');
}
// Return the amount of posts
// Returns the amount of posts
// $total = TRUE, returns the total of posts
// $total = FALSE, return the amount of published posts
public function numberPost($total=false)
@ -42,13 +43,14 @@ class dbPosts extends dbJSON
return $i;
}
// Returns the database
// Returns the complete database
public function getDB()
{
return $this->db;
}
// Return an array with the post's database, FALSE otherwise.
// Return an array with the post database, FALSE otherwise.
// Filtered by post key
public function getPostDB($key)
{
if($this->postExists($key)) {
@ -129,7 +131,7 @@ class dbPosts extends dbJSON
$args['status'] = 'scheduled';
}
// Verify arguments with the database fields.
// Verify arguments with the database fields
foreach($this->dbFields as $field=>$options)
{
// If the field is in the arguments
@ -139,8 +141,9 @@ class dbPosts extends dbJSON
$tmpValue = $this->generateTags($args['tags']);
}
else {
// Sanitize if will be saved on database.
// Where the argument will be stored, database or file
if( !$options['inFile'] ) {
// Sanitize if going to be stored on database
$tmpValue = Sanitize::html($args[$field]);
}
else {
@ -148,18 +151,16 @@ class dbPosts extends dbJSON
}
}
}
// Set a default value if not in the arguments
else
{
else {
// Set a default value if not in the arguments
$tmpValue = $options['value'];
}
// Check where the field will be written, in the file or in the database
// Check where the field will be stored in the file or in the database
if($options['inFile']) {
$dataForFile[$field] = Text::firstCharUp($field).': '.$tmpValue;
}
else
{
else {
// Set type
settype($tmpValue, gettype($options['value']));
@ -168,7 +169,7 @@ class dbPosts extends dbJSON
}
}
// Make the directory.
// Create the directory
if( Filesystem::mkdir(PATH_POSTS.$key) === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_POSTS.$key);
return false;
@ -184,19 +185,14 @@ class dbPosts extends dbJSON
// Calculate the checksum of the file
$dataForDb['md5file'] = md5_file(PATH_POSTS.$key.DS.FILENAME);
// Save the database
// Insert in the database
$this->db[$key] = $dataForDb;
// Sort posts before save
// Sort database posts before save
$this->sortByDate();
// Save database file
if( $this->save() === false ) {
// Trying to rollback
Log::set(__METHOD__.LOG_SEP.'Rollback...');
Filesystem::rmfile(PATH_POSTS.$key.DS.FILENAME);
Filesystem::rmdir(PATH_POSTS.$key);
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}

View File

@ -20,6 +20,23 @@ function reIndexTagsPosts()
return true;
}
function reIndexCategoriesPosts()
{
global $dbPosts;
global $dbCategories;
// Remove unpublished.
$dbPosts->removeUnpublished();
// Regenerate the tags index for posts.
$dbCategories->reindexPosts( $dbPosts->db );
// Restore the database, before remove the unpublished.
$dbPosts->restoreDB();
return true;
}
function buildPost($key)
{
global $dbPosts;