| 1 |
<?php
|
| 2 |
// $Id: freshbooks.module,v 1.4 2008/09/14 02:15:10 rszrama Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Integrates your site with the FreshBooks accounting application.
|
| 7 |
*
|
| 8 |
* FreshBooks makes it easy for you to track time spent on a project and then
|
| 9 |
* generate and e-mail (or snail mail) invoices to your clients. You can sign
|
| 10 |
* up for a trial account today using the following referral link to support
|
| 11 |
* continued development of this module:
|
| 12 |
*
|
| 13 |
* https://bywombats.freshbooks.com/signup/
|
| 14 |
*/
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_menu().
|
| 18 |
*/
|
| 19 |
function freshbooks_menu() {
|
| 20 |
$items = array();
|
| 21 |
|
| 22 |
$items['admin/settings/freshbooks'] = array(
|
| 23 |
'title' => 'FreshBooks',
|
| 24 |
'description' => 'Configure your site to integrate with FreshBooks.',
|
| 25 |
'page callback' => 'drupal_get_form',
|
| 26 |
'page arguments' => array('freshbooks_settings_form'),
|
| 27 |
'access arguments' => array('administer freshbooks'),
|
| 28 |
);
|
| 29 |
$items['admin/settings/freshbooks/test'] = array(
|
| 30 |
'title' => 'Test FreshBooks',
|
| 31 |
'description' => 'Test the API as it develops!',
|
| 32 |
'page callback' => 'freshbooks_test',
|
| 33 |
'access arguments' => array('administer freshbooks'),
|
| 34 |
);
|
| 35 |
|
| 36 |
return $items;
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Implementation of hook_perm().
|
| 41 |
*/
|
| 42 |
function freshbooks_perm() {
|
| 43 |
return array('administer freshbooks');
|
| 44 |
}
|
| 45 |
|
| 46 |
// Displays the module settings form.
|
| 47 |
function freshbooks_settings_form() {
|
| 48 |
$form = array();
|
| 49 |
|
| 50 |
// Setup a variable to hide the API credentials once they've been entered.
|
| 51 |
$api_url = variable_get('freshbooks_api_url', '');
|
| 52 |
$token = variable_get('freshbooks_token', '');
|
| 53 |
|
| 54 |
if (empty($api_url) || empty($token)) {
|
| 55 |
$collapse = FALSE;
|
| 56 |
}
|
| 57 |
else {
|
| 58 |
$collapse = TRUE;
|
| 59 |
}
|
| 60 |
|
| 61 |
$form['api'] = array(
|
| 62 |
'#type' => 'fieldset',
|
| 63 |
'#title' => t('Default API credentials'),
|
| 64 |
'#description' => t('Find your credentials by browsing to the "Enable FreshBooks API" settings page in your account. Once you agree to the terms of service, an API URL and authentication token will be displayed.'),
|
| 65 |
'#collapsible' => $collapse,
|
| 66 |
'#collapsed' => $collapse,
|
| 67 |
);
|
| 68 |
$form['api']['freshbooks_api_url'] = array(
|
| 69 |
'#type' => 'textfield',
|
| 70 |
'#title' => t('API URL'),
|
| 71 |
'#default_value' => variable_get('freshbooks_api_url', ''),
|
| 72 |
);
|
| 73 |
$form['api']['freshbooks_token'] = array(
|
| 74 |
'#type' => 'textfield',
|
| 75 |
'#title' => t('Authentication token'),
|
| 76 |
'#default_value' => variable_get('freshbooks_token', ''),
|
| 77 |
);
|
| 78 |
|
| 79 |
return system_settings_form($form);
|
| 80 |
}
|
| 81 |
|
| 82 |
// Tests the functionality of the module as it develops.
|
| 83 |
function freshbooks_test() {
|
| 84 |
// Attempt a connection and return an appropriate message!
|
| 85 |
if ($fb = freshbooks_connect()) {
|
| 86 |
drupal_set_message(t('Your API credentials have been verified by the FreshBooks server!'));
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
return t('Your API credentials failed authentication.');
|
| 90 |
}
|
| 91 |
|
| 92 |
$header = array(t('Client ID'), t('Organization'), t('Username'), t('First name'), t('Last name'), t('E-mail'));
|
| 93 |
$rows = array();
|
| 94 |
|
| 95 |
// Loop through the clients and add them to the table.
|
| 96 |
foreach ($fb->clientList() as $client) {
|
| 97 |
$rows[] = array(
|
| 98 |
check_plain($client->client_id),
|
| 99 |
check_plain($client->organization),
|
| 100 |
check_plain($client->username),
|
| 101 |
check_plain($client->first_name),
|
| 102 |
check_plain($client->last_name),
|
| 103 |
check_plain($client->email),
|
| 104 |
);
|
| 105 |
}
|
| 106 |
|
| 107 |
// Display a message if we found no clients.
|
| 108 |
if (empty($rows)) {
|
| 109 |
$rows[] = array('data' => t('No clients found.'), 'colspan' => 6);
|
| 110 |
}
|
| 111 |
|
| 112 |
$output .= theme('table', $header, $rows, array(), t('FreshBooks clients'));
|
| 113 |
|
| 114 |
return $output;
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Creates an object used for communicating with the FreshBooks server and tests
|
| 119 |
* the connection to verify the API credentials.
|
| 120 |
*
|
| 121 |
* @param $api_url
|
| 122 |
* The API URL for the FreshBooks account you want to use. Defaults to the URL
|
| 123 |
* set in the settings form.
|
| 124 |
* @param $token
|
| 125 |
* The authentication token used to verify the connection. Defaults to the
|
| 126 |
* token set in the settings form.
|
| 127 |
* @return
|
| 128 |
* The DrupalFreshBooks object used to communicate with the FreshBooks server
|
| 129 |
* is successful or FALSE if a connection could not be established.
|
| 130 |
*/
|
| 131 |
function freshbooks_connect($reconnect = FALSE, $api_url = NULL, $token = NULL) {
|
| 132 |
static $fb = FALSE;
|
| 133 |
|
| 134 |
// Return a static cached version if possible.
|
| 135 |
if ($fb && !$reconnect) {
|
| 136 |
return $fb;
|
| 137 |
}
|
| 138 |
|
| 139 |
// Put the current API URL and authentication token into variables.
|
| 140 |
$api_url = empty($api_url) ? variable_get('freshbooks_api_url', '') : $api_url;
|
| 141 |
$token = empty($token) ? variable_get('freshbooks_token', '') : $token;
|
| 142 |
|
| 143 |
// Fail if the API URL or authentication token haven't been set.
|
| 144 |
if (empty($api_url) || empty($token)) {
|
| 145 |
watchdog('freshbooks', 'Connection to FreshBooks failed because API credentials have not been set.', array(), WATCHDOG_ERROR);
|
| 146 |
return FALSE;
|
| 147 |
}
|
| 148 |
|
| 149 |
// Include the file that defines the class.
|
| 150 |
require_once(drupal_get_path('module', 'freshbooks') .'/freshbooks.class.inc');
|
| 151 |
|
| 152 |
// Create the object used to communicate with the server.
|
| 153 |
$fb = new DrupalFreshBooks($api_url, $token);
|
| 154 |
|
| 155 |
// Test to make sure we're using valid credentials.
|
| 156 |
if (!$fb->verifyCredentials()) {
|
| 157 |
$fb = FALSE;
|
| 158 |
}
|
| 159 |
|
| 160 |
return $fb;
|
| 161 |
}
|
| 162 |
|