[aur-dev] [PATCH 0/5] 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 (5): rpc.php: Allow POST method in RPC interface Refactor pkgbase_edit_comment() to use parameter comment ID pkg_comment_form.php: Make printing of header conditional aurjson.class.php: Add methods load_comment and save_comment pkg_comments.php: Add JavaScript function to edit comments web/html/pkgbase.php | 8 +++++--- web/html/rpc.php | 5 ++++- web/lib/aurjson.class.php | 35 ++++++++++++++++++++++++++++++++++- web/lib/pkgbasefuncs.inc.php | 8 +------- web/template/pkg_comment_form.php | 7 ++++++- web/template/pkg_comments.php | 25 +++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 13 deletions(-) -- 2.4.6
Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/html/rpc.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/web/html/rpc.php b/web/html/rpc.php index 415dcb8..aa1a562 100644 --- a/web/html/rpc.php +++ b/web/html/rpc.php @@ -2,7 +2,7 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib'); include_once("aurjson.class.php"); -if ( $_SERVER['REQUEST_METHOD'] != 'GET' ) { +if ( $_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'POST' ) { header('HTTP/1.1 405 Method Not Allowed'); exit(); } @@ -10,6 +10,9 @@ if ( $_SERVER['REQUEST_METHOD'] != 'GET' ) { if ( isset($_GET['type']) ) { $rpc_o = new AurJSON(); echo $rpc_o->handle($_GET); +} elseif ( isset($_POST['type']) ) { + $rpc_o = new AurJSON(); + echo $rpc_o->handle($_POST); } else { // dump a simple usage output for people to use. -- 2.4.6
To use pkgbase_edit_comment() from within the RPC interface, no longer hardcode a specific POST variable, but use a parameter that defines the comment ID. Move the check for the existence of the POST variable to pkgbase.php. Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/html/pkgbase.php | 8 +++++--- web/lib/pkgbasefuncs.inc.php | 8 +------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/web/html/pkgbase.php b/web/html/pkgbase.php index 1581869..e025459 100644 --- a/web/html/pkgbase.php +++ b/web/html/pkgbase.php @@ -111,9 +111,11 @@ if (check_token()) { $ret = true; $fragment = '#news'; } elseif (current_action("do_EditComment")) { - list($ret, $output) = pkgbase_edit_comment($_REQUEST['comment']); - if ($ret && isset($_POST["comment_id"])) { - $fragment = '#comment-' . intval($_POST["comment_id"]); + if (isset($_POST["comment_id"])) { + list($ret, $output) = pkgbase_edit_comment($_REQUEST['comment'], $_POST["comment_id"]); + if ($ret) { + $fragment = '#comment-' . intval($_POST["comment_id"]); + } } } diff --git a/web/lib/pkgbasefuncs.inc.php b/web/lib/pkgbasefuncs.inc.php index 6057d10..a210969 100644 --- a/web/lib/pkgbasefuncs.inc.php +++ b/web/lib/pkgbasefuncs.inc.php @@ -848,18 +848,12 @@ function pkgbase_delete_comment() { * * @return array Tuple of success/failure indicator and error message */ -function pkgbase_edit_comment($comment) { +function pkgbase_edit_comment($comment, $comment_id) { $uid = uid_from_sid($_COOKIE["AURSID"]); if (!$uid) { return array(false, __("You must be logged in before you can edit package information.")); } - if (isset($_POST["comment_id"])) { - $comment_id = $_POST["comment_id"]; - } else { - return array(false, __("Missing comment ID.")); - } - $dbh = DB::connect(); if (can_edit_comment($comment_id)) { $q = "UPDATE PackageComments "; -- 2.4.6
For use in the new RPC interface to edit comments, the form shouldn't always print a header. Make this conditional. Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/template/pkg_comment_form.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/web/template/pkg_comment_form.php b/web/template/pkg_comment_form.php index 16a92b1..2eeb6f9 100644 --- a/web/template/pkg_comment_form.php +++ b/web/template/pkg_comment_form.php @@ -1,8 +1,10 @@ +<?php /* $pkgbase_name will not be set when called from aurjson.class.php */ +if (isset($pkgbase_name)): ?> <div id="generic-form" class="box"> <h2><?= (isset($comment_id)) ? __('Edit comment for: %s', htmlspecialchars($pkgbase_name)) : __("Add Comment"); ?></h2> <form action="<?= get_pkgbase_uri($pkgbase_name) ?>" method="post"> <fieldset> -<?php +<?php endif; if (isset($_REQUEST['comment']) && check_token()) { echo '<p>' . __('Comment has been added.') . '</p>'; } @@ -21,7 +23,10 @@ if (isset($_REQUEST['comment']) && check_token()) { <p> <input type="submit" value="<?= (isset($comment_id)) ? __("Save") : __("Add Comment") ?>" /> </p> +<?php +if (isset($pkgbase_name)): ?> </fieldset> </form> </div> +<?php endif; -- 2.4.6
Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/lib/aurjson.class.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php index a272741..cdb5d7e 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', 'load-comment', 'save-comment' ); private static $exposed_fields = array( 'name', 'name-desc' @@ -477,5 +477,38 @@ class AurJSON { return json_encode($result_array); } + + private function load_comment($http_data) { + $comment_id = $http_data['arg']; + list($user_id, $comment) = comment_by_id($comment_id); + + if (!has_credential(CRED_COMMENT_EDIT, array($user_id)) || is_null($comment)) { + $output = false; + } else { + ob_start(); + include('pkg_comment_form.php'); + $html = ob_get_clean(); + $output = array('form' => $html); + } + + return json_encode($output); + } + + private function save_comment($http_data) { + include_once('pkgbasefuncs.inc.php'); + $argument_array = $http_data['arg']; + $comment = $argument_array['comment']; + $comment_id = $argument_array['id']; + + list($success, $message) = pkgbase_edit_comment($comment, $comment_id); + if ($success) { + $html = parse_comment($comment); + $output = array('success' => 1, 'html' => $html); + } else { + $output = array('success' => 0, 'error' => $message); + } + + return json_encode($output); + } } -- 2.4.6
Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com> --- web/template/pkg_comments.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/web/template/pkg_comments.php b/web/template/pkg_comments.php index 26fddfd..ec50dbf 100644 --- a/web/template/pkg_comments.php +++ b/web/template/pkg_comments.php @@ -72,3 +72,28 @@ $count = pkgbase_comments_count($base_id, $include_deleted); </h3> </div> <?php endif; ?> +<script> +$(document).ready(function() { + $(".edit-comment").click(function () { + var parent_element = this.parentElement; + var edit_form = $(parent_element).next(); + var comment_id = parent_element.id.substr(8); + $.getJSON('/rpc', {type: 'load-comment', arg: comment_id}, function (data) { + edit_form.html(data.form); + edit_form.find('input[type="submit"]').click(function () { + var comment = edit_form.find('textarea').val(); + $.post('/rpc', + {type: 'save-comment', arg: {comment: comment, id: comment_id}}, + function (data) { + if (data.success) { + edit_form.html('<p>' + data.html + '</p>'); + } else { + alert(data.error); + } + }); + }); + }); + return false; + }); +}); +</script> -- 2.4.6
participants (1)
-
Marcel Korpel