You are here

hubspot.admin.inc in HubSpot 7.2

Provides admin settings page to adjust HubSpot API key, debugging settings, JavaScript embedding, and form submission settings.

File

hubspot.admin.inc
View source
<?php

/**
 * @file
 * Provides admin settings page to adjust HubSpot API key, debugging settings,
 * JavaScript embedding, and form submission settings.
 */

/**
 * Form constructor for the Hubspot admin settings form.
 *
 * @see hubspot_admin_settings_validate()
 * @see hubspot_admin_settings_submit()
 */
function hubspot_admin_settings() {
  $form = array();
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
  );
  $form['settings'] = array(
    '#title' => t('Connectivity'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#group' => 'additional_settings',
  );
  $form['settings']['hubspot_portalid'] = array(
    '#title' => t('HubSpot Portal ID'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#default_value' => variable_get('hubspot_portalid', ''),
    '#description' => t('Enter the Hubspot Portal ID for this site.  It can be found by
      <a href="https://login.hubspot.com/login" target="_blank">logging into HubSpot</a> going to the Dashboard and
      examining the url. Example: "https://app.hubspot.com/dashboard-plus/12345/dash/".  The number after
      "dashboard-plus" is your Portal ID.'),
  );
  if (variable_get('hubspot_portalid', '')) {
    $form['settings']['hubspot_authentication'] = array(
      '#value' => t('Connect Hubspot Account'),
      '#type' => 'submit',
      '#validate' => array(),
      '#submit' => array(
        'hubspot_oauth_submit',
      ),
    );
    if (variable_get('hubspot_refresh_token', '')) {
      $form['settings']['hubspot_authentication']['#suffix'] = t('Your Hubspot account is connected.');
      $form['settings']['hubspot_authentication']['#value'] = t('Disconnect Hubspot Account');
      $form['settings']['hubspot_authentication']['#submit'] = array(
        'hubspot_oauth_disconnect',
      );
    }
  }
  $form['settings']['hubspot_log_code'] = array(
    '#title' => t('HubSpot Traffic Logging Code'),
    '#type' => 'textarea',
    '#default_value' => variable_get('hubspot_log_code', ''),
    '#description' => t('To enable HubSpot traffic logging on your site, paste the External Site Traffic Logging code
      here.'),
  );
  $form['debug'] = array(
    '#title' => t('Debugging'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#group' => 'additional_settings',
  );
  $form['debug']['hubspot_debug_on'] = array(
    '#title' => t('Debugging enabled'),
    '#type' => 'checkbox',
    '#default_value' => variable_get('hubspot_debug_on', 0),
    '#description' => t('If debugging is enabled, HubSpot errors will be emailed to the address below. Otherwise, they
      will be logged to the regular Drupal error log.'),
  );
  $form['debug']['hubspot_debug_email'] = array(
    '#title' => t('Debugging email'),
    '#type' => 'textfield',
    '#default_value' => variable_get('hubspot_debug_email', variable_get('site_mail', '')),
    '#description' => t('Email error reports to this address if debugging is enabled.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save Configuration',
  );
  return $form;
}

/**
 * Submit handler for hubspot_admin_settings().
 */
function hubspot_admin_settings_submit($form, &$form_state) {
  variable_set('hubspot_portalid', $form_state['values']['hubspot_portalid']);
  variable_set('hubspot_debug_email', $form_state['values']['hubspot_debug_email']);
  variable_set('hubspot_debug_on', $form_state['values']['hubspot_debug_on']);
  variable_set('hubspot_log_code', $form_state['values']['hubspot_log_code']);
  $txn = db_transaction();
  drupal_set_message(t('The configuration options have been saved.'));
}

/**
 * Form validation handler for hubspot_admin_settings().
 *
 * Ensures that the debug email address provided is valid if debugging is
 * enabled.
 */
function hubspot_admin_settings_validate($form, &$form_state) {
  if ($form_state['values']['hubspot_debug_on'] && !valid_email_address($form_state['values']['hubspot_debug_email'])) {
    form_set_error('hubspot_debug_email', t('You must provide a valid email address.'));
  }
}

/**
 * Form submission handler for hubspot_admin_settings().
 */
function hubspot_oauth_submit($form, &$form_state) {
  $data = array(
    'client_id' => HUBSPOT_CLIENT_ID,
    'portalId' => $form_state['values']['hubspot_portalid'],
    'redirect_uri' => url('hubspot/oauth', array(
      'query' => drupal_get_destination(),
      'absolute' => TRUE,
    )),
    'scope' => HUBSPOT_SCOPE,
  );
  $form_state['redirect'][] = url('https://app.hubspot.com/auth/authenticate', array(
    'query' => $data,
  ));
}

/**
 * Page callback.
 *
 * Saves OAuth tokens from HubSpot and redirects user.
 */
function hubspot_oauth_connect() {
  if (!empty($_GET['access_token']) && !empty($_GET['refresh_token']) && !empty($_GET['expires_in'])) {
    drupal_set_message(t('Successfully authenticated with Hubspot.'), 'status', FALSE);
    variable_set('hubspot_access_token', $_GET['access_token']);
    variable_set('hubspot_refresh_token', $_GET['refresh_token']);
    variable_set('hubspot_expires_in', $_GET['expires_in']);
  }
  if (!empty($_GET['error']) && $_GET['error'] == "access_denied") {
    drupal_set_message(t('You denied the request for authentication with Hubspot. Please click the button again and
      choose the AUTHORIZE option.'), 'error', FALSE);
  }
  drupal_goto();
}

/**
 * Form submit handler.
 *
 * Deletes Hubspot OAuth tokens.
 */
function hubspot_oauth_disconnect($form, &$form_state) {
  variable_del('hubspot_access_token');
  variable_del('hubspot_refresh_token');
  variable_del('hubspot_expires_in');
}

Functions

Namesort descending Description
hubspot_admin_settings Form constructor for the Hubspot admin settings form.
hubspot_admin_settings_submit Submit handler for hubspot_admin_settings().
hubspot_admin_settings_validate Form validation handler for hubspot_admin_settings().
hubspot_oauth_connect Page callback.
hubspot_oauth_disconnect Form submit handler.
hubspot_oauth_submit Form submission handler for hubspot_admin_settings().