From 9bd03d1d6acaa1a193b7886770ff249110b5d8c0 Mon Sep 17 00:00:00 2001 From: Carl Bennett Date: Sun, 1 Dec 2024 18:18:43 -0600 Subject: [PATCH] Refactor Document/{Create,Delete,Edit,Index,View} --- src/Controllers/Document/Create.php | 24 ++++++++------- src/Controllers/Document/Delete.php | 4 +-- src/Controllers/Document/Edit.php | 13 +++++---- src/Controllers/Document/View.php | 2 +- src/Models/Document/Create.php | 29 +++++++++++++++---- src/Models/Document/Delete.php | 30 ++++++++++++------- src/Models/Document/Edit.php | 45 ++++++++++++++++++++++------- src/Models/Document/Index.php | 17 ++++++++--- src/Models/Document/View.php | 20 +++++++++---- src/Templates/Document/Create.phtml | 11 +++---- src/Templates/Document/Delete.phtml | 12 ++++---- src/Templates/Document/Edit.phtml | 12 ++++---- 12 files changed, 146 insertions(+), 73 deletions(-) diff --git a/src/Controllers/Document/Create.php b/src/Controllers/Document/Create.php index f6e6879b..65cee404 100644 --- a/src/Controllers/Document/Create.php +++ b/src/Controllers/Document/Create.php @@ -5,16 +5,13 @@ use \BNETDocs\Libraries\Core\HttpCode; use \BNETDocs\Libraries\Core\Router; use \BNETDocs\Libraries\EventLog\Logger; +use \BNETDocs\Models\Document\Create as CreateModel; class Create extends \BNETDocs\Controllers\Base { - public const EMPTY_CONTENT = 'EMPTY_CONTENT'; - public const EMPTY_TITLE = 'EMPTY_TITLE'; - public const INTERNAL_ERROR = 'INTERNAL_ERROR'; - public function __construct() { - $this->model = new \BNETDocs\Models\Document\Create(); + $this->model = new CreateModel(); } public function invoke(?array $args): bool @@ -25,7 +22,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 ? CreateModel::ERROR_ACL_NOT_SET : CreateModel::ERROR_NOT_LOGGED_IN; return true; } @@ -55,11 +52,16 @@ protected function handlePost(): void $this->model->markdown = $markdown; $this->model->content = $content; - if (empty($title)) { - $this->model->error = self::EMPTY_TITLE; - } else if (empty($content)) { - $this->model->error = self::EMPTY_CONTENT; + if (empty($title)) + { + $this->model->error = CreateModel::ERROR_EMPTY_TITLE; } + else if (empty($content)) + { + $this->model->error = CreateModel::ERROR_EMPTY_CONTENT; + } + + if ($this->model->error) return; $document = new \BNETDocs\Libraries\Document(null); $document->setBrief($brief); @@ -71,7 +73,7 @@ protected function handlePost(): void if (!$document->commit()) { - $this->model->error = self::INTERNAL_ERROR; + $this->model->error = CreateModel::ERROR_INTERNAL; return; } $this->model->error = false; diff --git a/src/Controllers/Document/Delete.php b/src/Controllers/Document/Delete.php index d93a9c7a..365802f2 100644 --- a/src/Controllers/Document/Delete.php +++ b/src/Controllers/Document/Delete.php @@ -22,7 +22,7 @@ public function invoke(?array $args): bool if (!$this->model->acl_allowed) { $this->model->_responseCode = HttpCode::HTTP_FORBIDDEN; - $this->model->error = DeleteModel::ERROR_ACCESS_DENIED; + $this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN; return true; } @@ -43,7 +43,7 @@ public function invoke(?array $args): bool if (Router::requestMethod() == Router::METHOD_POST) { - $this->model->error = $this->model->document->deallocate() ? DeleteModel::ERROR_SUCCESS : DeleteModel::ERROR_INTERNAL; + $this->model->error = $this->model->document->deallocate() ? false : DeleteModel::ERROR_INTERNAL; $event = Logger::initEvent( \BNETDocs\Libraries\EventLog\EventTypes::DOCUMENT_DELETED, diff --git a/src/Controllers/Document/Edit.php b/src/Controllers/Document/Edit.php index d6612366..7f1626d4 100644 --- a/src/Controllers/Document/Edit.php +++ b/src/Controllers/Document/Edit.php @@ -5,12 +5,13 @@ use \BNETDocs\Libraries\Core\HttpCode; use \BNETDocs\Libraries\Core\Router; use \BNETDocs\Libraries\EventLog\Logger; +use \BNETDocs\Models\Document\Edit as EditModel; class Edit extends \BNETDocs\Controllers\Base { public function __construct() { - $this->model = new \BNETDocs\Models\Document\Edit(); + $this->model = new EditModel(); } 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_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; } @@ -33,7 +34,7 @@ public function invoke(?array $args): bool if (!$this->model->document) { $this->model->_responseCode = HttpCode::HTTP_NOT_FOUND; - $this->model->error = 'NOT_FOUND'; + $this->model->error = EditModel::ERROR_NOT_FOUND; return true; } @@ -71,11 +72,11 @@ protected function handlePost(): void if (empty($title)) { - $this->model->error = 'EMPTY_TITLE'; + $this->model->error = EditModel::ERROR_EMPTY_TITLE; } else if (empty($content)) { - $this->model->error = 'EMPTY_CONTENT'; + $this->model->error = EditModel::ERROR_EMPTY_CONTENT; } if ($this->model->error) return; @@ -87,7 +88,7 @@ protected function handlePost(): void $this->model->document->setPublished($publish); $this->model->document->incrementEdited(); - $this->model->error = $this->model->document->commit() ? false : 'INTERNAL_ERROR'; + $this->model->error = $this->model->document->commit() ? false : EditModel::ERROR_INTERNAL; if ($this->model->error !== false) return; $event = Logger::initEvent( diff --git a/src/Controllers/Document/View.php b/src/Controllers/Document/View.php index e3adc8e9..63ef3aa0 100644 --- a/src/Controllers/Document/View.php +++ b/src/Controllers/Document/View.php @@ -26,7 +26,7 @@ public function invoke(?array $args): bool $this->model->document_id = array_shift($args); try { $this->model->document = new \BNETDocs\Libraries\Document($this->model->document_id); } - catch (\UnexpectedValueException) { $this->model->document = null; } + catch (\BNETDocs\Exceptions\DocumentNotFoundException) { $this->model->document = null; } if ($this->model->document && !$this->model->document->isPublished() && !($this->model->active_user && $this->model->active_user->isStaff())) diff --git a/src/Models/Document/Create.php b/src/Models/Document/Create.php index 8f219d90..73d25b35 100644 --- a/src/Models/Document/Create.php +++ b/src/Models/Document/Create.php @@ -2,11 +2,28 @@ namespace BNETDocs\Models\Document; -class Create extends \BNETDocs\Models\ActiveUser +class Create extends \BNETDocs\Models\ActiveUser implements \JsonSerializable { - public bool $acl_allowed = false; - public ?string $brief = null; - public ?string $content = null; - public bool $markdown = true; - public ?string $title = null; + 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 bool $acl_allowed = false; + public ?string $brief = null; + public ?string $content = null; + public bool $markdown = true; + public ?string $title = null; + + public function jsonSerialize(): mixed + { + return \array_merge(parent::jsonSerialize(), [ + 'acl_allowed' => $this->acl_allowed, + 'brief' => $this->brief, + 'content' => $this->content, + 'markdown' => $this->markdown, + 'title' => $this->title, + ]); + } } diff --git a/src/Models/Document/Delete.php b/src/Models/Document/Delete.php index c0b861e6..71f2598f 100644 --- a/src/Models/Document/Delete.php +++ b/src/Models/Document/Delete.php @@ -2,17 +2,25 @@ namespace BNETDocs\Models\Document; -class Delete extends \BNETDocs\Models\ActiveUser +class Delete extends \BNETDocs\Models\ActiveUser implements \JsonSerializable { - public const ERROR_ACCESS_DENIED = 'ACCESS_DENIED'; - public const ERROR_INTERNAL = 'INTERNAL'; - public const ERROR_NONE = 'NONE'; - public const ERROR_NOT_FOUND = 'NOT_FOUND'; - public const ERROR_SUCCESS = 'SUCCESS'; + 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 bool $acl_allowed = false; - public ?\BNETDocs\Libraries\Document $document = null; - public mixed $error = self::ERROR_NONE; - public ?int $id = null; - public ?string $title = null; + public bool $acl_allowed = false; + public ?\BNETDocs\Libraries\Document $document = null; + public ?int $id = null; + public ?string $title = null; + + public function jsonSerialize(): mixed + { + return \array_merge(parent::jsonSerialize(), [ + 'acl_allowed' => $this->acl_allowed, + 'document' => $this->document, + 'id' => $this->id, + 'title' => $this->title, + ]); + } } diff --git a/src/Models/Document/Edit.php b/src/Models/Document/Edit.php index e92a8904..521653f8 100644 --- a/src/Models/Document/Edit.php +++ b/src/Models/Document/Edit.php @@ -2,16 +2,39 @@ namespace BNETDocs\Models\Document; -class Edit extends \BNETDocs\Models\ActiveUser +class Edit extends \BNETDocs\Models\ActiveUser implements \JsonSerializable { - public bool $acl_allowed = false; - public ?string $brief = null; - public ?string $category = null; - public ?array $comments = null; - public ?string $content = null; - public ?\BNETDocs\Libraries\Document $document = null; - public ?int $document_id = null; - public ?bool $markdown = null; - public ?bool $published = null; - public ?string $title = null; + 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 bool $acl_allowed = false; + public ?string $brief = null; + public ?string $category = null; + public ?array $comments = null; + public ?string $content = null; + public ?\BNETDocs\Libraries\Document $document = null; + public ?int $document_id = null; + public ?bool $markdown = null; + public ?bool $published = null; + public ?string $title = null; + + public function jsonSerialize(): mixed + { + return \array_merge(parent::jsonSerialize(), [ + 'acl_allowed' => $this->acl_allowed, + 'brief' => $this->brief, + 'category' => $this->category, + 'comments' => $this->comments, + 'content' => $this->content, + 'document' => $this->document, + 'document_id' => $this->document_id, + 'markdown' => $this->markdown, + 'published' => $this->published, + 'title' => $this->title, + ]); + } } diff --git a/src/Models/Document/Index.php b/src/Models/Document/Index.php index 0a5645ef..659b5f69 100644 --- a/src/Models/Document/Index.php +++ b/src/Models/Document/Index.php @@ -2,9 +2,18 @@ namespace BNETDocs\Models\Document; -class Index extends \BNETDocs\Models\ActiveUser +class Index extends \BNETDocs\Models\ActiveUser implements \JsonSerializable { - public array|false $documents = false; - public string $order = ''; - public int $sum_documents = 0; + public array|false $documents = false; + public string $order = ''; + public int $sum_documents = 0; + + public function jsonSerialize(): mixed + { + return \array_merge(parent::jsonSerialize(), [ + 'documents' => $this->documents, + 'order' => $this->order, + 'sum_documents' => $this->sum_documents, + ]); + } } diff --git a/src/Models/Document/View.php b/src/Models/Document/View.php index dd10ad43..e9a8c413 100644 --- a/src/Models/Document/View.php +++ b/src/Models/Document/View.php @@ -2,10 +2,20 @@ namespace BNETDocs\Models\Document; -class View extends \BNETDocs\Models\ActiveUser +class View extends \BNETDocs\Models\ActiveUser implements \JsonSerializable { - public bool $acl_allowed = false; - public ?array $comments = null; - public ?\BNETDocs\Libraries\Document $document = null; - public ?int $document_id = null; + public bool $acl_allowed = false; + public ?array $comments = null; + public ?\BNETDocs\Libraries\Document $document = null; + public ?int $document_id = null; + + public function jsonSerialize(): mixed + { + return \array_merge(parent::jsonSerialize(), [ + 'acl_allowed' => $this->acl_allowed, + 'comments' => $this->comments, + 'document' => $this->document, + 'document_id' => $this->document_id, + ]); + } } diff --git a/src/Templates/Document/Create.phtml b/src/Templates/Document/Create.phtml index a4ae1d71..96725717 100644 --- a/src/Templates/Document/Create.phtml +++ b/src/Templates/Document/Create.phtml @@ -1,6 +1,6 @@ getContext()->error; switch ($error) { - case 'ACL_NOT_SET': $message = 'You do not have the privilege to create documents.'; break; - case 'EMPTY_TITLE': $message = 'The title of the document is required.'; break; - case 'EMPTY_CONTENT': $message = 'The content of the document 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 documents.'; break; + case CreateModel::ERROR_EMPTY_TITLE: $message = 'The title of the document is required.'; break; + case CreateModel::ERROR_EMPTY_CONTENT: $message = 'The content of the document is required.'; break; + case CreateModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to create documents.'; break; + case CreateModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break; default: $message = $error; } $form_brief = filter_var($this->getContext()->brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS); diff --git a/src/Templates/Document/Delete.phtml b/src/Templates/Document/Delete.phtml index bc6cf5eb..6457e94b 100644 --- a/src/Templates/Document/Delete.phtml +++ b/src/Templates/Document/Delete.phtml @@ -11,17 +11,17 @@ $id = $this->getContext()->id; $doc_title = $this->getContext()->title; switch ($error) { - case DeleteModel::ERROR_ACCESS_DENIED: $message = 'You do not have the privilege to delete documents.'; break; + case DeleteModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to delete documents.'; 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 document by that id.'; break; - case DeleteModel::ERROR_SUCCESS: $message = 'You have successfully deleted the document!'; break; + case DeleteModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to delete documents.'; break; default: $message = $error; } require('./header.inc.phtml'); ?>
- + - +

Delete Document

@@ -33,10 +33,10 @@ require('./header.inc.phtml'); ?>
- +

Document Deleted

-

+

You have successfully deleted the document!

Delete Document

diff --git a/src/Templates/Document/Edit.phtml b/src/Templates/Document/Edit.phtml index 1a276c55..3e72bc4b 100644 --- a/src/Templates/Document/Edit.phtml +++ b/src/Templates/Document/Edit.phtml @@ -1,6 +1,7 @@ getContext()->document ? $this->getContext()->document-> $error = $this->getContext()->error; switch ($error) { - case 'ACL_NOT_SET': $message = 'You do not have the privilege to edit documents.'; break; - case 'NOT_FOUND': $message = 'Cannot find document by that id.'; break; - case 'EMPTY_TITLE': $message = 'The title of the document is required.'; break; - case 'EMPTY_CONTENT': $message = 'The content of the document 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 documents.'; break; + case EditModel::ERROR_EMPTY_CONTENT: $message = 'The content of the document is required.'; break; + case EditModel::ERROR_EMPTY_TITLE: $message = 'The title of the document is required.'; break; + case EditModel::ERROR_INTERNAL: $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break; + case EditModel::ERROR_NOT_FOUND: $message = 'Cannot find document by that id.'; break; + case EditModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to edit documents.'; break; default: $message = $error; } $form_brief = filter_var($this->getContext()->brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);