From f5f94531c9d5b369fdf9c287f63c41476496e5ee Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Fri, 2 Jul 2021 20:17:42 +0200 Subject: Add generic function to create pagination template This commit adds and uses the new function createPaginationTemplate to create the generic pagination template for various entity lists. This function replaces the many old confusing functions: * generateNaviTemplate * generatePageNaviTemplate * generatePostNaviTemplate * generateUserNaviTemplate * generateCategoryNaviTemplate * generateCategoryPostNaviTemplate --- core/functions.php | 65 +++++------------------------------------- core/include/category/list.php | 3 +- core/include/category/main.php | 3 +- core/include/home.php | 9 +++++- core/include/page/list.php | 4 ++- core/include/post/list.php | 3 +- core/include/user/list.php | 4 ++- 7 files changed, 27 insertions(+), 64 deletions(-) (limited to 'core') diff --git a/core/functions.php b/core/functions.php index 3282f1c..8a46b5b 100644 --- a/core/functions.php +++ b/core/functions.php @@ -9,66 +9,15 @@ use Template\Template as Template; use Template\Factory as TemplateFactory; #=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generateNaviTemplate(int $current, $location, $namespace): Template { - $Repository = Application::getRepository($namespace); - - $listSize = Application::get(strtoupper($namespace).'.LIST_SIZE'); - $lastSite = ceil($Repository->getCount() / $listSize); - - $PaginationTemplate = TemplateFactory::build('pagination'); - $PaginationTemplate->set('THIS', $current); - $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', "{$location}?site=%d"); - - return $PaginationTemplate; -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generatePageNaviTemplate($current): Template { - return generateNaviTemplate($current, Application::getPageURL(), 'Page'); -} - +# Create generic pagination template #=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generatePostNaviTemplate($current): Template { - return generateNaviTemplate($current, Application::getPostURL(), 'Post'); -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generateUserNaviTemplate($current): Template { - return generateNaviTemplate($current, Application::getUserURL(), 'User'); -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generateCategoryNaviTemplate($current): Template { - return generateNaviTemplate($current, Application::getCategoryURL(), 'Category'); -} - -#=============================================================================== -# Generate the post navigation template for posts in a category -#=============================================================================== -function generateCategoryPostNaviTemplate(int $current, Category $Category): Template { - $location = Application::getEntityURL($Category); - $Repository = Application::getRepository('Post'); - - $listSize = Application::get('POST.LIST_SIZE'); - $lastSite = ceil($Repository->getCountByCategory($Category) / $listSize); - - $PaginationTemplate = TemplateFactory::build('pagination'); - $PaginationTemplate->set('THIS', $current); - $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', "{$location}?site=%d"); +function createPaginationTemplate($current, $last, string $location): Template { + $Pagination = TemplateFactory::build('pagination'); + $Pagination->set('THIS', $current); + $Pagination->set('LAST', $last); + $Pagination->set('HREF', "{$location}?site=%d"); - return $PaginationTemplate; + return $Pagination; } #=============================================================================== diff --git a/core/include/category/list.php b/core/include/category/list.php index 4702a79..3a5720b 100644 --- a/core/include/category/list.php +++ b/core/include/category/list.php @@ -52,7 +52,8 @@ $ListTemplate = Template\Factory::build('category/list'); $ListTemplate->set('PAGINATION', [ 'THIS' => $currentSite, 'LAST' => $lastSite, - 'HTML' => generateCategoryNaviTemplate($currentSite) + 'HTML' => createPaginationTemplate( + $currentSite, $lastSite, Application::getCategoryURL()) ]); $ListTemplate->set('LIST', [ 'CATEGORIES' => $templates ?? [] diff --git a/core/include/category/main.php b/core/include/category/main.php index 066ea26..b1bea72 100644 --- a/core/include/category/main.php +++ b/core/include/category/main.php @@ -111,7 +111,8 @@ $CategoryTemplate->set('COUNT', [ $CategoryTemplate->set('PAGINATION', [ 'THIS' => $currentSite, 'LAST' => $lastSite, - 'HTML' => generateCategoryPostNaviTemplate($currentSite, $Category) + 'HTML' => createPaginationTemplate( + $currentSite, $lastSite, Application::getEntityURL($Category)) ]); $CategoryTemplate->set('LIST', [ 'POSTS' => $post_templates ?? [], diff --git a/core/include/home.php b/core/include/home.php index bc73bef..2893f0e 100644 --- a/core/include/home.php +++ b/core/include/home.php @@ -18,12 +18,19 @@ foreach($posts as $Post) { $templates[] = generatePostItemTemplate($Post, $User); } +#=============================================================================== +# Pagination +#=============================================================================== +$count = $PostRepository->getCount(); +$lastSite = ceil($count / Application::get('POST.LIST_SIZE')); + #=============================================================================== # Build document #=============================================================================== $HomeTemplate = Template\Factory::build('home'); $HomeTemplate->set('PAGINATION', [ - 'HTML' => generatePostNaviTemplate(1) + 'HTML' => createPaginationTemplate( + 1, $lastSite, Application::getPostURL()) ]); $HomeTemplate->set('LIST', [ 'POSTS' => $templates ?? [] diff --git a/core/include/page/list.php b/core/include/page/list.php index f7541fa..8cfbce5 100644 --- a/core/include/page/list.php +++ b/core/include/page/list.php @@ -55,7 +55,9 @@ $ListTemplate = Template\Factory::build('page/list'); $ListTemplate->set('PAGINATION', [ 'THIS' => $currentSite, 'LAST' => $lastSite, - 'HTML' => generatePageNaviTemplate($currentSite) + 'HTML' => createPaginationTemplate( + $currentSite, $lastSite, Application::getPageURL() + ) ]); $ListTemplate->set('LIST', [ 'PAGES' => $templates ?? [] diff --git a/core/include/post/list.php b/core/include/post/list.php index 4599883..ab48552 100644 --- a/core/include/post/list.php +++ b/core/include/post/list.php @@ -55,7 +55,8 @@ $ListTemplate = Template\Factory::build('post/list'); $ListTemplate->set('PAGINATION', [ 'THIS' => $currentSite, 'LAST' => $lastSite, - 'HTML' => generatePostNaviTemplate($currentSite) + 'HTML' => createPaginationTemplate( + $currentSite, $lastSite, Application::getPostURL()) ]); $ListTemplate->set('LIST', [ 'POSTS' => $templates ?? [] diff --git a/core/include/user/list.php b/core/include/user/list.php index f6f794a..f4aec77 100644 --- a/core/include/user/list.php +++ b/core/include/user/list.php @@ -53,7 +53,9 @@ $ListTemplate = Template\Factory::build('user/list'); $ListTemplate->set('PAGINATION', [ 'THIS' => $currentSite, 'LAST' => $lastSite, - 'HTML' => generateUserNaviTemplate($currentSite) + 'HTML' => createPaginationTemplate( + $currentSite, $lastSite, Application::getUserURL() + ) ]); $ListTemplate->set('LIST', [ 'USERS' => $templates ?? [] -- cgit v1.2.3