Skip to content

Commit 84e8c47

Browse files
committed
Add packet brief field
1 parent 39ab46d commit 84e8c47

File tree

8 files changed

+103
-12
lines changed

8 files changed

+103
-12
lines changed

src/controllers/Packet/Create.php

+13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function &run(Router &$router, View &$view, array &$args)
4343
$model->packet = new Packet(null);
4444

4545
self::assignDefault($model->form_fields, 'application_layer', $model->packet->getApplicationLayerId());
46+
self::assignDefault($model->form_fields, 'brief', $model->packet->getBrief(false));
4647
self::assignDefault($model->form_fields, 'deprecated', $model->packet->isDeprecated());
4748
self::assignDefault($model->form_fields, 'direction', $model->packet->getDirection());
4849
self::assignDefault($model->form_fields, 'format', $model->packet->getFormat());
@@ -72,6 +73,7 @@ public function &run(Router &$router, View &$view, array &$args)
7273
getenv('REMOTE_ADDR'),
7374
json_encode([
7475
'application_layer' => $model->packet->getApplicationLayer()->getLabel(),
76+
'brief' => $model->packet->getBrief(false),
7577
'created_dt' => $model->packet->getCreatedDateTime(),
7678
'deprecated' => $model->packet->isDeprecated(),
7779
'direction' => $model->packet->getDirectionLabel(),
@@ -105,6 +107,7 @@ protected static function assignDefault(&$form_fields, $key, $value)
105107
protected function handlePost(FormModel &$model)
106108
{
107109
$application_layer = $model->form_fields['application_layer'] ?? null;
110+
$brief = $model->form_fields['brief'] ?? null;
108111
$deprecated = $model->form_fields['deprecated'] ?? null;
109112
$direction = $model->form_fields['direction'] ?? null;
110113
$format = $model->form_fields['format'] ?? null;
@@ -127,6 +130,16 @@ protected function handlePost(FormModel &$model)
127130
return;
128131
}
129132

133+
try
134+
{
135+
$model->packet->setBrief($brief);
136+
}
137+
catch (OutOfBoundsException $e)
138+
{
139+
$model->error = FormModel::ERROR_OUTOFBOUNDS_BRIEF;
140+
return;
141+
}
142+
130143
try
131144
{
132145
$model->packet->setDirection((int) $direction);

src/controllers/Packet/Edit.php

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function &run(Router &$router, View &$view, array &$args)
5757
$model->comments = Comment::getAll(Comment::PARENT_TYPE_PACKET, $id);
5858

5959
self::assignDefault($model->form_fields, 'application_layer', $model->packet->getApplicationLayerId());
60+
self::assignDefault($model->form_fields, 'brief', $model->packet->getBrief(false));
6061
self::assignDefault($model->form_fields, 'deprecated', $model->packet->isDeprecated());
6162
self::assignDefault($model->form_fields, 'direction', $model->packet->getDirection());
6263
self::assignDefault($model->form_fields, 'format', $model->packet->getFormat());
@@ -86,6 +87,7 @@ public function &run(Router &$router, View &$view, array &$args)
8687
getenv('REMOTE_ADDR'),
8788
json_encode([
8889
'application_layer' => $model->packet->getApplicationLayer()->getLabel(),
90+
'brief' => $model->packet->getBrief(false),
8991
'created_dt' => $model->packet->getCreatedDateTime(),
9092
'deprecated' => $model->packet->isDeprecated(),
9193
'direction' => $model->packet->getDirectionLabel(),
@@ -119,6 +121,7 @@ protected static function assignDefault(&$form_fields, $key, $value)
119121
protected function handlePost(FormModel &$model)
120122
{
121123
$application_layer = $model->form_fields['application_layer'] ?? null;
124+
$brief = $model->form_fields['brief'] ?? null;
122125
$deprecated = $model->form_fields['deprecated'] ?? null;
123126
$direction = $model->form_fields['direction'] ?? null;
124127
$format = $model->form_fields['format'] ?? null;
@@ -141,6 +144,16 @@ protected function handlePost(FormModel &$model)
141144
return;
142145
}
143146

147+
try
148+
{
149+
$model->packet->setBrief($brief);
150+
}
151+
catch (OutOfBoundsException $e)
152+
{
153+
$model->error = FormModel::ERROR_OUTOFBOUNDS_BRIEF;
154+
return;
155+
}
156+
144157
try
145158
{
146159
$model->packet->setDirection((int) $direction);

src/libraries/Packet.php

+38-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_BRIEF = 191;
3940
const MAX_DIRECTION = 0x7FFFFFFFFFFFFFFF;
4041
const MAX_EDITED_COUNT = 0x7FFFFFFFFFFFFFFF;
4142
const MAX_FORMAT = 0xFFFF;
@@ -57,6 +58,7 @@ class Packet implements IDatabaseObject, JsonSerializable
5758
private $_id;
5859

5960
protected $application_layer_id;
61+
protected $brief;
6062
protected $created_datetime;
6163
protected $direction;
6264
protected $edited_count;
@@ -110,6 +112,7 @@ public function allocate()
110112
}
111113

112114
$this->setApplicationLayerId(self::DEFAULT_APPLICATION_LAYER_ID);
115+
$this->setBrief('');
113116
$this->setCreatedDateTime(new DateTime('now'));
114117
$this->setDirection(self::DEFAULT_DIRECTION);
115118
$this->setEditedCount(0);
@@ -136,6 +139,7 @@ public function allocate()
136139
`id`,
137140
`options_bitmask`,
138141
`packet_application_layer_id`,
142+
`packet_brief`,
139143
`packet_direction_id`,
140144
`packet_format`,
141145
`packet_id`,
@@ -188,6 +192,7 @@ protected function allocateObject(StdClass $value)
188192
$tz = new DateTimeZone(self::TZ_SQL);
189193

190194
$this->setApplicationLayerId($value->packet_application_layer_id);
195+
$this->setBrief($value->packet_brief);
191196
$this->setCreatedDateTime(new DateTime($value->created_datetime, $tz));
192197
$this->setDirection($value->packet_direction_id);
193198
$this->setEditedCount($value->edited_count);
@@ -223,6 +228,7 @@ public function commit()
223228
`id`,
224229
`options_bitmask`,
225230
`packet_application_layer_id`,
231+
`packet_brief`,
226232
`packet_direction_id`,
227233
`packet_format`,
228234
`packet_id`,
@@ -231,14 +237,15 @@ public function commit()
231237
`packet_transport_layer_id`,
232238
`user_id`
233239
) VALUES (
234-
:c_dt, :e_c, :e_dt, :id, :opts, :app_id, :d, :f, :pid, :n, :r, :tr_id, :uid
240+
:c_dt, :e_c, :e_dt, :id, :opts, :app_id, :b, :d, :f, :pid, :n, :r, :tr_id, :uid
235241
) ON DUPLICATE KEY UPDATE
236242
`created_datetime` = :c_dt,
237243
`edited_count` = :e_c,
238244
`edited_datetime` = :e_dt,
239245
`id` = :id,
240246
`options_bitmask` = :opts,
241247
`packet_application_layer_id` = :app_id,
248+
`packet_brief` = :b,
242249
`packet_direction_id` = :d,
243250
`packet_format` = :f,
244251
`packet_id` = :pid,
@@ -255,6 +262,7 @@ public function commit()
255262
);
256263

257264
$q->bindParam(':app_id', $this->application_layer_id, PDO::PARAM_INT);
265+
$q->bindParam(':b', $this->brief, (is_null($this->brief) ? PDO::PARAM_NULL : PDO::PARAM_STR));
258266
$q->bindParam(':c_dt', $created_datetime, PDO::PARAM_STR);
259267
$q->bindParam(':d', $this->direction, PDO::PARAM_INT);
260268
$q->bindParam(':e_c', $this->edited_count, PDO::PARAM_INT);
@@ -389,6 +397,17 @@ public function getApplicationLayerId()
389397
return $this->application_layer_id;
390398
}
391399

400+
public function getBrief(bool $format)
401+
{
402+
if (!($format && $this->getOption(self::OPTION_MARKDOWN)))
403+
{
404+
return $this->brief;
405+
}
406+
407+
$md = new Parsedown();
408+
return $md->text($this->brief);
409+
}
410+
392411
public function getCreatedDateTime()
393412
{
394413
return $this->created_datetime;
@@ -626,6 +645,7 @@ public function jsonSerialize()
626645
'id' => $this->getId(),
627646
'options_bitmask' => $this->getOptions(),
628647
'packet_application_layer_id' => $this->getApplicationLayerId(),
648+
'packet_brief' => $this->getBrief(false),
629649
'packet_direction_id' => $this->getDirection(),
630650
'packet_format' => $this->getFormat(),
631651
'packet_id' => $this->getPacketId(),
@@ -653,6 +673,23 @@ public function setApplicationLayerId(int $value)
653673
$this->application_layer_id = $value;
654674
}
655675

676+
/**
677+
* Sets the brief description of this packet.
678+
*
679+
* @param string $value The brief description.
680+
*/
681+
public function setBrief(string $value)
682+
{
683+
if (strlen($value) > self::MAX_BRIEF)
684+
{
685+
throw new OutOfBoundsException(sprintf(
686+
'value must be between 0-%d characters', self::MAX_BRIEF
687+
));
688+
}
689+
690+
$this->brief = $value;
691+
}
692+
656693
/**
657694
* Sets the Date and Time this Packet was created.
658695
*

src/models/Packet/Form.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Form extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
88
const ERROR_NONE = 'NONE';
99
const ERROR_NOT_FOUND = 'NOT_FOUND';
1010
const ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID = 'OUTOFBOUNDS_APPLICATION_LAYER_ID';
11+
const ERROR_OUTOFBOUNDS_BRIEF = 'OUTOFBOUNDS_BRIEF';
1112
const ERROR_OUTOFBOUNDS_DIRECTION = 'OUTOFBOUNDS_DIRECTION';
1213
const ERROR_OUTOFBOUNDS_FORMAT = 'OUTOFBOUNDS_FORMAT';
1314
const ERROR_OUTOFBOUNDS_ID = 'OUTOFBOUNDS_ID';

src/templates/MarkdownBootstrapFix.inc.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* Adds CSS classes to Markdown output
66
*/
7-
function MarkdownBootstrapFix(string $v, bool $sm = false)
7+
function MarkdownBootstrapFix(string $v, bool $sm = false, bool $lpm = false)
88
{
99
// Tables
1010
$v = str_replace('<table>', '<table class="table table-hover table-markdown ' . ($sm ? 'table-sm ' : '') . 'table-striped">', $v);
@@ -21,5 +21,8 @@ function MarkdownBootstrapFix(string $v, bool $sm = false)
2121
// Code Blocks
2222
$v = str_replace('<pre><code', '<pre class="border border-primary overflow-auto pre-scrollable rounded bg-dark text-light"><code', $v);
2323

24+
// Last Paragraph Margin
25+
if ($lpm) $v = preg_replace('/(?:<p>(.*)<\/p>)$/i', '<p class="mb-0">$1</p>', $v, 1);
26+
2427
return $v;
2528
}

src/templates/Packet/Form.inc.phtml

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use \CarlBennett\MVC\Libraries\Common; ?>
1919
</div><div class="col-lg-3">
2020
<div class="form-group">
2121
<label class="font-weight-bold" for="packet_id">Id: <span class="small text-muted">(supports prefixes like &amp;h, 0x, etc.)</span></label>
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)?>"/>
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)?>"/>
2323
</div>
2424
</div><div class="col-lg-4">
2525
<div class="form-group">
@@ -85,18 +85,22 @@ use \CarlBennett\MVC\Libraries\Common; ?>
8585
</table>
8686
</div>
8787
<div class="form-group">
88-
<label class="font-weight-bold" for="remarks">Remarks: <span class="small text-muted">(a description of what, when, where, and why this packet is used, and any related info)</span></label>
88+
<label class="font-weight-bold" for="brief">Brief: <span class="small text-muted">(a brief description, for use in margins around the site &ndash; if blank, the first part from the remarks are used instead)</span></label>
89+
<input class="bg-dark border border-primary form-control text-light" type="text" name="brief" id="brief" placeholder="Enter the optional brief description here" tabindex="10" value="<?=filter_var($form_fields['brief'] ?? null, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"/>
90+
</div>
91+
<div class="form-group">
92+
<label class="font-weight-bold" for="remarks">Remarks: <span class="small text-muted">(a full description of what, when, where, and why this packet is used, and any related info)</span></label>
8993
<span class="float-right">
9094
<div class="custom-control custom-switch">
91-
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="10"
95+
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="11"
9296
title="Use markdown or use raw HTML" value="1"<?=($form_fields['markdown'] ?? null ? ' checked' : '')?>/>
9397
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
9498
</div>
9599
</span>
96-
<textarea class="bg-dark border border-primary form-control text-light" style="height:200px;" name="remarks" id="remarks" placeholder="Enter the message remarks here" tabindex="11" required><?=filter_var($form_fields['remarks'] ?? null, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></textarea>
100+
<textarea class="bg-dark border border-primary form-control text-light" style="height:200px;" name="remarks" id="remarks" placeholder="Enter the message remarks here" tabindex="12" required><?=filter_var($form_fields['remarks'] ?? null, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></textarea>
97101
</div>
98102
<div class="form-group text-center">
99-
<input class="btn btn-success" type="submit" value="Save" tabindex="12"/>
103+
<input class="btn btn-success" type="submit" value="Save" tabindex="13"/>
100104
</div>
101105
</form>
102106
<? if (isset($comments)) { $comment_parent_type = Comment::PARENT_TYPE_PACKET; $comment_parent_id = $form_fields['packet_id'] ?? null; require('./Comment/Section.inc.phtml'); } ?>

src/templates/Packet/Index.phtml

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ $form_order_by = [
1818
'user-id-asc' => 'User Id (Ascending)',
1919
'user-id-desc' => 'User Id (Descending)',
2020
];
21+
require_once('./MarkdownBootstrapFix.inc.php');
2122
require('./header.inc.phtml'); ?>
2223
<div class="container">
2324
<h2><?=$title?></h2>
@@ -62,19 +63,30 @@ require('./header.inc.phtml'); ?>
6263
$avatar_url = $user->getAvatarURI(22);
6364
$user_url = $user->getURI();
6465
}
66+
$brief = $packet->getBrief(true);
6567
$deprecated = $packet->isDeprecated();
6668
$packet_id = $packet->getPacketId(true);
6769
$published = $packet->isPublished();
70+
$remarks = $packet->getRemarks(true);
6871
$research = $packet->isInResearch();
6972

73+
if (!empty($brief))
74+
{
75+
$brief = \BNETDocs\Templates\MarkdownBootstrapFix($brief, true, true);
76+
}
77+
else
78+
{
79+
$brief = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($remarks, FILTER_SANITIZE_STRING)), "\n", 128), '. ', 128), '.');
80+
}
81+
7082
ob_start();
7183
if ($deprecated) require('./Deprecated.inc.phtml');
7284
if ($research) require('./InResearch.inc.phtml');
7385
if (!$published) require('./Draft.inc.phtml');
7486
$tpl_packet_flags = ob_get_clean();
7587
if (!empty($tpl_packet_flags)) $tpl_packet_flags = ' ' . $tpl_packet_flags; ?>
7688
<tr>
77-
<td><strong><a href="<?=$packet->getURI()?>"><?=filter_var($packet->getLabel(), FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></a></strong><?=$tpl_packet_flags?><br/><span class="text-muted"><?=rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($packet->getRemarks(true), FILTER_SANITIZE_STRING)), "\n", 128), '. ', 128), '.')?></span></td>
89+
<td><strong><a href="<?=$packet->getURI()?>"><?=filter_var($packet->getLabel(), FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></a></strong><?=$tpl_packet_flags?><br/><span class="text-muted"><?=$brief?></span></td>
7890
<td><? if ($user) { ?><a href="<? echo $user_url; ?>"><img class="avatar" src="<? echo $avatar_url; ?>"/>&nbsp;<? echo filter_var($user->getName(), FILTER_SANITIZE_STRING); ?></a><? } else { ?>Anonymous<? } ?></td>
7991
</tr>
8092
<? } ?>

src/templates/Packet/View.phtml

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,22 @@ if ($object)
1515
{
1616
$created_dt = $object->getCreatedDateTime();
1717
$deprecated = $object->isDeprecated();
18-
$description = Common::stripUpTo(trim(filter_var($object->getRemarks(true), FILTER_SANITIZE_STRING)), "\n", 300);
18+
$brief = $object->getBrief(true);
1919
$edited_dt = $object->getEditedDateTime();
2020
$packet_id = $object->getPacketId(true);
21-
$published = $object->isPublished();
22-
$research = $object->isInResearch();
21+
$published = $object->isPublished();
22+
$remarks = $object->getRemarks(true);
23+
$research = $object->isInResearch();
2324
$title = $object->getName();
2425
$url = $object->getURI();
2526
$user = $object->getUser();
2627

28+
$description = filter_var($brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
29+
if (empty($description))
30+
{
31+
$description = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($remarks, FILTER_SANITIZE_STRING)), "\n", 300), '. ', 300), '.');
32+
}
33+
2734
if ($user)
2835
{
2936
$user_name = $user->getName();
@@ -88,10 +95,11 @@ require('./header.inc.phtml'); ?>
8895
<tr><th class="text-right">Message Format:<br/><sup class="text-muted">(does not include protocol header)</sup></th><td>
8996
<pre class="border border-primary overflow-auto rounded bg-dark text-light"><code class="language-objectivec"><?=$object->getFormat()?></code></pre>
9097
</td></tr>
98+
<?=(!empty($brief) ? sprintf('<tr><th class="text-right">Brief:</th><td>%s</td></tr>', \BNETDocs\Templates\MarkdownBootstrapFix($brief, true, true)) : '')?>
9199
</tbody></table>
92100

93101
<h1>Remarks</h1>
94-
<div class="container"><?=\BNETDocs\Templates\MarkdownBootstrapFix($object->getRemarks(true), true)?></div>
102+
<div class="container"><?=\BNETDocs\Templates\MarkdownBootstrapFix($remarks, true, false)?></div>
95103

96104
<div class="card mt-3"><div class="card-body">
97105
<span class="float-right text-muted">

0 commit comments

Comments
 (0)