This method will be used by the JavaScript comment editing and produces
a form containing the comment.

Signed-off-by: Marcel Korpel <marcel.kor...@gmail.com>
---
Changes since v3:
* Update commit message.
* Improve JSDoc to match the other ones in aurjson.class.php.
* Correct check for existence of query parameters and return decent
  error message (like in other cases of failures).
* Move retrieval of query parameters in one block.
* Improve formatting.

 web/lib/aurjson.class.php | 51 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/web/lib/aurjson.class.php b/web/lib/aurjson.class.php
index a272741..87d941e 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,54 @@ class AurJSON {
 
                return json_encode($result_array);
        }
+
+       /**
+        * Get the HTML markup of the comment form.
+        *
+        * @param string $comment_id ID of comment.
+        * @param string $base_id ID of package base.
+        * @param string $pkgbase_name Name of package base.
+        *
+        * @return string The JSON formatted response.
+        */
+       private function get_comment_form($http_data) {
+               if (!isset($http_data['base_id']) || 
!isset($http_data['pkgbase_name'])) {
+                       $output = array(
+                               'success' => 0,
+                               'error' => __('Parameters pkgbase_name and/or 
base_id are not defined.')
+                       );
+                       return json_encode($output);
+               }
+
+               $comment_id = intval($http_data['arg']);
+               $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.')
+                       );
+                       return json_encode($output);
+               } elseif (is_null($comment)) {
+                       $output = array(
+                               'success' => 0,
+                               'error' => __('Comment does not exist.')
+                       );
+                       return json_encode($output);
+               }
+
+               ob_start();
+               include('pkg_comment_form.php');
+               $html = ob_get_clean();
+               $output = array(
+                       'success' => 1,
+                       'form' => $html
+               );
+
+               return json_encode($output);
+       }
 }
 
-- 
2.4.6

Reply via email to