diff options
Diffstat (limited to 'core/include/post')
-rw-r--r-- | core/include/post/list.php | 35 | ||||
-rw-r--r-- | core/include/post/main.php | 113 |
2 files changed, 73 insertions, 75 deletions
diff --git a/core/include/post/list.php b/core/include/post/list.php index 84de108..4599883 100644 --- a/core/include/post/list.php +++ b/core/include/post/list.php @@ -2,16 +2,21 @@ #=============================================================================== # Get instances #=============================================================================== -$Database = Application::getDatabase(); $Language = Application::getLanguage(); #=============================================================================== +# Get repositories +#=============================================================================== +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); + +#=============================================================================== # Pagination #=============================================================================== $site_size = Application::get('POST.LIST_SIZE'); $site_sort = Application::get('POST.LIST_SORT'); -$count = $Database->query(sprintf('SELECT COUNT(id) FROM %s', Post\Attribute::TABLE))->fetchColumn(); +$count = $PostRepository->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('POST.SINGLE_REDIRECT') === TRUE AND $count === '1') { - $Statement = $Database->query(sprintf('SELECT id FROM %s LIMIT 1', Post\Attribute::TABLE)); - $Post = Post\Factory::build($Statement->fetchColumn()); +if(Application::get('POST.SINGLE_REDIRECT') === TRUE AND $count === 1) { + $Post = $PostRepository->getLast(); HTTP::redirect(Application::getEntityURL($Post)); } -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$postIDs = $Database->query(sprintf($execSQL, Post\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +#=============================================================================== +# Get paginated post list +#=============================================================================== +$posts = $PostRepository->getPaginated( + $site_sort, + $site_size, + ($currentSite-1) * $site_size +); -foreach($postIDs as $postID) { - try { - $Post = Post\Factory::build($postID); - $User = User\Factory::build($Post->get('user')); - $templates[] = generatePostItemTemplate($Post, $User); - } - catch(Post\Exception $Exception){} - catch(User\Exception $Exception){} +foreach($posts as $Post) { + $User = $UserRepository->find($Post->get('user')); + $templates[] = generatePostItemTemplate($Post, $User); } #=============================================================================== diff --git a/core/include/post/main.php b/core/include/post/main.php index 57cc402..86008f6 100644 --- a/core/include/post/main.php +++ b/core/include/post/main.php @@ -1,79 +1,72 @@ <?php #=============================================================================== -# Get instances +# Get repositories #=============================================================================== -$Database = Application::getDatabase(); -$Language = Application::getLanguage(); +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); #=============================================================================== -# TRY: Post\Exception +# Try to find post by slug URL or unique ID #=============================================================================== -try { - if(Application::get('POST.SLUG_URLS')) { - $Post = Post\Factory::buildBySlug($param); +if(Application::get('POST.SLUG_URLS')) { + if(!$Post = $PostRepository->findBy('slug', $param)) { + if($Post = $PostRepository->find($param)) { + HTTP::redirect(Application::getEntityURL($Post)); + } } +} - else { - $Post = Post\Factory::build($param); +else { + if(!$Post = $PostRepository->find($param)) { + if($Post = $PostRepository->findBy('slug', $param)) { + HTTP::redirect(Application::getEntityURL($Post)); + } } +} - $User = User\Factory::build($Post->get('user')); - - $post_data = generateItemTemplateData($Post); - $user_data = generateItemTemplateData($User); - - #=============================================================================== - # Add post data for previous and next post - #=============================================================================== - try { - $PrevPost = Post\Factory::build($Post->getPrevID()); - $post_data['PREV'] = generateItemTemplateData($PrevPost); - } catch(Post\Exception $Exception){} - - try { - $NextPost = Post\Factory::build($Post->getNextID()); - $post_data['NEXT'] = generateItemTemplateData($NextPost); - } catch(Post\Exception $Exception){} - - #=============================================================================== - # Build document - #=============================================================================== - $PostTemplate = Template\Factory::build('post/main'); - $PostTemplate->set('POST', $post_data); - $PostTemplate->set('USER', $user_data); +#=============================================================================== +# Throw 404 error if post could not be found +#=============================================================================== +if(!isset($Post)) { + Application::error404(); +} - $MainTemplate = Template\Factory::build('main'); - $MainTemplate->set('HTML', $PostTemplate); - $MainTemplate->set('HEAD', [ - 'NAME' => $post_data['ATTR']['NAME'], - 'DESC' => description($post_data['BODY']['HTML'](), Application::get('POST.DESCRIPTION_SIZE')), - 'PERM' => $post_data['URL'], - 'OG_IMAGES' => $post_data['FILE']['LIST'] - ]); +#=============================================================================== +# Generate template data +#=============================================================================== +$User = $UserRepository->find($Post->get('user')); +$post_data = generateItemTemplateData($Post); +$user_data = generateItemTemplateData($User); - # Get access to the current item data from main template - $MainTemplate->set('TYPE', 'POST'); - $MainTemplate->set('POST', $post_data); - $MainTemplate->set('USER', $user_data); +#=============================================================================== +# Add template data for previous and next post +#=============================================================================== +if($PrevPost = $PostRepository->findPrev($Post)) { + $post_data['PREV'] = generateItemTemplateData($PrevPost); +} - echo $MainTemplate; +if($NextPost = $PostRepository->findNext($Post)) { + $post_data['NEXT'] = generateItemTemplateData($NextPost); } #=============================================================================== -# CATCH: Post\Exception +# Build document #=============================================================================== -catch(Post\Exception $Exception) { - try { - if(Application::get('POST.SLUG_URLS') === FALSE) { - $Post = Post\Factory::buildBySlug($param); - } else { - $Post = Post\Factory::build($param); - } +$PostTemplate = Template\Factory::build('post/main'); +$PostTemplate->set('POST', $post_data); +$PostTemplate->set('USER', $user_data); - HTTP::redirect(Application::getEntityURL($Post)); - } +$MainTemplate = Template\Factory::build('main'); +$MainTemplate->set('TYPE', 'POST'); +$MainTemplate->set('POST', $post_data); +$MainTemplate->set('USER', $user_data); +$MainTemplate->set('HTML', $PostTemplate); +$MainTemplate->set('HEAD', [ + 'NAME' => $post_data['ATTR']['NAME'], + 'DESC' => description($post_data['BODY']['HTML'](), + Application::get('POST.DESCRIPTION_SIZE')), + 'PERM' => $post_data['URL'], + 'OG_IMAGES' => $post_data['FILE']['LIST'] +]); - catch(Post\Exception $Exception) { - Application::error404(); - } -} +echo $MainTemplate; |