Skip to content

Commit 54a0dc5

Browse files
committed
Refactor Packet/{Create,Delete,Edit} & Form
1 parent e5a9757 commit 54a0dc5

File tree

8 files changed

+70
-70
lines changed

8 files changed

+70
-70
lines changed

src/Controllers/Packet/Create.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function invoke(?array $args): bool
2222
if (!$this->model->active_user || !$this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_PACKET_CREATE))
2323
{
2424
$this->model->_responseCode = HttpCode::HTTP_UNAUTHORIZED;
25-
$this->model->error = FormModel::ERROR_ACL_DENIED;
25+
$this->model->error = $this->model->active_user ? FormModel::ERROR_ACL_NOT_SET : FormModel::ERROR_NOT_LOGGED_IN;
2626
return true;
2727
}
2828

@@ -46,9 +46,9 @@ public function invoke(?array $args): bool
4646
self::assignDefault($this->model->form_fields, 'used_by', Product::getProductsFromIds($packet->getUsedBy()));
4747

4848
if (Router::requestMethod() == Router::METHOD_POST) $this->handlePost($this->model);
49-
else $this->model->error = FormModel::ERROR_NONE;
49+
else $this->model->error = null;
5050

51-
if ($this->model->error === FormModel::ERROR_SUCCESS)
51+
if ($this->model->error === false)
5252
{
5353
$event = Logger::initEvent(
5454
\BNETDocs\Libraries\EventLog\EventTypes::PACKET_CREATED,
@@ -124,7 +124,7 @@ public function invoke(?array $args): bool
124124
}
125125
}
126126

127-
$this->model->_responseCode = ($this->model->error === FormModel::ERROR_SUCCESS ? HttpCode::HTTP_OK : HttpCode::HTTP_INTERNAL_SERVER_ERROR);
127+
$this->model->_responseCode = !$this->model->error ? HttpCode::HTTP_OK : HttpCode::HTTP_INTERNAL_SERVER_ERROR;
128128
return true;
129129
}
130130

@@ -184,6 +184,6 @@ protected function handlePost(): void
184184
$this->model->packet->setPublished($published ? true : false);
185185
$this->model->packet->setUser($this->model->active_user);
186186

187-
$this->model->error = $this->model->packet->commit() ? FormModel::ERROR_SUCCESS : FormModel::ERROR_INTERNAL;
187+
$this->model->error = $this->model->packet->commit() ? false : FormModel::ERROR_INTERNAL;
188188
}
189189
}

src/Controllers/Packet/Delete.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
use \BNETDocs\Libraries\Core\Router;
77
use \BNETDocs\Libraries\Discord\EmbedField as DiscordEmbedField;
88
use \BNETDocs\Libraries\EventLog\Logger;
9+
use \BNETDocs\Models\Packet\Delete as DeleteModel;
910

1011
class Delete extends \BNETDocs\Controllers\Base
1112
{
1213
public function __construct()
1314
{
14-
$this->model = new \BNETDocs\Models\Packet\Delete();
15+
$this->model = new DeleteModel();
1516
}
1617

1718
public function invoke(?array $args): bool
@@ -22,24 +23,24 @@ public function invoke(?array $args): bool
2223
if (!$this->model->acl_allowed)
2324
{
2425
$this->model->_responseCode = HttpCode::HTTP_UNAUTHORIZED;
25-
$this->model->error = 'ACL_NOT_SET';
26+
$this->model->error = $this->model->active_user ? DeleteModel::ERROR_ACL_NOT_SET : DeleteModel::ERROR_NOT_LOGGED_IN;
2627
return true;
2728
}
2829

2930
$q = Router::query();
30-
$this->model->id = $q['id'] ?? null;
31+
$this->model->packet_id = $q['id'] ?? null;
3132

32-
try { if (!is_null($this->model->id)) $this->model->packet = new \BNETDocs\Libraries\Packet\Packet($this->model->id); }
33+
try { if (!is_null($this->model->packet_id)) $this->model->packet = new \BNETDocs\Libraries\Packet\Packet($this->model->packet_id); }
3334
catch (\UnexpectedValueException) { $this->model->packet = null; }
3435

3536
if (!$this->model->packet)
3637
{
3738
$this->model->_responseCode = HttpCode::HTTP_NOT_FOUND;
38-
$this->model->error = 'NOT_FOUND';
39+
$this->model->error = DeleteModel::ERROR_NOT_FOUND;
3940
return true;
4041
}
4142

42-
$this->model->title = $this->model->packet->getLabel();
43+
$this->model->label = $this->model->packet->getLabel();
4344

4445
if (Router::requestMethod() == Router::METHOD_POST) $this->tryDelete();
4546
$this->model->_responseCode = $this->model->error ? HttpCode::HTTP_INTERNAL_SERVER_ERROR : HttpCode::HTTP_OK;
@@ -48,15 +49,15 @@ public function invoke(?array $args): bool
4849

4950
protected function tryDelete(): void
5051
{
51-
$this->model->error = $this->model->packet->deallocate() ? false : 'INTERNAL_ERROR';
52+
$this->model->error = $this->model->packet->deallocate() ? false : DeleteModel::ERROR_INTERNAL;
5253

5354
$event = Logger::initEvent(
5455
\BNETDocs\Libraries\EventLog\EventTypes::PACKET_DELETED,
5556
$this->model->active_user,
5657
getenv('REMOTE_ADDR'),
5758
[
5859
'error' => $this->model->error,
59-
'packet_id' => $this->model->id,
60+
'packet_id' => $this->model->packet_id,
6061
'packet' => $this->model->packet,
6162
]
6263
);

src/Controllers/Packet/Edit.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function invoke(?array $args): bool
2323
{
2424
if (!$this->model->active_user || !$this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_PACKET_MODIFY))
2525
{
26-
$this->model->error = FormModel::ERROR_ACL_DENIED;
26+
$this->model->error = $this->model->active_user ? FormModel::ERROR_ACL_NOT_SET : FormModel::ERROR_NOT_LOGGED_IN;
2727
$this->model->_responseCode = HttpCode::HTTP_UNAUTHORIZED;
2828
return true;
2929
}
@@ -60,7 +60,7 @@ public function invoke(?array $args): bool
6060

6161
if (Router::requestMethod() == Router::METHOD_POST) $this->handlePost($this->model);
6262

63-
if ($this->model->error === FormModel::ERROR_SUCCESS)
63+
if ($this->model->error === false)
6464
{
6565
$event = Logger::initEvent(
6666
\BNETDocs\Libraries\EventLog\EventTypes::PACKET_EDITED,
@@ -141,7 +141,7 @@ public function invoke(?array $args): bool
141141
}
142142
}
143143

144-
$this->model->_responseCode = HttpCode::HTTP_OK;
144+
$this->model->_responseCode = !$this->model->error ? HttpCode::HTTP_OK : HttpCode::HTTP_INTERNAL_SERVER_ERROR;
145145
return true;
146146
}
147147

@@ -167,61 +167,59 @@ protected function handlePost(): void
167167
$transport_layer = $this->model->form_fields['transport_layer'] ?? null;
168168
$used_by = $this->model->form_fields['used_by'] ?? [];
169169

170-
$this->model->error = FormModel::ERROR_SUCCESS;
170+
$this->model->error = false;
171171

172172
try { $this->model->packet->setApplicationLayerId($application_layer); }
173173
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID; }
174174
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID; }
175-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
175+
finally { if ($this->model->error !== false) return; }
176176

177177
try { $this->model->packet->setBrief($brief); }
178178
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_BRIEF; }
179179
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_BRIEF; }
180-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
180+
finally { if ($this->model->error !== false) return; }
181181

182182
try { $this->model->packet->setDirection((int) $direction); }
183183
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_DIRECTION; }
184184
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_DIRECTION; }
185-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
185+
finally { if ($this->model->error !== false) return; }
186186

187187
try { $this->model->packet->setPacketId($packet_id); }
188188
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_PACKET_ID; }
189189
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_PACKET_ID; }
190-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
190+
finally { if ($this->model->error !== false) return; }
191191

192192
try { $this->model->packet->setName($name); }
193193
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_NAME; }
194194
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_NAME; }
195-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
195+
finally { if ($this->model->error !== false) return; }
196196

197197
try { $this->model->packet->setFormat($format); }
198198
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_FORMAT; }
199199
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_FORMAT; }
200-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
200+
finally { if ($this->model->error !== false) return; }
201201

202202
try { $this->model->packet->setRemarks($remarks); }
203203
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_REMARKS; }
204204
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_REMARKS; }
205-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
205+
finally { if ($this->model->error !== false) return; }
206206

207207
try { $this->model->packet->setTransportLayerId($transport_layer); }
208208
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID; }
209209
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID; }
210-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
210+
finally { if ($this->model->error !== false) return; }
211211

212212
try { $this->model->packet->setUsedBy($used_by); }
213213
catch (OutOfBoundsException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_USED_BY; }
214214
catch (UnexpectedValueException) { $this->model->error = FormModel::ERROR_OUTOFBOUNDS_USED_BY; }
215-
finally { if ($this->model->error !== FormModel::ERROR_SUCCESS) return; }
216-
217-
$this->model->error = FormModel::ERROR_INTERNAL;
215+
finally { if ($this->model->error !== false) return; }
218216

219217
$this->model->packet->setOption(Packet::OPTION_DEPRECATED, $deprecated ? true : false);
220218
$this->model->packet->setOption(Packet::OPTION_MARKDOWN, $markdown ? true : false);
221219
$this->model->packet->setOption(Packet::OPTION_PUBLISHED, $published ? true : false);
222220
$this->model->packet->setOption(Packet::OPTION_RESEARCH, $research ? true : false);
223221
$this->model->packet->incrementEdited();
224222

225-
if ($this->model->packet->commit()) $this->model->error = FormModel::ERROR_SUCCESS;
223+
$this->model->error = $this->model->packet->commit() ? false : FormModel::ERROR_INTERNAL;
226224
}
227225
}

src/Models/Packet/Delete.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44

55
class Delete extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
66
{
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';
11+
712
public bool $acl_allowed = false;
8-
public ?int $id = null;
13+
public ?string $label = null;
914
public ?\BNETDocs\Libraries\Packet\Packet $packet = null;
10-
public ?string $title = null;
15+
public ?int $packet_id = null;
1116

1217
public function jsonSerialize(): mixed
1318
{
1419
return \array_merge(parent::jsonSerialize(), [
1520
'acl_allowed' => $this->acl_allowed,
16-
'id' => $this->id,
21+
'label' => $this->label,
1722
'packet' => $this->packet,
18-
'title' => $this->title,
23+
'packet_id' => $this->packet_id,
1924
]);
2025
}
2126
}

src/Models/Packet/Form.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22

33
namespace BNETDocs\Models\Packet;
44

5-
class Form extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
5+
class Form extends \BNETDocs\Models\Core\HttpForm implements \JsonSerializable
66
{
77
// possible values for $error:
8-
const ERROR_ACL_DENIED = 'ACL_DENIED';
9-
const ERROR_INTERNAL = 'INTERNAL';
10-
const ERROR_NONE = 'NONE';
11-
const ERROR_NOT_FOUND = 'NOT_FOUND';
12-
const ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID = 'OUTOFBOUNDS_APPLICATION_LAYER_ID';
13-
const ERROR_OUTOFBOUNDS_BRIEF = 'OUTOFBOUNDS_BRIEF';
14-
const ERROR_OUTOFBOUNDS_DIRECTION = 'OUTOFBOUNDS_DIRECTION';
15-
const ERROR_OUTOFBOUNDS_FORMAT = 'OUTOFBOUNDS_FORMAT';
16-
const ERROR_OUTOFBOUNDS_ID = 'OUTOFBOUNDS_ID';
17-
const ERROR_OUTOFBOUNDS_NAME = 'OUTOFBOUNDS_NAME';
18-
const ERROR_OUTOFBOUNDS_PACKET_ID = 'OUTOFBOUNDS_PACKET_ID';
19-
const ERROR_OUTOFBOUNDS_REMARKS = 'OUTOFBOUNDS_REMARKS';
20-
const ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID = 'OUTOFBOUNDS_TRANSPORT_LAYER_ID';
21-
const ERROR_OUTOFBOUNDS_USED_BY = 'OUTOFBOUNDS_USED_BY';
22-
const ERROR_SUCCESS = 'SUCCESS';
8+
public const ERROR_ACL_NOT_SET = 'ERROR_ACL_NOT_SET';
9+
public const ERROR_INTERNAL = 'INTERNAL';
10+
public const ERROR_NOT_FOUND = 'NOT_FOUND';
11+
public const ERROR_NOT_LOGGED_IN = 'NOT_LOGGED_IN';
12+
public const ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID = 'OUTOFBOUNDS_APPLICATION_LAYER_ID';
13+
public const ERROR_OUTOFBOUNDS_BRIEF = 'OUTOFBOUNDS_BRIEF';
14+
public const ERROR_OUTOFBOUNDS_DIRECTION = 'OUTOFBOUNDS_DIRECTION';
15+
public const ERROR_OUTOFBOUNDS_FORMAT = 'OUTOFBOUNDS_FORMAT';
16+
public const ERROR_OUTOFBOUNDS_ID = 'OUTOFBOUNDS_ID';
17+
public const ERROR_OUTOFBOUNDS_NAME = 'OUTOFBOUNDS_NAME';
18+
public const ERROR_OUTOFBOUNDS_PACKET_ID = 'OUTOFBOUNDS_PACKET_ID';
19+
public const ERROR_OUTOFBOUNDS_REMARKS = 'OUTOFBOUNDS_REMARKS';
20+
public const ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID = 'OUTOFBOUNDS_TRANSPORT_LAYER_ID';
21+
public const ERROR_OUTOFBOUNDS_USED_BY = 'OUTOFBOUNDS_USED_BY';
2322

2423
public ?array $comments = null;
25-
public array $form_fields = [];
2624
public ?\BNETDocs\Libraries\Packet\Packet $packet = null;
2725
public ?array $products = null;
2826

@@ -33,7 +31,6 @@ public function jsonSerialize(): mixed
3331
{
3432
return \array_merge(parent::jsonSerialize(), [
3533
'comments' => $this->comments,
36-
'form_fields' => $this->form_fields,
3734
'packet' => $this->packet,
3835
'products' => $this->products,
3936
]);

src/Templates/Packet/Create.phtml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ $comments = null;
1313
$error = $this->getContext()->error;
1414
switch ($error)
1515
{
16-
case FormModel::ERROR_ACL_DENIED: $message = 'You do not have the privilege to create packets.'; break;
16+
case FormModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to create packets.'; break;
1717
case FormModel::ERROR_INTERNAL: $message = 'An internal error occurred. Try again later.'; break;
18-
case FormModel::ERROR_NONE: $message = null; break;
1918
case FormModel::ERROR_NOT_FOUND: $message = 'The requested packet does not exist or could not be found.'; break;
19+
case FormModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to create packets.'; break;
2020
case FormModel::ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID: $message = sprintf('The application layer must be between 0-%d.', Packet::MAX_APPLICATION_LAYER_ID); break;
2121
case FormModel::ERROR_OUTOFBOUNDS_DIRECTION: $message = sprintf('The direction must be between 0-%d.', Packet::MAX_DIRECTION); break;
2222
case FormModel::ERROR_OUTOFBOUNDS_FORMAT: $message = sprintf('The format length must be between 1-%d.', Packet::MAX_FORMAT); break;
@@ -26,22 +26,21 @@ switch ($error)
2626
case FormModel::ERROR_OUTOFBOUNDS_REMARKS: $message = sprintf('The remarks length must be between 0-%d.', Packet::MAX_REMARKS); break;
2727
case FormModel::ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID: $message = sprintf('The transport layer must be between 0-%d.', Packet::MAX_TRANSPORT_LAYER_ID); break;
2828
case FormModel::ERROR_OUTOFBOUNDS_USED_BY: $message = 'The products selected as using this packet were invalid.'; break;
29-
case FormModel::ERROR_SUCCESS: $message = 'The packet has been created successfully!'; break;
3029
default: $message = $error;
3130
}
3231
$form_fields = $this->getContext()->form_fields;
3332
$form_products = $this->getContext()->products;
3433
$packet = $this->getContext()->packet;
3534
require('./header.inc.phtml');
3635
echo '<div class="container">' . PHP_EOL;
37-
if ($error === FormModel::ERROR_ACL_DENIED)
36+
if ($error === FormModel::ERROR_ACL_NOT_SET || $error === FormModel::ERROR_NOT_LOGGED_IN)
3837
{
3938
require('./LoginRequired.inc.phtml');
4039
}
41-
else if ($error === FormModel::ERROR_SUCCESS)
40+
else if ($error === false)
4241
{
4342
printf('<h1 class="text-success">%s</h1>%s', $title, PHP_EOL);
44-
printf('<div class="alert alert-success"><p class="mb-0">%s</p></div>%s', $message, PHP_EOL);
43+
printf('<div class="alert alert-success"><p class="mb-0">The packet has been created successfully!</p></div>%s', PHP_EOL);
4544
printf('<div class="text-center">%s', PHP_EOL);
4645
printf('<a class="btn btn-primary" href="javascript:history.go(-1);">Back</a>%s', PHP_EOL);
4746
if (isset($packet))

src/Templates/Packet/Delete.phtml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
22
namespace BNETDocs\Templates\Packet;
3+
use \BNETDocs\Models\Packet\Delete as DeleteModel;
34
use \CarlBennett\MVC\Libraries\Pair;
45
$title = 'Delete Packet';
56
$description = 'This form allows an individual to delete a packet.';
@@ -8,23 +9,23 @@ $this->opengraph->attach(new Pair('type', 'article'));
89
$error = $this->getContext()->error;
910
switch ($error)
1011
{
11-
case 'ACL_NOT_SET': $message = 'You do not have the privilege to delete packets.'; break;
12-
case 'NOT_FOUND': $message = 'Cannot find packet by that id.'; break;
13-
case 'NOT_LOGGED_IN': $message = 'You must be logged in to delete packets.'; break;
14-
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
12+
case DeleteModel::ERROR_ACL_NOT_SET: $message = 'You do not have the privilege to delete packets.'; break;
13+
case DeleteModel::ERROR_INTERNAL: $message = 'An internal error occurred. Try again later.'; break;
14+
case DeleteModel::ERROR_NOT_FOUND: $message = 'The requested packet does not exist or could not be found.'; break;
15+
case DeleteModel::ERROR_NOT_LOGGED_IN: $message = 'You must be logged in to delete packets.'; break;
1516
default: $message = $error;
1617
}
17-
$id = filter_var($this->getContext()->id, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
18+
$packet_id = filter_var($this->getContext()->packet_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
1819
require('./header.inc.phtml'); ?>
1920
<div class="container">
2021
<? if (is_null($error)) { ?>
2122
<h1 class="text-danger"><?=$title?></h1>
2223
<p class="text-danger"><?=$description?></p>
23-
<form method="POST" action="?id=<?=$id?>">
24+
<form method="POST" action="?id=<?=$packet_id?>">
2425
<div class="alert alert-danger">
2526
<p class="mb-0">Are you sure you wish to delete this packet?</p>
2627
</div>
27-
<pre><code class="language-objectivec"><?=filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></code></pre>
28+
<pre><code class="language-objectivec"><?=filter_var($this->getContext()->label, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></code></pre>
2829
<input class="btn btn-danger" type="submit" value="Delete Packet" tabindex="2" autofocus="autofocus"/>
2930
</form>
3031
<? } else if ($error === false) { ?>

0 commit comments

Comments
 (0)