From cb3746c082ae395aeebcab367ac35bf61575a1d6 Mon Sep 17 00:00:00 2001
From: Moshe Weitzman <weitzman@tejasa.com>
Date: Tue, 9 Aug 2011 00:31:36 -0400
Subject: [PATCH] Issue #112805 by greg.1.anderson, moshe weitzman |
 jfm@www.moerks.dk: Added JSON menu callback for project
 issues.

---
 comment_upload.module |   36 ++++++++++++++++++++++++++++++++++--
 1 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/comment_upload.module b/comment_upload.module
index 4cb9ca0..80847e3 100644
--- a/comment_upload.module
+++ b/comment_upload.module
@@ -195,6 +195,38 @@ function comment_upload_file_download($filepath) {
 }
 
 /*
+ * Implements hook_project_issue_json_alter().
+ */
+function comment_upload_project_issue_json_alter(&$issue) {
+  $result = comment_upload_fetch_all($issue['id'], FALSE);
+  while ($row = db_fetch_object($result)) {
+    $issue['attachments'][$row->cid]['contributor'] = $row->uid;
+    $issue['attachments'][$row->cid]['comment-id'] = $row->cid;
+    $issue['attachments'][$row->cid]['urls'][$row->fid] = file_create_url($row->filepath);
+  }
+}
+
+/**
+ * Retrieve all files attached to a given node.
+ *
+ * @param $nid
+ *   A node ID.
+ * @param $include_unpublished
+ *   If TRUE, return all nodes; if FALSE, only return nodes where status == COMMENT_PUBLISHED
+ * @return resource
+ *   A database result resource or FALSE if the query fails.
+ */
+function comment_upload_fetch_all($nid, $include_unpublished = FALSE) {
+  $q = "SELECT cu.fid, cu.nid, cu.cid, f.filepath, c.status, c.thread, u.uid, u.name  FROM {comment_upload} cu INNER JOIN {files} f ON cu.fid = f.fid INNER JOIN {comments} c ON cu.cid = c.cid INNER JOIN {users} u ON c.uid = u.uid WHERE cu.nid = %d";
+  if ($include_unpublished) {
+    return db_query("$q ORDER BY cu.cid ASC, fid ASC", $nid);
+  }
+  else {
+    return db_query("$q AND c.status = %d ORDER BY cu.cid ASC, fid ASC", $nid, COMMENT_PUBLISHED);
+  }
+}
+
+/*
  * Listen to node deletion and delete files from comments on that node.
  *
  * (comment_nodeapi does not call comment APIs when deleting).
@@ -203,7 +235,7 @@ function comment_upload_file_download($filepath) {
  */
 function comment_upload_nodeapi(&$node, $op, $arg = 0) {
   if ($op == 'delete') {
-    $result = db_query("SELECT cu.fid, cu.nid, f.filepath FROM {comment_upload} cu INNER JOIN {files} f ON cu.fid = f.fid WHERE cu.nid = %d", $node->nid);
+    $result = comment_upload_fetch_all($node->nid, TRUE);
     while ($row = db_fetch_array($result)) {
       // comment_upload_delete_file just needs a filepath and a fid.
       comment_upload_delete_file($row);
@@ -667,4 +699,4 @@ function template_preprocess_comment_upload_attachments(&$variables) {
       }
     }
   }
-}
\ No newline at end of file
+}
-- 
1.7.6

