Skip to content

Commit 0792a75

Browse files
committed
Convert news edit form to bootstrap
1 parent 4d0ba9b commit 0792a75

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

src/controllers/News/Edit.php

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?php
2-
1+
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
32
namespace BNETDocs\Controllers\News;
43

54
use \BNETDocs\Libraries\Authentication;
@@ -21,8 +20,10 @@
2120
use \DateTimeZone;
2221
use \InvalidArgumentException;
2322

24-
class Edit extends Controller {
25-
public function &run(Router &$router, View &$view, array &$args) {
23+
class Edit extends Controller
24+
{
25+
public function &run(Router &$router, View &$view, array &$args)
26+
{
2627
$data = $router->getRequestQueryArray();
2728
$model = new NewsEditModel();
2829
$model->active_user = Authentication::$user;
@@ -41,6 +42,14 @@ public function &run(Router &$router, View &$view, array &$args) {
4142
User::OPTION_ACL_NEWS_MODIFY
4243
));
4344

45+
if (!$model->acl_allowed)
46+
{
47+
$model->_responseCode = 403;
48+
$model->error = 'ACL_NOT_SET';
49+
$view->render($model);
50+
return $model;
51+
}
52+
4453
try { $model->news_post = new NewsPost($model->news_post_id); }
4554
catch (NewsPostNotFoundException $e) { $model->news_post = null; }
4655
catch (InvalidArgumentException $e) { $model->news_post = null; }
@@ -75,12 +84,16 @@ public function &run(Router &$router, View &$view, array &$args) {
7584
return $model;
7685
}
7786

78-
protected function handlePost(Router &$router, NewsEditModel &$model) {
79-
if (!$model->acl_allowed) {
87+
protected function handlePost(Router &$router, NewsEditModel &$model)
88+
{
89+
if (!$model->acl_allowed)
90+
{
8091
$model->error = 'ACL_NOT_SET';
8192
return;
8293
}
83-
if (!isset(Common::$database)) {
94+
95+
if (!isset(Common::$database))
96+
{
8497
Common::$database = DatabaseDriver::getDatabaseObject();
8598
}
8699

@@ -99,16 +112,12 @@ protected function handlePost(Router &$router, NewsEditModel &$model) {
99112
$model->content = $content;
100113
$model->rss_exempt = $rss_exempt;
101114

102-
if (empty($title)) {
103-
$model->error = 'EMPTY_TITLE';
104-
} else if (empty($content)) {
105-
$model->error = 'EMPTY_CONTENT';
106-
}
107-
108-
$user_id = $model->active_user->getId();
115+
$model->error = (empty($title) ? 'EMPTY_TITLE' : (empty($content) ? 'EMPTY_CONTENT' : null));
109116

110-
try {
117+
if ($model->error) return;
111118

119+
try
120+
{
112121
$model->news_post->setCategoryId($model->category);
113122
$model->news_post->setTitle($model->title);
114123
$model->news_post->setMarkdown($model->markdown);
@@ -124,28 +133,25 @@ protected function handlePost(Router &$router, NewsEditModel &$model) {
124133
);
125134

126135
$success = $model->news_post->save();
127-
128-
} catch (QueryException $e) {
129-
136+
$model->error = false;
137+
}
138+
catch (QueryException $e)
139+
{
130140
// SQL error occurred. We can show a friendly message to the user while
131141
// also notifying this problem to staff.
132142
Logger::logException($e);
133143

134144
$success = false;
135-
136-
}
137-
138-
if (!$success) {
139145
$model->error = 'INTERNAL_ERROR';
140-
} else {
141-
$model->error = false;
142146
}
143147

144-
Logger::logEvent(
148+
Logger::logEvent
149+
(
145150
EventTypes::NEWS_EDITED,
146-
$user_id,
151+
($model->active_user ? $model->active_user->getId() : null),
147152
getenv('REMOTE_ADDR'),
148-
json_encode([
153+
json_encode
154+
([
149155
'error' => $model->error,
150156
'news_post_id' => $model->news_post_id,
151157
'category_id' => $model->news_post->getCategoryId(),

src/templates/Document/Edit.phtml

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ $form_markdown = $this->getContext()->markdown;
2525
$form_title = filter_var($this->getContext()->title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2626
require('./header.inc.phtml'); ?>
2727
<div class="container">
28+
<? if ($this->getContext()->acl_allowed) { ?>
2829
<h1><?=$title?></h1>
2930
<p><?=$description?></p>
3031
<? if (is_null($error) || $error == 'NOT_FOUND') {
@@ -41,6 +42,7 @@ require('./header.inc.phtml'); ?>
4142
<div class="text-center">
4243
<a class="btn btn-primary" href="<?=Common::relativeUrlToAbsolute('/document/' . rawurlencode($document_id))?>"><img class="float-left" src="<?=Common::relativeUrlToAbsolute('/a/svg/chevron-right-white.svg' . $_unique_asset)?>"/> Return to Document</a>
4344
</div>
44-
<? } ?>
45+
<? }
46+
} else { require('./LoginRequired.inc.phtml'); } ?>
4547
</div>
4648
<? require('./footer.inc.phtml'); ?>

src/templates/News/Edit.phtml

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ $news_post = $this->getContext()->news_post;
2626
$news_post_id = filter_var($this->getContext()->news_post_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
2727
require('./header.inc.phtml'); ?>
2828
<div class="container">
29+
<? if ($this->getContext()->acl_allowed) { ?>
2930
<h1><?=$title?></h1>
3031
<p><?=$description?></p>
3132
<? if ($error !== false) {
@@ -39,6 +40,7 @@ require('./header.inc.phtml'); ?>
3940
<div class="alert alert-success">
4041
<p class="mb-0">Your news post has been edited successfully!</p>
4142
</div>
42-
<? } ?>
43+
<? }
44+
} else { require('./LoginRequired.inc.phtml'); } ?>
4345
</div>
4446
<? require('./footer.inc.phtml'); ?>

src/templates/News/Form.inc.phtml

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
2-
namespace BNETDocs\Templates\Document; ?>
2+
namespace BNETDocs\Templates\News; ?>
33
<form method="POST" action="?<?=($news_post_id ? 'id=' . filter_var($news_post_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS) : '')?>">
44
<div class="form-group">
55
<label class="font-weight-bold" for="category">Category:</label><br/>
66
<select class="border border-primary custom-select bg-dark text-light" name="category" id="category" tabindex="1" required autofocus="autofocus" size="1">
7-
<?php $category = $this->getContext()->category;
8-
foreach ($this->getContext()->news_categories as $item) { ?>
9-
<option value="<?php echo htmlspecialchars($item->getId(), ENT_HTML5, "UTF-8"); ?>"<?php if ($category === $item->getId()) { ?> selected="selected"<?php } ?>><?php echo htmlspecialchars($item->getLabel(), ENT_HTML5, "UTF-8"); ?></option>
10-
<?php } ?>
7+
<? $category = $this->getContext()->category; foreach ($this->getContext()->news_categories as $item) { ?>
8+
<option value="<?=filter_var($item->getId(), FILTER_SANITIZE_FULL_SPECIAL_CHARS)?>"<?=($category === $item->getId() ? ' selected="selected"' : '')?>><?=filter_var($item->getLabel(), FILTER_SANITIZE_FULL_SPECIAL_CHARS)?></option>
9+
<? } ?>
1110
</select>
1211
</div>
1312
<div class="form-group">

0 commit comments

Comments
 (0)