Index: unfuddle_api.classes.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/unfuddle_api/unfuddle_api.classes.inc,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 unfuddle_api.classes.inc
--- unfuddle_api.classes.inc	8 Nov 2009 20:42:33 -0000	1.2.2.1
+++ unfuddle_api.classes.inc	16 Nov 2009 23:02:01 -0000
@@ -9,34 +9,34 @@
  * Primary Unfuddle API implementation class.
  */
 class Unfuddle {
-  
+
   protected $url;
   protected $user;
   protected $pass;
   protected $project;
-  
+
   protected $format = 'json';
-  
+
   public function __construct($url = NULL, $user = NULL, $pass = NULL, $project = NULL) {
     $this->url = is_null($url) ? variable_get('unfuddle_api_url', '') : $url;
     $this->user = is_null($user) ? variable_get('unfuddle_api_user', '') : $user;
     $this->pass = is_null($pass) ? variable_get('unfuddle_api_pass', '') : $pass;
     $this->project = is_null($project) ? variable_get('unfuddle_api_project', '') : $project;
   }
-  
+
   public function setURL($url) {
     $this->url = $url;
   }
-  
+
   public function setAuth($user, $pass) {
     $this->user = $user;
     $this->pass = $pass;
   }
-  
+
   public function setProject($projectID) {
     $this->project = $projectID;
   }
-  
+
   // Projects.
   public function getProjects() {
     try {
@@ -49,7 +49,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getProject($projectID = NULL) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
     try {
@@ -62,7 +62,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   // People.
   public function getPeople($projectID = NULL) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
@@ -76,7 +76,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getProjectPeople($projectID = NULL) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
     try {
@@ -89,7 +89,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getPerson($personID) {
     try {
       $person = $this->request($this->url . '/api/v1/people/' . $personID);
@@ -101,7 +101,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   // Messages.
   public function getMessages($projectID = NULL) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
@@ -115,7 +115,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getMessage($projectID = NULL, $messageID) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
     try {
@@ -128,18 +128,17 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function createMessage($projectID = NULL, $title, $body) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
-    $message =
-    "<message>\n".
-    "  <body>" . $body . "</body>\n".
-    "  <title>" . $title . "</title>\n".
-    "  </message>\n";
-    
+
+    $xml = new SimpleXMLElement('<message></message>');
+    $xml->addChild('body', $body);
+    $xml->addChild('title', $title);
+
     // POST message.
     try {
-      $response = $this->request($this->url . '/api/v1/projects/' . $projectID . '/messages', $message, 'POST');
+      $response = $this->request($this->url . '/api/v1/projects/' . $projectID . '/messages', $xml->asXML(), 'POST');
       watchdog('unfuddle_api', 'Unfuddle message !title created', array('!title' => $title));
       // Extract the message ID from the Location URL.
       return substr($response->headers['Location'], strrpos($response->headers['Location'], '/') + 1);
@@ -150,7 +149,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getCategories($projectID = NULL) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
     try {
@@ -163,7 +162,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getCategory($projectID = NULL, $categoryID) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
     try {
@@ -176,7 +175,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   // Tickets.
   public function getTickets($projectID = NULL) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
@@ -190,7 +189,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getTicket($projectID = NULL, $ticketID) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
     try {
@@ -203,7 +202,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
+
   public function getTicketByNumber($projectID = NULL, $ticketNum) {
     $projectID = is_null($projectID) ? $this->project : $projectID;
     try {
@@ -216,22 +215,25 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
-  public function createTicket($projectID = NULL, $summary, $description, $fields = array()) {
-    $projectID = is_null($projectID) ? $this->project : $projectID;
-    $message =
-    "<ticket>\n".
-    "  <summary>" . $summary . "</summary>\n".
-    "  <description><![CDATA[" . $description . "]]></description>\n".
-    "  <priority>" . isset($fields['priority']) ? $fields['priority'] : 5  . "</priority>\n".
-    "  <project-id>" . $projectID . "</project-id>\n".
-    "  <status>new</status>\n".
-    "  </ticket>\n";
-    // @TODO handle other fields, maybe create a SimpleXML object?
-    
+
+  public function createTicket($summary, $description, $fields = array()) {
+    // Merge default values.
+    $fields += array(
+      'priority' => 3,
+      'project-id' => $this->project,
+      'status' => 'new',
+    );
+
+    $xml = new SimpleXMLElement('<ticket></ticket>');
+    $xml->addChild('summary', $summary);
+    $xml->addChild('description', '<![CDATA[' . $description . ']]>');
+    foreach ($fields as $field => $data) {
+      $xml->addChild($field, $data);
+    }
+
     // POST message.
     try {
-      $response = $this->request($this->url . '/api/v1/projects/' . $projectID . '/tickets', $message, 'POST');
+      $response = $this->request($this->url . '/api/v1/projects/' . $fields['project-id'] . '/tickets', $xml->asXML(), 'POST');
       watchdog('unfuddle_api', 'Unfuddle ticket !summary created', array('!summary' => $summary));
       // Extract the ticket ID from the Location URL.
       return substr($response->headers['Location'], strrpos($response->headers['Location'], '/') + 1);
@@ -242,9 +244,7 @@ class Unfuddle {
       return FALSE;
     }
   }
-  
-  
-  
+
   /**
    * Build and perform the request.
    */
@@ -271,7 +271,7 @@ class Unfuddle {
         $headers['Accept'] = 'application/xml';
         break;
     }
-    
+
     $response = drupal_http_request($url, $headers, $method, $data);
     switch ($response->code) {
       case '200':
@@ -286,7 +286,7 @@ class Unfuddle {
         throw new Exception($response->error);
     }
   }
-  
+
   protected function parseResponse($response) {
     switch ($this->format) {
       case 'json':
@@ -295,4 +295,4 @@ class Unfuddle {
         return @simplexml_load_string($response);
     }
   }
-}
\ No newline at end of file
+}
