You are here

function hubspot_block_view in HubSpot 7

Same name and namespace in other branches
  1. 7.3 hubspot.module \hubspot_block_view()
  2. 7.2 hubspot.module \hubspot_block_view()

Implements hook_block_view() to provide a HubSpot recent leads block.

File

./hubspot.module, line 291
Sends Webform results to HubSpot's Leads API by using Webform's provided hooks.

Code

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();

      // This part of the HubSpot API returns HTTP error codes on failure, with no message
      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'] as $lead) {

        // Note that $lead->insertedAt comes in milliseconds, not seconds, since the epoch
        $block['content']['#items'][] = l($lead->firstName . ' ' . $lead->lastName, $lead->leadLink) . ' ' . t('(@time ago)', array(
          '@time' => format_interval(time() - intval($lead->insertedAt / 1000)),
        ));
      }
      break;
  }
  return $block;
}