aboutsummaryrefslogtreecommitdiffstats
path: root/core/functions.php
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-01 20:11:34 +0200
committerThomas Lange <code@nerdmind.de>2021-07-01 20:11:34 +0200
commite6cef37e0c782fe770db20888d99c17d10e2c479 (patch)
treeaed0790a0fa8eb7cceef7bb32aef4f69b724d619 /core/functions.php
parentf0ea19767d502ec7b5afff7c66c2681292175a3b (diff)
downloadblog-e6cef37e0c782fe770db20888d99c17d10e2c479.tar.gz
blog-e6cef37e0c782fe770db20888d99c17d10e2c479.tar.xz
blog-e6cef37e0c782fe770db20888d99c17d10e2c479.zip
Add category system to categorize posts (readme)
This commit implements a new category system to categorize posts. Each category can have an unlimited number of nested children categories. A single post don't necessarily need to be in a category, but it can. Each category can have a full content body like posts or pages, so you have enough space to describe the content of your categories. Please note that you need to have at least the following MySQL/MariaDB versions to use the category system, because it uses "WITH RECURSIVE" database queries, the so-called "Common-Table-Expressions (CTE)". MariaDB: 10.2.2 MySQL: 8.0 See: https://mariadb.com/kb/en/with/ See: https://dev.mysql.com/doc/refman/8.0/en/with.html
Diffstat (limited to 'core/functions.php')
-rw-r--r--core/functions.php64
1 files changed, 63 insertions, 1 deletions
diff --git a/core/functions.php b/core/functions.php
index dadaab8..ea4b7eb 100644
--- a/core/functions.php
+++ b/core/functions.php
@@ -1,8 +1,9 @@
<?php
+use ORM\EntityInterface;
+use ORM\Entities\Category;
use ORM\Entities\Page;
use ORM\Entities\Post;
use ORM\Entities\User;
-use ORM\EntityInterface;
use Template\Template as Template;
use Template\Factory as TemplateFactory;
@@ -48,6 +49,37 @@ function generateUserNaviTemplate($current): Template {
#===============================================================================
# Helper function to reduce duplicate code
#===============================================================================
+function generateCategoryNaviTemplate($current): Template {
+ return generateNaviTemplate($current, Application::getCategoryURL(), 'Category');
+}
+
+#===============================================================================
+# Helper function to reduce duplicate code
+#===============================================================================
+function generateCategoryItemTemplate(Category $Category, bool $is_root = FALSE): Template {
+ $CategoryRepository = Application::getRepository('Category');
+ $PostRepository = Application::getRepository('Post');
+
+ foreach($CategoryRepository->findWithParents($Category->getID()) as $Category) {
+ $category_data = generateItemTemplateData($Category);
+ $category_list[] = $category_data;
+ }
+
+ $Template = TemplateFactory::build('category/item');
+ $Template->set('IS_ROOT', $is_root);
+ $Template->set('CATEGORY', $category_data ?? []);
+ $Template->set('CATEGORIES', $category_list ?? []);
+ $Template->set('COUNT', [
+ 'POST' => $PostRepository->getCountByCategory($Category),
+ 'CHILDREN' => $CategoryRepository->getChildrenCount($Category)
+ ]);
+
+ return $Template;
+}
+
+#===============================================================================
+# Helper function to reduce duplicate code
+#===============================================================================
function generatePageItemTemplate(Page $Page, User $User): Template {
$Template = TemplateFactory::build('page/item');
$Template->set('PAGE', generateItemTemplateData($Page));
@@ -60,9 +92,18 @@ function generatePageItemTemplate(Page $Page, User $User): Template {
# Helper function to reduce duplicate code
#===============================================================================
function generatePostItemTemplate(Post $Post, User $User): Template {
+ $CategoryRepository = Application::getRepository('Category');
+
+ foreach($CategoryRepository->findWithParents($Post->get('category')) as $Category) {
+ $category_data = generateItemTemplateData($Category);
+ $categories[] = $category_data;
+ }
+
$Template = TemplateFactory::build('post/item');
$Template->set('POST', generateItemTemplateData($Post));
$Template->set('USER', generateItemTemplateData($User));
+ $Template->set('CATEGORY', $category_data ?? []);
+ $Template->set('CATEGORIES', $categories ?? []);
return $Template;
}
@@ -112,6 +153,24 @@ function generateItemTemplateData(EntityInterface $Entity): array {
}
#===============================================================================
+# Generate a nested tree from a category data array
+#===============================================================================
+function generateCategoryDataTree(array $category_data, $root = 0): array {
+ foreach($category_data as &$category){
+ $tree[intval($category['PARENT'])][] = &$category;
+ unset($category['PARENT']);
+ }
+
+ foreach($category_data as &$category){
+ if (isset($tree[$category['ID']])){
+ $category['CHILDS'] = $tree[$category['ID']];
+ }
+ }
+
+ return $tree[$root] ?? [];
+}
+
+#===============================================================================
# Generate pseudo GUID for entity
#===============================================================================
function generatePseudoGUID(EntityInterface $Entity) {
@@ -166,6 +225,9 @@ function parseContentTags(string $text): string {
#===============================================================================
function parseEntityContent(EntityInterface $Entity): string {
switch($class = get_class($Entity)) {
+ case 'ORM\Entities\Category':
+ $prefix = 'CATEGORY';
+ break;
case 'ORM\Entities\Page':
$prefix = 'PAGE';
break;