aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-20 00:27:46 +0200
committerThomas Lange <code@nerdmind.de>2021-07-20 00:34:47 +0200
commit01d4727f939c0b9530fe5fc976b7accb9e078db1 (patch)
tree2053b57a25ef42cec5c6e79ae837e5a4d0ceb69e
parentdd4b3d9ebb85c9bc8138212fd7cb207ab154f626 (diff)
downloadblog-01d4727f939c0b9530fe5fc976b7accb9e078db1.tar.gz
blog-01d4727f939c0b9530fe5fc976b7accb9e078db1.tar.xz
blog-01d4727f939c0b9530fe5fc976b7accb9e078db1.zip
Rename and reorganize several "get count" methods
-rw-r--r--core/functions.php5
-rw-r--r--core/include/category/main.php6
-rw-r--r--core/include/user/main.php6
-rw-r--r--core/namespace/ORM/Repositories/Category.php17
-rw-r--r--core/namespace/ORM/Repositories/Page.php10
-rw-r--r--core/namespace/ORM/Repositories/Post.php21
-rw-r--r--core/namespace/ORM/Repositories/User.php27
7 files changed, 49 insertions, 43 deletions
diff --git a/core/functions.php b/core/functions.php
index 547f84a..97ff802 100644
--- a/core/functions.php
+++ b/core/functions.php
@@ -29,7 +29,6 @@ function createPaginationTemplate($current, $last, string $location): Template {
#===============================================================================
function generateCategoryItemTemplate(Category $Category, bool $is_root = FALSE): Template {
$CategoryRepository = Application::getRepository('Category');
- $PostRepository = Application::getRepository('Post');
foreach($CategoryRepository->findWithParents($Category->getID()) as $Category) {
$category_data = generateItemTemplateData($Category);
@@ -41,8 +40,8 @@ function generateCategoryItemTemplate(Category $Category, bool $is_root = FALSE)
$Template->set('CATEGORY', $category_data ?? []);
$Template->set('CATEGORIES', $category_list ?? []);
$Template->set('COUNT', [
- 'POST' => $PostRepository->getCountByCategory($Category),
- 'CHILDREN' => $CategoryRepository->getChildrenCount($Category)
+ 'POST' => $CategoryRepository->getNumberOfPosts($Category),
+ 'CHILDREN' => $CategoryRepository->getNumberOfChildren($Category)
]);
return $Template;
diff --git a/core/include/category/main.php b/core/include/category/main.php
index 2e84aa1..46ffa75 100644
--- a/core/include/category/main.php
+++ b/core/include/category/main.php
@@ -74,7 +74,7 @@ foreach($child_categories as $ChildCategory) {
$site_size = Application::get('POST.LIST_SIZE');
$site_sort = Application::get('POST.LIST_SORT');
-$count = $PostRepository->getCountByCategory($Category);
+$count = $CategoryRepository->getNumberOfPosts($Category);
$lastSite = ceil($count / $site_size);
$currentSite = HTTP::GET('site') ?? 1;
@@ -106,8 +106,8 @@ $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)
+ 'POST' => $CategoryRepository->getNumberOfPosts($Category),
+ 'CHILDREN' => $CategoryRepository->getNumberOfChildren($Category)
]);
$CategoryTemplate->set('PAGINATION', [
'THIS' => $currentSite,
diff --git a/core/include/user/main.php b/core/include/user/main.php
index c0cb20d..ef27351 100644
--- a/core/include/user/main.php
+++ b/core/include/user/main.php
@@ -2,8 +2,6 @@
#===============================================================================
# Get repositories
#===============================================================================
-$PageRepository = Application::getRepository('Page');
-$PostRepository = Application::getRepository('Post');
$UserRepository = Application::getRepository('User');
#===============================================================================
@@ -54,8 +52,8 @@ if($NextUser = $UserRepository->findNext($User)) {
$UserTemplate = Template\Factory::build('user/main');
$UserTemplate->set('USER', $user_data);
$UserTemplate->set('COUNT', [
- 'POST' => $PostRepository->getCountByUser($User),
- 'PAGE' => $PageRepository->getCountByUser($User)
+ 'PAGE' => $UserRepository->getNumberOfPages($User),
+ 'POST' => $UserRepository->getNumberOfPosts($User)
]);
$MainTemplate = Template\Factory::build('main');
diff --git a/core/namespace/ORM/Repositories/Category.php b/core/namespace/ORM/Repositories/Category.php
index c00fdf3..df32f69 100644
--- a/core/namespace/ORM/Repositories/Category.php
+++ b/core/namespace/ORM/Repositories/Category.php
@@ -9,6 +9,19 @@ class Category extends Repository {
public static function getClassName(): string { return 'ORM\Entities\Category'; }
#===============================================================================
+ # Get number of *posts* assigned to $Category
+ #===============================================================================
+ public function getNumberOfPosts(CategoryEntity $Category): int {
+ $query = 'SELECT COUNT(id) FROM %s WHERE category = ?';
+ $query = sprintf($query, Post::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$Category->getID()]);
+
+ return $Statement->fetchColumn();
+ }
+
+ #===============================================================================
# Find category with parents based on primary key
#===============================================================================
public function findWithParents($id): array {
@@ -60,9 +73,9 @@ class Category extends Repository {
}
#===============================================================================
- # Get children count of $Category
+ # Get number of children categories assigned to $Category
#===============================================================================
- public function getChildrenCount(CategoryEntity $Category): int {
+ public function getNumberOfChildren(CategoryEntity $Category): int {
$query = 'WITH RECURSIVE tree AS (
SELECT * FROM %s WHERE id = ? UNION
SELECT c.* FROM %s c, tree WHERE tree.id = c.parent
diff --git a/core/namespace/ORM/Repositories/Page.php b/core/namespace/ORM/Repositories/Page.php
index 595b420..714605e 100644
--- a/core/namespace/ORM/Repositories/Page.php
+++ b/core/namespace/ORM/Repositories/Page.php
@@ -6,14 +6,4 @@ use ORM\Entities\User;
class Page extends Repository {
public static function getTableName(): string { return 'page'; }
public static function getClassName(): string { return 'ORM\Entities\Page'; }
-
- public function getCountByUser(User $User): int {
- $query = 'SELECT COUNT(id) FROM %s WHERE user = ?';
- $query = sprintf($query, static::getTableName());
-
- $Statement = $this->Database->prepare($query);
- $Statement->execute([$User->getID()]);
-
- return $Statement->fetchColumn();
- }
}
diff --git a/core/namespace/ORM/Repositories/Post.php b/core/namespace/ORM/Repositories/Post.php
index d6328e6..7f672e5 100644
--- a/core/namespace/ORM/Repositories/Post.php
+++ b/core/namespace/ORM/Repositories/Post.php
@@ -7,25 +7,4 @@ use ORM\Entities\Category;
class Post extends Repository {
public static function getTableName(): string { return 'post'; }
public static function getClassName(): string { return 'ORM\Entities\Post'; }
-
- public function getCountByUser(User $User): int {
- $query = 'SELECT COUNT(id) FROM %s WHERE user = ?';
- $query = sprintf($query, static::getTableName());
-
- $Statement = $this->Database->prepare($query);
- $Statement->execute([$User->getID()]);
-
- return $Statement->fetchColumn();
- }
-
- # TODO: This only gets the count of the direct category, not its children
- public function getCountByCategory(Category $Category): int {
- $query = 'SELECT COUNT(id) FROM %s WHERE category = ?';
- $query = sprintf($query, static::getTableName());
-
- $Statement = $this->Database->prepare($query);
- $Statement->execute([$Category->getID()]);
-
- return $Statement->fetchColumn();
- }
}
diff --git a/core/namespace/ORM/Repositories/User.php b/core/namespace/ORM/Repositories/User.php
index 629d9c1..ce3ff2c 100644
--- a/core/namespace/ORM/Repositories/User.php
+++ b/core/namespace/ORM/Repositories/User.php
@@ -1,8 +1,35 @@
<?php
namespace ORM\Repositories;
use ORM\Repository;
+use ORM\Entities\User as UserEntity;
class User extends Repository {
public static function getTableName(): string { return 'user'; }
public static function getClassName(): string { return 'ORM\Entities\User'; }
+
+ #===============================================================================
+ # Get number of *pages* assigned to $User
+ #===============================================================================
+ public function getNumberOfPages(UserEntity $User): int {
+ $query = 'SELECT COUNT(id) FROM %s WHERE user = ?';
+ $query = sprintf($query, Page::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$User->getID()]);
+
+ return $Statement->fetchColumn();
+ }
+
+ #===============================================================================
+ # Get number of *posts* assigned to $User
+ #===============================================================================
+ public function getNumberOfPosts(UserEntity $User): int {
+ $query = 'SELECT COUNT(id) FROM %s WHERE user = ?';
+ $query = sprintf($query, Post::getTableName());
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute([$User->getID()]);
+
+ return $Statement->fetchColumn();
+ }
}