diff options
Diffstat (limited to 'core/include/page')
-rw-r--r-- | core/include/page/list.php | 35 | ||||
-rw-r--r-- | core/include/page/main.php | 113 |
2 files changed, 73 insertions, 75 deletions
diff --git a/core/include/page/list.php b/core/include/page/list.php index 7618009..f7541fa 100644 --- a/core/include/page/list.php +++ b/core/include/page/list.php @@ -2,16 +2,21 @@ #=============================================================================== # Get instances #=============================================================================== -$Database = Application::getDatabase(); $Language = Application::getLanguage(); #=============================================================================== +# Get repositories +#=============================================================================== +$PageRepository = Application::getRepository('Page'); +$UserRepository = Application::getRepository('User'); + +#=============================================================================== # Pagination #=============================================================================== $site_size = Application::get('PAGE.LIST_SIZE'); $site_sort = Application::get('PAGE.LIST_SORT'); -$count = $Database->query(sprintf('SELECT COUNT(id) FROM %s', Page\Attribute::TABLE))->fetchColumn(); +$count = $PageRepository->getCount(); $lastSite = ceil($count / $site_size); $currentSite = HTTP::GET('site') ?? 1; @@ -24,23 +29,23 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { #=============================================================================== # Single redirect #=============================================================================== -if(Application::get('PAGE.SINGLE_REDIRECT') === TRUE AND $count === '1') { - $Statement = $Database->query(sprintf('SELECT id FROM %s LIMIT 1', Page\Attribute::TABLE)); - $Page = Page\Factory::build($Statement->fetchColumn()); +if(Application::get('PAGE.SINGLE_REDIRECT') === TRUE AND $count === 1) { + $Page = $PageRepository->getLast(); HTTP::redirect(Application::getEntityURL($Page)); } -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$pageIDs = $Database->query(sprintf($execSQL, Page\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +#=============================================================================== +# Get paginated page list +#=============================================================================== +$pages = $PageRepository->getPaginated( + $site_sort, + $site_size, + ($currentSite-1) * $site_size +); -foreach($pageIDs as $pageID) { - try { - $Page = Page\Factory::build($pageID); - $User = User\Factory::build($Page->get('user')); - $templates[] = generatePageItemTemplate($Page, $User); - } - catch(Page\Exception $Exception){} - catch(User\Exception $Exception){} +foreach($pages as $Page) { + $User = $UserRepository->find($Page->get('user')); + $templates[] = generatePageItemTemplate($Page, $User); } #=============================================================================== diff --git a/core/include/page/main.php b/core/include/page/main.php index 01ef40d..39e1d3c 100644 --- a/core/include/page/main.php +++ b/core/include/page/main.php @@ -1,79 +1,72 @@ <?php #=============================================================================== -# Get instances +# Get repositories #=============================================================================== -$Database = Application::getDatabase(); -$Language = Application::getLanguage(); +$PageRepository = Application::getRepository('Page'); +$UserRepository = Application::getRepository('User'); #=============================================================================== -# TRY: Page\Exception +# Try to find page by slug URL or unique ID #=============================================================================== -try { - if(Application::get('PAGE.SLUG_URLS')) { - $Page = Page\Factory::buildBySlug($param); +if(Application::get('PAGE.SLUG_URLS')) { + if(!$Page = $PageRepository->findBy('slug', $param)) { + if($Page = $PageRepository->find($param)) { + HTTP::redirect(Application::getEntityURL($Page)); + } } +} - else { - $Page = Page\Factory::build($param); +else { + if(!$Page = $PageRepository->find($param)) { + if($Page = $PageRepository->findBy('slug', $param)) { + HTTP::redirect(Application::getEntityURL($Page)); + } } +} - $User = User\Factory::build($Page->get('user')); - - $page_data = generateItemTemplateData($Page); - $user_data = generateItemTemplateData($User); - - #=============================================================================== - # Add page data for previous and next page - #=============================================================================== - try { - $PrevPage = Page\Factory::build($Page->getPrevID()); - $page_data['PREV'] = generateItemTemplateData($PrevPage); - } catch(Page\Exception $Exception){} - - try { - $NextPage = Page\Factory::build($Page->getNextID()); - $page_data['NEXT'] = generateItemTemplateData($NextPage); - } catch(Page\Exception $Exception){} - - #=============================================================================== - # Build document - #=============================================================================== - $PageTemplate = Template\Factory::build('page/main'); - $PageTemplate->set('PAGE', $page_data); - $PageTemplate->set('USER', $user_data); +#=============================================================================== +# Throw 404 error if page could not be found +#=============================================================================== +if(!isset($Page)) { + Application::error404(); +} - $MainTemplate = Template\Factory::build('main'); - $MainTemplate->set('HTML', $PageTemplate); - $MainTemplate->set('HEAD', [ - 'NAME' => $page_data['ATTR']['NAME'], - 'DESC' => description($page_data['BODY']['HTML'](), Application::get('PAGE.DESCRIPTION_SIZE')), - 'PERM' => $page_data['URL'], - 'OG_IMAGES' => $page_data['FILE']['LIST'] - ]); +#=============================================================================== +# Generate template data +#=============================================================================== +$User = $UserRepository->find($Page->get('user')); +$page_data = generateItemTemplateData($Page); +$user_data = generateItemTemplateData($User); - # Get access to the current item data from main template - $MainTemplate->set('TYPE', 'PAGE'); - $MainTemplate->set('PAGE', $page_data); - $MainTemplate->set('USER', $user_data); +#=============================================================================== +# Add template data for previous and next page +#=============================================================================== +if($PrevPage = $PageRepository->findPrev($Page)) { + $page_data['PREV'] = generateItemTemplateData($PrevPage); +} - echo $MainTemplate; +if($NextPage = $PageRepository->findNext($Page)) { + $page_data['NEXT'] = generateItemTemplateData($NextPage); } #=============================================================================== -# CATCH: Page\Exception +# Build document #=============================================================================== -catch(Page\Exception $Exception) { - try { - if(Application::get('PAGE.SLUG_URLS') === FALSE) { - $Page = Page\Factory::buildBySlug($param); - } else { - $Page = Page\Factory::build($param); - } +$PageTemplate = Template\Factory::build('page/main'); +$PageTemplate->set('PAGE', $page_data); +$PageTemplate->set('USER', $user_data); - HTTP::redirect(Application::getEntityURL($Page)); - } +$MainTemplate = Template\Factory::build('main'); +$MainTemplate->set('TYPE', 'PAGE'); +$MainTemplate->set('PAGE', $page_data); +$MainTemplate->set('USER', $user_data); +$MainTemplate->set('HTML', $PageTemplate); +$MainTemplate->set('HEAD', [ + 'NAME' => $page_data['ATTR']['NAME'], + 'DESC' => description($page_data['BODY']['HTML'](), + Application::get('PAGE.DESCRIPTION_SIZE')), + 'PERM' => $page_data['URL'], + 'OG_IMAGES' => $page_data['FILE']['LIST'] +]); - catch(Page\Exception $Exception) { - Application::error404(); - } -} +echo $MainTemplate; |