[aur-dev] [PATCH v3 0/4] Add JavaScript method to edit comments
For a better user experience, enhance the comment edit form with a JavaScript method to edit comments on the same page, using a customized RPC interface. If JavaScript is not available, the page falls back to a standard web page, where a user can edit a comment. Marcel Korpel (4): pkg_comments.php: Merge two DIVs with same ID Split pkg_comment_form.php so the outer box is not always included aurjson.class.php: Add method load_comment pkg_comments.php: Add JavaScript function to edit comments web/html/commentedit.php | 2 +- web/html/css/aurweb.css | 6 ++++++ web/html/images/ajax-loader.gif | Bin 0 -> 723 bytes web/html/index.php | 4 ++++ web/lib/aurjson.class.php | 43 +++++++++++++++++++++++++++++++++++++- web/lib/pkgbasefuncs.inc.php | 2 +- web/lib/pkgfuncs.inc.php | 2 +- web/template/pkg_comment_box.php | 4 ++++ web/template/pkg_comment_form.php | 4 ---- web/template/pkg_comments.php | 43 +++++++++++++++++++++++++++++++++----- 10 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 web/html/images/ajax-loader.gif create mode 100644 web/template/pkg_comment_box.php -- 2.4.6
Signed-off-by: Marcel Korpel
On Tue, 21 Jul 2015 at 21:56:56, Marcel Korpel wrote:
Signed-off-by: Marcel Korpel
--- Change since v1: * Always close outer <div> element. web/template/pkg_comments.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/web/template/pkg_comments.php b/web/template/pkg_comments.php index 26fddfd..373f0ed 100644 --- a/web/template/pkg_comments.php +++ b/web/template/pkg_comments.php @@ -62,13 +62,11 @@ $count = pkgbase_comments_count($base_id, $include_deleted); <?= parse_comment($row['Comments']) ?> </p> </div> - <?php endwhile; ?> -</div> +
We don't merge multiple statements when using the alternative syntax for control structures. Some please leave them as-is. Looks good otherwise!
-<?php if ($count > 10 && !isset($_GET['comments'])): ?> -<div id="news"> +if ($count > 10 && !isset($_GET['comments'])): ?> <h3> <a href="<?= htmlentities(get_pkgbase_uri($pkgbase_name), ENT_QUOTES) . '?' . mkurl('comments=all') ?>" title="<?= __('View all comments') ?> (<?= $count ?>)"><?= __('All comments', $count) ?></a> </h3> -</div> <?php endif; ?> +</div> -- 2.4.6
For use in the new RPC interface to edit comments, the form shouldn't
always print a header. Create a new template pkg_comment_box.php that
prints form and box, change template pkg_comment_form.php to only
print the form.
Signed-off-by: Marcel Korpel
Signed-off-by: Marcel Korpel
Commit message needs to be changed. Would also be nice to add 1-2 sentences that explain how this is used. On Tue, 21 Jul 2015 at 21:56:58, Marcel Korpel wrote:
Signed-off-by: Marcel Korpel
--- Changes since v2: * Drop save_comment() method. * Rename load_comment() to get_comment_form(). * Provide JSDoc. * Always use the actual parameter $http_data instead of superglobals. * Always use intval() on integer parameters. * Provide a better error handling interface. * Use early returns to be able to use less deep nesting. web/lib/aurjson.class.php | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index a272741..6a51597 100644 --- a/web/lib/aurjson.class.php +++ b/web/lib/aurjson.class.php @@ -14,7 +14,7 @@ class AurJSON { private $version = 1; private static $exposed_methods = array( 'search', 'info', 'multiinfo', 'msearch', 'suggest', - 'suggest-pkgbase' + 'suggest-pkgbase', 'get-comment-form' ); private static $exposed_fields = array( 'name', 'name-desc' @@ -477,5 +477,46 @@ class AurJSON {
return json_encode($result_array); } + + /** + * Get the HTML markup of the comment form. + * + * @param string $http_data Query parameters. + * + * @return string The JSON formatted response. + */ + private function get_comment_form($http_data) { + $comment_id = intval($http_data['arg']);
Can we move this further down so that $comment_id, $base_id and $pkgbase_name are obtained (and sanitized) at the "same time"?
+ + if (!isset($http_data['base_id']) && isset($http_data['pkgbase_name'])) {
This check looks wrong. Did you forget to amend the second part of the condition?
+ return json_encode(false); + } + + $base_id = intval($http_data['base_id']); + $pkgbase_name = $http_data['pkgbase_name']; + + list($user_id, $comment) = comment_by_id($comment_id); + + if (!has_credential(CRED_COMMENT_EDIT, array($user_id))) { + $output = array( + 'success' => 0, + 'error' => __('You do not have the right to edit this comment.'));
Just a minor nit but we usually indent multiline arrays like this: $output = array( 'success' => 0, 'error' => __('You do not have the right to edit this comment.') );
+ return json_encode($output); + } elseif (is_null($comment)) { + $output = array( + 'success' => 0, + 'error' => __('Comment does not exist.'));
Same here.
+ return json_encode($output); + } + + ob_start(); + include('pkg_comment_form.php'); + $html = ob_get_clean(); + $output = array( + 'success' => 1, + 'form' => $html);
Same here.
+ + return json_encode($output); + } }
-- 2.4.6
Signed-off-by: Marcel Korpel
participants (2)
-
Lukas Fleischer
-
Marcel Korpel