You are here

function google_analytics_counter_auth_admin in Google Analytics Counter 7.3

Same name and namespace in other branches
  1. 7.2 google_analytics_counter_auth.inc \google_analytics_counter_auth_admin()

Menu callback - admin form for OAuth and other settings.

1 string reference to 'google_analytics_counter_auth_admin'
google_analytics_counter_menu in ./google_analytics_counter.module
Menu for this module

File

./google_analytics_counter_auth.inc, line 90
Provides the GAFeed object type and associated methods.

Code

function google_analytics_counter_auth_admin() {
  $form = array();
  $account = google_analytics_counter_new_gafeed();
  if ($account && $account
    ->isAuthenticated()) {
    $webprops = $account
      ->queryWebProperties()->results->items;
    $profiles = $account
      ->queryProfiles()->results->items;
    $options = array();
    $profile_id = variable_get('google_analytics_counter_profile_id', 0);
    $set_default = FALSE;

    // Add optgroups for each web property.
    if (!empty($profiles)) {
      foreach ($profiles as $profile) {
        $webprop = NULL;
        foreach ($webprops as $webprop_value) {
          if ($webprop_value->id == $profile->webPropertyId) {
            $webprop = $webprop_value;
            break;
          }
        }
        $options[$webprop->name][$profile->id] = theme('google_analytics_counter_profile_label', array(
          'profile' => $profile,
        ));

        // Rough attempt to see if the current site is in the account list.
        if (empty($profile_id) && parse_url($webprop->websiteUrl, PHP_URL_PATH) == $_SERVER['HTTP_HOST']) {
          $profile_id = $profile->id;
          $set_default = TRUE;
        }
      }
    }

    // If no profile ID is set yet, set the first profile in the list.
    if (empty($profile_id)) {
      $profile_id = key($options[key($options)]);
      $set_default = TRUE;
    }
    if ($set_default) {
      variable_set('google_analytics_counter_profile_id', $profile_id);
    }

    // Load current profile object.
    foreach ($profiles as $profile) {
      if ($profile->id == $profile_id) {
        $current_profile = $profile;
        variable_set('google_analytics_counter_default_page', isset($current_profile->defaultPage) ? '/' . $current_profile->defaultPage : '/');
        break;
      }
    }
    $form['ga'] = array(
      '#type' => 'fieldset',
      '#title' => t('Settings'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#weight' => 1,
    );
    $form['ga']['google_analytics_counter_profile_id'] = array(
      '#type' => 'select',
      '#title' => t('Reports profile'),
      '#options' => $options,
      '#default_value' => $profile_id,
      '#description' => t("Choose your Google Analytics profile. The currently active profile is: %profile", array(
        '%profile' => theme('google_analytics_counter_profile_label', array(
          'profile' => $current_profile,
        )),
      )),
      '#required' => TRUE,
    );
    if (!empty($options) || $options[0]) {
      $form['ga']['google_analytics_counter_profile_id']['#required'] = TRUE;
    }
    $form['ga']['settings_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save settings'),
    );
    $form['revoke'] = array(
      '#type' => 'fieldset',
      '#title' => t('Revoke access and logout'),
      '#description' => t('Revoke your access token to Google Analytics. This action will log you out of your Google Analytics account and stop all reports from displaying on your site.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 5,
    );
    $form['revoke']['revoke_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Revoke access token'),
    );
  }
  else {
    $form['setup'] = array(
      '#type' => 'fieldset',
      '#title' => t('Initial setup'),
      '#description' => t("When you submit this form, you will be redirected to Google for authentication. Login with the account that has credentials to the Google Analytics profile you'd like to use."),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['setup']['client_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Client ID'),
      '#default_value' => variable_get('google_analytics_counter_client_id', ''),
      '#size' => 30,
      '#description' => t('Client ID created for the app in the access tab of the !google_link', array(
        '!google_link' => l(t('Google API Console'), 'http://code.google.com/apis/console', array(
          'attributes' => array(
            'target' => '_blank',
          ),
        )),
      )),
      '#weight' => -9,
    );
    $form['setup']['client_secret'] = array(
      '#type' => 'textfield',
      '#title' => t('Client Secret'),
      '#default_value' => variable_get('google_analytics_counter_client_secret', ''),
      '#size' => 30,
      '#description' => t('Client Secret created for the app in the Google API Console'),
      '#weight' => -8,
    );
    $redirect_uri = variable_get('google_analytics_counter_redirect_uri', GoogleAnalyticsCounterFeed::currentUrl());
    $form['setup']['redirect_host'] = array(
      '#type' => 'textfield',
      '#title' => t('Redirect host'),
      '#default_value' => variable_get('google_analytics_counter_redirect_host', ''),
      '#size' => 30,
      '#description' => t('Use to override the host for the callback uri (necessary on some servers, e.g. when using SSL and Varnish). Include schema and host, but not uri path. Example: http://example.com. Current redirect URI is %redirect_uri. Leave blank to use default (blank will work for most cases).', array(
        '%redirect_uri' => $redirect_uri,
      )),
      '#weight' => -7,
    );
    $form['setup']['setup_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Start setup and authorize account'),
    );
  }
  return $form;
}