diff options
Diffstat (limited to 'admin/post')
-rw-r--r-- | admin/post/delete.php | 70 | ||||
-rw-r--r-- | admin/post/index.php | 29 | ||||
-rw-r--r-- | admin/post/insert.php | 46 | ||||
-rw-r--r-- | admin/post/search.php | 17 | ||||
-rw-r--r-- | admin/post/update.php | 107 |
5 files changed, 141 insertions, 128 deletions
diff --git a/admin/post/delete.php b/admin/post/delete.php index 94b60fc..1f398e1 100644 --- a/admin/post/delete.php +++ b/admin/post/delete.php @@ -11,46 +11,46 @@ define('AUTHENTICATION', TRUE); require '../../core/application.php'; #=============================================================================== -# TRY: Post\Exception +# Get repositories #=============================================================================== -try { - $Post = Post\Factory::build(HTTP::GET('id')); - $Attribute = $Post->getAttribute(); +$PostRepository = Application::getRepository('Post'); - if(HTTP::issetPOST(['token' => Application::getSecurityToken()], 'delete')) { - try { - if($Attribute->delete($Database)) { - HTTP::redirect(Application::getAdminURL('post/')); - } - } catch(PDOException $Exception) { - $messages[] = $Exception->getMessage(); +#=============================================================================== +# Throw 404 error if post could not be found +#=============================================================================== +if(!$Post = $PostRepository->find(HTTP::GET('id'))) { + Application::error404(); +} + +#=============================================================================== +# Check for delete request +#=============================================================================== +if(HTTP::issetPOST(['token' => Application::getSecurityToken()], 'delete')) { + try { + if($PostRepository->delete($Post)) { + HTTP::redirect(Application::getAdminURL('post/')); } + } catch(PDOException $Exception) { + $messages[] = $Exception->getMessage(); } - - #=============================================================================== - # Build document - #=============================================================================== - $FormTemplate = Template\Factory::build('post/form'); - $FormTemplate->set('HTML', parseEntityContent($Post)); - $FormTemplate->set('FORM', [ - 'TYPE' => 'DELETE', - 'INFO' => $messages ?? [], - 'DATA' => array_change_key_case($Attribute->getAll(), CASE_UPPER), - 'TOKEN' => Application::getSecurityToken() - ]); - - $DeleteTemplate = Template\Factory::build('post/delete'); - $DeleteTemplate->set('HTML', $FormTemplate); - - $MainTemplate = Template\Factory::build('main'); - $MainTemplate->set('NAME', $Language->text('title_post_delete')); - $MainTemplate->set('HTML', $DeleteTemplate); - echo $MainTemplate; } #=============================================================================== -# CATCH: Post\Exception +# Build document #=============================================================================== -catch(Post\Exception $Exception) { - Application::error404(); -} +$FormTemplate = Template\Factory::build('post/form'); +$FormTemplate->set('HTML', parseEntityContent($Post)); +$FormTemplate->set('FORM', [ + 'TYPE' => 'DELETE', + 'INFO' => $messages ?? [], + 'DATA' => array_change_key_case($Post->getAll(), CASE_UPPER), + 'TOKEN' => Application::getSecurityToken() +]); + +$DeleteTemplate = Template\Factory::build('post/delete'); +$DeleteTemplate->set('HTML', $FormTemplate); + +$MainTemplate = Template\Factory::build('main'); +$MainTemplate->set('NAME', $Language->text('title_post_delete')); +$MainTemplate->set('HTML', $DeleteTemplate); +echo $MainTemplate; diff --git a/admin/post/index.php b/admin/post/index.php index 8eedfbd..f6fcfb6 100644 --- a/admin/post/index.php +++ b/admin/post/index.php @@ -11,12 +11,19 @@ define('AUTHENTICATION', TRUE); require '../../core/application.php'; #=============================================================================== +# Get repositories +#=============================================================================== +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); + +#=============================================================================== # Pagination #=============================================================================== $site_size = Application::get('ADMIN.POST.LIST_SIZE'); $site_sort = Application::get('ADMIN.POST.LIST_SORT'); -$lastSite = ceil($Database->query(sprintf('SELECT COUNT(id) FROM %s', Post\Attribute::TABLE))->fetchColumn() / $site_size); +$count = $PostRepository->getCount(); +$lastSite = ceil($count / $site_size); $currentSite = HTTP::GET('site') ?? 1; $currentSite = intval($currentSite); @@ -26,19 +33,17 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { } #=============================================================================== -# Fetch post IDs from database +# Get paginated post list #=============================================================================== -$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); +$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/admin/post/insert.php b/admin/post/insert.php index b4e2542..c640a39 100644 --- a/admin/post/insert.php +++ b/admin/post/insert.php @@ -10,20 +10,32 @@ define('AUTHENTICATION', TRUE); #=============================================================================== require '../../core/application.php'; -$Attribute = new Post\Attribute(); +#=============================================================================== +# Get repositories +#=============================================================================== +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); + +#=============================================================================== +# Instantiate new Post entity +#=============================================================================== +$Post = new Post\Entity; +#=============================================================================== +# Check for insert request +#=============================================================================== if(HTTP::issetPOST('user', 'slug', 'name', 'body', 'argv', 'time_insert', 'time_update', 'insert')) { - $Attribute->set('user', HTTP::POST('user')); - $Attribute->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('name'))); - $Attribute->set('name', HTTP::POST('name') ? HTTP::POST('name') : NULL); - $Attribute->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL); - $Attribute->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL); - $Attribute->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s')); - $Attribute->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s')); + $Post->set('user', HTTP::POST('user')); + $Post->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('name'))); + $Post->set('name', HTTP::POST('name') ? HTTP::POST('name') : NULL); + $Post->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL); + $Post->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL); + $Post->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s')); + $Post->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s')); if(HTTP::issetPOST(['token' => Application::getSecurityToken()])) { try { - if($Attribute->insert($Database)) { + if($PostRepository->insert($Post)) { HTTP::redirect(Application::getAdminURL('post/')); } } catch(PDOException $Exception) { @@ -36,12 +48,12 @@ if(HTTP::issetPOST('user', 'slug', 'name', 'body', 'argv', 'time_insert', 'time_ } } -$userIDs = $Database->query(sprintf('SELECT id FROM %s ORDER BY fullname ASC', User\Attribute::TABLE)); - -foreach($userIDs->fetchAll($Database::FETCH_COLUMN) as $userID) { - $User = User\Factory::build($userID); - $userAttributes[] = [ - 'ID' => $User->get('id'), +#=============================================================================== +# Generate user list +#=============================================================================== +foreach($UserRepository->getAll([], 'fullname ASC') as $User) { + $userList[] = [ + 'ID' => $User->getID(), 'FULLNAME' => $User->get('fullname'), 'USERNAME' => $User->get('username'), ]; @@ -54,8 +66,8 @@ $FormTemplate = Template\Factory::build('post/form'); $FormTemplate->set('FORM', [ 'TYPE' => 'INSERT', 'INFO' => $messages ?? [], - 'DATA' => array_change_key_case($Attribute->getAll(), CASE_UPPER), - 'USER_LIST' => $userAttributes ?? [], + 'DATA' => array_change_key_case($Post->getAll(), CASE_UPPER), + 'USER_LIST' => $userList ?? [], 'TOKEN' => Application::getSecurityToken() ]); diff --git a/admin/post/search.php b/admin/post/search.php index 616359e..cfa2159 100644 --- a/admin/post/search.php +++ b/admin/post/search.php @@ -11,20 +11,15 @@ define('AUTHENTICATION', TRUE); require '../../core/application.php'; #=============================================================================== -# IF: Handle search request +# Check for search request #=============================================================================== if($search = HTTP::GET('q')) { - if($postIDs = Post\Item::getSearchResultIDs($search, [NULL, NULL, NULL], $Database)) { - foreach($postIDs as $postID) { - try { - $Post = Post\Factory::build($postID); - $User = User\Factory::build($Post->get('user')); + $PostRepository = Application::getRepository('Post'); + $UserRepository = Application::getRepository('User'); - $templates[] = generatePostItemTemplate($Post, $User); - } - catch(Post\Exception $Exception){} - catch(User\Exception $Exception){} - } + foreach($PostRepository->search($search) as $Post) { + $User = $UserRepository->find($Post->get('user')); + $templates[] = generatePostItemTemplate($Post, $User); } } diff --git a/admin/post/update.php b/admin/post/update.php index e22a54a..df0956b 100644 --- a/admin/post/update.php +++ b/admin/post/update.php @@ -11,69 +11,70 @@ define('AUTHENTICATION', TRUE); require '../../core/application.php'; #=============================================================================== -# TRY: Post\Exception +# Get repositories #=============================================================================== -try { - $Post = Post\Factory::build(HTTP::GET('id')); - $Attribute = $Post->getAttribute(); +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); - if(HTTP::issetPOST('user', 'slug', 'name', 'body', 'argv', 'time_insert', 'time_update', 'update')) { - $Attribute->set('user', HTTP::POST('user')); - $Attribute->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('name'))); - $Attribute->set('name', HTTP::POST('name') ? HTTP::POST('name') : NULL); - $Attribute->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL); - $Attribute->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL); - $Attribute->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s')); - $Attribute->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s')); +#=============================================================================== +# Throw 404 error if post could not be found +#=============================================================================== +if(!$Post = $PostRepository->find(HTTP::GET('id'))) { + Application::error404(); +} - if(HTTP::issetPOST(['token' => Application::getSecurityToken()])) { - try { - $Attribute->update($Database); - } catch(PDOException $Exception) { - $messages[] = $Exception->getMessage(); - } - } +#=============================================================================== +# Check for update request +#=============================================================================== +if(HTTP::issetPOST('user', 'slug', 'name', 'body', 'argv', 'time_insert', 'time_update', 'update')) { + $Post->set('user', HTTP::POST('user')); + $Post->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('name'))); + $Post->set('name', HTTP::POST('name') ? HTTP::POST('name') : NULL); + $Post->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL); + $Post->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL); + $Post->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s')); + $Post->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s')); - else { - $messages[] = $Language->text('error_security_csrf'); + if(HTTP::issetPOST(['token' => Application::getSecurityToken()])) { + try { + $PostRepository->update($Post); + } catch(PDOException $Exception) { + $messages[] = $Exception->getMessage(); } } - $userIDs = $Database->query(sprintf('SELECT id FROM %s ORDER BY fullname ASC', User\Attribute::TABLE)); - - foreach($userIDs->fetchAll($Database::FETCH_COLUMN) as $userID) { - $User = User\Factory::build($userID); - $userAttributes[] = [ - 'ID' => $User->get('id'), - 'FULLNAME' => $User->get('fullname'), - 'USERNAME' => $User->get('username'), - ]; + else { + $messages[] = $Language->text('error_security_csrf'); } - - #=============================================================================== - # Build document - #=============================================================================== - $FormTemplate = Template\Factory::build('post/form'); - $FormTemplate->set('FORM', [ - 'TYPE' => 'UPDATE', - 'INFO' => $messages ?? [], - 'DATA' => array_change_key_case($Attribute->getAll(), CASE_UPPER), - 'USER_LIST' => $userAttributes ?? [], - 'TOKEN' => Application::getSecurityToken() - ]); - - $PostUpdateTemplate = Template\Factory::build('post/update'); - $PostUpdateTemplate->set('HTML', $FormTemplate); - - $MainTemplate = Template\Factory::build('main'); - $MainTemplate->set('NAME', $Language->text('title_post_update')); - $MainTemplate->set('HTML', $PostUpdateTemplate); - echo $MainTemplate; } #=============================================================================== -# CATCH: Post\Exception +# Generate user list #=============================================================================== -catch(Post\Exception $Exception) { - Application::error404(); +foreach($UserRepository->getAll([], 'fullname ASC') as $User) { + $userList[] = [ + 'ID' => $User->getID(), + 'FULLNAME' => $User->get('fullname'), + 'USERNAME' => $User->get('username'), + ]; } + +#=============================================================================== +# Build document +#=============================================================================== +$FormTemplate = Template\Factory::build('post/form'); +$FormTemplate->set('FORM', [ + 'TYPE' => 'UPDATE', + 'INFO' => $messages ?? [], + 'DATA' => array_change_key_case($Post->getAll(), CASE_UPPER), + 'USER_LIST' => $userList ?? [], + 'TOKEN' => Application::getSecurityToken() +]); + +$PostUpdateTemplate = Template\Factory::build('post/update'); +$PostUpdateTemplate->set('HTML', $FormTemplate); + +$MainTemplate = Template\Factory::build('main'); +$MainTemplate->set('NAME', $Language->text('title_post_update')); +$MainTemplate->set('HTML', $PostUpdateTemplate); +echo $MainTemplate; |