-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBlogCategoryUtil.php
More file actions
113 lines (100 loc) · 3.19 KB
/
Copy pathBlogCategoryUtil.php
File metadata and controls
113 lines (100 loc) · 3.19 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Module\Blog\Util;
use Illuminate\Support\Facades\Cache;
use ModStart\Core\Assets\AssetsUtil;
use ModStart\Core\Dao\ModelUtil;
use ModStart\Core\Util\TreeUtil;
use Module\Blog\Model\Blog;
use Module\Blog\Model\BlogCategory;
class BlogCategoryUtil
{
public static function clearCache()
{
Cache::forget('Blog:Categories');
}
public static function all()
{
return Cache::rememberForever('Blog:Categories', function () {
$records = ModelUtil::all(BlogCategory::class, [], ['*'], ['sort', 'asc']);
AssetsUtil::recordsFixFullOrDefault($records, 'cover', 'asset/image/none.png');
foreach ($records as $k => $v) {
$records[$k]['_url'] = UrlUtil::category($v);
}
return $records;
});
}
public static function categoryTree()
{
$nodes = self::all();
return TreeUtil::nodesToTree($nodes);
}
public static function categoryTreeFlat()
{
$tree = self::categoryTree();
$nodes = TreeUtil::treeToListWithLevel($tree);
foreach ($nodes as $i => $v) {
$chain = TreeUtil::nodesChain($nodes, $v['id']);
$nodes[$i]['_fullTitle'] = join('-', array_map(function ($item) {
return $item['title'];
}, $chain));
}
return $nodes;
}
public static function categoryChainWithItems($categoryId)
{
$records = self::all();
return TreeUtil::nodesChainWithItems($records, $categoryId);
}
public static function get($id)
{
foreach (self::all() as $one) {
if ($one['id'] == $id) {
return $one;
}
}
return null;
}
public static function listChildCategories($categoryId)
{
$records = self::all();
$records = array_filter($records, function ($item) use ($categoryId) {
return $item['pid'] == $categoryId;
});
return array_values($records);
}
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 = Blog::published()->whereIn('categoryId', $ids)->count();
}
ModelUtil::update(BlogCategory::class, $item['id'], [
'blogCount' => $blogCount,
]);
}
}
self::clearCache();
}
}