From 3a688d001580fe6c469baca837f77c152a13b314 Mon Sep 17 00:00:00 2001 From: Carl Bennett Date: Sat, 14 Dec 2024 00:58:08 -0600 Subject: [PATCH] Use model constants for error codes --- src/Controllers/News/Create.php | 8 +++--- src/Controllers/News/Delete.php | 9 +++--- src/Controllers/News/Edit.php | 12 ++++---- src/Models/News/Create.php | 49 +++++++++++++++++---------------- src/Models/News/Delete.php | 5 ++++ src/Models/News/Edit.php | 7 +++++ src/Templates/News/Create.phtml | 10 ++++--- src/Templates/News/Delete.phtml | 9 +++--- src/Templates/News/Edit.phtml | 12 ++++---- 9 files changed, 71 insertions(+), 50 deletions(-) diff --git a/src/Controllers/News/Create.php b/src/Controllers/News/Create.php index 6335191e..4a98b6bd 100644 --- a/src/Controllers/News/Create.php +++ b/src/Controllers/News/Create.php @@ -28,7 +28,7 @@ public function invoke(?array $args): bool if (!$this->model->acl_allowed) { $this->model->_responseCode = HttpCode::HTTP_FORBIDDEN; - $this->model->error = CreateModel::ACL_NOT_SET; + $this->model->error = $this->model->active_user ? CreateModel::ERROR_ACL_NOT_SET : CreateModel::ERROR_NOT_LOGGED_IN; return true; } @@ -68,11 +68,11 @@ protected function handlePost(): void if (empty($this->model->title)) { - $this->model->error = CreateModel::EMPTY_TITLE; + $this->model->error = CreateModel::ERROR_EMPTY_TITLE; } else if (empty($this->model->content)) { - $this->model->error = CreateModel::EMPTY_CONTENT; + $this->model->error = CreateModel::ERROR_EMPTY_CONTENT; } else { @@ -86,7 +86,7 @@ protected function handlePost(): void $this->model->news_post->setTitle($this->model->title); $this->model->news_post->setUserId($this->model->active_user->getId()); - $this->model->error = $this->model->news_post->commit() ? false : CreateModel::INTERNAL_ERROR; + $this->model->error = $this->model->news_post->commit() ? false : CreateModel::ERROR_INTERNAL; } if ($this->model->error !== false) return; diff --git a/src/Controllers/News/Delete.php b/src/Controllers/News/Delete.php index eb6230f4..1f58e72c 100644 --- a/src/Controllers/News/Delete.php +++ b/src/Controllers/News/Delete.php @@ -5,12 +5,13 @@ use \BNETDocs\Libraries\Core\HttpCode; use \BNETDocs\Libraries\Core\Router; use \BNETDocs\Libraries\EventLog\Logger; +use \BNETDocs\Models\News\Delete as DeleteModel; class Delete extends \BNETDocs\Controllers\Base { public function __construct() { - $this->model = new \BNETDocs\Models\News\Delete(); + $this->model = new DeleteModel(); } public function invoke(?array $args): bool @@ -21,7 +22,7 @@ public function invoke(?array $args): bool if (!$this->model->acl_allowed) { $this->model->_responseCode = HttpCode::HTTP_UNAUTHORIZED; - $this->model->error = 'ACL_NOT_SET'; + $this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN; return true; } @@ -34,7 +35,7 @@ public function invoke(?array $args): bool if (!$this->model->news_post) { $this->model->_responseCode = HttpCode::HTTP_NOT_FOUND; - $this->model->error = 'NOT_FOUND'; + $this->model->error = DeleteModel::ERROR_NOT_FOUND; return true; } @@ -47,7 +48,7 @@ public function invoke(?array $args): bool protected function tryDelete(): void { - $this->model->error = $this->model->news_post->deallocate() ? false : 'INTERNAL_ERROR'; + $this->model->error = $this->model->news_post->deallocate() ? false : DeleteModel::ERROR_INTERNAL; if ($this->model->error !== false) return; $event = Logger::initEvent( diff --git a/src/Controllers/News/Edit.php b/src/Controllers/News/Edit.php index e6852e44..b1ce9f2b 100644 --- a/src/Controllers/News/Edit.php +++ b/src/Controllers/News/Edit.php @@ -6,12 +6,13 @@ use \BNETDocs\Libraries\Core\HttpCode; use \BNETDocs\Libraries\Core\Router; use \BNETDocs\Libraries\EventLog\Logger; +use \BNETDocs\Models\News\Edit as EditModel; class Edit extends \BNETDocs\Controllers\Base { public function __construct() { - $this->model = new \BNETDocs\Models\News\Edit(); + $this->model = new EditModel(); } public function invoke(?array $args): bool @@ -22,7 +23,7 @@ public function invoke(?array $args): bool if (!$this->model->acl_allowed) { $this->model->_responseCode = HttpCode::HTTP_FORBIDDEN; - $this->model->error = 'ACL_NOT_SET'; + $this->model->error = $this->model->active_user ? EditModel::ERROR_ACL_NOT_SET : EditModel::ERROR_NOT_LOGGED_IN; return true; } @@ -35,7 +36,7 @@ public function invoke(?array $args): bool if (!$this->model->news_post) { $this->model->_responseCode = HttpCode::HTTP_NOT_FOUND; - $this->model->error = 'NOT_FOUND'; + $this->model->error = EditModel::ERROR_NOT_FOUND; return true; } @@ -77,7 +78,8 @@ protected function handlePost(): void $this->model->content = $content; $this->model->rss_exempt = (bool) $rss_exempt; - $this->model->error = empty($title) ? 'EMPTY_TITLE' : (empty($content) ? 'EMPTY_CONTENT' : null); + $this->model->error = empty($title) ? EditModel::ERROR_EMPTY_TITLE + : (empty($content) ? EditModel::ERROR_EMPTY_CONTENT : null); if ($this->model->error) return; $this->model->news_post->setCategoryId($this->model->category); @@ -88,7 +90,7 @@ protected function handlePost(): void $this->model->news_post->setPublished($publish); $this->model->news_post->incrementEdited(); - $this->model->error = $this->model->news_post->commit() ? false : 'INTERNAL_ERROR'; + $this->model->error = $this->model->news_post->commit() ? false : EditModel::ERROR_INTERNAL; $event = Logger::initEvent( \BNETDocs\Libraries\EventLog\EventTypes::NEWS_EDITED, diff --git a/src/Models/News/Create.php b/src/Models/News/Create.php index 4aca4b16..aecc6a8a 100644 --- a/src/Models/News/Create.php +++ b/src/Models/News/Create.php @@ -4,30 +4,31 @@ class Create extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable { - public const ACL_NOT_SET = 'ACL_NOT_SET'; - public const EMPTY_CONTENT = 'EMPTY_CONTENT'; - public const EMPTY_TITLE = 'EMPTY_TITLE'; - public const INTERNAL_ERROR = 'INTERNAL_ERROR'; + public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET'; + public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT'; + public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE'; + public const ERROR_INTERNAL = 'INTERNAL_ERROR'; + public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN'; - public ?int $category_id = null; - public string $content = ''; - public mixed $error = 'INTERNAL_ERROR'; - public bool $markdown = false; - public ?\BNETDocs\Libraries\News\Post $news_post = null; - public ?array $news_categories = null; - public bool $rss_exempt = false; - public string $title = ''; + public ?int $category_id = null; + public string $content = ''; + public mixed $error = 'INTERNAL_ERROR'; + public bool $markdown = false; + public ?\BNETDocs\Libraries\News\Post $news_post = null; + public ?array $news_categories = null; + public bool $rss_exempt = false; + public string $title = ''; - public function jsonSerialize(): mixed - { - return \array_merge(parent::jsonSerialize(), [ - 'category' => $this->category_id, - 'content' => $this->content, - 'error' => $this->error, - 'markdown' => $this->markdown, - 'news_categories' => $this->news_categories, - 'rss_exempt' => $this->rss_exempt, - 'title' => $this->title, - ]); - } + public function jsonSerialize(): mixed + { + return \array_merge(parent::jsonSerialize(), [ + 'category' => $this->category_id, + 'content' => $this->content, + 'error' => $this->error, + 'markdown' => $this->markdown, + 'news_categories' => $this->news_categories, + 'rss_exempt' => $this->rss_exempt, + 'title' => $this->title, + ]); + } } diff --git a/src/Models/News/Delete.php b/src/Models/News/Delete.php index 4f27fce5..69ced645 100644 --- a/src/Models/News/Delete.php +++ b/src/Models/News/Delete.php @@ -4,6 +4,11 @@ class Delete extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable { + public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET'; + public const ERROR_INTERNAL = 'INTERNAL_ERROR'; + public const ERROR_NOT_FOUND = 'NOT_FOUND'; + public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN'; + public ?int $id = null; public ?\BNETDocs\Libraries\News\Post $news_post = null; public string $title = ''; diff --git a/src/Models/News/Edit.php b/src/Models/News/Edit.php index d9641c30..59b7eb9a 100644 --- a/src/Models/News/Edit.php +++ b/src/Models/News/Edit.php @@ -4,6 +4,13 @@ class Edit extends \BNETDocs\Models\Core\AccessControl implements \JsonSerializable { + public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET'; + public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT'; + public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE'; + public const ERROR_INTERNAL = 'INTERNAL_ERROR'; + public const ERROR_NOT_FOUND = 'NOT_FOUND'; + public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN'; + public ?int $category = null; public ?array $comments = null; public ?string $content = null; diff --git a/src/Templates/News/Create.phtml b/src/Templates/News/Create.phtml index 2b8544d5..c7527c7a 100644 --- a/src/Templates/News/Create.phtml +++ b/src/Templates/News/Create.phtml @@ -1,15 +1,17 @@ getContext()->error; switch ($error) { - case 'ACL_NOT_SET': $message = 'You do not have the privilege to create news posts.'; break; - case 'EMPTY_TITLE': $message = 'The title of the news post is required.'; break; - case 'EMPTY_CONTENT': $message = 'The content of the news post is required.'; break; - case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break; + case CreateModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to create news posts.'; break; + case CreateModel::ERROR_EMPTY_TITLE: $message = 'The title of the news post is required.'; break; + case CreateModel::ERROR_EMPTY_CONTENT: $message = 'The content of the news post is required.'; break; + case CreateModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to create news posts.'; break; + case CreateModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break; default: $message = $error; } $form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS); diff --git a/src/Templates/News/Delete.phtml b/src/Templates/News/Delete.phtml index a514ff30..cb733629 100644 --- a/src/Templates/News/Delete.phtml +++ b/src/Templates/News/Delete.phtml @@ -1,5 +1,6 @@ getContext()->id; $news_post_title = $this->getContext()->title; switch ($error) { - case 'ACL_NOT_SET': $message = 'You do not have the privilege to delete news posts.'; break; - case 'NOT_FOUND': $message = 'Cannot find news post by that id.'; break; - case 'NOT_LOGGED_IN': $message = 'You must be logged in to delete news posts.'; break; - case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break; + case DeleteModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to delete news posts.'; break; + case DeleteModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break; + case DeleteModel::ERROR_NOT_FOUND: $message = 'Cannot find news post by that id.'; break; + case DeleteModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to delete news posts.'; break; default: $message = $error; } require('./Includes/header.inc.phtml'); ?> diff --git a/src/Templates/News/Edit.phtml b/src/Templates/News/Edit.phtml index e1a106f3..e43e92fa 100644 --- a/src/Templates/News/Edit.phtml +++ b/src/Templates/News/Edit.phtml @@ -1,6 +1,7 @@ getContext()->comments; $error = $this->getContext()->error; switch ($error) { - case 'ACL_NOT_SET': $message = 'You do not have the privilege to edit news posts.'; break; - case 'NOT_FOUND': $message = 'Cannot find news post by that id.'; break; - case 'EMPTY_TITLE': $message = 'The title of the news post is required.'; break; - case 'EMPTY_CONTENT': $message = 'The content of the news post is required.'; break; - case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break; + case EditModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to edit news posts.'; break; + case EditModel::ERROR_EMPTY_CONTENT: $message = 'The content of the news post is required.'; break; + case EditModel::ERROR_EMPTY_TITLE: $message = 'The title of the news post is required.'; break; + case EditModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Try again later.'; break; + case EditModel::ERROR_NOT_FOUND: $message = 'Cannot find news post by that id.'; break; + case EditModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to edit news posts.'; break; default: $message = $error; } $form_category = $this->getContext()->category;