Skip to content

Commit 232be88

Browse files
committed
Show brief field in documents
1 parent 0c430b7 commit 232be88

File tree

10 files changed

+79
-47
lines changed

10 files changed

+79
-47
lines changed

src/controllers/Document/Create.php

+23-18
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,37 @@ protected function handlePost(Router &$router, DocumentCreateModel &$model) {
4444
if (!isset(Common::$database)) {
4545
Common::$database = DatabaseDriver::getDatabaseObject();
4646
}
47-
$data = $router->getRequestBodyArray();
48-
$title = (isset($data['title' ]) ? $data['title' ] : null);
49-
$markdown = (isset($data['markdown' ]) ? $data['markdown' ] : null);
50-
$content = (isset($data['content' ]) ? $data['content' ] : null);
51-
$publish = (isset($data['publish' ]) ? $data['publish' ] : null);
52-
$save = (isset($data['save' ]) ? $data['save' ] : null);
53-
54-
$model->title = $title;
47+
$data = $router->getRequestBodyArray();
48+
$title = $data['title'] ?? null;
49+
$markdown = $data['markdown'] ?? null;
50+
$brief = $data['brief'] ?? null;
51+
$content = $data['content'] ?? null;
52+
$publish = $data['publish'] ?? null;
53+
$save = $data['save'] ?? null;
54+
55+
$markdown = ($markdown ? true : false);
56+
$publish = ($publish ? true : false);
57+
58+
$model->title = $title;
59+
$model->brief = $brief;
5560
$model->markdown = $markdown;
56-
$model->content = $content;
61+
$model->content = $content;
5762

5863
if (empty($title)) {
5964
$model->error = 'EMPTY_TITLE';
6065
} else if (empty($content)) {
6166
$model->error = 'EMPTY_CONTENT';
6267
}
6368

64-
$options_bitmask = 0;
65-
if ($markdown) $options_bitmask |= Document::OPTION_MARKDOWN;
66-
if ($publish ) $options_bitmask |= Document::OPTION_PUBLISHED;
67-
6869
$user_id = $model->user->getId();
6970

7071
try {
7172

7273
$document = new Document(null);
74+
$document->setBrief($brief);
7375
$document->setContent($content);
74-
$document->setOptions($options_bitmask);
76+
$document->setMarkdown($markdown);
77+
$document->setPublished($publish);
7578
$document->setTitle($title);
7679
$document->setUserId($user_id);
7780
$document->commit();
@@ -91,10 +94,12 @@ protected function handlePost(Router &$router, DocumentCreateModel &$model) {
9194
$user_id,
9295
getenv('REMOTE_ADDR'),
9396
json_encode([
94-
'error' => $model->error,
95-
'options_bitmask' => $options_bitmask,
96-
'title' => $title,
97-
'content' => $content,
97+
'brief' => $brief,
98+
'content' => $content,
99+
'error' => $model->error,
100+
'markdown' => $markdown,
101+
'published' => $publish,
102+
'title' => $title,
98103
])
99104
);
100105
}

src/controllers/Document/Edit.php

+23-21
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,10 @@
2323

2424
class Edit extends Controller {
2525
public function &run(Router &$router, View &$view, array &$args) {
26-
$data = $router->getRequestQueryArray();
27-
$model = new DocumentEditModel();
28-
$model->content = null;
29-
$model->document = null;
30-
$model->document_id = (isset($data['id']) ? $data['id'] : null);
31-
$model->error = null;
32-
$model->markdown = null;
33-
$model->published = null;
34-
$model->title = null;
35-
$model->user = Authentication::$user;
26+
$data = $router->getRequestQueryArray();
27+
$model = new DocumentEditModel();
28+
$model->document_id = (isset($data['id']) ? $data['id'] : null);
29+
$model->user = Authentication::$user;
3630

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

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

77-
$data = $router->getRequestBodyArray();
78-
$category = (isset($data['category' ]) ? $data['category' ] : null);
79-
$title = (isset($data['title' ]) ? $data['title' ] : null);
80-
$markdown = (isset($data['markdown' ]) ? $data['markdown' ] : null);
81-
$content = (isset($data['content' ]) ? $data['content' ] : null);
82-
$publish = (isset($data['publish' ]) ? $data['publish' ] : null);
83-
$save = (isset($data['save' ]) ? $data['save' ] : null);
72+
$data = $router->getRequestBodyArray();
73+
$brief = $data['brief'] ?? null;
74+
$category = $data['category'] ?? null;
75+
$content = $data['content'] ?? null;
76+
$markdown = $data['markdown'] ?? null;
77+
$publish = $data['publish'] ?? null;
78+
$save = $data['save'] ?? null;
79+
$title = $data['title'] ?? null;
80+
81+
$markdown = ($markdown ? true : false);
82+
$publish = ($publish ? true : false);
8483

8584
$model->category = $category;
8685
$model->title = $title;
86+
$model->brief = $brief;
8787
$model->markdown = $markdown;
8888
$model->content = $content;
8989

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

100100
$model->document->setTitle($model->title);
101-
$model->document->setMarkdown($model->markdown);
101+
$model->document->setBrief($model->brief);
102+
$model->document->setMarkdown($markdown);
102103
$model->document->setContent($model->content);
103-
$model->document->setPublished($publish ? true : false);
104+
$model->document->setPublished($publish);
104105

105106
$model->document->incrementEdited();
106107
$model->document->commit();
@@ -120,11 +121,12 @@ protected function handlePost(Router &$router, DocumentEditModel &$model) {
120121
$user_id,
121122
getenv('REMOTE_ADDR'),
122123
json_encode([
123-
'error' => $model->error,
124+
'brief' => $model->document->getBrief(false),
125+
'content' => $model->document->getContent(false),
124126
'document_id' => $model->document_id,
127+
'error' => $model->error,
125128
'options_bitmask' => $model->document->getOptions(),
126129
'title' => $model->document->getTitle(),
127-
'content' => $model->document->getContent(false),
128130
])
129131
);
130132
}

src/controllers/Document/Index.php

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
class Index extends Controller {
2020
public function &run(Router &$router, View &$view, array &$args) {
2121
$model = new DocumentIndexModel();
22-
2322
$query = $router->getRequestQueryArray();
2423

2524
$model->order = (

src/models/Document/Create.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Create extends Model {
88

99
public $acl_allowed;
10+
public $brief;
1011
public $content;
1112
public $error;
1213
public $markdown;

src/models/Document/Edit.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Edit extends Model {
88

99
public $acl_allowed;
10+
public $brief;
1011
public $category;
1112
public $content;
1213
public $document;

src/templates/Document/Create.phtml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ switch ($error)
1717
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
1818
default: $message = $error;
1919
}
20+
$form_brief = filter_var($this->getContext()->brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2021
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2122
$form_markdown = $this->getContext()->markdown;
2223
$form_title = filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

src/templates/Document/Edit.phtml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ switch ($error)
2020
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
2121
default: $message = $error;
2222
}
23+
$form_brief = filter_var($this->getContext()->brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2324
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2425
$form_markdown = $this->getContext()->markdown;
2526
$form_title = filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

src/templates/Document/Form.inc.phtml

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ namespace BNETDocs\Templates\Document; ?>
55
<label class="font-weight-bold" class="form-label" for="title">Title:</label><br/>
66
<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?>"/>
77
</div>
8+
<div class="form-group">
9+
<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>
10+
<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)?>"/>
11+
</div>
812
<div class="form-group">
913
<label class="font-weight-bold" for="content">Content:</label>
1014
<div class="custom-control custom-switch float-right">
11-
<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"' : '')?>/>
15+
<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"' : '')?>/>
1216
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
1317
</div>
14-
<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>
18+
<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>
1519
</div>
1620
<div class="form-group text-center">
17-
<a class="btn btn-primary" href="<?=($document_url ?? 'javascript:history.go(-1);')?>" tabindex="4">Back</a>
21+
<a class="btn btn-primary" href="<?=($document_url ?? 'javascript:history.go(-1);')?>" tabindex="5">Back</a>
1822
<span class="m-1"></span>
19-
<input class="btn btn-secondary" type="submit" name="save" value="Save Draft" tabindex="5"/>
20-
<input class="btn btn-success" type="submit" name="publish" value="Publish" tabindex="6"/>
23+
<input class="btn btn-secondary" type="submit" name="save" value="Save Draft" tabindex="6"/>
24+
<input class="btn btn-success" type="submit" name="publish" value="Publish" tabindex="7"/>
2125
</div>
2226
</form>

src/templates/Document/Index.phtml

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ $form_order_by = [
2020
'user-id-asc' => 'User Id (Ascending)',
2121
'user-id-desc' => 'User Id (Descending)',
2222
];
23+
require_once('./MarkdownBootstrapFix.inc.php');
2324
require('./header.inc.phtml'); ?>
2425
<div class="container">
2526
<h2><?=$title?></h2>
@@ -39,13 +40,22 @@ require('./header.inc.phtml'); ?>
3940
<? foreach ($this->getContext()->documents as $document)
4041
{
4142
$doc_title = filter_var($document->getTitle(), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
42-
$doc_brief = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($document->getContent(true), FILTER_SANITIZE_STRING)), "\n", 128), '. ', 128), '.');
43+
$doc_brief = $document->getBrief(true);
44+
$doc_content = $document->getContent(true);
4345
$doc_user = $document->getUser();
4446
$doc_user_string = ($doc_user ?
4547
sprintf('<a href="%s"><img class="mr-2 rounded" src="%s"/>%s</a>',
4648
$doc_user->getURI(), $doc_user->getAvatarURI(40), filter_var($doc_user->getName(), FILTER_SANITIZE_FULL_SPECIAL_CHARS)
4749
) : 'Anonymous'
4850
);
51+
if (!empty($doc_brief))
52+
{
53+
$doc_brief = \BNETDocs\Templates\MarkdownBootstrapFix($doc_brief, true, true);
54+
}
55+
else
56+
{
57+
$doc_brief = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($doc_content, FILTER_SANITIZE_STRING)), "\n", 128), '. ', 128), '.');
58+
}
4959
printf('<tr><td><strong><a href="%s">%s</a></strong><br/><span class="text-muted">%s</span></td><td>%s</td></tr>',
5060
$document->getURI(), $doc_title, $doc_brief, $doc_user_string
5161
);

src/templates/Document/View.phtml

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ if ($object)
1616
{
1717
$url = $object->getURI();
1818
$title = $object->getTitle();
19-
$description = Common::stripUpTo(trim(filter_var($object->getContent(true), FILTER_SANITIZE_STRING)), "\n", 300);
19+
$brief = $object->getBrief(true);
20+
$content = $object->getContent(true);
2021
$created_dt = $object->getCreatedDateTime();
2122
$edited_dt = $object->getEditedDateTime();
2223
$user = $object->getUser();
2324
$draft = !$object->isPublished();
25+
26+
$description = filter_var($brief, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
27+
if (empty($description))
28+
{
29+
$description = rtrim(Common::stripUpTo(Common::stripUpTo(trim(filter_var($content, FILTER_SANITIZE_STRING)), "\n", 300), '. ', 300), '.');
30+
}
2431
}
2532
$this->opengraph->attach(new Pair('url', $url));
2633
$edit_url = Common::relativeUrlToAbsolute('/document/edit?id=' . rawurlencode($object_id));
@@ -43,6 +50,7 @@ require('./header.inc.phtml'); ?>
4350
<? } ?>
4451

4552
<h1 class="display-4"><a href="<?=$url?>"><?=filter_var($title, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></a></h1>
53+
<?=(!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)) : '')?>
4654
<?=\BNETDocs\Templates\MarkdownBootstrapFix($object->getContent(true), true)?>
4755

4856
<div class="card"><div class="card-body">

0 commit comments

Comments
 (0)