From 0acb134498dea9f41997f1cd8a54f81b14c8b12d Mon Sep 17 00:00:00 2001
From: Kyle Browning <kylebrowning@me.com>
Date: Fri, 25 Mar 2011 13:15:06 -0700
Subject: [PATCH] voxpelli updates for [#1058934]

---
 resources/comment_resource.inc |   55 ------------------------------
 resources/node_resource.inc    |   72 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 71 insertions(+), 56 deletions(-)

diff --git a/resources/comment_resource.inc b/resources/comment_resource.inc
index 94ce4ad..ecf7627 100644
--- a/resources/comment_resource.inc
+++ b/resources/comment_resource.inc
@@ -86,39 +86,6 @@ function _comment_resource_definition() {
         ),
       ),
       'actions' => array(
-        'loadNodeComments' => array(
-          'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
-          'help'   => t('This method returns the number of new comments on a given node since a given timestamp.'),
-          'access callback' => '_comment_resource_access',
-          'access arguments' => array('view'),
-          'access arguments append' => TRUE,
-          'callback' => '_comment_resource_load_node_comments',
-          'args'     => array(
-            array(
-              'name'         => 'nid',
-              'type'         => 'int',
-              'description'  => t('The node id to load comments for.'),
-              'source'       => 'data',
-              'optional'     => FALSE,
-            ),
-            array(
-              'name'         => 'count',
-              'type'         => 'int',
-              'description'  => t('Number of comments to load.'),
-              'source'       => 'data',
-              'optional'     => TRUE,
-              'default value'=> 10,
-            ),
-            array(
-              'name'         => 'start',
-              'type'         => 'int',
-              'description'  => t('If count is set to non-zero value, you can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.'),
-              'source'       => 'data',
-              'optional'     => TRUE,
-              'default value'=> 0,
-            ),
-          ),
-        ),
         'countAll' => array(
           'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
           'help'   => t('This method returns the number of comments on a given node.'),
@@ -276,28 +243,6 @@ function _comment_resource_delete($cid) {
 }
 
 /**
- * Returns the comments of a specified node.
- *
- * @param $nid
- *   Unique identifier for the node.
- * @param $count
- *   Number of comments to return.
- * @param $start
- *   Which comment to start with. if present, $start and $count are used together
- *   to create a LIMIT clause for selecting comments. This could be used to do paging.
- * @return
- *   An array of comment objects.
- */
-function _comment_resource_load_node_comments($nid, $count, $start) {
-  $comments = array();
-  $result = db_query_range('SELECT c.cid FROM {comment} c WHERE c.nid = :nid', $start, $count, array(':nid' => $nid));
-  foreach ($result as $record) {
-    $comments[] = comment_load($record->cid);
-  }
-  return $comments;
-}
-
-/**
  * Returns the number of comments on a given node id.
  *
  * @param $nid
diff --git a/resources/node_resource.inc b/resources/node_resource.inc
index 64ab64b..2c7cf5b 100644
--- a/resources/node_resource.inc
+++ b/resources/node_resource.inc
@@ -1,7 +1,7 @@
 <?php
 
 function _node_resource_definition() {
-  return array(
+  $node_resource = array(
     'node' => array(
       'retrieve' => array(
         'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
@@ -133,6 +133,41 @@ function _node_resource_definition() {
       ),
     ),
   );
+  if (module_exists('comment')) {
+    $comments = array(
+      'file'                    => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
+      'help'                    => t('This method returns the number of new comments on a given node.'),
+      'access callback'         => 'user_access',
+      'access arguments'        => array('access comments'),
+      'access arguments append' => FALSE,
+      'callback'                => '_node_resource_load_node_comments',
+      'args'                    => array(
+        array(
+          'name'         => 'nid',
+          'type'         => 'int',
+          'description'  => t('The node id to load comments for.'),
+          'source'       => array('path' => 0),
+          'optional'     => FALSE,
+        ),
+        array(
+          'name'         => 'count',
+          'type'         => 'int',
+          'description'  => t('Number of comments to load.'),
+          'source'       => array('param' => 'count'),
+          'optional'     => TRUE,
+        ),
+        array(
+          'name'         => 'offset',
+          'type'         => 'int',
+          'description'  => t('If count is set to non-zero value, you can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.'),
+          'source'       => array('param' => 'offset'),
+          'optional'     => TRUE,
+        ),
+      ),
+    );
+    $node_resource['node']['relationships']['comments'] =  $comments;
+  }
+  return $node_resource;
 }
 
 /**
@@ -424,3 +459,38 @@ function _node_resource_load_node_files($nid, $include_file_contents = TRUE) {
   return $files;
 }
 
+/**
+ * Returns the comments of a specified node.
+ *
+ * @param $nid
+ *   Unique identifier for the node.
+ * @param $count
+ *   Number of comments to return.
+ * @param $start
+ *   Which comment to start with. If present, $start and $count are used together
+ *   to create a LIMIT clause for selecting comments. This could be used to do paging.
+ * @return
+ *   An array of comment objects.
+ */
+function _node_resource_load_node_comments($nid, $count = 0, $start = 0) {
+  $comments = array();
+
+  $query = db_select('comment', 'c');
+  $query->innerJoin('node', 'n', 'n.nid = c.nid');
+  $query->addTag('node_access');
+  $query->fields('c', array('cid'))
+    ->condition('c.nid', $nid);
+
+  if ($count) {
+    $query->range($start, $count);
+  }
+
+  $result = $query->execute()
+    ->fetchAll();
+
+  foreach ($result as $record) {
+    $comments[] = comment_load($record->cid);
+  }
+
+  return $comments;
+}
-- 
1.7.1

