| 1 |
<?php |
<?php |
| 2 |
// $Id: unfuddle_api.classes.inc,v 1.2.2.2 2009/11/23 22:26:19 lynn Exp $ |
// $Id: unfuddle_api.classes.inc,v 1.2.2.3 2009/11/23 22:28:06 lynn Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file Contains Unfuddle API classes for Unfuddle API Drupal module. |
* @file Contains Unfuddle API classes for Unfuddle API Drupal module. |
| 9 |
* Primary Unfuddle API implementation class. |
* Primary Unfuddle API implementation class. |
| 10 |
*/ |
*/ |
| 11 |
class Unfuddle { |
class Unfuddle { |
| 12 |
|
|
| 13 |
protected $url; |
protected $url; |
| 14 |
protected $user; |
protected $user; |
| 15 |
protected $pass; |
protected $pass; |
| 16 |
protected $project; |
protected $project; |
| 17 |
|
|
| 18 |
protected $format = 'json'; |
protected $format = 'json'; |
| 19 |
|
|
| 20 |
public function __construct($url = NULL, $user = NULL, $pass = NULL, $project = NULL) { |
public function __construct($url = NULL, $user = NULL, $pass = NULL, $project = NULL) { |
| 21 |
$this->url = is_null($url) ? variable_get('unfuddle_api_url', '') : $url; |
$this->url = is_null($url) ? variable_get('unfuddle_api_url', '') : $url; |
| 22 |
$this->user = is_null($user) ? variable_get('unfuddle_api_user', '') : $user; |
$this->user = is_null($user) ? variable_get('unfuddle_api_user', '') : $user; |
| 23 |
$this->pass = is_null($pass) ? variable_get('unfuddle_api_pass', '') : $pass; |
$this->pass = is_null($pass) ? variable_get('unfuddle_api_pass', '') : $pass; |
| 24 |
$this->project = is_null($project) ? variable_get('unfuddle_api_project', '') : $project; |
$this->project = is_null($project) ? variable_get('unfuddle_api_project', '') : $project; |
| 25 |
} |
} |
| 26 |
|
|
| 27 |
public function setURL($url) { |
public function setURL($url) { |
| 28 |
$this->url = $url; |
$this->url = $url; |
| 29 |
} |
} |
| 30 |
|
|
| 31 |
public function setAuth($user, $pass) { |
public function setAuth($user, $pass) { |
| 32 |
$this->user = $user; |
$this->user = $user; |
| 33 |
$this->pass = $pass; |
$this->pass = $pass; |
| 34 |
} |
} |
| 35 |
|
|
| 36 |
public function setProject($projectID) { |
public function setProject($projectID) { |
| 37 |
$this->project = $projectID; |
$this->project = $projectID; |
| 38 |
} |
} |
| 39 |
|
|
| 40 |
// Projects. |
// Projects. |
| 41 |
public function getProjects() { |
public function getProjects() { |
| 42 |
try { |
try { |
| 49 |
return FALSE; |
return FALSE; |
| 50 |
} |
} |
| 51 |
} |
} |
| 52 |
|
|
| 53 |
public function getProject($projectID = NULL) { |
public function getProject($projectID = NULL) { |
| 54 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 55 |
try { |
try { |
| 62 |
return FALSE; |
return FALSE; |
| 63 |
} |
} |
| 64 |
} |
} |
| 65 |
|
|
| 66 |
// People. |
// People. |
| 67 |
public function getPeople($projectID = NULL) { |
public function getPeople($projectID = NULL) { |
| 68 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 76 |
return FALSE; |
return FALSE; |
| 77 |
} |
} |
| 78 |
} |
} |
| 79 |
|
|
| 80 |
public function getProjectPeople($projectID = NULL) { |
public function getProjectPeople($projectID = NULL) { |
| 81 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 82 |
try { |
try { |
| 89 |
return FALSE; |
return FALSE; |
| 90 |
} |
} |
| 91 |
} |
} |
| 92 |
|
|
| 93 |
public function getPerson($personID) { |
public function getPerson($personID) { |
| 94 |
try { |
try { |
| 95 |
$person = $this->request($this->url . '/api/v1/people/' . $personID); |
$person = $this->request($this->url . '/api/v1/people/' . $personID); |
| 101 |
return FALSE; |
return FALSE; |
| 102 |
} |
} |
| 103 |
} |
} |
| 104 |
|
|
| 105 |
// Messages. |
// Messages. |
| 106 |
public function getMessages($projectID = NULL) { |
public function getMessages($projectID = NULL) { |
| 107 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 115 |
return FALSE; |
return FALSE; |
| 116 |
} |
} |
| 117 |
} |
} |
| 118 |
|
|
| 119 |
public function getMessage($projectID = NULL, $messageID) { |
public function getMessage($projectID = NULL, $messageID) { |
| 120 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 121 |
try { |
try { |
| 128 |
return FALSE; |
return FALSE; |
| 129 |
} |
} |
| 130 |
} |
} |
| 131 |
|
|
| 132 |
public function createMessage($projectID = NULL, $title, $body) { |
public function createMessage($projectID = NULL, $title, $body) { |
| 133 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 134 |
$message = |
|
| 135 |
"<message>\n". |
$xml = new SimpleXMLElement('<message></message>'); |
| 136 |
" <body>" . $body . "</body>\n". |
$xml->addChild('body', $body); |
| 137 |
" <title>" . $title . "</title>\n". |
$xml->addChild('title', $title); |
| 138 |
" </message>\n"; |
|
|
|
|
| 139 |
// POST message. |
// POST message. |
| 140 |
try { |
try { |
| 141 |
$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'); |
| 142 |
watchdog('unfuddle_api', 'Unfuddle message !title created', array('!title' => $title)); |
watchdog('unfuddle_api', 'Unfuddle message !title created', array('!title' => $title)); |
| 143 |
// Extract the message ID from the Location URL. |
// Extract the message ID from the Location URL. |
| 144 |
return substr($response->headers['Location'], strrpos($response->headers['Location'], '/') + 1); |
return substr($response->headers['Location'], strrpos($response->headers['Location'], '/') + 1); |
| 149 |
return FALSE; |
return FALSE; |
| 150 |
} |
} |
| 151 |
} |
} |
| 152 |
|
|
| 153 |
public function getCategories($projectID = NULL) { |
public function getCategories($projectID = NULL) { |
| 154 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 155 |
try { |
try { |
| 162 |
return FALSE; |
return FALSE; |
| 163 |
} |
} |
| 164 |
} |
} |
| 165 |
|
|
| 166 |
public function getCategory($projectID = NULL, $categoryID) { |
public function getCategory($projectID = NULL, $categoryID) { |
| 167 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 168 |
try { |
try { |
| 175 |
return FALSE; |
return FALSE; |
| 176 |
} |
} |
| 177 |
} |
} |
| 178 |
|
|
| 179 |
// Tickets. |
// Tickets. |
| 180 |
public function getTickets($projectID = NULL) { |
public function getTickets($projectID = NULL) { |
| 181 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 189 |
return FALSE; |
return FALSE; |
| 190 |
} |
} |
| 191 |
} |
} |
| 192 |
|
|
| 193 |
public function getTicket($projectID = NULL, $ticketID) { |
public function getTicket($projectID = NULL, $ticketID) { |
| 194 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 195 |
try { |
try { |
| 202 |
return FALSE; |
return FALSE; |
| 203 |
} |
} |
| 204 |
} |
} |
| 205 |
|
|
| 206 |
public function getTicketByNumber($projectID = NULL, $ticketNum) { |
public function getTicketByNumber($projectID = NULL, $ticketNum) { |
| 207 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
$projectID = is_null($projectID) ? $this->project : $projectID; |
| 208 |
try { |
try { |
| 215 |
return FALSE; |
return FALSE; |
| 216 |
} |
} |
| 217 |
} |
} |
| 218 |
|
|
| 219 |
public function createTicket($projectID = NULL, $summary, $description, $fields = array()) { |
public function createTicket($summary, $description, $fields = array()) { |
| 220 |
$projectID = is_null($projectID) ? $this->project : $projectID; |
// Merge default values. |
| 221 |
$message = |
$fields += array( |
| 222 |
"<ticket>\n". |
'priority' => 3, |
| 223 |
" <summary>" . $summary . "</summary>\n". |
'project-id' => $this->project, |
| 224 |
" <description><![CDATA[" . $description . "]]></description>\n". |
'status' => 'new', |
| 225 |
" <priority>" . (isset($fields['priority']) ? $fields['priority'] : 3) . "</priority>\n". |
); |
| 226 |
" <project-id>" . $projectID . "</project-id>\n". |
|
| 227 |
" <status>new</status>\n". |
$xml = new SimpleXMLElement('<ticket></ticket>'); |
| 228 |
" </ticket>\n"; |
$xml->addChild('summary', $summary); |
| 229 |
// @TODO handle other fields, maybe create a SimpleXML object? |
$xml->addChild('description', '<![CDATA[' . $description . ']]>'); |
| 230 |
|
foreach ($fields as $field => $data) { |
| 231 |
|
$xml->addChild($field, $data); |
| 232 |
|
} |
| 233 |
|
|
| 234 |
// POST message. |
// POST message. |
| 235 |
try { |
try { |
| 236 |
$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'); |
| 237 |
watchdog('unfuddle_api', 'Unfuddle ticket !summary created', array('!summary' => $summary)); |
watchdog('unfuddle_api', 'Unfuddle ticket !summary created', array('!summary' => $summary)); |
| 238 |
// Extract the ticket ID from the Location URL. |
// Extract the ticket ID from the Location URL. |
| 239 |
return substr($response->headers['Location'], strrpos($response->headers['Location'], '/') + 1); |
return substr($response->headers['Location'], strrpos($response->headers['Location'], '/') + 1); |
| 244 |
return FALSE; |
return FALSE; |
| 245 |
} |
} |
| 246 |
} |
} |
| 247 |
|
|
|
|
|
|
|
|
| 248 |
/** |
/** |
| 249 |
* Build and perform the request. |
* Build and perform the request. |
| 250 |
*/ |
*/ |
| 271 |
$headers['Accept'] = 'application/xml'; |
$headers['Accept'] = 'application/xml'; |
| 272 |
break; |
break; |
| 273 |
} |
} |
| 274 |
|
|
| 275 |
$response = drupal_http_request($url, $headers, $method, $data); |
$response = drupal_http_request($url, $headers, $method, $data); |
| 276 |
switch ($response->code) { |
switch ($response->code) { |
| 277 |
case '200': |
case '200': |
| 286 |
throw new Exception($response->error); |
throw new Exception($response->error); |
| 287 |
} |
} |
| 288 |
} |
} |
| 289 |
|
|
| 290 |
protected function parseResponse($response) { |
protected function parseResponse($response) { |
| 291 |
switch ($this->format) { |
switch ($this->format) { |
| 292 |
case 'json': |
case 'json': |