diff options
Diffstat (limited to 'admin/page/update.php')
-rw-r--r-- | admin/page/update.php | 107 |
1 files changed, 54 insertions, 53 deletions
diff --git a/admin/page/update.php b/admin/page/update.php index 5e54b03..2db0bc1 100644 --- a/admin/page/update.php +++ b/admin/page/update.php @@ -11,69 +11,70 @@ define('AUTHENTICATION', TRUE); require '../../core/application.php'; #=============================================================================== -# TRY: Page\Exception +# Get repositories #=============================================================================== -try { - $Page = Page\Factory::build(HTTP::GET('id')); - $Attribute = $Page->getAttribute(); +$PageRepository = Application::getRepository('Page'); +$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 page could not be found +#=============================================================================== +if(!$Page = $PageRepository->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')) { + $Page->set('user', HTTP::POST('user')); + $Page->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('name'))); + $Page->set('name', HTTP::POST('name') ? HTTP::POST('name') : NULL); + $Page->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL); + $Page->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL); + $Page->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s')); + $Page->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 { + $PageRepository->update($Page); + } 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('page/form'); - $FormTemplate->set('FORM', [ - 'TYPE' => 'UPDATE', - 'INFO' => $messages ?? [], - 'DATA' => array_change_key_case($Attribute->getAll(), CASE_UPPER), - 'USER_LIST' => $userAttributes ?? [], - 'TOKEN' => Application::getSecurityToken() - ]); - - $PageUpdateTemplate = Template\Factory::build('page/update'); - $PageUpdateTemplate->set('HTML', $FormTemplate); - - $MainTemplate = Template\Factory::build('main'); - $MainTemplate->set('NAME', $Language->text('title_page_update')); - $MainTemplate->set('HTML', $PageUpdateTemplate); - echo $MainTemplate; } #=============================================================================== -# CATCH: Page\Exception +# Generate user list #=============================================================================== -catch(Page\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('page/form'); +$FormTemplate->set('FORM', [ + 'TYPE' => 'UPDATE', + 'INFO' => $messages ?? [], + 'DATA' => array_change_key_case($Page->getAll(), CASE_UPPER), + 'USER_LIST' => $userList ?? [], + 'TOKEN' => Application::getSecurityToken() +]); + +$PageUpdateTemplate = Template\Factory::build('page/update'); +$PageUpdateTemplate->set('HTML', $FormTemplate); + +$MainTemplate = Template\Factory::build('main'); +$MainTemplate->set('NAME', $Language->text('title_page_update')); +$MainTemplate->set('HTML', $PageUpdateTemplate); +echo $MainTemplate; |