From 09a2651c728072827d5f2f909184b9a18b863ff6 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Tue, 13 Dec 2016 23:14:21 +0100 Subject: [PATCH] Added counter badge in article category list --- .../templates/boxArticleCategories.tpl | 15 ++++++-- .../category/ArticleCategoryCache.class.php | 37 ++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/com.woltlab.wcf/templates/boxArticleCategories.tpl b/com.woltlab.wcf/templates/boxArticleCategories.tpl index 3f77499710..37d4f03834 100644 --- a/com.woltlab.wcf/templates/boxArticleCategories.tpl +++ b/com.woltlab.wcf/templates/boxArticleCategories.tpl @@ -1,19 +1,28 @@
    {foreach from=$categoryList item=categoryItem} categoryID == $categoryItem->categoryID} class="active"{/if} data-category-id="{@$categoryItem->categoryID}"> - {$categoryItem->getTitle()} + + {$categoryItem->getTitle()} + {#$categoryItem->getArticles()} + {if $activeCategory && ($activeCategory->categoryID == $categoryItem->categoryID || $activeCategory->isParentCategory($categoryItem->getDecoratedObject())) && $categoryItem->hasChildren()}
      {foreach from=$categoryItem item=subCategoryItem} categoryID == $subCategoryItem->categoryID} class="active"{/if} data-category-id="{@$subCategoryItem->categoryID}"> - {$subCategoryItem->getTitle()} + + {$subCategoryItem->getTitle()} + {#$subCategoryItem->getArticles()} + {if $activeCategory && ($activeCategory->categoryID == $subCategoryItem->categoryID || $activeCategory->parentCategoryID == $subCategoryItem->categoryID) && $subCategoryItem->hasChildren()}
        {foreach from=$subCategoryItem item=subSubCategoryItem} categoryID == $subSubCategoryItem->categoryID} class="active"{/if} data-category-id="{@$subSubCategoryItem->categoryID}"> - {$subSubCategoryItem->getTitle()} + + {$subSubCategoryItem->getTitle()} + {#$subSubCategoryItem->getArticles()} + {/foreach}
      diff --git a/wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php b/wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php index dc777ff8c5..db67e883a5 100644 --- a/wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php +++ b/wcfsetup/install/files/lib/data/article/category/ArticleCategoryCache.class.php @@ -1,6 +1,8 @@ articles = []; + $sql = "SELECT COUNT(*) AS count, categoryID FROM wcf" . WCF_N . "_article WHERE publicationStatus = ? GROUP BY categoryID"; $statement = WCF::getDB()->prepareStatement($sql); $statement->execute([Article::PUBLISHED]); - $this->articles = $statement->fetchMap('categoryID', 'count'); + $articles = $statement->fetchMap('categoryID', 'count'); + + $categoryToParent = []; + /** @var Category $category */ + foreach (CategoryHandler::getInstance()->getCategories(ArticleCategory::OBJECT_TYPE_NAME) as $category) { + if (!isset($categoryToParent[$category->parentCategoryID])) $categoryToParent[$category->parentCategoryID] = []; + $categoryToParent[$category->parentCategoryID][] = $category->categoryID; + } + + $this->countArticles($categoryToParent, $articles, 0); + } + + /** + * Counts the articles contained in this category and its children. + * + * @param integer[] $categoryToParent + * @param integer[] $articles + * @param integer $categoryID + * @return integer + */ + protected function countArticles(array &$categoryToParent, array &$articles, $categoryID) { + $count = (isset($articles[$categoryID])) ? $articles[$categoryID] : 0; + if (isset($categoryToParent[$categoryID])) { + /** @var Category $category */ + foreach ($categoryToParent[$categoryID] as $childCategoryID) { + $count += $this->countArticles($categoryToParent, $articles, $childCategoryID); + } + } + + if ($categoryID) $this->articles[$categoryID] = $count; + + return $count; } /** -- 2.20.1