diff options
author | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:18:02 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:46:42 +0200 |
commit | 8cd1105b111b89106f24c5b50795afb5ff28a935 (patch) | |
tree | c789faac9de4d83cb409c45779e76be8938a4d66 /core/include/user | |
parent | 7937df540b7d70b2bb87797442d0a0a0df197133 (diff) | |
download | blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.gz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.xz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.zip |
Implement new Repository and Entity classes
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. :)
Diffstat (limited to 'core/include/user')
-rw-r--r-- | core/include/user/list.php | 30 | ||||
-rw-r--r-- | core/include/user/main.php | 116 |
2 files changed, 72 insertions, 74 deletions
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 @@ <?php #=============================================================================== -# Get instances +# Get repositories #=============================================================================== -$Database = Application::getDatabase(); -$Language = Application::getLanguage(); +$PageRepository = Application::getRepository('Page'); +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); #=============================================================================== -# TRY: User\Exception +# Try to find user by slug URL or unique ID #=============================================================================== -try { - if(Application::get('USER.SLUG_URLS')) { - $User = User\Factory::buildBySlug($param); +if(Application::get('USER.SLUG_URLS')) { + if(!$User = $UserRepository->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; |