Skip to content

Commit 9bd03d1

Browse files
committed
Refactor Document/{Create,Delete,Edit,Index,View}
1 parent 5f4a73c commit 9bd03d1

File tree

12 files changed

+146
-73
lines changed

12 files changed

+146
-73
lines changed

src/Controllers/Document/Create.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
use \BNETDocs\Libraries\Core\HttpCode;
66
use \BNETDocs\Libraries\Core\Router;
77
use \BNETDocs\Libraries\EventLog\Logger;
8+
use \BNETDocs\Models\Document\Create as CreateModel;
89

910
class Create extends \BNETDocs\Controllers\Base
1011
{
11-
public const EMPTY_CONTENT = 'EMPTY_CONTENT';
12-
public const EMPTY_TITLE = 'EMPTY_TITLE';
13-
public const INTERNAL_ERROR = 'INTERNAL_ERROR';
14-
1512
public function __construct()
1613
{
17-
$this->model = new \BNETDocs\Models\Document\Create();
14+
$this->model = new CreateModel();
1815
}
1916

2017
public function invoke(?array $args): bool
@@ -25,7 +22,7 @@ public function invoke(?array $args): bool
2522
if (!$this->model->acl_allowed)
2623
{
2724
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
28-
$this->model->error = 'ACL_NOT_SET';
25+
$this->model->error = $this->model->active_user ? CreateModel::ERROR_ACL_NOT_SET : CreateModel::ERROR_NOT_LOGGED_IN;
2926
return true;
3027
}
3128

@@ -55,11 +52,16 @@ protected function handlePost(): void
5552
$this->model->markdown = $markdown;
5653
$this->model->content = $content;
5754

58-
if (empty($title)) {
59-
$this->model->error = self::EMPTY_TITLE;
60-
} else if (empty($content)) {
61-
$this->model->error = self::EMPTY_CONTENT;
55+
if (empty($title))
56+
{
57+
$this->model->error = CreateModel::ERROR_EMPTY_TITLE;
6258
}
59+
else if (empty($content))
60+
{
61+
$this->model->error = CreateModel::ERROR_EMPTY_CONTENT;
62+
}
63+
64+
if ($this->model->error) return;
6365

6466
$document = new \BNETDocs\Libraries\Document(null);
6567
$document->setBrief($brief);
@@ -71,7 +73,7 @@ protected function handlePost(): void
7173

7274
if (!$document->commit())
7375
{
74-
$this->model->error = self::INTERNAL_ERROR;
76+
$this->model->error = CreateModel::ERROR_INTERNAL;
7577
return;
7678
}
7779
$this->model->error = false;

src/Controllers/Document/Delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function invoke(?array $args): bool
2222
if (!$this->model->acl_allowed)
2323
{
2424
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
25-
$this->model->error = DeleteModel::ERROR_ACCESS_DENIED;
25+
$this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN;
2626
return true;
2727
}
2828

@@ -43,7 +43,7 @@ public function invoke(?array $args): bool
4343

4444
if (Router::requestMethod() == Router::METHOD_POST)
4545
{
46-
$this->model->error = $this->model->document->deallocate() ? DeleteModel::ERROR_SUCCESS : DeleteModel::ERROR_INTERNAL;
46+
$this->model->error = $this->model->document->deallocate() ? false : DeleteModel::ERROR_INTERNAL;
4747

4848
$event = Logger::initEvent(
4949
\BNETDocs\Libraries\EventLog\EventTypes::DOCUMENT_DELETED,

src/Controllers/Document/Edit.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use \BNETDocs\Libraries\Core\HttpCode;
66
use \BNETDocs\Libraries\Core\Router;
77
use \BNETDocs\Libraries\EventLog\Logger;
8+
use \BNETDocs\Models\Document\Edit as EditModel;
89

910
class Edit extends \BNETDocs\Controllers\Base
1011
{
1112
public function __construct()
1213
{
13-
$this->model = new \BNETDocs\Models\Document\Edit();
14+
$this->model = new EditModel();
1415
}
1516

1617
public function invoke(?array $args): bool
@@ -21,7 +22,7 @@ public function invoke(?array $args): bool
2122
if (!$this->model->acl_allowed)
2223
{
2324
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
24-
$this->model->error = 'ACL_NOT_SET';
25+
$this->model->error = $this->model->active_user ? EditModel::ERROR_ACL_NOT_SET : EditModel::ERROR_NOT_LOGGED_IN;
2526
return true;
2627
}
2728

@@ -33,7 +34,7 @@ public function invoke(?array $args): bool
3334
if (!$this->model->document)
3435
{
3536
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
36-
$this->model->error = 'NOT_FOUND';
37+
$this->model->error = EditModel::ERROR_NOT_FOUND;
3738
return true;
3839
}
3940

@@ -71,11 +72,11 @@ protected function handlePost(): void
7172

7273
if (empty($title))
7374
{
74-
$this->model->error = 'EMPTY_TITLE';
75+
$this->model->error = EditModel::ERROR_EMPTY_TITLE;
7576
}
7677
else if (empty($content))
7778
{
78-
$this->model->error = 'EMPTY_CONTENT';
79+
$this->model->error = EditModel::ERROR_EMPTY_CONTENT;
7980
}
8081

8182
if ($this->model->error) return;
@@ -87,7 +88,7 @@ protected function handlePost(): void
8788
$this->model->document->setPublished($publish);
8889
$this->model->document->incrementEdited();
8990

90-
$this->model->error = $this->model->document->commit() ? false : 'INTERNAL_ERROR';
91+
$this->model->error = $this->model->document->commit() ? false : EditModel::ERROR_INTERNAL;
9192
if ($this->model->error !== false) return;
9293

9394
$event = Logger::initEvent(

src/Controllers/Document/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function invoke(?array $args): bool
2626
$this->model->document_id = array_shift($args);
2727

2828
try { $this->model->document = new \BNETDocs\Libraries\Document($this->model->document_id); }
29-
catch (\UnexpectedValueException) { $this->model->document = null; }
29+
catch (\BNETDocs\Exceptions\DocumentNotFoundException) { $this->model->document = null; }
3030

3131
if ($this->model->document && !$this->model->document->isPublished()
3232
&& !($this->model->active_user && $this->model->active_user->isStaff()))

src/Models/Document/Create.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@
22

33
namespace BNETDocs\Models\Document;
44

5-
class Create extends \BNETDocs\Models\ActiveUser
5+
class Create extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public bool $acl_allowed = false;
8-
public ?string $brief = null;
9-
public ?string $content = null;
10-
public bool $markdown = true;
11-
public ?string $title = null;
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
9+
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
10+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
11+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
12+
13+
public bool $acl_allowed = false;
14+
public ?string $brief = null;
15+
public ?string $content = null;
16+
public bool $markdown = true;
17+
public ?string $title = null;
18+
19+
public function jsonSerialize(): mixed
20+
{
21+
return \array_merge(parent::jsonSerialize(), [
22+
'acl_allowed' => $this->acl_allowed,
23+
'brief' => $this->brief,
24+
'content' => $this->content,
25+
'markdown' => $this->markdown,
26+
'title' => $this->title,
27+
]);
28+
}
1229
}

src/Models/Document/Delete.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22

33
namespace BNETDocs\Models\Document;
44

5-
class Delete extends \BNETDocs\Models\ActiveUser
5+
class Delete extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public const ERROR_ACCESS_DENIED = 'ACCESS_DENIED';
8-
public const ERROR_INTERNAL = 'INTERNAL';
9-
public const ERROR_NONE = 'NONE';
10-
public const ERROR_NOT_FOUND = 'NOT_FOUND';
11-
public const ERROR_SUCCESS = 'SUCCESS';
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
9+
public const ERROR_NOT_FOUND = 'NOT_FOUND';
10+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
1211

13-
public bool $acl_allowed = false;
14-
public ?\BNETDocs\Libraries\Document $document = null;
15-
public mixed $error = self::ERROR_NONE;
16-
public ?int $id = null;
17-
public ?string $title = null;
12+
public bool $acl_allowed = false;
13+
public ?\BNETDocs\Libraries\Document $document = null;
14+
public ?int $id = null;
15+
public ?string $title = null;
16+
17+
public function jsonSerialize(): mixed
18+
{
19+
return \array_merge(parent::jsonSerialize(), [
20+
'acl_allowed' => $this->acl_allowed,
21+
'document' => $this->document,
22+
'id' => $this->id,
23+
'title' => $this->title,
24+
]);
25+
}
1826
}

src/Models/Document/Edit.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,39 @@
22

33
namespace BNETDocs\Models\Document;
44

5-
class Edit extends \BNETDocs\Models\ActiveUser
5+
class Edit extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public bool $acl_allowed = false;
8-
public ?string $brief = null;
9-
public ?string $category = null;
10-
public ?array $comments = null;
11-
public ?string $content = null;
12-
public ?\BNETDocs\Libraries\Document $document = null;
13-
public ?int $document_id = null;
14-
public ?bool $markdown = null;
15-
public ?bool $published = null;
16-
public ?string $title = null;
7+
public const ERROR_ACL_NOT_SET = 'ACL_NOT_SET';
8+
public const ERROR_EMPTY_CONTENT = 'EMPTY_CONTENT';
9+
public const ERROR_EMPTY_TITLE = 'EMPTY_TITLE';
10+
public const ERROR_INTERNAL = 'INTERNAL_ERROR';
11+
public const ERROR_NOT_FOUND = 'NOT_FOUND';
12+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
13+
14+
public bool $acl_allowed = false;
15+
public ?string $brief = null;
16+
public ?string $category = null;
17+
public ?array $comments = null;
18+
public ?string $content = null;
19+
public ?\BNETDocs\Libraries\Document $document = null;
20+
public ?int $document_id = null;
21+
public ?bool $markdown = null;
22+
public ?bool $published = null;
23+
public ?string $title = null;
24+
25+
public function jsonSerialize(): mixed
26+
{
27+
return \array_merge(parent::jsonSerialize(), [
28+
'acl_allowed' => $this->acl_allowed,
29+
'brief' => $this->brief,
30+
'category' => $this->category,
31+
'comments' => $this->comments,
32+
'content' => $this->content,
33+
'document' => $this->document,
34+
'document_id' => $this->document_id,
35+
'markdown' => $this->markdown,
36+
'published' => $this->published,
37+
'title' => $this->title,
38+
]);
39+
}
1740
}

src/Models/Document/Index.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22

33
namespace BNETDocs\Models\Document;
44

5-
class Index extends \BNETDocs\Models\ActiveUser
5+
class Index extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public array|false $documents = false;
8-
public string $order = '';
9-
public int $sum_documents = 0;
7+
public array|false $documents = false;
8+
public string $order = '';
9+
public int $sum_documents = 0;
10+
11+
public function jsonSerialize(): mixed
12+
{
13+
return \array_merge(parent::jsonSerialize(), [
14+
'documents' => $this->documents,
15+
'order' => $this->order,
16+
'sum_documents' => $this->sum_documents,
17+
]);
18+
}
1019
}

src/Models/Document/View.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
namespace BNETDocs\Models\Document;
44

5-
class View extends \BNETDocs\Models\ActiveUser
5+
class View extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
7-
public bool $acl_allowed = false;
8-
public ?array $comments = null;
9-
public ?\BNETDocs\Libraries\Document $document = null;
10-
public ?int $document_id = null;
7+
public bool $acl_allowed = false;
8+
public ?array $comments = null;
9+
public ?\BNETDocs\Libraries\Document $document = null;
10+
public ?int $document_id = null;
11+
12+
public function jsonSerialize(): mixed
13+
{
14+
return \array_merge(parent::jsonSerialize(), [
15+
'acl_allowed' => $this->acl_allowed,
16+
'comments' => $this->comments,
17+
'document' => $this->document,
18+
'document_id' => $this->document_id,
19+
]);
20+
}
1121
}

src/Templates/Document/Create.phtml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\Document;
3-
use \CarlBennett\MVC\Libraries\Common;
3+
use \BNETDocs\Models\Document\Create as CreateModel;
44
use \CarlBennett\MVC\Libraries\Pair;
55
$title = 'Create Document';
66
$description = 'This page enables a user to create documents on the site.';
@@ -11,10 +11,11 @@ $document_url = null;
1111
$error = $this->getContext()->error;
1212
switch ($error)
1313
{
14-
case 'ACL_NOT_SET': $message = 'You do not have the privilege to create documents.'; break;
15-
case 'EMPTY_TITLE': $message = 'The title of the document is required.'; break;
16-
case 'EMPTY_CONTENT': $message = 'The content of the document is required.'; break;
17-
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
14+
case CreateModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to create documents.'; break;
15+
case CreateModel::ERROR_EMPTY_TITLE: $message = 'The title of the document is required.'; break;
16+
case CreateModel::ERROR_EMPTY_CONTENT: $message = 'The content of the document is required.'; break;
17+
case CreateModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to create documents.'; break;
18+
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;
1819
default: $message = $error;
1920
}
2021
$form_brief = filter_var($this->getContext()->brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

0 commit comments

Comments
 (0)