Skip to content

Commit d5bfe3f

Browse files
committed
Implement commenting on news posts
1 parent d544b93 commit d5bfe3f

File tree

9 files changed

+140
-102
lines changed

9 files changed

+140
-102
lines changed

src/controllers/API/Comment.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/controllers/Comment/Create.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace BNETDocs\Controllers\Comment;
4+
5+
use \BNETDocs\Libraries\Comment as CommentLib;
6+
use \BNETDocs\Libraries\Common;
7+
use \BNETDocs\Libraries\Controller;
8+
use \BNETDocs\Libraries\Exceptions\CommentNotFoundException;
9+
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
10+
use \BNETDocs\Libraries\Router;
11+
use \BNETDocs\Libraries\UserSession;
12+
use \BNETDocs\Models\Comment\Create as CreateModel;
13+
use \BNETDocs\Views\Comment\CreateJSON as CreateJSONView;
14+
use \UnexpectedValueException;
15+
16+
class Create extends Controller {
17+
18+
public function run(Router &$router) {
19+
switch ($router->getRequestPathExtension()) {
20+
case "json": case "":
21+
$view = new CreateJSONView();
22+
break;
23+
default:
24+
throw new UnspecifiedViewException();
25+
}
26+
$model = new CreateModel();
27+
$model->user_session = UserSession::load($router);
28+
29+
$code = 500;
30+
if (!$model->user_session) {
31+
$model->response = ["error" => "Unauthorized"];
32+
$code = 403;
33+
} else if ($router->getRequestMethod() !== "POST") {
34+
$router->setResponseHeader("Allow", "POST");
35+
$model->response = ["error" => "Method Not Allowed","allow" => ["POST"]];
36+
$code = 405;
37+
} else {
38+
$code = $this->createComment($router, $model);
39+
}
40+
41+
ob_start();
42+
$view->render($model);
43+
$router->setResponseCode($code);
44+
$router->setResponseTTL(0);
45+
$router->setResponseHeader("Content-Type", $view->getMimeType());
46+
if (!empty($model->origin) && $code >= 300 && $code <= 399) {
47+
$router->setResponseHeader("Location", $model->origin);
48+
}
49+
$router->setResponseContent(ob_get_contents());
50+
ob_end_clean();
51+
}
52+
53+
protected function createComment(Router &$router, CreateModel &$model) {
54+
$query = $router->getRequestBodyArray();
55+
$p_id = (isset($query["parent_id" ]) ? $query["parent_id" ] : null);
56+
$p_type = (isset($query["parent_type"]) ? $query["parent_type"] : null);
57+
$content = (isset($query["content" ]) ? $query["content" ] : null);
58+
59+
if ($p_id !== null) $p_id = (int) $p_id;
60+
if ($p_type !== null) $p_type = (int) $p_type;
61+
62+
switch ($p_type) {
63+
case CommentLib::PARENT_TYPE_DOCUMENT: $origin = "/document/"; break;
64+
case CommentLib::PARENT_TYPE_COMMENT: $origin = "/comment/"; break;
65+
case CommentLib::PARENT_TYPE_NEWS_POST: $origin = "/news/"; break;
66+
case CommentLib::PARENT_TYPE_PACKET: $origin = "/packet/"; break;
67+
case CommentLib::PARENT_TYPE_SERVER: $origin = "/server/"; break;
68+
case CommentLib::PARENT_TYPE_USER: $origin = "/user/"; break;
69+
default: throw new UnexpectedValueException("Parent type: " . $p_type);
70+
}
71+
$origin = Common::relativeUrlToAbsolute($origin . $p_id . "#comments");
72+
$model->origin = $origin;
73+
74+
if (empty($content)) {
75+
$success = false;
76+
} else {
77+
$success = CommentLib::create(
78+
$p_type, $p_id, $model->user_session->user_id, $content
79+
);
80+
}
81+
82+
$model->response = [
83+
"content" => $content,
84+
"error" => ($success ? false : true),
85+
"origin" => $origin,
86+
"parent_id" => $p_id,
87+
"parent_type" => $p_type
88+
];
89+
90+
return 303;
91+
}
92+
93+
}

src/libraries/Comment.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,35 @@ public function __construct($data) {
6262
}
6363
}
6464

65+
public static function create($parent_type, $parent_id, $user_id, $content) {
66+
if (!isset(Common::$database)) {
67+
Common::$database = DatabaseDriver::getDatabaseObject();
68+
}
69+
$successful = false;
70+
try {
71+
$stmt = Common::$database->prepare("
72+
INSERT INTO `comments` (
73+
`id`, `parent_type`, `parent_id`, `user_id`, `created_datetime`,
74+
`edited_count`, `edited_datetime`, `content`
75+
) VALUES (
76+
NULL, :parent_type, :parent_id, :user_id, NOW(), 0, NULL, :content
77+
);
78+
");
79+
$stmt->bindParam(":parent_type", $parent_type, PDO::PARAM_INT);
80+
$stmt->bindParam(":parent_id", $parent_id, PDO::PARAM_INT);
81+
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
82+
$stmt->bindParam(":content", $content, PDO::PARAM_STR);
83+
$successful = $stmt->execute();
84+
$stmt->closeCursor();
85+
} catch (PDOException $e) {
86+
throw new QueryException("Cannot create comment", $e);
87+
} finally {
88+
$ck = "bnetdocs-comment-" . $parent_type . "-" . $parent_id;
89+
Common::$cache->delete($ck);
90+
return $successful;
91+
}
92+
}
93+
6594
public static function getAll($parent_type, $parent_id) {
6695
$ck = "bnetdocs-comment-" . $parent_type . "-" . $parent_id;
6796
$cv = Common::$cache->get($ck);

src/libraries/Router.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace BNETDocs\Libraries;
44

5-
use \BNETDocs\Controllers\API\Comment as APICommentController;
65
use \BNETDocs\Controllers\Attachment\Download as AttachmentDownloadController;
6+
use \BNETDocs\Controllers\Comment\Create as CommentCreateController;
77
use \BNETDocs\Controllers\Credits as CreditsController;
88
use \BNETDocs\Controllers\Document\Index as DocumentIndexController;
99
use \BNETDocs\Controllers\Document\Popular as DocumentPopularController;
@@ -266,10 +266,10 @@ public function route(Pair &$redirect = null) {
266266
);
267267
}
268268
break;
269-
case "api":
269+
case "comment":
270270
switch ($subpath) {
271-
case "comment":
272-
$controller = new APICommentController();
271+
case "create":
272+
$controller = new CommentCreateController();
273273
break;
274274
default:
275275
throw new ControllerNotFoundException(

src/models/API/Comment.php renamed to src/models/Comment/Create.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22

3-
namespace BNETDocs\Models\API;
3+
namespace BNETDocs\Models\Comment;
44

55
use \BNETDocs\Libraries\Model;
66

7-
class Comment extends Model {
7+
class Create extends Model {
88

9+
public $origin;
910
public $response;
1011
public $user_session;
1112

1213
public function __construct() {
1314
parent::__construct();
15+
$this->origin = null;
1416
$this->response = null;
1517
$this->user_session = null;
1618
}

src/templates/News/View.phtml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace BNETDocs\Templates;
33

4+
use \BNETDocs\Libraries\Comment;
45
use \BNETDocs\Libraries\Common;
56
use \BNETDocs\Libraries\Gravatar;
67
use \BNETDocs\Libraries\NewsPost;
@@ -80,7 +81,7 @@ require("./header.inc.phtml");
8081
</footer>
8182
</article>
8283
<article>
83-
<header>Comments</header>
84+
<header><a name="comments">Comments</a></header>
8485
<section>
8586
<?php if (!$comments) { ?>
8687
<p class="center"><em>no one has commented yet.</em></p>
@@ -100,9 +101,11 @@ require("./header.inc.phtml");
100101
</section>
101102
<?php if ($logged_in) { ?>
102103
<section>
103-
<form method="POST" action="?">
104+
<form method="POST" action="<?php echo Common::relativeUrlToAbsolute("/comment/create"); ?>">
105+
<input type="hidden" name="parent_type" value="<?php echo Comment::PARENT_TYPE_NEWS_POST; ?>"/>
106+
<input type="hidden" name="parent_id" value="<?php echo htmlspecialchars($object_id, ENT_HTML5, "UTF-8"); ?>"/>
104107
<p class="center"><label for="comment-content">Comment on this post:</label></p>
105-
<p class="center"><textarea id="comment-content" name="comment-content" cols="80" rows="5"></textarea></p>
108+
<p class="center"><textarea id="comment-content" name="content" cols="80" rows="5"></textarea></p>
106109
<p class="center"><input type="submit" value="Comment"/></p>
107110
</form>
108111
</section>

src/views/API/CommentJSON.php renamed to src/views/Comment/CreateJSON.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
<?php
22

3-
namespace BNETDocs\Views\API;
3+
namespace BNETDocs\Views\Comment;
44

55
use \BNETDocs\Libraries\Common;
66
use \BNETDocs\Libraries\Exceptions\IncorrectModelException;
77
use \BNETDocs\Libraries\Model;
88
use \BNETDocs\Libraries\View;
9-
use \BNETDocs\Models\API\Comment as CommentModel;
10-
use \ReflectionExtension;
9+
use \BNETDocs\Models\Comment\Create as CreateModel;
1110

12-
class CommentJSON extends View {
11+
class CreateJSON extends View {
1312

1413
public function getMimeType() {
1514
return "application/json;charset=utf-8";
1615
}
1716

1817
public function render(Model &$model) {
19-
if (!$model instanceof CommentModel) {
18+
if (!$model instanceof CreateModel) {
2019
throw new IncorrectModelException();
2120
}
2221
echo json_encode($model->response, Common::prettyJSONIfBrowser());

src/views/StatusJSON.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use \BNETDocs\Libraries\Model;
88
use \BNETDocs\Libraries\View;
99
use \BNETDocs\Models\Status as StatusModel;
10-
use \ReflectionExtension;
1110

1211
class StatusJSON extends View {
1312

src/views/StatusPlain.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use \BNETDocs\Libraries\Model;
88
use \BNETDocs\Libraries\View;
99
use \BNETDocs\Models\Status as StatusModel;
10-
use \ReflectionExtension;
1110

1211
class StatusPlain extends View {
1312

0 commit comments

Comments
 (0)