aboutsummaryrefslogtreecommitdiffstats
path: root/admin/post/update.php
diff options
context:
space:
mode:
Diffstat (limited to 'admin/post/update.php')
-rw-r--r--admin/post/update.php107
1 files changed, 54 insertions, 53 deletions
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;