Skip to content

Commit 585f658

Browse files
committed
Refactor packet form, add app and transport layers
1 parent f6a5288 commit 585f658

File tree

7 files changed

+120
-11
lines changed

7 files changed

+120
-11
lines changed

src/controllers/Packet/Create.php

+44
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ public function &run(Router &$router, View &$view, array &$args)
4242
$model->products = Product::getAllProducts();
4343
$model->packet = new Packet(null);
4444

45+
self::assignDefault($model->form_fields, 'application_layer', $model->packet->getApplicationLayerId());
46+
self::assignDefault($model->form_fields, 'deprecated', $model->packet->isDeprecated());
47+
self::assignDefault($model->form_fields, 'packet_id', $model->packet->getPacketId(true));
48+
self::assignDefault($model->form_fields, 'name', $model->packet->getName());
49+
self::assignDefault($model->form_fields, 'format', $model->packet->getFormat());
50+
self::assignDefault($model->form_fields, 'remarks', $model->packet->getRemarks(false));
51+
self::assignDefault($model->form_fields, 'research', $model->packet->isInResearch());
52+
self::assignDefault($model->form_fields, 'markdown', $model->packet->isMarkdown());
53+
self::assignDefault($model->form_fields, 'published', $model->packet->isPublished());
54+
self::assignDefault($model->form_fields, 'transport_layer', $model->packet->getTransportLayerId());
55+
56+
if ($router->getRequestMethod() == 'GET')
57+
{
58+
self::assignDefault($model->form_fields, 'used_by', Product::getProductsFromIds($model->packet->getUsedBy()));
59+
}
60+
4561
if ($router->getRequestMethod() == 'POST')
4662
{
4763
$this->handlePost($model);
@@ -76,8 +92,15 @@ public function &run(Router &$router, View &$view, array &$args)
7692
return $model;
7793
}
7894

95+
protected static function assignDefault(&$form_fields, $key, $value)
96+
{
97+
if (isset($form_fields[$key])) return;
98+
$form_fields[$key] = $value;
99+
}
100+
79101
protected function handlePost(FormModel &$model)
80102
{
103+
$application_layer = $model->form_fields['application_layer'] ?? null;
81104
$deprecated = $model->form_fields['deprecated'] ?? null;
82105
$direction = $model->form_fields['direction'] ?? null;
83106
$format = $model->form_fields['format'] ?? null;
@@ -87,8 +110,19 @@ protected function handlePost(FormModel &$model)
87110
$published = $model->form_fields['published'] ?? null;
88111
$remarks = $model->form_fields['remarks'] ?? null;
89112
$research = $model->form_fields['research'] ?? null;
113+
$transport_layer = $model->form_fields['transport_layer'] ?? null;
90114
$used_by = $model->form_fields['used_by'] ?? [];
91115

116+
try
117+
{
118+
$model->packet->setApplicationLayerId($application_layer);
119+
}
120+
catch (OutOfBoundsException $e)
121+
{
122+
$model->error = FormModel::ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID;
123+
return;
124+
}
125+
92126
try
93127
{
94128
$model->packet->setDirection((int) $direction);
@@ -144,6 +178,16 @@ protected function handlePost(FormModel &$model)
144178
return;
145179
}
146180

181+
try
182+
{
183+
$model->packet->setTransportLayerId($transport_layer);
184+
}
185+
catch (OutOfBoundsException $e)
186+
{
187+
$model->error = FormModel::ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID;
188+
return;
189+
}
190+
147191
try
148192
{
149193
$model->packet->setUsedBy($used_by);

src/controllers/Packet/Edit.php

+26
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function &run(Router &$router, View &$view, array &$args)
5656

5757
$model->comments = Comment::getAll(Comment::PARENT_TYPE_PACKET, $id);
5858

59+
self::assignDefault($model->form_fields, 'application_layer', $model->packet->getApplicationLayerId());
5960
self::assignDefault($model->form_fields, 'deprecated', $model->packet->isDeprecated());
6061
self::assignDefault($model->form_fields, 'packet_id', $model->packet->getPacketId(true));
6162
self::assignDefault($model->form_fields, 'name', $model->packet->getName());
@@ -64,6 +65,7 @@ public function &run(Router &$router, View &$view, array &$args)
6465
self::assignDefault($model->form_fields, 'research', $model->packet->isInResearch());
6566
self::assignDefault($model->form_fields, 'markdown', $model->packet->isMarkdown());
6667
self::assignDefault($model->form_fields, 'published', $model->packet->isPublished());
68+
self::assignDefault($model->form_fields, 'transport_layer', $model->packet->getTransportLayerId());
6769

6870
if ($router->getRequestMethod() == 'GET')
6971
{
@@ -82,6 +84,7 @@ public function &run(Router &$router, View &$view, array &$args)
8284
$model->active_user->getId(),
8385
getenv('REMOTE_ADDR'),
8486
json_encode([
87+
'application_layer' => $model->packet->getApplicationLayer()->getLabel(),
8588
'created_dt' => $model->packet->getCreatedDateTime(),
8689
'deprecated' => $model->packet->isDeprecated(),
8790
'draft' => !$model->packet->isPublished(),
@@ -94,6 +97,7 @@ public function &run(Router &$router, View &$view, array &$args)
9497
'owner' => $model->packet->getUser(),
9598
'remarks' => $model->packet->getRemarks(false),
9699
'research' => $model->packet->isInResearch(),
100+
'transport_layer' => $model->packet->getTransportLayer()->getLabel(),
97101
'used_by' => $model->packet->getUsedBy(),
98102
])
99103
);
@@ -112,6 +116,7 @@ protected static function assignDefault(&$form_fields, $key, $value)
112116

113117
protected function handlePost(FormModel &$model)
114118
{
119+
$application_layer = $model->form_fields['application_layer'] ?? null;
115120
$deprecated = $model->form_fields['deprecated'] ?? null;
116121
$direction = $model->form_fields['direction'] ?? null;
117122
$format = $model->form_fields['format'] ?? null;
@@ -121,8 +126,19 @@ protected function handlePost(FormModel &$model)
121126
$published = $model->form_fields['published'] ?? null;
122127
$remarks = $model->form_fields['remarks'] ?? null;
123128
$research = $model->form_fields['research'] ?? null;
129+
$transport_layer = $model->form_fields['transport_layer'] ?? null;
124130
$used_by = $model->form_fields['used_by'] ?? [];
125131

132+
try
133+
{
134+
$model->packet->setApplicationLayerId($application_layer);
135+
}
136+
catch (OutOfBoundsException $e)
137+
{
138+
$model->error = FormModel::ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID;
139+
return;
140+
}
141+
126142
try
127143
{
128144
$model->packet->setDirection((int) $direction);
@@ -178,6 +194,16 @@ protected function handlePost(FormModel &$model)
178194
return;
179195
}
180196

197+
try
198+
{
199+
$model->packet->setTransportLayerId($transport_layer);
200+
}
201+
catch (OutOfBoundsException $e)
202+
{
203+
$model->error = FormModel::ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID;
204+
return;
205+
}
206+
181207
try
182208
{
183209
$model->packet->setUsedBy($used_by);

src/libraries/Packet.php

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Packet implements IDatabaseObject, JsonSerializable
3636

3737
// Maximum SQL field lengths, alter as appropriate
3838
const MAX_APPLICATION_LAYER_ID = 0x7FFFFFFFFFFFFFFF;
39+
const MAX_DIRECTION = 0x7FFFFFFFFFFFFFFF;
3940
const MAX_EDITED_COUNT = 0x7FFFFFFFFFFFFFFF;
4041
const MAX_FORMAT = 0xFFFF;
4142
const MAX_ID = 0x7FFFFFFFFFFFFFFF;

src/models/Packet/Form.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ class Form extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
77
const ERROR_INTERNAL = 'INTERNAL';
88
const ERROR_NONE = 'NONE';
99
const ERROR_NOT_FOUND = 'NOT_FOUND';
10+
const ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID = 'OUTOFBOUNDS_APPLICATION_LAYER_ID';
1011
const ERROR_OUTOFBOUNDS_DIRECTION = 'OUTOFBOUNDS_DIRECTION';
1112
const ERROR_OUTOFBOUNDS_FORMAT = 'OUTOFBOUNDS_FORMAT';
1213
const ERROR_OUTOFBOUNDS_ID = 'OUTOFBOUNDS_ID';
1314
const ERROR_OUTOFBOUNDS_NAME = 'OUTOFBOUNDS_NAME';
1415
const ERROR_OUTOFBOUNDS_PACKET_ID = 'OUTOFBOUNDS_PACKET_ID';
1516
const ERROR_OUTOFBOUNDS_REMARKS = 'OUTOFBOUNDS_REMARKS';
17+
const ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID = 'OUTOFBOUNDS_TRANSPORT_LAYER_ID';
1618
const ERROR_OUTOFBOUNDS_USED_BY = 'OUTOFBOUNDS_USED_BY';
1719
const ERROR_SUCCESS = 'SUCCESS';
1820

src/templates/Packet/Create.phtml

+3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ switch ($error)
1717
case FormModel::ERROR_INTERNAL: $message = 'An internal error occurred. Try again later.'; break;
1818
case FormModel::ERROR_NONE: $message = null; break;
1919
case FormModel::ERROR_NOT_FOUND: $message = 'The requested packet does not exist or could not be found.'; break;
20+
case FormModel::ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID: $message = sprintf('The application layer must be between 0-%d.', Packet::MAX_APPLICATION_LAYER_ID); break;
21+
case FormModel::ERROR_OUTOFBOUNDS_DIRECTION: $message = sprintf('The direction must be between 0-%d.', Packet::MAX_DIRECTION); break;
2022
case FormModel::ERROR_OUTOFBOUNDS_FORMAT: $message = sprintf('The format length must be between 1-%d.', Packet::MAX_FORMAT); break;
2123
case FormModel::ERROR_OUTOFBOUNDS_ID: $message = sprintf('The id must be between 0-%d.', Packet::MAX_ID); break;
2224
case FormModel::ERROR_OUTOFBOUNDS_NAME: $message = sprintf('The name length must be between 1-%d.', Packet::MAX_NAME); break;
2325
case FormModel::ERROR_OUTOFBOUNDS_PACKET_ID: $message = sprintf('The packet id must be between 0-%d.', Packet::MAX_PACKET_ID); break;
2426
case FormModel::ERROR_OUTOFBOUNDS_REMARKS: $message = sprintf('The remarks length must be between 0-%d.', Packet::MAX_REMARKS); break;
27+
case FormModel::ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID: $message = sprintf('The transport layer must be between 0-%d.', Packet::MAX_TRANSPORT_LAYER_ID); break;
2528
case FormModel::ERROR_OUTOFBOUNDS_USED_BY: $message = 'The products selected as using this packet were invalid.'; break;
2629
case FormModel::ERROR_SUCCESS: $message = 'The packet has been created successfully!'; break;
2730
default: $message = $error;

src/templates/Packet/Edit.phtml

+3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ switch ($error)
1717
case FormModel::ERROR_INTERNAL: $message = 'An internal error occurred. Try again later.'; break;
1818
case FormModel::ERROR_NONE: $message = null; break;
1919
case FormModel::ERROR_NOT_FOUND: $message = 'The requested packet does not exist or could not be found.'; break;
20+
case FormModel::ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID: $message = sprintf('The application layer must be between 0-%d.', Packet::MAX_APPLICATION_LAYER_ID); break;
21+
case FormModel::ERROR_OUTOFBOUNDS_DIRECTION: $message = sprintf('The direction must be between 0-%d.', Packet::MAX_DIRECTION); break;
2022
case FormModel::ERROR_OUTOFBOUNDS_FORMAT: $message = sprintf('The format length must be between 1-%d.', Packet::MAX_FORMAT); break;
2123
case FormModel::ERROR_OUTOFBOUNDS_ID: $message = sprintf('The id must be between 0-%d.', Packet::MAX_ID); break;
2224
case FormModel::ERROR_OUTOFBOUNDS_NAME: $message = sprintf('The name length must be between 1-%d.', Packet::MAX_NAME); break;
2325
case FormModel::ERROR_OUTOFBOUNDS_PACKET_ID: $message = sprintf('The packet id must be between 0-%d.', Packet::MAX_PACKET_ID); break;
2426
case FormModel::ERROR_OUTOFBOUNDS_REMARKS: $message = sprintf('The remarks length must be between 0-%d.', Packet::MAX_REMARKS); break;
27+
case FormModel::ERROR_OUTOFBOUNDS_TRANSPORT_LAYER_ID: $message = sprintf('The transport layer must be between 0-%d.', Packet::MAX_TRANSPORT_LAYER_ID); break;
2528
case FormModel::ERROR_OUTOFBOUNDS_USED_BY: $message = 'The products selected as using this packet were invalid.'; break;
2629
case FormModel::ERROR_SUCCESS: $message = 'The packet has been edited successfully!'; break;
2730
default: $message = $error;

src/templates/Packet/Form.inc.phtml

+41-11
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,64 @@
22
namespace BNETDocs\Templates\Packet;
33
use \BNETDocs\Libraries\Comment;
44
use \BNETDocs\Libraries\Packet;
5+
use \BNETDocs\Libraries\Packet\Application as PacketAppLayer;
6+
use \BNETDocs\Libraries\Packet\Transport as PacketTransportLayer;
57
use \CarlBennett\MVC\Libraries\Common; ?>
68
<form method="POST">
79
<div class="row">
810
<div class="col-lg-3">
911
<div class="form-group">
1012
<label class="font-weight-bold" for="direction">Direction:</label>
11-
<select class="bg-dark border border-primary custom-select text-light" name="direction" id="direction">
12-
<option value="<?=Packet::DIRECTION_CLIENT_SERVER?>"<?=($form_fields['direction'] ?? null === Packet::DIRECTION_CLIENT_SERVER ? ' checked="checked"' : '')?>>Client to Server</option>
13-
<option value="<?=Packet::DIRECTION_SERVER_CLIENT?>"<?=($form_fields['direction'] ?? null === Packet::DIRECTION_SERVER_CLIENT ? ' checked="checked"' : '')?>>Server to Client</option>
14-
<option value="<?=Packet::DIRECTION_PEER_TO_PEER?>"<?=($form_fields['direction'] ?? null === Packet::DIRECTION_PEER_TO_PEER ? ' checked="checked"' : '')?>>Peer to Peer</option>
13+
<select class="bg-dark border border-primary custom-select text-light" name="direction" id="direction" tabindex="1">
14+
<option value="<?=Packet::DIRECTION_CLIENT_SERVER?>"<?=($form_fields['direction'] ?? null === Packet::DIRECTION_CLIENT_SERVER ? ' selected' : '')?>>Client to Server</option>
15+
<option value="<?=Packet::DIRECTION_SERVER_CLIENT?>"<?=($form_fields['direction'] ?? null === Packet::DIRECTION_SERVER_CLIENT ? ' selected' : '')?>>Server to Client</option>
16+
<option value="<?=Packet::DIRECTION_PEER_TO_PEER?>"<?=($form_fields['direction'] ?? null === Packet::DIRECTION_PEER_TO_PEER ? ' selected' : '')?>>Peer to Peer</option>
1517
</select>
1618
</div>
1719
</div><div class="col-lg-3">
1820
<div class="form-group">
1921
<label class="font-weight-bold" for="packet_id">Id: <span class="small text-muted">(supports prefixes like &amp;h, 0x, etc.)</span></label>
20-
<input class="bg-dark border border-primary form-control text-light" type="text" name="packet_id" id="packet_id" placeholder="Enter the message id here" tabindex="1" required autofocus="autofocus" value="<?=filter_var($form_fields['packet_id'] ?? null, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"/>
22+
<input class="bg-dark border border-primary form-control text-light" type="text" name="packet_id" id="packet_id" placeholder="Enter the message id here" tabindex="2" required autofocus="autofocus" value="<?=filter_var($form_fields['packet_id'] ?? null, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"/>
2123
</div>
2224
</div><div class="col-lg-4">
2325
<div class="form-group">
2426
<label class="font-weight-bold" for="name">Name: <span class="small text-muted">(e.g. SID_NULL)</span></label>
25-
<input class="bg-dark border border-primary form-control text-light" type="text" name="name" id="name" placeholder="Enter the message name here" tabindex="2" required value="<?=$form_fields['name'] ?? ''?>"/>
27+
<input class="bg-dark border border-primary form-control text-light" type="text" name="name" id="name" placeholder="Enter the message name here" tabindex="3" required value="<?=$form_fields['name'] ?? ''?>"/>
2628
</div>
2729
</div><div class="col-lg-2">
2830
<div class="form-group">
2931
<label class="font-weight-bold">Options:</label>
30-
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="deprecated" name="deprecated" value="1"<?=($form_fields['deprecated'] ?? null ? ' checked="checked"' : '')?> tabindex="6"/><label class="custom-control-label text-danger" for="deprecated">Deprecated</label></div>
31-
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="research" name="research" value="1"<?=($form_fields['research'] ?? null ? ' checked="checked"' : '')?> tabindex="7"/> <label class="custom-control-label text-warning" for="research">In Research</label></div>
32-
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="published" name="published" value="1"<?=($form_fields['published'] ?? null ? ' checked="checked"' : '')?> tabindex="8"/> <label class="custom-control-label text-success" for="published">Published</label></div>
32+
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="deprecated" name="deprecated" value="1"<?=($form_fields['deprecated'] ?? null ? ' selected' : '')?> tabindex="4"/><label class="custom-control-label text-danger" for="deprecated">Deprecated</label></div>
33+
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="research" name="research" value="1"<?=($form_fields['research'] ?? null ? ' selected' : '')?> tabindex="5"/> <label class="custom-control-label text-warning" for="research">In Research</label></div>
34+
<div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="published" name="published" value="1"<?=($form_fields['published'] ?? null ? ' selected' : '')?> tabindex="6"/> <label class="custom-control-label text-success" for="published">Published</label></div>
35+
</div>
36+
</div>
37+
</div>
38+
<div class="row">
39+
<div class="col-lg-6">
40+
<div class="form-group">
41+
<label class="font-weight-bold" for="transport_layer">Transport layer:</label>
42+
<select class="bg-dark border border-primary custom-select text-light" name="transport_layer" id="transport_layer" tabindex="7">
43+
<? foreach (PacketTransportLayer::getAllAsArray() as $id => $label)
44+
{
45+
printf('<option value="%s"%s>%s (%s)</option>',
46+
$id, ($id === ($form_fields['transport_layer'] ?? null) ? ' selected' : ''), $label[1], $label[0]
47+
);
48+
} ?>
49+
</select>
50+
</div>
51+
</div>
52+
<div class="col-lg-6">
53+
<div class="form-group">
54+
<label class="font-weight-bold" for="application_layer">Application layer:</label>
55+
<select class="bg-dark border border-primary custom-select text-light" name="application_layer" id="application_layer" tabindex="7">
56+
<? foreach (PacketAppLayer::getAllAsArray() as $id => $label)
57+
{
58+
printf('<option value="%s"%s>%s (%s)</option>',
59+
$id, ($id === ($form_fields['application_layer'] ?? null) ? ' selected' : ''), $label[1], $label[0]
60+
);
61+
} ?>
62+
</select>
3363
</div>
3464
</div>
3565
</div>
@@ -48,7 +78,7 @@ use \CarlBennett\MVC\Libraries\Common; ?>
4878
$p = $form_products[$p_i];
4979
$p_id = $p->getBnetProductId();
5080
$p_label = $p->getLabel();
51-
printf('<td><div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="used_by_%s" name="used_by[]" value="%s"%s/><label class="custom-control-label" for="used_by_%s">%s</label></div></td>', $p_id, $p_id, (in_array($p, $form_fields['used_by'] ?? []) ? ' checked="checked"' : ''), $p_id, $p_label);
81+
printf('<td><div class="custom-control custom-switch"><input class="custom-control-input" type="checkbox" id="used_by_%s" name="used_by[]" value="%s"%s/><label class="custom-control-label" for="used_by_%s">%s</label></div></td>', $p_id, $p_id, (in_array($p, $form_fields['used_by'] ?? []) ? ' selected' : ''), $p_id, $p_label);
5282
if ($p_i % 2 === 1) echo '</tr>';
5383
} ?>
5484
</tbody>
@@ -59,7 +89,7 @@ use \CarlBennett\MVC\Libraries\Common; ?>
5989
<span class="float-right">
6090
<div class="custom-control custom-switch">
6191
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="5"
62-
title="Use markdown or use raw HTML" value="1"<?=($form_fields['markdown'] ?? null ? ' checked="checked"' : '')?>/>
92+
title="Use markdown or use raw HTML" value="1"<?=($form_fields['markdown'] ?? null ? ' selected' : '')?>/>
6393
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
6494
</div>
6595
</span>

0 commit comments

Comments
 (0)