Skip to content

Commit 4b6d42e

Browse files
committed
Finish comment editing feature
1 parent 881f00c commit 4b6d42e

File tree

4 files changed

+125
-75
lines changed

4 files changed

+125
-75
lines changed

src/controllers/Comment/Edit.php

Lines changed: 103 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,139 +9,181 @@
99
use \BNETDocs\Libraries\Exceptions\CommentNotFoundException;
1010
use \BNETDocs\Libraries\Logger;
1111
use \BNETDocs\Libraries\User;
12+
1213
use \BNETDocs\Models\Comment\Edit as CommentEditModel;
1314

1415
use \CarlBennett\MVC\Libraries\Common;
1516
use \CarlBennett\MVC\Libraries\Controller;
17+
use \CarlBennett\MVC\Libraries\Exceptions\QueryException;
1618
use \CarlBennett\MVC\Libraries\Router;
1719
use \CarlBennett\MVC\Libraries\View;
1820

21+
use \DateTime;
22+
use \DateTimeZone;
1923
use \InvalidArgumentException;
2024
use \UnexpectedValueException;
2125

2226
class Edit extends Controller {
27+
public function &run( Router &$router, View &$view, array &$args ) {
28+
29+
$query_data = $router->getRequestQueryArray();
30+
$post_data = $router->getRequestBodyArray();
31+
32+
$model = new CommentEditModel();
33+
34+
$model->csrf_id = mt_rand();
35+
$model->csrf_token = CSRF::generate( $model->csrf_id );
36+
$model->user = Authentication::$user;
37+
38+
$model->id = (
39+
isset( $query_data[ 'id' ]) ? $query_data[ 'id' ] : null
40+
);
41+
$model->content = (
42+
isset( $post_data[ 'content' ]) ? $post_data[ 'content' ] : null
43+
);
44+
45+
try { $model->comment = new Comment( $model->id ); }
46+
catch ( CommentNotFoundException $e ) { $model->comment = null; }
47+
catch ( InvalidArgumentException $e ) { $model->comment = null; }
2348

24-
public function &run(Router &$router, View &$view, array &$args) {
25-
26-
$data = $router->getRequestQueryArray();
27-
$model = new CommentEditModel();
28-
$model->comment = null;
29-
$model->csrf_id = mt_rand();
30-
$model->csrf_token = CSRF::generate($model->csrf_id);
31-
$model->error = null;
32-
$model->id = (isset($data["id"]) ? $data["id"] : null);
33-
$model->parent_id = null;
34-
$model->parent_type = null;
35-
$model->user = Authentication::$user;
36-
37-
try { $model->comment = new Comment($model->id); }
38-
catch (CommentNotFoundException $e) { $model->comment = null; }
39-
catch (InvalidArgumentException $e) { $model->comment = null; }
40-
41-
$model->acl_allowed = ($model->user && (
42-
$model->user->getAcl(User::OPTION_ACL_COMMENT_DELETE) ||
49+
$model->acl_allowed = ( $model->user && (
50+
$model->user->getAcl( User::OPTION_ACL_COMMENT_MODIFY ) ||
4351
$model->user->getId() == $model->comment->getUserId()
4452
));
4553

46-
if ($model->comment === null) {
47-
$model->error = "NOT_FOUND";
54+
if ( is_null( $model->comment )) {
55+
$model->error = 'NOT_FOUND';
4856
} else {
49-
$model->content = $model->comment->getContent(true);
57+
if ( is_null( $model->content )) {
58+
$model->content = $model->comment->getContent( false );
59+
}
60+
5061
$model->parent_type = $model->comment->getParentType();
5162
$model->parent_id = $model->comment->getParentId();
5263

53-
if ($router->getRequestMethod() == "POST") {
54-
$this->tryDelete($router, $model);
64+
switch ( $model->parent_type ) {
65+
case Comment::PARENT_TYPE_DOCUMENT:
66+
$model->return_url = '/document/' . $model->parent_id; break;
67+
case Comment::PARENT_TYPE_COMMENT:
68+
$model->return_url = '/comment/' . $model->parent_id; break;
69+
case Comment::PARENT_TYPE_NEWS_POST:
70+
$model->return_url = '/news/' . $model->parent_id; break;
71+
case Comment::PARENT_TYPE_PACKET:
72+
$model->return_url = '/packet/' . $model->parent_id; break;
73+
case Comment::PARENT_TYPE_SERVER:
74+
$model->return_url = '/server/' . $model->parent_id; break;
75+
case Comment::PARENT_TYPE_USER:
76+
$model->return_url = '/user/' . $model->parent_id; break;
77+
default: throw new UnexpectedValueException(
78+
'Parent type: ' . $model->parent_type
79+
);
80+
}
81+
$model->return_url = Common::relativeUrlToAbsolute( $model->return_url );
82+
83+
if ( $router->getRequestMethod() == 'POST' ) {
84+
$this->tryModify( $router, $model );
5585
}
5686
}
5787

58-
$view->render($model);
88+
$view->render( $model );
5989

60-
$model->_responseCode = ($model->acl_allowed ? 200 : 403);
61-
$model->_responseHeaders["Content-Type"] = $view->getMimeType();
90+
$model->_responseCode = ( $model->acl_allowed ? 200 : 403 );
91+
$model->_responseHeaders[ 'Content-Type' ] = $view->getMimeType();
6292
$model->_responseTTL = 0;
6393

6494
return $model;
6595
}
6696

67-
protected function tryDelete(Router &$router, CommentDeleteModel &$model) {
68-
if (!isset($model->user)) {
69-
$model->error = "NOT_LOGGED_IN";
97+
protected function tryModify( Router &$router, CommentEditModel &$model ) {
98+
if ( !isset( $model->user )) {
99+
$model->error = 'NOT_LOGGED_IN';
70100
return;
71101
}
72-
if (!$model->acl_allowed) {
73-
$model->error = "ACL_NOT_SET";
102+
if ( !$model->acl_allowed ) {
103+
$model->error = 'ACL_NOT_SET';
74104
return;
75105
}
76106

77-
$data = $router->getRequestBodyArray();
78-
$csrf_id = (isset($data["csrf_id" ]) ? $data["csrf_id" ] : null);
79-
$csrf_token = (isset($data["csrf_token"]) ? $data["csrf_token"] : null);
80-
$csrf_valid = CSRF::validate($csrf_id, $csrf_token);
107+
$post_data = $router->getRequestBodyArray();
108+
109+
$csrf_id = (
110+
isset( $post_data[ 'csrf_id' ]) ? $post_data[ 'csrf_id' ] : null
111+
);
112+
$csrf_token = (
113+
isset( $post_data[ 'csrf_token' ]) ? $post_data[ 'csrf_token' ] : null
114+
);
115+
$csrf_valid = CSRF::validate( $csrf_id, $csrf_token );
81116

82-
if (!$csrf_valid) {
83-
$model->error = "INVALID_CSRF";
117+
if ( !$csrf_valid ) {
118+
$model->error = 'INVALID_CSRF';
84119
return;
85120
}
86-
CSRF::invalidate($csrf_id);
121+
122+
CSRF::invalidate( $csrf_id );
87123

88124
$model->error = false;
89125

90-
$id = (int) $model->id;
91-
$parent_type = (int) $model->parent_type;
92-
$parent_id = (int) $model->parent_id;
93-
$user_id = $model->user->getId();
126+
$id = (int) $model->id;
127+
$parent_type = (int) $model->parent_type;
128+
$parent_id = (int) $model->parent_id;
129+
$user_id = $model->user->getId();
94130

95131
$log_key = null;
96-
switch ($parent_type) {
132+
switch ( $parent_type ) {
97133
case Comment::PARENT_TYPE_DOCUMENT:
98-
$log_key = EventTypes::COMMENT_DELETED_DOCUMENT; break;
134+
$log_key = EventTypes::COMMENT_EDITED_DOCUMENT; break;
99135
case Comment::PARENT_TYPE_COMMENT:
100-
$log_key = EventTypes::COMMENT_DELETED_COMMENT; break;
136+
$log_key = EventTypes::COMMENT_EDITED_COMMENT; break;
101137
case Comment::PARENT_TYPE_NEWS_POST:
102-
$log_key = EventTypes::COMMENT_DELETED_NEWS; break;
138+
$log_key = EventTypes::COMMENT_EDITED_NEWS; break;
103139
case Comment::PARENT_TYPE_PACKET:
104-
$log_key = EventTypes::COMMENT_DELETED_PACKET; break;
140+
$log_key = EventTypes::COMMENT_EDITED_PACKET; break;
105141
case Comment::PARENT_TYPE_SERVER:
106-
$log_key = EventTypes::COMMENT_DELETED_SERVER; break;
142+
$log_key = EventTypes::COMMENT_EDITED_SERVER; break;
107143
case Comment::PARENT_TYPE_USER:
108-
$log_key = EventTypes::COMMENT_DELETED_USER; break;
144+
$log_key = EventTypes::COMMENT_EDITED_USER; break;
109145
default: throw new UnexpectedValueException(
110146
'Parent type: ' . $parent_type
111147
);
112148
}
113149

114150
try {
115151

116-
$success = Comment::delete($id, $parent_type, $parent_id);
152+
$model->comment->setContent( $model->content );
153+
$model->comment->setEditedCount( $model->comment->getEditedCount() + 1 );
154+
$model->comment->setEditedDateTime(
155+
new DateTime( 'now', new DateTimeZone( 'Etc/UTC' ))
156+
);
157+
158+
$success = $model->comment->save();
117159

118-
} catch (QueryException $e) {
160+
} catch ( QueryException $e ) {
119161

120162
// SQL error occurred. We can show a friendly message to the user while
121163
// also notifying this problem to staff.
122-
Logger::logException($e);
164+
Logger::logException( $e );
123165

124166
$success = false;
125167

126168
}
127169

128-
if (!$success) {
129-
$model->error = "INTERNAL_ERROR";
170+
if ( !$success ) {
171+
$model->error = 'INTERNAL_ERROR';
130172
} else {
131173
$model->error = false;
132174
}
133175

134176
Logger::logEvent(
135177
$log_key,
136178
$user_id,
137-
getenv("REMOTE_ADDR"),
179+
getenv( 'REMOTE_ADDR' ),
138180
json_encode([
139-
"error" => $model->error,
140-
"comment_id" => $id,
141-
"parent_type" => $parent_type,
142-
"parent_id" => $parent_id
181+
'error' => $model->error,
182+
'comment_id' => $id,
183+
'content' => $model->content,
184+
'parent_type' => $parent_type,
185+
'parent_id' => $parent_id
143186
])
144187
);
145188
}
146-
147189
}

src/libraries/Comment.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ public function save() {
345345
LIMIT 1;
346346
');
347347
$stmt->bindParam(':content', $this->content, PDO::PARAM_STR);
348-
$stmt->bindParam(':created_dt', $this->created_datetime, PDO::PARAM_INT);
348+
$stmt->bindParam(':created_dt', $this->created_datetime, PDO::PARAM_STR);
349349
$stmt->bindParam(':edited_count', $this->edited_count, PDO::PARAM_INT);
350-
$stmt->bindParam(':edited_dt', $this->edited_datetime, PDO::PARAM_INT);
350+
$stmt->bindParam(':edited_dt', $this->edited_datetime, PDO::PARAM_STR);
351351
$stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
352352
$stmt->bindParam(':parent_id', $this->parent_id, PDO::PARAM_INT);
353353
$stmt->bindParam(':parent_type', $this->parent_type, PDO::PARAM_INT);
@@ -376,10 +376,22 @@ public function save() {
376376
);
377377

378378
return true;
379-
} catch (PDOException $e) {
379+
} catch ( PDOException $e ) {
380380
throw new QueryException( 'Cannot save comment', $e );
381381
}
382382
return false;
383383
}
384384

385+
public function setContent( $value ) {
386+
$this->content = $value;
387+
}
388+
389+
public function setEditedCount( $value ) {
390+
$this->edited_count = $value;
391+
}
392+
393+
public function setEditedDateTime( \DateTime $value ) {
394+
$this->edited_datetime = $value->format( 'Y-m-d H:i:s' );
395+
}
396+
385397
}

src/models/Comment/Edit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Edit extends Model {
1414
public $id;
1515
public $parent_id;
1616
public $parent_type;
17+
public $return_url;
1718
public $user;
1819

1920
}

src/templates/Comment/Edit.phtml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BNETDocs\Templates\Comment;
44

5+
use \CarlBennett\MVC\Libraries\Common;
56
use \CarlBennett\MVC\Libraries\Pair;
67

78
$title = "Edit Comment";
@@ -50,30 +51,24 @@ require("./header.inc.phtml");
5051
<article>
5152
<?php if (is_null($this->getContext()->error) && !is_null($c)) { ?>
5253
<header>Edit Comment</header>
53-
<form method="POST">
54-
<input type="hidden" name="comment_id" value="<?php echo $c_id; ?>"/>
54+
<form method="POST" action="<?php echo Common::relativeUrlToAbsolute( '/comment/edit?id=' . $c_id ); ?>">
5555
<input type="hidden" name="csrf_id" value="<?php echo $this->getContext()->csrf_id; ?>"/>
5656
<input type="hidden" name="csrf_token" value="<?php echo $this->getContext()->csrf_token; ?>"/>
5757
<section>
5858
<table class="comments"><tbody>
59-
<tr><td><a href="<?php echo $c_user_url; ?>"><img class="avatar" src="<?php echo $c_user_avatar; ?>"/> <?php echo filter_var($c_user_name, FILTER_SANITIZE_STRING); ?></a><br/><time class="comment_timestamp" datetime="<?php echo $c->getCreatedDateTime()->format("c"); ?>"><?php echo $c->getCreatedDateTime()->format("D M j, Y g:ia T"); ?></time></td><td><textarea id="comment-content" name="content" cols="80" rows="5"><?php echo $c->getContent(false); ?></textarea></td></tr>
59+
<tr><td><a href="<?php echo $c_user_url; ?>"><img class="avatar" src="<?php echo $c_user_avatar; ?>"/> <?php echo filter_var($c_user_name, FILTER_SANITIZE_STRING); ?></a><br/><time class="comment_timestamp" datetime="<?php echo $c->getCreatedDateTime()->format("c"); ?>"><?php echo $c->getCreatedDateTime()->format("D M j, Y g:ia T"); ?></time></td><td><textarea id="comment-content" name="content" cols="80" rows="5" tabindex="1" autofocus="autofocus"><?php echo filter_var( $c->getContent( false ), FILTER_SANITIZE_FULL_SPECIAL_CHARS ); ?></textarea></td></tr>
6060
</tbody></table><hr/>
6161
<p>
62-
<input class="float-right bg-green" type="submit" value="Edit Comment" tabindex="2" autofocus="autofocus"/>
63-
<a class="button button-bg-red" href="javascript:history.go(-1);" id="cancel-btn">Cancel</a>
62+
<input class="float-right bg-green" type="submit" value="Edit Comment" tabindex="2"/>
63+
<a class="button button-bg-red" href="<?php echo $this->getContext()->return_url; ?>" id="cancel-btn">Cancel</a>
6464
</p>
65-
<script type="text/javascript">
66-
if (history.length == 1) {
67-
document.getElementById('cancel-btn').className += ' button-disabled';
68-
}
69-
</script>
7065
</section>
7166
</form>
7267
<?php } else if ($this->getContext()->error === false) { ?>
7368
<header class="green">Comment Edited</header>
7469
<section class="green">
7570
<p>You have successfully edited the comment!</p>
76-
<p>Use the navigation to the left to move to another page.</p>
71+
<p><a href="<?php echo $this->getContext()->return_url; ?>#comments">Return to previous page</a></p>
7772
</section>
7873
<?php } else { ?>
7974
<header class="red">Edit Comment</header>

0 commit comments

Comments
 (0)