From e6cef37e0c782fe770db20888d99c17d10e2c479 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Thu, 1 Jul 2021 20:11:34 +0200 Subject: 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 --- core/functions.php | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'core/functions.php') 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 @@ 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 #=============================================================================== @@ -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; } @@ -111,6 +152,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 #=============================================================================== @@ -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; -- cgit v1.2.3