Skip to content

Commit

Permalink
Refactor Document/{Create,Delete,Edit,Index,View}
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 2, 2024
1 parent 5f4a73c commit 9bd03d1
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 73 deletions.
24 changes: 13 additions & 11 deletions src/Controllers/Document/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/Document/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down
13 changes: 7 additions & 6 deletions src/Controllers/Document/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Document/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
29 changes: 23 additions & 6 deletions src/Models/Document/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
30 changes: 19 additions & 11 deletions src/Models/Document/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
45 changes: 34 additions & 11 deletions src/Models/Document/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
17 changes: 13 additions & 4 deletions src/Models/Document/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
20 changes: 15 additions & 5 deletions src/Models/Document/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}
}
11 changes: 6 additions & 5 deletions src/Templates/Document/Create.phtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Templates\Document;
use \CarlBennett\MVC\Libraries\Common;
use \BNETDocs\Models\Document\Create as CreateModel;
use \CarlBennett\MVC\Libraries\Pair;
$title = 'Create Document';
$description = 'This page enables a user to create documents on the site.';
Expand All @@ -11,10 +11,11 @@ $document_url = null;
$error = $this->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);
Expand Down
12 changes: 6 additions & 6 deletions src/Templates/Document/Delete.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -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'); ?>
<div class="container">
<? if ($error === DeleteModel::ERROR_ACCESS_DENIED) { ?>
<? if ($error === DeleteModel::ERROR_ACL_NOT_SET) { ?>
<? require('./LoginRequired.inc.phtml'); ?>
<? } else if ($error === DeleteModel::ERROR_NONE) { ?>
<? } else if (\is_null($error)) { ?>
<h1 class="text-danger">Delete Document</h1>
<form method="POST" action="?id=<?=filter_var($id, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>">
<div class="form-group">
Expand All @@ -33,10 +33,10 @@ require('./header.inc.phtml'); ?>
<input class="btn btn-danger" type="submit" value="Delete Document" tabindex="2" autofocus="autofocus"/>
</div>
</form>
<? } else if ($error === DeleteModel::ERROR_SUCCESS) { ?>
<? } else if ($error === false) { ?>
<h1 class="text-success">Document Deleted</h1>
<div class="alert alert-success">
<p class="mb-0"><?=$message?></p>
<p class="mb-0">You have successfully deleted the document!</p>
</div>
<? } else { ?>
<h1 class="text-danger">Delete Document</h1>
Expand Down
12 changes: 7 additions & 5 deletions src/Templates/Document/Edit.phtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Templates\Document;
use \BNETDocs\Libraries\Comment;
use \BNETDocs\Models\Document\Edit as EditModel;
use \CarlBennett\MVC\Libraries\Common;
use \CarlBennett\MVC\Libraries\Pair;
$title = 'Edit Document';
Expand All @@ -13,11 +14,12 @@ $document_url = ($this->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);
Expand Down

0 comments on commit 9bd03d1

Please sign in to comment.