From 8cd1105b111b89106f24c5b50795afb5ff28a935 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Tue, 22 Jun 2021 01:18:02 +0200 Subject: Implement new Repository and Entity classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds new Repository and Entity classes which are better abstracted from the rest of the application. They dont know anymore about configuration options or how to parse to HTML because this is not the job for the ORM but for other parts of the application. The previous commits were a preparation for this big change. An entity now represents just a single record from a specific table of the database – nothing more. The repositories job is it to fetch or update records of the database and instantiate the entities. Another problem that was solved is the high amount of database queries that was needed before. For example, on the blogs home page first were all 10 latest post IDs fetched from the database and then another query was executed with "WHERE id = :id" for *each* single post?! ... This problem is solved with the new repository classes; they now use a single query to fetch and build the entities of the 10 latest posts. This change also solves the problem with database queries spread across the application and limits the exzessive use of try/catch blocks which were used before. The new classes make the whole code much cleaner. :) --- core/include/feed/main.php | 59 +++++++++------------- core/include/home.php | 31 ++++++------ core/include/page/list.php | 35 +++++++------ core/include/page/main.php | 113 ++++++++++++++++++++--------------------- core/include/post/list.php | 35 +++++++------ core/include/post/main.php | 113 ++++++++++++++++++++--------------------- core/include/search/main.php | 36 +++++++------- core/include/user/list.php | 30 ++++++----- core/include/user/main.php | 116 ++++++++++++++++++++----------------------- 9 files changed, 276 insertions(+), 292 deletions(-) (limited to 'core/include') diff --git a/core/include/feed/main.php b/core/include/feed/main.php index 4dc7c81..637c54b 100644 --- a/core/include/feed/main.php +++ b/core/include/feed/main.php @@ -1,9 +1,8 @@ query(sprintf($execSQL, Post\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); + $posts = $PostRepository->getPaginated( + Application::get('POST.FEED_SORT'), + Application::get('POST.FEED_SIZE') + ); - foreach($postIDs as $postID) { - try { - $Post = Post\Factory::build($postID); - $User = User\Factory::build($Post->get('user')); + foreach($posts as $Post) { + $User = $UserRepository->find($Post->get('user')); - $ItemTemplate = Template\Factory::build('feed/item_post'); - $ItemTemplate->set('POST', generateItemTemplateData($Post)); - $ItemTemplate->set('USER', generateItemTemplateData($User)); + $ItemTemplate = Template\Factory::build('feed/item_post'); + $ItemTemplate->set('POST', generateItemTemplateData($Post)); + $ItemTemplate->set('USER', generateItemTemplateData($User)); - $post_templates[] = $ItemTemplate; - } - - catch(Post\Exception $Exception){} - catch(User\Exception $Exception){} + $post_templates[] = $ItemTemplate; } } @@ -41,26 +35,21 @@ if(!isset($param) OR $param !== 'page') { # Page feed #=============================================================================== if(!isset($param) OR $param !== 'post') { - $PAGE['FEED_SORT'] = Application::get('PAGE.FEED_SORT'); - $PAGE['FEED_SIZE'] = Application::get('PAGE.FEED_SIZE'); - - $execSQL = "SELECT id FROM %s ORDER BY {$PAGE['FEED_SORT']} LIMIT {$PAGE['FEED_SIZE']}"; - $pageIDs = $Database->query(sprintf($execSQL, Page\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); + $PageRepository = Application::getRepository('Page'); - foreach($pageIDs as $pageID) { - try { - $Page = Page\Factory::build($pageID); - $User = User\Factory::build($Page->get('user')); + $pages = $PageRepository->getPaginated( + Application::get('PAGE.FEED_SORT'), + Application::get('PAGE.FEED_SIZE') + ); - $ItemTemplate = Template\Factory::build('feed/item_page'); - $ItemTemplate->set('PAGE', generateItemTemplateData($Page)); - $ItemTemplate->set('USER', generateItemTemplateData($User)); + foreach($pages as $Page) { + $User = $UserRepository->find($Page->get('user')); - $page_templates[] = $ItemTemplate; - } + $ItemTemplate = Template\Factory::build('feed/item_page'); + $ItemTemplate->set('PAGE', generateItemTemplateData($Page)); + $ItemTemplate->set('USER', generateItemTemplateData($User)); - catch(Page\Exception $Exception){} - catch(User\Exception $Exception){} + $page_templates[] = $ItemTemplate; } } diff --git a/core/include/home.php b/core/include/home.php index 76c9287..bc73bef 100644 --- a/core/include/home.php +++ b/core/include/home.php @@ -1,23 +1,21 @@ query(sprintf($execSQL, Post\Attribute::TABLE)); - -$postIDs = $Statement->fetchAll($Database::FETCH_COLUMN); +#=============================================================================== +# Get paginated post list +#=============================================================================== +$posts = $PostRepository->getPaginated( + Application::get('POST.LIST_SORT'), + Application::get('POST.LIST_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); } #=============================================================================== @@ -35,7 +33,8 @@ $MainTemplate = Template\Factory::build('main'); $MainTemplate->set('HTML', $HomeTemplate); $MainTemplate->set('HEAD', [ 'NAME' => Application::get('BLOGMETA.HOME'), - 'DESC' => Application::get('BLOGMETA.NAME').' – '.Application::get('BLOGMETA.DESC'), + 'DESC' => Application::get('BLOGMETA.NAME').' – ' + .Application::get('BLOGMETA.DESC'), 'PERM' => Application::getURL() ]); 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 @@ 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; 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 @@ 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; diff --git a/core/include/search/main.php b/core/include/search/main.php index 6bc6bdc..564f0dd 100644 --- a/core/include/search/main.php +++ b/core/include/search/main.php @@ -2,15 +2,22 @@ #=============================================================================== # Get instances #=============================================================================== -$Database = Application::getDatabase(); $Language = Application::getLanguage(); -$D_LIST = $Database->query(sprintf('SELECT DISTINCT DAY(time_insert) AS temp FROM %s ORDER BY temp', Post\Attribute::TABLE)); -$M_LIST = $Database->query(sprintf('SELECT DISTINCT MONTH(time_insert) AS temp FROM %s ORDER BY temp', Post\Attribute::TABLE)); -$Y_LIST = $Database->query(sprintf('SELECT DISTINCT YEAR(time_insert) AS temp FROM %s ORDER BY temp', Post\Attribute::TABLE)); +#=============================================================================== +# Get repositories +#=============================================================================== +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); if($search = HTTP::GET('q')) { - if(!$postIDs = Post\Item::getSearchResultIDs($search, [HTTP::GET('d'), HTTP::GET('m'), HTTP::GET('y')], $Database)) { + $filter = [ + 'day' => HTTP::GET('d'), + 'month' => HTTP::GET('m'), + 'year' => HTTP::GET('y') + ]; + + if(!$posts = $PostRepository->search($search, $filter)) { $message = $Language->text('search_no_results', escapeHTML($search)); } } @@ -22,9 +29,9 @@ $form_data = [ 'Y' => HTTP::GET('y'), ], 'OPTIONS' => [ - 'D' => $D_LIST->fetchAll($Database::FETCH_COLUMN), - 'M' => $M_LIST->fetchAll($Database::FETCH_COLUMN), - 'Y' => $Y_LIST->fetchAll($Database::FETCH_COLUMN), + 'D' => $PostRepository->getDistinctDays(), + 'M' => $PostRepository->getDistinctMonths(), + 'Y' => $PostRepository->getDistinctYears(), ] ]; @@ -36,15 +43,10 @@ $search_data = [ #=============================================================================== # Build document #=============================================================================== -if(isset($postIDs) AND !empty($postIDs)) { - 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){} +if(!empty($posts)) { + foreach($posts as $Post) { + $User = $UserRepository->find($Post->get('user')); + $templates[] = generatePostItemTemplate($Post, $User); } $ResultTemplate = Template\Factory::build('search/result'); diff --git a/core/include/user/list.php b/core/include/user/list.php index b64eee5..f6f794a 100644 --- a/core/include/user/list.php +++ b/core/include/user/list.php @@ -2,16 +2,20 @@ #=============================================================================== # Get instances #=============================================================================== -$Database = Application::getDatabase(); $Language = Application::getLanguage(); +#=============================================================================== +# Get repositories +#=============================================================================== +$UserRepository = Application::getRepository('User'); + #=============================================================================== # Pagination #=============================================================================== $site_size = Application::get('USER.LIST_SIZE'); $site_sort = Application::get('USER.LIST_SORT'); -$count = $Database->query(sprintf('SELECT COUNT(id) FROM %s', User\Attribute::TABLE))->fetchColumn(); +$count = $UserRepository->getCount(); $lastSite = ceil($count / $site_size); $currentSite = HTTP::GET('site') ?? 1; @@ -24,20 +28,22 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { #=============================================================================== # Single redirect #=============================================================================== -if(Application::get('USER.SINGLE_REDIRECT') === TRUE AND $count === '1') { - $Statement = $Database->query(sprintf('SELECT id FROM %s LIMIT 1', User\Attribute::TABLE)); - $User = User\Factory::build($Statement->fetchColumn()); +if(Application::get('USER.SINGLE_REDIRECT') === TRUE AND $count === 1) { + $User = $UserRepository->getLast(); HTTP::redirect(Application::getEntityURL($User)); } -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$userIDs = $Database->query(sprintf($execSQL, User\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +#=============================================================================== +# Get paginated user list +#=============================================================================== +$users = $UserRepository->getPaginated( + $site_sort, + $site_size, + ($currentSite-1) * $site_size +); -foreach($userIDs as $userID) { - try { - $User = User\Factory::build($userID); - $templates[] = generateUserItemTemplate($User); - } catch(User\Exception $Exception){} +foreach($users as $User) { + $templates[] = generateUserItemTemplate($User); } #=============================================================================== diff --git a/core/include/user/main.php b/core/include/user/main.php index 34ec883..c0cb20d 100644 --- a/core/include/user/main.php +++ b/core/include/user/main.php @@ -1,81 +1,73 @@ findBy('slug', $param)) { + if($User = $UserRepository->find($param)) { + HTTP::redirect(Application::getEntityURL($User)); + } } +} - else { - $User = User\Factory::build($param); +else { + if(!$User = $UserRepository->find($param)) { + if($User = $UserRepository->findBy('slug', $param)) { + HTTP::redirect(Application::getEntityURL($User)); + } } +} - $user_data = generateItemTemplateData($User); - - #=============================================================================== - # Add user data for previous and next user - #=============================================================================== - try { - $PrevUser = User\Factory::build($User->getPrevID()); - $user_data['PREV'] = generateItemTemplateData($PrevUser); - } catch(User\Exception $Exception){} - - try { - $NextUser = User\Factory::build($User->getNextID()); - $user_data['NEXT'] = generateItemTemplateData($NextUser); - } catch(User\Exception $Exception){} - - $PostCountStatement = $Database->query(sprintf('SELECT COUNT(*) FROM %s WHERE user = %d', Post\Attribute::TABLE, $User->getID())); - $PageCountStatement = $Database->query(sprintf('SELECT COUNT(*) FROM %s WHERE user = %d', Page\Attribute::TABLE, $User->getID())); - - #=============================================================================== - # Build document - #=============================================================================== - $UserTemplate = Template\Factory::build('user/main'); - $UserTemplate->set('USER', $user_data); - $UserTemplate->set('COUNT', [ - 'POST' => $PostCountStatement->fetchColumn(), - 'PAGE' => $PageCountStatement->fetchColumn() - ]); +#=============================================================================== +# Throw 404 error if user could not be found +#=============================================================================== +if(!isset($User)) { + Application::error404(); +} - $MainTemplate = Template\Factory::build('main'); - $MainTemplate->set('HTML', $UserTemplate); - $MainTemplate->set('HEAD', [ - 'NAME' => $user_data['ATTR']['FULLNAME'], - 'DESC' => description($user_data['BODY']['HTML'](), Application::get('USER.DESCRIPTION_SIZE')), - 'PERM' => $user_data['URL'], - 'OG_IMAGES' => $user_data['FILE']['LIST'] - ]); +#=============================================================================== +# Generate template data +#=============================================================================== +$user_data = generateItemTemplateData($User); - # Get access to the current item data from main template - $MainTemplate->set('TYPE', 'USER'); - $MainTemplate->set('USER', $user_data); +#=============================================================================== +# Add template data for previous and next user +#=============================================================================== +if($PrevUser = $UserRepository->findPrev($User)) { + $user_data['PREV'] = generateItemTemplateData($PrevUser); +} - echo $MainTemplate; +if($NextUser = $UserRepository->findNext($User)) { + $user_data['NEXT'] = generateItemTemplateData($NextUser); } #=============================================================================== -# CATCH: User\Exception +# Build document #=============================================================================== -catch(User\Exception $Exception) { - try { - if(Application::get('USER.SLUG_URLS') === FALSE) { - $User = User\Factory::buildBySlug($param); - } else { - $User = User\Factory::build($param); - } +$UserTemplate = Template\Factory::build('user/main'); +$UserTemplate->set('USER', $user_data); +$UserTemplate->set('COUNT', [ + 'POST' => $PostRepository->getCountByUser($User), + 'PAGE' => $PageRepository->getCountByUser($User) +]); - HTTP::redirect(Application::getEntityURL($User)); - } +$MainTemplate = Template\Factory::build('main'); +$MainTemplate->set('TYPE', 'USER'); +$MainTemplate->set('USER', $user_data); +$MainTemplate->set('HTML', $UserTemplate); +$MainTemplate->set('HEAD', [ + 'NAME' => $user_data['ATTR']['FULLNAME'], + 'DESC' => description($user_data['BODY']['HTML'](), + Application::get('USER.DESCRIPTION_SIZE')), + 'PERM' => $user_data['URL'], + 'OG_IMAGES' => $user_data['FILE']['LIST'] +]); - catch(User\Exception $Exception) { - Application::error404(); - } -} +echo $MainTemplate; -- cgit v1.2.3