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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
<?php
#===============================================================================
# Get repositories
#===============================================================================
$CategoryRepository = Application::getRepository('Category');
$PostRepository = Application::getRepository('Post');
$UserRepository = Application::getRepository('User');
#===============================================================================
# Try to find category (with parents) by slug URL or unique ID
#===============================================================================
if(Application::get('CATEGORY.SLUG_URLS')) {
if(!$categories = $CategoryRepository->findWithParentsBy('slug', $param)) {
if($categories = $CategoryRepository->findWithParents($param)) {
$redirect_scheduled = TRUE;
}
}
}
else {
if(!$categories = $CategoryRepository->findWithParents($param)) {
if($categories = $CategoryRepository->findWithParentsBy('slug', $param)) {
$redirect_scheduled = TRUE;
}
}
}
#===============================================================================
# Throw 404 error if category (with parents) could not be found
#===============================================================================
if(!isset($categories)) {
Application::error404();
}
#===============================================================================
# The last element represents the current category
#===============================================================================
$Category = $categories[array_key_last($categories)];
#===============================================================================
# If category with parents was found by alternative, redirect to correct URL
#===============================================================================
if(isset($redirect_scheduled)) {
HTTP::redirect(Application::getEntityURL($Category));
}
#===============================================================================
# Generate category template data (including parents)
#===============================================================================
foreach($categories as $_Category) {
$category_list[] = generateItemTemplateData($_Category);
}
#===============================================================================
# Define data variable for current category
#===============================================================================
$category_data = $category_list[array_key_last($category_list)];
#===============================================================================
# Generate category children list
#===============================================================================
$child_categories = $CategoryRepository->getAll(
['parent' => $Category->getID()],
Application::get('CATEGORY.LIST_SORT')
);
foreach($child_categories as $ChildCategory) {
$child_templates[] = generateCategoryItemTemplate($ChildCategory);
}
#===============================================================================
# Pagination (for posts in this category)
#===============================================================================
$site_size = Application::get('POST.LIST_SIZE');
$site_sort = Application::get('POST.LIST_SORT');
$count = $PostRepository->getCountByCategory($Category);
$lastSite = ceil($count / $site_size);
$currentSite = HTTP::GET('site') ?? 1;
$currentSite = intval($currentSite);
if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) {
Application::error404();
}
#===============================================================================
# Get paginated post list for this category
#===============================================================================
$posts = $PostRepository->getAll(
['category' => $Category->getID()],
$site_sort,
($currentSite-1) * $site_size.','.$site_size
);
foreach($posts as $Post) {
$User = $UserRepository->find($Post->get('user'));
$post_templates[] = generatePostItemTemplate($Post, $User);
}
#===============================================================================
# Build document
#===============================================================================
$CategoryTemplate = Template\Factory::build('category/main');
$CategoryTemplate->set('CATEGORY', $category_data);
$CategoryTemplate->set('CATEGORIES', $category_list ?? []);
$CategoryTemplate->set('COUNT', [
'POST' => $PostRepository->getCountByCategory($Category),
'CHILDREN' => $CategoryRepository->getChildrenCount($Category)
]);
$CategoryTemplate->set('PAGINATION', [
'THIS' => $currentSite,
'LAST' => $lastSite,
'HTML' => generatePostNaviTemplate($currentSite)
]);
$CategoryTemplate->set('LIST', [
'POSTS' => $post_templates ?? [],
'CATEGORIES' => $child_templates ?? []
]);
$MainTemplate = Template\Factory::build('main');
$MainTemplate->set('TYPE', 'CATEGORY');
$MainTemplate->set('CATEGORY', $category_data);
$MainTemplate->set('CATEGORIES', $category_list ?? []);
$MainTemplate->set('HTML', $CategoryTemplate);
$MainTemplate->set('HEAD', [
'NAME' => $category_data['ATTR']['NAME'],
'DESC' => description($category_data['BODY']['HTML'](),
Application::get('CATEGORY.DESCRIPTION_SIZE')),
'PERM' => $category_data['URL'],
'OG_IMAGES' => $category_data['FILE']['LIST']
]);
echo $MainTemplate;
|