Skip to content

Commit f69aca2

Browse files
committed
Use the same form for document create and edit
1 parent e07292a commit f69aca2

File tree

4 files changed

+73
-107
lines changed

4 files changed

+73
-107
lines changed

src/templates/Document/Create.phtml

+13-28
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,35 @@ $title = 'Create Document';
66
$description = 'This page enables a user to create documents on the site.';
77
$this->opengraph->attach(new Pair('url', '/document/create'));
88
$this->opengraph->attach(new Pair('type', 'article'));
9-
switch ($this->getContext()->error)
9+
$document_id = null;
10+
$document_url = null;
11+
$error = $this->getContext()->error;
12+
switch ($error)
1013
{
1114
case 'ACL_NOT_SET': $message = 'You do not have the privilege to create documents.'; break;
1215
case 'EMPTY_TITLE': $message = 'The title of the document is required.'; break;
1316
case 'EMPTY_CONTENT': $message = 'The content of the document is required.'; break;
1417
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
15-
default: $message = $this->getContext()->error;
18+
default: $message = $error;
1619
}
1720
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
1821
$form_markdown = $this->getContext()->markdown;
1922
$form_title = filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2023
require('./header.inc.phtml'); ?>
2124
<div class="container mb-3">
22-
<h2>Create Document</h2>
23-
<? if ($this->getContext()->error !== false) { ?>
24-
<? if (!empty($message)) { ?>
25+
<h1><?=$title?></h1>
26+
<p><?=$description?></p>
27+
<? if ($error !== false) {
28+
if (!empty($message)) { ?>
2529
<div class="alert alert-danger">
2630
<p class="mb-0"><?=$message?></p>
2731
</div>
28-
<? } ?>
29-
<form method="POST" action="?">
30-
<div class="form-group">
31-
<label class="font-weight-bold" class="form-label" for="title">Title:</label><br/>
32-
<input class="form-control bg-dark text-light" type="text" name="title" id="title" tabindex="1" required autofocus="autofocus" value="<?=$form_title?>"/>
33-
</div>
34-
<div class="form-group">
35-
<label class="font-weight-bold" for="content">Content:</label>
36-
<div class="custom-control custom-switch float-right">
37-
<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"' : '')?>/>
38-
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
39-
</div>
40-
<textarea class="border border-secondary form-control bg-dark text-light" name="content" id="content" tabindex="2" required style="height:200px;"><?=$form_content?></textarea>
41-
</div>
42-
<div class="form-group text-center">
43-
<a class="btn btn-primary" href="javascript:history.go(-1);" tabindex="4">Back</a>
44-
<span class="m-1"></span>
45-
<input class="btn btn-secondary" type="submit" name="save" value="Save as Draft" tabindex="5"/>
46-
<input class="btn btn-success" type="submit" name="publish" value="Publish" tabindex="6"/>
47-
</div>
48-
</form>
49-
<? } else { ?>
32+
<? }
33+
require('./Document/Form.inc.phtml');
34+
} else { ?>
5035
<div class="alert alert-success">
5136
<p class="mb-0">The document has been created.</p>
5237
</div>
5338
<? } ?>
5439
</div>
55-
<? require("./footer.inc.phtml"); ?>
40+
<? require('./footer.inc.phtml'); ?>

src/templates/Document/Edit.phtml

+37-78
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,41 @@
1-
<?php
2-
1+
<?php /* vim: set colorcolumn=: */
32
namespace BNETDocs\Templates\Document;
4-
53
use \CarlBennett\MVC\Libraries\Common;
64
use \CarlBennett\MVC\Libraries\Pair;
7-
8-
$title = "Edit Document";
9-
$description = "This form allows an individual to edit a document.";
10-
11-
$this->opengraph->attach(new Pair("url", "/document/edit"));
12-
$this->opengraph->attach(new Pair("type", "article"));
13-
14-
switch ($this->getContext()->error) {
15-
case "ACL_NOT_SET":
16-
$message = "You do not have the privilege to edit documents.";
17-
break;
18-
case "NOT_FOUND":
19-
$message = "Cannot find document by that id.";
20-
break;
21-
case "EMPTY_TITLE":
22-
$message = "The title of the document is required.";
23-
break;
24-
case "EMPTY_CONTENT":
25-
$message = "The content of the document is required.";
26-
break;
27-
case "INTERNAL_ERROR":
28-
$message = "An internal error occurred while processing your request. "
29-
. "Our staff have been notified of the issue. Try again later.";
30-
break;
31-
default:
32-
$message = $this->getContext()->error;
5+
$title = 'Edit Document';
6+
$description = 'This form allows an individual to edit a document.';
7+
$this->opengraph->attach(new Pair('url', '/document/edit'));
8+
$this->opengraph->attach(new Pair('type', 'article'));
9+
$document_id = $this->getContext()->document_id;
10+
$document_url = ($this->getContext()->document ? $this->getContext()->document->getURI() : Common::relativeUrlToAbsolute('/document/' . rawurlencode($document_id)));
11+
$error = $this->getContext()->error;
12+
switch ($error)
13+
{
14+
case 'ACL_NOT_SET': $message = 'You do not have the privilege to edit documents.'; break;
15+
case 'NOT_FOUND': $message = 'Cannot find document by that id.'; break;
16+
case 'EMPTY_TITLE': $message = 'The title of the document is required.'; break;
17+
case 'EMPTY_CONTENT': $message = 'The content of the document is required.'; break;
18+
case 'INTERNAL_ERROR': $message = 'An internal error occurred while processing your request. Our staff have been notified of the issue. Try again later.'; break;
19+
default: $message = $error;
3320
}
34-
35-
require("./header.inc.phtml");
36-
?>
37-
<article>
38-
<?php if ($this->getContext()->error !== false) { ?>
39-
<header>Edit Document</header>
40-
<?php if (!empty($message)) { ?>
41-
<section class="red"><p><?php echo $message; ?></p></section>
42-
<?php } ?>
43-
<?php if ($this->getContext()->error != "NOT_FOUND") { ?>
44-
<form method="POST" action="?id=<?php echo
45-
htmlspecialchars($this->getContext()->document_id, ENT_HTML5, "UTF-8"); ?>">
46-
<section>
47-
<label for="title">Title:</label><br/>
48-
<input type="text" name="title" id="title" tabindex="1" required
49-
autofocus="autofocus" value="<?php echo
50-
filter_var($this->getContext()->title, FILTER_SANITIZE_STRING);
51-
?>"/>
52-
</section>
53-
<section>
54-
<label for="content">Content:</label>
55-
<span style="float:right;">
56-
<label for="markdown" title="Use markdown or use raw HTML">Markdown</label>
57-
<input type="checkbox" name="markdown" id="markdown" tabindex="3"
58-
title="Use markdown or use raw HTML" value="1"<?php
59-
if ($this->getContext()->markdown)
60-
echo " checked=\"checked\"";
61-
?>/>
62-
</span>
63-
<textarea name="content" id="content" tabindex="2" required
64-
style="height:200px;"><?php echo
65-
htmlspecialchars($this->getContext()->content, ENT_HTML5, "UTF-8");
66-
?></textarea>
67-
</section>
68-
<section>
69-
<input type="submit" name="publish" value="Publish" tabindex="4"/>
70-
<input type="submit" name="save" value="Unpublish" tabindex="5"/>
71-
</section>
72-
</form>
73-
<?php } ?>
74-
<?php } else { ?>
75-
<header class="green">Edit Document</header>
76-
<section class="green">
77-
<p>Your document has been edited.</p>
78-
<p>Use the navigation to the left to move to another page.</p>
79-
</section>
80-
<?php } ?>
81-
</article>
82-
<?php require("./footer.inc.phtml"); ?>
21+
$form_content = filter_var($this->getContext()->content, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
22+
$form_markdown = $this->getContext()->markdown;
23+
$form_title = filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
24+
require('./header.inc.phtml'); ?>
25+
<div class="container mb-3">
26+
<h1><?=$title?></h1>
27+
<p><?=$description?></p>
28+
<? if (is_null($error) || $error == 'NOT_FOUND') {
29+
require('./Document/Form.inc.phtml');
30+
} else if ($error !== false) { ?>
31+
<div class="alert alert-danger">
32+
<p class="mb-0"><?=$message?></p>
33+
</div>
34+
<? } else { ?>
35+
<div class="alert alert-success">
36+
<p class="mb-0">The document was successfully edited!</p>
37+
</div>
38+
<a class="btn btn-primary" href="<?=Common::relativeUrlToAbsolute('/document/' . rawurlencode($document_id))?>">Back</a>
39+
<? } ?>
40+
</div>
41+
<? require('./footer.inc.phtml'); ?>

src/templates/Document/Form.inc.phtml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php /* vim: set colorcolumn=: */
2+
namespace BNETDocs\Templates\Document; ?>
3+
<form method="POST" action="?<?=($document_id ? 'id=' . filter_var($document_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS) : '')?>">
4+
<div class="form-group">
5+
<label class="font-weight-bold" class="form-label" for="title">Title:</label><br/>
6+
<input class="form-control bg-dark text-light" type="text" name="title" id="title" tabindex="1" required autofocus="autofocus" value="<?=$form_title?>"/>
7+
</div>
8+
<div class="form-group">
9+
<label class="font-weight-bold" for="content">Content:</label>
10+
<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"' : '')?>/>
12+
<label class="custom-control-label" for="markdown" title="Use markdown or use raw HTML">Markdown</label>
13+
</div>
14+
<textarea class="border border-secondary form-control bg-dark text-light" name="content" id="content" tabindex="2" required style="height:200px;"><?=$form_content?></textarea>
15+
</div>
16+
<div class="form-group text-center">
17+
<a class="btn btn-primary" href="<?=($document_url ?? 'javascript:history.go(-1);')?>" tabindex="4">Back</a>
18+
<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"/>
21+
</div>
22+
</form>

src/templates/Document/View.phtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require('./header.inc.phtml'); ?>
4040
<span class="float-right ml-1"><a class="btn btn-sm btn-secondary" href="<?=$edit_url?>" title="Edit">📝</a></span>
4141
<? } ?>
4242

43-
<h2><a href="<?=$url?>"><?=filter_var($title, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></a></h2>
43+
<h1 class="display-4"><a href="<?=$url?>"><?=filter_var($title, FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></a></h1>
4444
<?=\BNETDocs\Templates\MarkdownBootstrapFix($object->getContent(true))?>
4545

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

0 commit comments

Comments
 (0)