forked from modstart/ModStartBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlogCategoryUtil.php
More file actions
77 lines (67 loc) · 1.99 KB
/
Copy pathBlogCategoryUtil.php
File metadata and controls
77 lines (67 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace Module\Blog\Util;
use Illuminate\Support\Facades\Cache;
use ModStart\Core\Dao\ModelUtil;
use ModStart\Core\Util\TreeUtil;
class BlogCategoryUtil
{
public static function clearCache()
{
Cache::forget('BlogCategories');
}
public static function all()
{
return Cache::rememberForever('BlogCategories', function () {
return ModelUtil::all('blog_category', [], ['*'], ['sort', 'desc']);
});
}
public static function categoryTree()
{
$nodes = self::all();
return TreeUtil::nodesToTree($nodes);
}
public static function get($id)
{
foreach (self::all() as $one) {
if ($one['id'] == $id) {
return $one;
}
}
return null;
}
public static function childrenIds($categoryId)
{
if ($categoryId <= 0) {
return [];
}
$nodes = self::all();
return array_merge([$categoryId], TreeUtil::nodesChildrenIds($nodes, $categoryId));
}
public static function updateCount($categoryIds)
{
if (!is_array($categoryIds)) {
$categoryIds = [$categoryIds];
}
$categoryIds = array_unique($categoryIds);
foreach ($categoryIds as $catId) {
$chapter = self::get($catId);
if (empty($chapter)) {
continue;
}
$tree = self::categoryTree();
$chain = TreeUtil::treeChain($tree, $catId);
foreach ($chain as $item) {
$ids = TreeUtil::treeNodeChildrenIds($tree, $item['id']);
if (empty($ids)) {
$blogCount = 0;
} else {
$blogCount = ModelUtil::model('blog')->whereIn('categoryId', $ids)->count();
}
ModelUtil::update('blog_category', $item['id'], [
'blogCount' => $blogCount,
]);
}
}
self::clearCache();
}
}