diff options
author | Thomas Lange <code@nerdmind.de> | 2017-05-30 21:03:03 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2017-05-30 21:03:03 +0200 |
commit | fd6af01e313f441189c4cd69668766d2ded7ced3 (patch) | |
tree | 0a0ae7cb2dd19c2a52b0799c37bb53b0e5e9e892 /core/functions.php | |
parent | 1f1de386b09e48a02fccd62aa82ff319533969cc (diff) | |
download | blog-f931e63fe4bb39d478eb733721002e57d33c08a3.tar.gz blog-f931e63fe4bb39d478eb733721002e57d33c08a3.tar.xz blog-f931e63fe4bb39d478eb733721002e57d33c08a3.zip |
Several changes have been made in this commit, which together with the previous commits result in version 2.2 [changed template parameters]:v2.2
+ Implemented [core]: A new method called "getAll" has been added to the "Attribute" class which now returns all attributes as key->value array. The first parameter can be an array with attribute names which shall be excluded from returning.
+ Optimization [core]: The function "generateItemData" has been renamed to "generateItemTemplateData" and has been restructured to use the new implemented method "getAll" from the "Attribute" class.
+ Optimization [core]: The function "generateNaviTemplate" has been added and the functions "generatePageNaviTemplate", "generatePostNaviTemplate" and "generateUserNaviTemplate" have been restructured to make use of the new "generateNaviTemplate" function to reduce duplicate code.
+ Optimization [core]: Several files within the admin directory has been optimized to use the new "getAll" method of the "Attribute" class.
+ Optimization [core]: Several code optimizations have been made to the "HTTP" class.
+ Optimization [admin template]: All occurences of $PAGE['ID'], $POST['ID'] and $USER['ID'] have been replaced with $PAGE['ATTR']['ID'], $POST['ATTR']['ID'] and $USER['ATTR']['ID'] to make it more consistent. You now have to use ['ATTR']['ID'] instead of ['ID'] in your templates to get the ID of an item!
Template upgrade to version 2.2 (only for customized templates):
SEARCH: ['ID']
REPLACE: ['ATTR']['ID']
Diffstat (limited to 'core/functions.php')
-rw-r--r-- | core/functions.php | 132 |
1 files changed, 31 insertions, 101 deletions
diff --git a/core/functions.php b/core/functions.php index ccaf008..35faed4 100644 --- a/core/functions.php +++ b/core/functions.php @@ -2,16 +2,18 @@ #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePageNaviTemplate($currentSite): Template\Template { +function generateNaviTemplate(int $current, $location, $namespace): Template\Template { $Database = Application::getDatabase(); - $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', Page\Attribute::TABLE)); + $Attribute = "{$namespace}\\Attribute"; - $lastSite = ceil($Statement->fetchColumn() / Application::get('PAGE.LIST_SIZE')); + $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', $Attribute::TABLE)); + + $lastSite = ceil($Statement->fetchColumn() / Application::get(strtoupper($namespace).'.LIST_SIZE')); $PaginationTemplate = Template\Factory::build('pagination'); - $PaginationTemplate->set('THIS', $currentSite); + $PaginationTemplate->set('THIS', $current); $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', Application::getPageURL('?site=%d')); + $PaginationTemplate->set('HREF', "{$location}?site=%d"); return $PaginationTemplate; } @@ -19,35 +21,22 @@ function generatePageNaviTemplate($currentSite): Template\Template { #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePostNaviTemplate($currentSite): Template\Template { - $Database = Application::getDatabase(); - $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', Post\Attribute::TABLE)); - - $lastSite = ceil($Statement->fetchColumn() / Application::get('POST.LIST_SIZE')); - - $PaginationTemplate = Template\Factory::build('pagination'); - $PaginationTemplate->set('THIS', $currentSite); - $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', Application::getPostURL('?site=%d')); - - return $PaginationTemplate; +function generatePageNaviTemplate($current): Template\Template { + return generateNaviTemplate($current, Application::getPageURL(), 'Page'); } #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generateUserNaviTemplate($currentSite): Template\Template { - $Database = Application::getDatabase(); - $Statement = $Database->query(sprintf('SELECT COUNT(id) FROM %s', User\Attribute::TABLE)); - - $lastSite = ceil($Statement->fetchColumn() / Application::get('USER.LIST_SIZE')); - - $PaginationTemplate = Template\Factory::build('pagination'); - $PaginationTemplate->set('THIS', $currentSite); - $PaginationTemplate->set('LAST', $lastSite); - $PaginationTemplate->set('HREF', Application::getUserURL('?site=%d')); +function generatePostNaviTemplate($current): Template\Template { + return generateNaviTemplate($current, Application::getPostURL(), 'Post'); +} - return $PaginationTemplate; +#=============================================================================== +# Helper function to reduce duplicate code +#=============================================================================== +function generateUserNaviTemplate($current): Template\Template { + return generateNaviTemplate($current, Application::getUserURL(), 'User'); } #=============================================================================== @@ -55,8 +44,8 @@ function generateUserNaviTemplate($currentSite): Template\Template { #=============================================================================== function generatePageItemTemplate(Page\Item $Page, User\Item $User): Template\Template { $Template = Template\Factory::build('page/item'); - $Template->set('PAGE', generatePageItemData($Page)); - $Template->set('USER', generateUserItemData($User)); + $Template->set('PAGE', generateItemTemplateData($Page)); + $Template->set('USER', generateItemTemplateData($User)); return $Template; } @@ -66,8 +55,8 @@ function generatePageItemTemplate(Page\Item $Page, User\Item $User): Template\Te #=============================================================================== function generatePostItemTemplate(Post\Item $Post, User\Item $User): Template\Template { $Template = Template\Factory::build('post/item'); - $Template->set('POST', generatePostItemData($Post)); - $Template->set('USER', generateUserItemData($User)); + $Template->set('POST', generateItemTemplateData($Post)); + $Template->set('USER', generateItemTemplateData($User)); return $Template; } @@ -77,7 +66,7 @@ function generatePostItemTemplate(Post\Item $Post, User\Item $User): Template\Te #=============================================================================== function generateUserItemTemplate(User\Item $User): Template\Template { $Template = Template\Factory::build('user/item'); - $Template->set('USER', generateUserItemData($User)); + $Template->set('USER', generateItemTemplateData($User)); return $Template; } @@ -85,13 +74,17 @@ function generateUserItemTemplate(User\Item $User): Template\Template { #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generateItemData(Item $Item): array { +function generateItemTemplateData(Item $Item): array { + $ATTR = $Item->getAttribute()->getAll(['password']); + $ATTR = array_change_key_case($ATTR, CASE_UPPER); + return [ - 'ID' => $Item->getID(), 'URL' => $Item->getURL(), 'GUID' => $Item->getGUID(), 'ARGV' => $Item->getArguments(), + 'ATTR' => $ATTR, + 'PREV' => FALSE, 'NEXT' => FALSE, @@ -106,69 +99,6 @@ function generateItemData(Item $Item): array { 'HTML' => function() use($Item) { return $Item->getHTML(); } - ], - - 'ATTR' => [ - 'USER' => $Item->attr('user'), - 'SLUG' => $Item->attr('slug'), - 'NAME' => $Item->attr('name'), - 'BODY' => $Item->attr('body'), - 'ARGV' => $Item->attr('argv'), - 'TIME_INSERT' => $Item->attr('time_insert'), - 'TIME_UPDATE' => $Item->attr('time_update') - ] - ]; -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generatePageItemData(Page\Item $Page): array { - return generateItemData($Page); -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generatePostItemData(Post\Item $Post): array { - return generateItemData($Post); -} - -#=============================================================================== -# Helper function to reduce duplicate code -#=============================================================================== -function generateUserItemData(User\Item $User): array { - return [ - 'ID' => $User->getID(), - 'URL' => $User->getURL(), - 'GUID' => $User->getGUID(), - 'ARGV' => $User->getArguments(), - - 'PREV' => FALSE, - 'NEXT' => FALSE, - - 'FILE' => [ - 'LIST' => $User->getFiles() - ], - - 'BODY' => [ - 'TEXT' => function() use($User) { - return $User->getBody(); - }, - 'HTML' => function() use($User) { - return $User->getHTML(); - } - ], - - 'ATTR' => [ - 'SLUG' => $User->attr('slug'), - 'BODY' => $User->attr('body'), - 'ARGV' => $User->attr('argv'), - 'USERNAME' => $User->attr('username'), - 'FULLNAME' => $User->attr('fullname'), - 'MAILADDR' => $User->attr('mailaddr'), - 'TIME_INSERT' => $User->attr('time_insert'), - 'TIME_UPDATE' => $User->attr('time_update') ] ]; } @@ -354,7 +284,7 @@ function makeSlugURL($string, $separator = '-') { function PAGE(int $id): array { try { $Page = Page\Factory::build($id); - return generatePageItemData($Page); + return generateItemTemplateData($Page); } catch(Page\Exception $Exception) { return []; } @@ -366,7 +296,7 @@ function PAGE(int $id): array { function POST(int $id): array { try { $Post = Post\Factory::build($id); - return generatePostItemData($Post); + return generateItemTemplateData($Post); } catch(Post\Exception $Exception) { return []; } @@ -378,7 +308,7 @@ function POST(int $id): array { function USER(int $id): array { try { $User = User\Factory::build($id); - return generateUserItemData($User); + return generateItemTemplateData($User); } catch(User\Exception $Exception) { return []; } |