Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 66 additions & 17 deletions plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ protected function _getCategoryList(
'siteId' => null,
'order' => 'BlogCategories.lft asc',
'conditions' => [],
'threaded' => false
], $options);
'threaded' => false,
'categoryPostCounts' => [],
if ($viewCount && !$options['threaded'] && empty($options['categoryPostCounts'])) {
$options['categoryPostCounts'] = $this->getCategoryPostCounts($blogContentId);
}

// 検索条件
$conditions = $options['conditions'];
Expand Down Expand Up @@ -261,7 +264,6 @@ protected function _getCategoryList(

// 検索実行
$query = $this->find($findType)
->contain(['BlogPosts' => ['BlogContents' => ['Contents']]])
->where($conditions)
->select($fields)
->orderBy($options['order']);
Expand All @@ -280,20 +282,7 @@ protected function _getCategoryList(
foreach ($entities as $entity) {
// 表示件数
if ($viewCount) {
$childrenIds = $this->find('list', keyField: 'id', valueField: 'id')
->where([
['BlogCategories.lft > ' => $entity->lft],
['BlogCategories.rght < ' => $entity->rght]
])->toArray();
$categoryId = [$entity->id];
if ($childrenIds) {
$categoryId = array_merge($categoryId, $childrenIds);
}
$entity->count = $this->BlogPosts->find()
->where(array_merge(
['BlogPosts.blog_category_id IN' => $categoryId],
$this->BlogPosts->getConditionAllowPublish()
))->count();
$entity->count = $options['categoryPostCounts'][$entity->id] ?? 0;
}
// 子カテゴリ
if ($current < $depth) {
Expand All @@ -313,6 +302,66 @@ protected function _getCategoryList(
return $entities;
}

/**
* カテゴリごとの記事数を集計
*
* @param int|null $blogContentId
* @return array
*/
private function getCategoryPostCounts(?int $blogContentId): array
{
$conditions = [];
if ($blogContentId) {
$conditions['BlogCategories.blog_content_id'] = $blogContentId;
}
$categories = $this->find()
->select(['id', 'lft', 'rght'])
->where($conditions)
->all()
->toList();
if (!$categories) {
return [];
}

$postCounts = [];
$postConditions = [
'blog_category_id IN' => array_column($categories, 'id'),
...$this->BlogPosts->getConditionAllowPublish()
];
$query = $this->BlogPosts->find();
$query->select([
'blog_category_id',
'post_count' => $query->func()->count('*'),
])
->where($postConditions)
->groupBy('blog_category_id');
foreach ($query as $countRow) {
$postCounts[$countRow['blog_category_id']] = $countRow['post_count'];
}

$categoryPostCounts = [];
foreach ($categories as $category) {
$categoryId = $category['id'];
$totalCount = $postCounts[$categoryId] ?? 0;

// 子カテゴリの記事件数を親へ加算
foreach ($categories as $targetCategory) {
$targetCategoryId = $targetCategory['id'];
if ($targetCategoryId === $categoryId) {
continue;
}
if ($targetCategory['lft'] <= $category['lft'] || $targetCategory['rght'] >= $category['rght']) {
continue;
}
$totalCount += $postCounts[$targetCategoryId] ?? 0;
}
Comment thread
seto1 marked this conversation as resolved.

$categoryPostCounts[$categoryId] = $totalCount;
}

return $categoryPostCounts;
}

/**
* アクセス制限としてカテゴリの新規追加ができるか確認する
*
Expand Down
Loading