View source
<?php
define('HUBSPOT_CLIENT_ID', '734f89bf-1b88-11e1-829a-3b413536dd4c');
define('HUBSPOT_SCOPE', 'leads-rw contacts-rw offline');
function hubspot_menu() {
$items['admin/config/services/hubspot'] = array(
'title' => 'HubSpot integration settings',
'description' => 'Set up HubSpot integration and leads insertion.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'hubspot_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'hubspot.admin.inc',
);
$items['hubspot/oauth'] = array(
'title' => 'HubSpot OAuth redirect',
'description' => 'Collects OAuth tokens.',
'page callback' => 'hubspot_oauth_connect',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
'file' => 'hubspot.admin.inc',
);
return $items;
}
function hubspot_oauth_refresh() {
$data = array(
'refresh_token' => variable_get('hubspot_refresh_token'),
'client_id' => HUBSPOT_CLIENT_ID,
'grant_type' => 'refresh_token',
);
$data = drupal_http_build_query($data);
$options = array(
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
),
'method' => 'POST',
'data' => $data,
);
$return = drupal_http_request('https://api.hubapi.com/auth/v1/refresh', $options);
if ($return->code == '200') {
$return_data = json_decode($return->data, TRUE);
$hubspot_access_token = $return_data['access_token'];
variable_set('hubspot_access_token', $hubspot_access_token);
$hubspot_refresh_token = $return_data['refresh_token'];
variable_set('hubspot_refresh_token', $hubspot_refresh_token);
$hubspot_expires_in = $return_data['expires_in'];
variable_set('hubspot_expires_in', $hubspot_expires_in);
return TRUE;
}
else {
drupal_set_message(t('Refresh token failed with Error Code "%code: %status_message". Reconnect to your Hubspot
account.'), 'error', FALSE);
watchdog('hubspot', 'Refresh token failed with Error Code "%code: %status_message". Visit the Hubspot module
settings page and reconnect to your Hubspot account.', array(
'%code' => $return->code,
'%status_message' => $return->status_message,
), WATCHDOG_INFO);
return FALSE;
}
}
function hubspot_page_build(&$page) {
$page['page_bottom']['hubspot_code'] = array(
'#type' => 'markup',
'#markup' => variable_get('hubspot_log_code', ''),
);
}
function hubspot_permission() {
return array(
'see recent hubspot leads' => array(
'title' => t('See recent HubSpot leads'),
'description' => t("View the recent leads block when it's enabled."),
'restrict access' => TRUE,
),
);
}
function hubspot_block_info() {
$blocks = array();
$blocks['hubspot_recent'] = array(
'info' => t('HubSpot Recent Leads'),
'properties' => array(
'administrative' => TRUE,
),
);
return $blocks;
}
function hubspot_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'hubspot_recent':
if (!user_access('see recent hubspot leads')) {
return;
}
$block['subject'] = t('HubSpot Recent Leads');
$leads = hubspot_get_recent();
if (!empty($leads['Error']) || $leads['HTTPCode'] != 200) {
$block['content'] = t('An error occurred when fetching the HubSpot leads data: @error', array(
'@error' => !empty($leads['Error']) ? $leads['Error'] : $leads['HTTPCode'],
));
return $block;
}
elseif (empty($leads['Data'])) {
$block['content'] = t('No leads to show.');
return $block;
}
$block['content'] = array(
'#theme' => 'item_list',
'#items' => array(),
'#type' => 'ul',
);
foreach ($leads['Data']->contacts as $lead) {
foreach ($leads['Data']->contacts as $lead) {
$block['content']['#items'][] = l($lead->properties->firstname->value . ' ' . $lead->properties->lastname->value, $lead->{'profile-url'}) . ' ' . t('(@time ago)', array(
'@time' => format_interval(time() - floor($lead->addedAt / 1000)),
));
}
}
break;
}
return $block;
}
function hubspot_get_recent($n = 5) {
$access_token = variable_get('hubspot_access_token', '');
$n = intval($n);
if (empty($access_token)) {
return array(
'Error' => t('This site is not connected to a HubSpot Account.'),
);
}
$result = drupal_http_request("https://api.hubapi.com/contacts/v1/lists/recently_updated/contacts/recent?access_token={$access_token}&count={$n}");
if ($result->code == 401) {
$refresh = hubspot_oauth_refresh();
if ($refresh) {
$access_token = variable_get('hubspot_access_token', '');
$result = drupal_http_request("https://api.hubapi.com/contacts/v1/lists/recently_updated/contacts/recent?access_token={$access_token}&count={$n}");
}
}
return array(
'Data' => json_decode($result->data),
'Error' => isset($result->error) ? $result->error : '',
'HTTPCode' => $result->code,
);
}