Skip to content

Commit

Permalink
Show brief field in documents
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Jul 24, 2021
1 parent 0c430b7 commit 232be88
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 47 deletions.
41 changes: 23 additions & 18 deletions src/controllers/Document/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,37 @@ protected function handlePost(Router &$router, DocumentCreateModel &$model) {
if (!isset(Common::$database)) {
Common::$database = DatabaseDriver::getDatabaseObject();
}
$data = $router->getRequestBodyArray();
$title = (isset($data['title' ]) ? $data['title' ] : null);
$markdown = (isset($data['markdown' ]) ? $data['markdown' ] : null);
$content = (isset($data['content' ]) ? $data['content' ] : null);
$publish = (isset($data['publish' ]) ? $data['publish' ] : null);
$save = (isset($data['save' ]) ? $data['save' ] : null);

$model->title = $title;
$data = $router->getRequestBodyArray();
$title = $data['title'] ?? null;
$markdown = $data['markdown'] ?? null;
$brief = $data['brief'] ?? null;
$content = $data['content'] ?? null;
$publish = $data['publish'] ?? null;
$save = $data['save'] ?? null;

$markdown = ($markdown ? true : false);
$publish = ($publish ? true : false);

$model->title = $title;
$model->brief = $brief;
$model->markdown = $markdown;
$model->content = $content;
$model->content = $content;

if (empty($title)) {
$model->error = 'EMPTY_TITLE';
} else if (empty($content)) {
$model->error = 'EMPTY_CONTENT';
}

$options_bitmask = 0;
if ($markdown) $options_bitmask |= Document::OPTION_MARKDOWN;
if ($publish ) $options_bitmask |= Document::OPTION_PUBLISHED;

$user_id = $model->user->getId();

try {

$document = new Document(null);
$document->setBrief($brief);
$document->setContent($content);
$document->setOptions($options_bitmask);
$document->setMarkdown($markdown);
$document->setPublished($publish);
$document->setTitle($title);
$document->setUserId($user_id);
$document->commit();
Expand All @@ -91,10 +94,12 @@ protected function handlePost(Router &$router, DocumentCreateModel &$model) {
$user_id,
getenv('REMOTE_ADDR'),
json_encode([
'error' => $model->error,
'options_bitmask' => $options_bitmask,
'title' => $title,
'content' => $content,
'brief' => $brief,
'content' => $content,
'error' => $model->error,
'markdown' => $markdown,
'published' => $publish,
'title' => $title,
])
);
}
Expand Down
44 changes: 23 additions & 21 deletions src/controllers/Document/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@

class Edit extends Controller {
public function &run(Router &$router, View &$view, array &$args) {
$data = $router->getRequestQueryArray();
$model = new DocumentEditModel();
$model->content = null;
$model->document = null;
$model->document_id = (isset($data['id']) ? $data['id'] : null);
$model->error = null;
$model->markdown = null;
$model->published = null;
$model->title = null;
$model->user = Authentication::$user;
$data = $router->getRequestQueryArray();
$model = new DocumentEditModel();
$model->document_id = (isset($data['id']) ? $data['id'] : null);
$model->user = Authentication::$user;

$model->acl_allowed = ($model->user && $model->user->getOption(
User::OPTION_ACL_DOCUMENT_MODIFY
Expand All @@ -50,6 +44,7 @@ public function &run(Router &$router, View &$view, array &$args) {
$model->document_id
);

$model->brief = $model->document->getBrief(false);
$model->content = $model->document->getContent(false);
$model->markdown = $model->document->isMarkdown();
$model->published = $model->document->isPublished();
Expand All @@ -74,16 +69,21 @@ protected function handlePost(Router &$router, DocumentEditModel &$model) {
Common::$database = DatabaseDriver::getDatabaseObject();
}

$data = $router->getRequestBodyArray();
$category = (isset($data['category' ]) ? $data['category' ] : null);
$title = (isset($data['title' ]) ? $data['title' ] : null);
$markdown = (isset($data['markdown' ]) ? $data['markdown' ] : null);
$content = (isset($data['content' ]) ? $data['content' ] : null);
$publish = (isset($data['publish' ]) ? $data['publish' ] : null);
$save = (isset($data['save' ]) ? $data['save' ] : null);
$data = $router->getRequestBodyArray();
$brief = $data['brief'] ?? null;
$category = $data['category'] ?? null;
$content = $data['content'] ?? null;
$markdown = $data['markdown'] ?? null;
$publish = $data['publish'] ?? null;
$save = $data['save'] ?? null;
$title = $data['title'] ?? null;

$markdown = ($markdown ? true : false);
$publish = ($publish ? true : false);

$model->category = $category;
$model->title = $title;
$model->brief = $brief;
$model->markdown = $markdown;
$model->content = $content;

Expand All @@ -98,9 +98,10 @@ protected function handlePost(Router &$router, DocumentEditModel &$model) {
try {

$model->document->setTitle($model->title);
$model->document->setMarkdown($model->markdown);
$model->document->setBrief($model->brief);
$model->document->setMarkdown($markdown);
$model->document->setContent($model->content);
$model->document->setPublished($publish ? true : false);
$model->document->setPublished($publish);

$model->document->incrementEdited();
$model->document->commit();
Expand All @@ -120,11 +121,12 @@ protected function handlePost(Router &$router, DocumentEditModel &$model) {
$user_id,
getenv('REMOTE_ADDR'),
json_encode([
'error' => $model->error,
'brief' => $model->document->getBrief(false),
'content' => $model->document->getContent(false),
'document_id' => $model->document_id,
'error' => $model->error,
'options_bitmask' => $model->document->getOptions(),
'title' => $model->document->getTitle(),
'content' => $model->document->getContent(false),
])
);
}
Expand Down
1 change: 0 additions & 1 deletion src/controllers/Document/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
class Index extends Controller {
public function &run(Router &$router, View &$view, array &$args) {
$model = new DocumentIndexModel();

$query = $router->getRequestQueryArray();

$model->order = (
Expand Down
1 change: 1 addition & 0 deletions src/models/Document/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Create extends Model {

public $acl_allowed;
public $brief;
public $content;
public $error;
public $markdown;
Expand Down
1 change: 1 addition & 0 deletions src/models/Document/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Edit extends Model {

public $acl_allowed;
public $brief;
public $category;
public $content;
public $document;
Expand Down
1 change: 1 addition & 0 deletions src/templates/Document/Create.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ switch ($error)
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
default: $message = $error;
}
$form_brief = filter_var($this->getContext()->brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$form_markdown = $this->getContext()->markdown;
$form_title = filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Expand Down
1 change: 1 addition & 0 deletions src/templates/Document/Edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ switch ($error)
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
default: $message = $error;
}
$form_brief = filter_var($this->getContext()->brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$form_markdown = $this->getContext()->markdown;
$form_title = filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
Expand Down
14 changes: 9 additions & 5 deletions src/templates/Document/Form.inc.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ namespace BNETDocs\Templates\Document; ?>
<label class="font-weight-bold" class="form-label" for="title">Title:</label><br/>
<input class="border border-primary form-control bg-dark text-light" type="text" name="title" id="title" tabindex="1" required autofocus="autofocus" placeholder="Enter the document title here" value="<?=$form_title?>"/>
</div>
<div class="form-group">
<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 content is 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="2" value="<?=filter_var($form_brief ?? null, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"/>
</div>
<div class="form-group">
<label class="font-weight-bold" for="content">Content:</label>
<div class="custom-control custom-switch float-right">
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="3" title="Use markdown or use raw HTML" value="1"<?=($form_markdown ? ' checked="checked"' : '')?>/>
<input class="custom-control-input" type="checkbox" name="markdown" id="markdown" tabindex="4" title="Use markdown or use raw HTML" value="1"<?=($form_markdown ? ' checked="checked"' : '')?>/>
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
</div>
<textarea class="border border-primary form-control bg-dark text-light" name="content" id="content" tabindex="2" required placeholder="Enter the document content here" style="height:200px;"><?=$form_content?></textarea>
<textarea class="border border-primary form-control bg-dark text-light" name="content" id="content" tabindex="3" required placeholder="Enter the document content here" style="height:200px;"><?=$form_content?></textarea>
</div>
<div class="form-group text-center">
<a class="btn btn-primary" href="<?=($document_url ?? 'javascript:history.go(-1);')?>" tabindex="4">Back</a>
<a class="btn btn-primary" href="<?=($document_url ?? 'javascript:history.go(-1);')?>" tabindex="5">Back</a>
<span class="m-1"></span>
<input class="btn btn-secondary" type="submit" name="save" value="Save Draft" tabindex="5"/>
<input class="btn btn-success" type="submit" name="publish" value="Publish" tabindex="6"/>
<input class="btn btn-secondary" type="submit" name="save" value="Save Draft" tabindex="6"/>
<input class="btn btn-success" type="submit" name="publish" value="Publish" tabindex="7"/>
</div>
</form>
12 changes: 11 additions & 1 deletion src/templates/Document/Index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 All @@ -39,13 +40,22 @@ require('./header.inc.phtml'); ?>
<? foreach ($this->getContext()->documents as $document)
{
$doc_title = filter_var($document->getTitle(), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$doc_brief = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($document->getContent(true), FILTER_SANITIZE_STRING)), "\n", 128), '. ', 128), '.');
$doc_brief = $document->getBrief(true);
$doc_content = $document->getContent(true);
$doc_user = $document->getUser();
$doc_user_string = ($doc_user ?
sprintf('<a href="%s"><img class="mr-2 rounded" src="%s"/>%s</a>',
$doc_user->getURI(), $doc_user->getAvatarURI(40), filter_var($doc_user->getName(), FILTER_SANITIZE_FULL_SPECIAL_CHARS)
) : 'Anonymous'
);
if (!empty($doc_brief))
{
$doc_brief = \BNETDocs\Templates\MarkdownBootstrapFix($doc_brief, true, true);
}
else
{
$doc_brief = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($doc_content, FILTER_SANITIZE_STRING)), "\n", 128), '. ', 128), '.');
}
printf('<tr><td><strong><a href="%s">%s</a></strong><br/><span class="text-muted">%s</span></td><td>%s</td></tr>',
$document->getURI(), $doc_title, $doc_brief, $doc_user_string
);
Expand Down
10 changes: 9 additions & 1 deletion src/templates/Document/View.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ if ($object)
{
$url = $object->getURI();
$title = $object->getTitle();
$description = Common::stripUpTo(trim(filter_var($object->getContent(true), FILTER_SANITIZE_STRING)), "\n", 300);
$brief = $object->getBrief(true);
$content = $object->getContent(true);
$created_dt = $object->getCreatedDateTime();
$edited_dt = $object->getEditedDateTime();
$user = $object->getUser();
$draft = !$object->isPublished();

$description = filter_var($brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if (empty($description))
{
$description = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($content, FILTER_SANITIZE_STRING)), "\n", 300), '. ', 300), '.');
}
}
$this->opengraph->attach(new Pair('url', $url));
$edit_url = Common::relativeUrlToAbsolute('/document/edit?id=' . rawurlencode($object_id));
Expand All @@ -43,6 +50,7 @@ require('./header.inc.phtml'); ?>
<? } ?>

<h1 class="display-4"><a href="<?=$url?>"><?=filter_var($title, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></a></h1>
<?=(!empty($brief) ? sprintf(' <div class="alert alert-secondary"><span class="float-left font-weight-bold mr-1">Brief:</span> %s</div>', \BNETDocs\Templates\MarkdownBootstrapFix($brief, true, true)) : '')?>
<?=\BNETDocs\Templates\MarkdownBootstrapFix($object->getContent(true), true)?>

<div class="card"><div class="card-body">
Expand Down

0 comments on commit 232be88

Please sign in to comment.