Skip to content

Commit

Permalink
Add packet brief field
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Jul 21, 2021
1 parent 39ab46d commit 84e8c47
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 12 deletions.
13 changes: 13 additions & 0 deletions src/controllers/Packet/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function &run(Router &$router, View &$view, array &$args)
$model->packet = new Packet(null);

self::assignDefault($model->form_fields, 'application_layer', $model->packet->getApplicationLayerId());
self::assignDefault($model->form_fields, 'brief', $model->packet->getBrief(false));
self::assignDefault($model->form_fields, 'deprecated', $model->packet->isDeprecated());
self::assignDefault($model->form_fields, 'direction', $model->packet->getDirection());
self::assignDefault($model->form_fields, 'format', $model->packet->getFormat());
Expand Down Expand Up @@ -72,6 +73,7 @@ public function &run(Router &$router, View &$view, array &$args)
getenv('REMOTE_ADDR'),
json_encode([
'application_layer' => $model->packet->getApplicationLayer()->getLabel(),
'brief' => $model->packet->getBrief(false),
'created_dt' => $model->packet->getCreatedDateTime(),
'deprecated' => $model->packet->isDeprecated(),
'direction' => $model->packet->getDirectionLabel(),
Expand Down Expand Up @@ -105,6 +107,7 @@ protected static function assignDefault(&$form_fields, $key, $value)
protected function handlePost(FormModel &$model)
{
$application_layer = $model->form_fields['application_layer'] ?? null;
$brief = $model->form_fields['brief'] ?? null;
$deprecated = $model->form_fields['deprecated'] ?? null;
$direction = $model->form_fields['direction'] ?? null;
$format = $model->form_fields['format'] ?? null;
Expand All @@ -127,6 +130,16 @@ protected function handlePost(FormModel &$model)
return;
}

try
{
$model->packet->setBrief($brief);
}
catch (OutOfBoundsException $e)
{
$model->error = FormModel::ERROR_OUTOFBOUNDS_BRIEF;
return;
}

try
{
$model->packet->setDirection((int) $direction);
Expand Down
13 changes: 13 additions & 0 deletions src/controllers/Packet/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function &run(Router &$router, View &$view, array &$args)
$model->comments = Comment::getAll(Comment::PARENT_TYPE_PACKET, $id);

self::assignDefault($model->form_fields, 'application_layer', $model->packet->getApplicationLayerId());
self::assignDefault($model->form_fields, 'brief', $model->packet->getBrief(false));
self::assignDefault($model->form_fields, 'deprecated', $model->packet->isDeprecated());
self::assignDefault($model->form_fields, 'direction', $model->packet->getDirection());
self::assignDefault($model->form_fields, 'format', $model->packet->getFormat());
Expand Down Expand Up @@ -86,6 +87,7 @@ public function &run(Router &$router, View &$view, array &$args)
getenv('REMOTE_ADDR'),
json_encode([
'application_layer' => $model->packet->getApplicationLayer()->getLabel(),
'brief' => $model->packet->getBrief(false),
'created_dt' => $model->packet->getCreatedDateTime(),
'deprecated' => $model->packet->isDeprecated(),
'direction' => $model->packet->getDirectionLabel(),
Expand Down Expand Up @@ -119,6 +121,7 @@ protected static function assignDefault(&$form_fields, $key, $value)
protected function handlePost(FormModel &$model)
{
$application_layer = $model->form_fields['application_layer'] ?? null;
$brief = $model->form_fields['brief'] ?? null;
$deprecated = $model->form_fields['deprecated'] ?? null;
$direction = $model->form_fields['direction'] ?? null;
$format = $model->form_fields['format'] ?? null;
Expand All @@ -141,6 +144,16 @@ protected function handlePost(FormModel &$model)
return;
}

try
{
$model->packet->setBrief($brief);
}
catch (OutOfBoundsException $e)
{
$model->error = FormModel::ERROR_OUTOFBOUNDS_BRIEF;
return;
}

try
{
$model->packet->setDirection((int) $direction);
Expand Down
39 changes: 38 additions & 1 deletion src/libraries/Packet.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Packet implements IDatabaseObject, JsonSerializable

// Maximum SQL field lengths, alter as appropriate
const MAX_APPLICATION_LAYER_ID = 0x7FFFFFFFFFFFFFFF;
const MAX_BRIEF = 191;
const MAX_DIRECTION = 0x7FFFFFFFFFFFFFFF;
const MAX_EDITED_COUNT = 0x7FFFFFFFFFFFFFFF;
const MAX_FORMAT = 0xFFFF;
Expand All @@ -57,6 +58,7 @@ class Packet implements IDatabaseObject, JsonSerializable
private $_id;

protected $application_layer_id;
protected $brief;
protected $created_datetime;
protected $direction;
protected $edited_count;
Expand Down Expand Up @@ -110,6 +112,7 @@ public function allocate()
}

$this->setApplicationLayerId(self::DEFAULT_APPLICATION_LAYER_ID);
$this->setBrief('');
$this->setCreatedDateTime(new DateTime('now'));
$this->setDirection(self::DEFAULT_DIRECTION);
$this->setEditedCount(0);
Expand All @@ -136,6 +139,7 @@ public function allocate()
`id`,
`options_bitmask`,
`packet_application_layer_id`,
`packet_brief`,
`packet_direction_id`,
`packet_format`,
`packet_id`,
Expand Down Expand Up @@ -188,6 +192,7 @@ protected function allocateObject(StdClass $value)
$tz = new DateTimeZone(self::TZ_SQL);

$this->setApplicationLayerId($value->packet_application_layer_id);
$this->setBrief($value->packet_brief);
$this->setCreatedDateTime(new DateTime($value->created_datetime, $tz));
$this->setDirection($value->packet_direction_id);
$this->setEditedCount($value->edited_count);
Expand Down Expand Up @@ -223,6 +228,7 @@ public function commit()
`id`,
`options_bitmask`,
`packet_application_layer_id`,
`packet_brief`,
`packet_direction_id`,
`packet_format`,
`packet_id`,
Expand All @@ -231,14 +237,15 @@ public function commit()
`packet_transport_layer_id`,
`user_id`
) VALUES (
:c_dt, :e_c, :e_dt, :id, :opts, :app_id, :d, :f, :pid, :n, :r, :tr_id, :uid
:c_dt, :e_c, :e_dt, :id, :opts, :app_id, :b, :d, :f, :pid, :n, :r, :tr_id, :uid
) ON DUPLICATE KEY UPDATE
`created_datetime` = :c_dt,
`edited_count` = :e_c,
`edited_datetime` = :e_dt,
`id` = :id,
`options_bitmask` = :opts,
`packet_application_layer_id` = :app_id,
`packet_brief` = :b,
`packet_direction_id` = :d,
`packet_format` = :f,
`packet_id` = :pid,
Expand All @@ -255,6 +262,7 @@ public function commit()
);

$q->bindParam(':app_id', $this->application_layer_id, PDO::PARAM_INT);
$q->bindParam(':b', $this->brief, (is_null($this->brief) ? PDO::PARAM_NULL : PDO::PARAM_STR));
$q->bindParam(':c_dt', $created_datetime, PDO::PARAM_STR);
$q->bindParam(':d', $this->direction, PDO::PARAM_INT);
$q->bindParam(':e_c', $this->edited_count, PDO::PARAM_INT);
Expand Down Expand Up @@ -389,6 +397,17 @@ public function getApplicationLayerId()
return $this->application_layer_id;
}

public function getBrief(bool $format)
{
if (!($format && $this->getOption(self::OPTION_MARKDOWN)))
{
return $this->brief;
}

$md = new Parsedown();
return $md->text($this->brief);
}

public function getCreatedDateTime()
{
return $this->created_datetime;
Expand Down Expand Up @@ -626,6 +645,7 @@ public function jsonSerialize()
'id' => $this->getId(),
'options_bitmask' => $this->getOptions(),
'packet_application_layer_id' => $this->getApplicationLayerId(),
'packet_brief' => $this->getBrief(false),
'packet_direction_id' => $this->getDirection(),
'packet_format' => $this->getFormat(),
'packet_id' => $this->getPacketId(),
Expand Down Expand Up @@ -653,6 +673,23 @@ public function setApplicationLayerId(int $value)
$this->application_layer_id = $value;
}

/**
* Sets the brief description of this packet.
*
* @param string $value The brief description.
*/
public function setBrief(string $value)
{
if (strlen($value) > self::MAX_BRIEF)
{
throw new OutOfBoundsException(sprintf(
'value must be between 0-%d characters', self::MAX_BRIEF
));
}

$this->brief = $value;
}

/**
* Sets the Date and Time this Packet was created.
*
Expand Down
1 change: 1 addition & 0 deletions src/models/Packet/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Form extends \BNETDocs\Models\ActiveUser implements \JsonSerializable
const ERROR_NONE = 'NONE';
const ERROR_NOT_FOUND = 'NOT_FOUND';
const ERROR_OUTOFBOUNDS_APPLICATION_LAYER_ID = 'OUTOFBOUNDS_APPLICATION_LAYER_ID';
const ERROR_OUTOFBOUNDS_BRIEF = 'OUTOFBOUNDS_BRIEF';
const ERROR_OUTOFBOUNDS_DIRECTION = 'OUTOFBOUNDS_DIRECTION';
const ERROR_OUTOFBOUNDS_FORMAT = 'OUTOFBOUNDS_FORMAT';
const ERROR_OUTOFBOUNDS_ID = 'OUTOFBOUNDS_ID';
Expand Down
5 changes: 4 additions & 1 deletion src/templates/MarkdownBootstrapFix.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Adds CSS classes to Markdown output
*/
function MarkdownBootstrapFix(string $v, bool $sm = false)
function MarkdownBootstrapFix(string $v, bool $sm = false, bool $lpm = false)
{
// Tables
$v = str_replace('<table>', '<table class="table table-hover table-markdown ' . ($sm ? 'table-sm ' : '') . 'table-striped">', $v);
Expand All @@ -21,5 +21,8 @@ function MarkdownBootstrapFix(string $v, bool $sm = false)
// Code Blocks
$v = str_replace('<pre><code', '<pre class="border border-primary overflow-auto pre-scrollable rounded bg-dark text-light"><code', $v);

// Last Paragraph Margin
if ($lpm) $v = preg_replace('/(?:<p>(.*)<\/p>)$/i', '<p class="mb-0">$1</p>', $v, 1);

return $v;
}
14 changes: 9 additions & 5 deletions src/templates/Packet/Form.inc.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use \CarlBennett\MVC\Libraries\Common; ?>
</div><div class="col-lg-3">
<div class="form-group">
<label class="font-weight-bold" for="packet_id">Id: <span class="small text-muted">(supports prefixes like &amp;h, 0x, etc.)</span></label>
<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)?>"/>
<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)?>"/>
</div>
</div><div class="col-lg-4">
<div class="form-group">
Expand Down Expand Up @@ -85,18 +85,22 @@ use \CarlBennett\MVC\Libraries\Common; ?>
</table>
</div>
<div class="form-group">
<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>
<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>
<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)?>"/>
</div>
<div class="form-group">
<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>
<span class="float-right">
<div class="custom-control custom-switch">
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="10"
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="11"
title="Use markdown or use raw HTML" value="1"<?=($form_fields['markdown'] ?? null ? ' checked' : '')?>/>
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
</div>
</span>
<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>
<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>
</div>
<div class="form-group text-center">
<input class="btn btn-success" type="submit" value="Save" tabindex="12"/>
<input class="btn btn-success" type="submit" value="Save" tabindex="13"/>
</div>
</form>
<? if (isset($comments)) { $comment_parent_type = Comment::PARENT_TYPE_PACKET; $comment_parent_id = $form_fields['packet_id'] ?? null; require('./Comment/Section.inc.phtml'); } ?>
14 changes: 13 additions & 1 deletion src/templates/Packet/Index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $form_order_by = [
'user-id-asc' => 'User Id (Ascending)',
'user-id-desc' => 'User Id (Descending)',
];
require_once('./MarkdownBootstrapFix.inc.php');
require('./header.inc.phtml'); ?>
<div class="container">
<h2><?=$title?></h2>
Expand Down Expand Up @@ -62,19 +63,30 @@ require('./header.inc.phtml'); ?>
$avatar_url = $user->getAvatarURI(22);
$user_url = $user->getURI();
}
$brief = $packet->getBrief(true);
$deprecated = $packet->isDeprecated();
$packet_id = $packet->getPacketId(true);
$published = $packet->isPublished();
$remarks = $packet->getRemarks(true);
$research = $packet->isInResearch();

if (!empty($brief))
{
$brief = \BNETDocs\Templates\MarkdownBootstrapFix($brief, true, true);
}
else
{
$brief = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($remarks, FILTER_SANITIZE_STRING)), "\n", 128), '. ', 128), '.');
}

ob_start();
if ($deprecated) require('./Deprecated.inc.phtml');
if ($research) require('./InResearch.inc.phtml');
if (!$published) require('./Draft.inc.phtml');
$tpl_packet_flags = ob_get_clean();
if (!empty($tpl_packet_flags)) $tpl_packet_flags = ' ' . $tpl_packet_flags; ?>
<tr>
<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>
<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>
<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>
</tr>
<? } ?>
Expand Down
16 changes: 12 additions & 4 deletions src/templates/Packet/View.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ if ($object)
{
$created_dt = $object->getCreatedDateTime();
$deprecated = $object->isDeprecated();
$description = Common::stripUpTo(trim(filter_var($object->getRemarks(true), FILTER_SANITIZE_STRING)), "\n", 300);
$brief = $object->getBrief(true);
$edited_dt = $object->getEditedDateTime();
$packet_id = $object->getPacketId(true);
$published = $object->isPublished();
$research = $object->isInResearch();
$published = $object->isPublished();
$remarks = $object->getRemarks(true);
$research = $object->isInResearch();
$title = $object->getName();
$url = $object->getURI();
$user = $object->getUser();

$description = filter_var($brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (empty($description))
{
$description = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($remarks, FILTER_SANITIZE_STRING)), "\n", 300), '. ', 300), '.');
}

if ($user)
{
$user_name = $user->getName();
Expand Down Expand Up @@ -88,10 +95,11 @@ require('./header.inc.phtml'); ?>
<tr><th class="text-right">Message Format:<br/><sup class="text-muted">(does not include protocol header)</sup></th><td>
<pre class="border border-primary overflow-auto rounded bg-dark text-light"><code class="language-objectivec"><?=$object->getFormat()?></code></pre>
</td></tr>
<?=(!empty($brief) ? sprintf('<tr><th class="text-right">Brief:</th><td>%s</td></tr>', \BNETDocs\Templates\MarkdownBootstrapFix($brief, true, true)) : '')?>
</tbody></table>

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

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

0 comments on commit 84e8c47

Please sign in to comment.