Index: trunk/phpgwapi/inc/adodb/contrib/toxmlrpc.inc.php
===================================================================
--- trunk/phpgwapi/inc/adodb/contrib/toxmlrpc.inc.php	(revision 2)
+++ trunk/phpgwapi/inc/adodb/contrib/toxmlrpc.inc.php	(revision 34)
@@ -6,8 +6,11 @@
     * @author Daniele Baroncelli
     * @author Gaetano Giunta
-    * @copyright (c) 2003 Giunta/Baroncelli. All rights reserved.
+    * @copyright (c) 2003-2004 Giunta/Baroncelli. All rights reserved.
     * 
     * @todo some more error checking here and there
     * @todo document the xmlrpc-struct used to encode recordset info
+    * @todo verify if using xmlrpc_encode($rs->GetArray()) would work with:
+    *       - ADODB_FETCH_BOTH
+    *       - null values
     */
 
@@ -16,5 +19,5 @@
     */    
     require_once('xmlrpc.inc');
-    require_once('adodb.inc.php');
+    if (!defined('ADODB_DIR')) require_once('adodb.inc.php');
             
     /**
@@ -23,4 +26,22 @@
     function rs2xmlrpcval(&$adodbrs) {
 
+        $header =& rs2xmlrpcval_header($adodbrs);
+        $body =& rs2xmlrpcval_body($adodbrs);
+
+        // put it all together and build final xmlrpc struct
+        $xmlrpcrs =& new xmlrpcval ( array(
+                "header" => $header,
+                "body" => $body,
+                ), "struct");
+
+        return $xmlrpcrs;
+
+    }
+
+    /**
+    * Builds an xmlrpc struct value describing an AdoDB recordset
+    */
+    function rs2xmlrpcval_header($adodbrs)
+    {
         $numfields = $adodbrs->FieldCount();
         $numrecords = $adodbrs->RecordCount();
@@ -32,23 +53,23 @@
             $fieldarray = array();
             if (isset($fld->name))
-                $fieldarray["name"] = new xmlrpcval ($fld->name);
+                $fieldarray["name"] =& new xmlrpcval ($fld->name);
             if (isset($fld->type))
-                $fieldarray["type"] = new xmlrpcval ($fld->type);
+                $fieldarray["type"] =& new xmlrpcval ($fld->type);
             if (isset($fld->max_length))
-                $fieldarray["max_length"] = new xmlrpcval ($fld->max_length, "int");
+                $fieldarray["max_length"] =& new xmlrpcval ($fld->max_length, "int");
             if (isset($fld->not_null))
-                $fieldarray["not_null"] = new xmlrpcval ($fld->not_null, "boolean");
+                $fieldarray["not_null"] =& new xmlrpcval ($fld->not_null, "boolean");
             if (isset($fld->has_default))
-                $fieldarray["has_default"] = new xmlrpcval ($fld->has_default, "boolean");
+                $fieldarray["has_default"] =& new xmlrpcval ($fld->has_default, "boolean");
             if (isset($fld->default_value))
-                $fieldarray["default_value"] = new xmlrpcval ($fld->default_value);
-            $fieldstruct[$i] = new xmlrpcval ($fieldarray, "struct");
+                $fieldarray["default_value"] =& new xmlrpcval ($fld->default_value);
+            $fieldstruct[$i] =& new xmlrpcval ($fieldarray, "struct");
         }
-        $fieldcount = new xmlrpcval ($numfields, "int");
-        $recordcount = new xmlrpcval ($numrecords, "int");
-        $sql = new xmlrpcval ($adodbrs->sql);
-        $fieldinfo = new xmlrpcval ($fieldstruct, "array");
+        $fieldcount =& new xmlrpcval ($numfields, "int");
+        $recordcount =& new xmlrpcval ($numrecords, "int");
+        $sql =& new xmlrpcval ($adodbrs->sql);
+        $fieldinfo =& new xmlrpcval ($fieldstruct, "array");
 
-        $header = new xmlrpcval ( array(
+        $header =& new xmlrpcval ( array(
                 "fieldcount" => $fieldcount,
                 "recordcount" => $recordcount,
@@ -57,5 +78,17 @@
                 ), "struct");
 
+        return $header;
+    }
+
+    /**
+    * Builds an xmlrpc struct value out of an AdoDB recordset
+    * (data values only, no data definition)
+    */
+    function rs2xmlrpcval_body($adodbrs)
+    {
+        $numfields = $adodbrs->FieldCount();
+
         // build structure containing recordset data
+        $adodbrs->MoveFirst();
         $rows = array();
         while (!$adodbrs->EOF) {
@@ -64,31 +97,24 @@
             if ($adodbrs->fetchMode == 'ADODB_FETCH_BOTH' || count($adodbrs->fields) == 2 * $adodbrs->FieldCount())
                 for ($i = 0; $i < $numfields; $i++)
-                    if ($columns[$i] === null)
-                        $columns[$i] = new xmlrpcval ('');
+                    if ($adodbrs->fields[$i] === null)
+                        $columns[$i] =& new xmlrpcval ('');
                     else
-                        $columns[$i] = xmlrpc_encode ($adodbrs->fields[$i]);
+                        $columns[$i] =& xmlrpc_encode ($adodbrs->fields[$i]);
             else
                 foreach ($adodbrs->fields as $val)
                     if ($val === null)
-                        $columns[$i] = new xmlrpcval ('');
+                        $columns[] =& new xmlrpcval ('');
                     else
-                        $columns[] = xmlrpc_encode ($val);
+                        $columns[] =& xmlrpc_encode ($val);
 
-            $rows[] = new xmlrpcval ($columns, "array");
+            $rows[] =& new xmlrpcval ($columns, "array");
 
             $adodbrs->MoveNext();
         }
-        $body = new xmlrpcval ($rows, "array");
+        $body =& new xmlrpcval ($rows, "array");
 
-        // put it all together and build final xmlrpc struct
-        $xmlrpcrs = new xmlrpcval ( array(
-                "header" => $header,
-                "body" => $body,
-                ), "struct");
-
-        return $xmlrpcrs;
-
+        return $body;    
     }
-
+    
     /**
     * Returns an xmlrpc struct value as string out of an AdoDB recordset
@@ -125,5 +151,5 @@
         for ($i = 0; $i < $numfields; $i++) {
             $temp =& $fieldinfo->arraymem($i);
-            $fld = new ADOFieldObject();
+            $fld =& new ADOFieldObject();
             while (list($key,$value) = $temp->structeach()) {
                 if ($key == "name") $fld->name = $value->scalarval();
@@ -149,5 +175,5 @@
 
         // finally build in-memory recordset object and return it
-        $rs = new ADORecordSet_array();
+        $rs =& new ADORecordSet_array();
         $rs->InitArrayFields($data_array,$fields_array);
         return $rs;
