You are here

function google_analytics_counter_auth_admin in Google Analytics Counter 7.2

Same name and namespace in other branches
  1. 7.3 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 34
Provides the GAFeed object type and associated methods.

Code

function google_analytics_counter_auth_admin() {
  $form = array();
  $account = google_analytics_counter_new_gafeed();

  //dpm($account);
  if ($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 */
    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', '/' . @$current_profile->defaultPage);
        break;
      }
    }

    //dpm($current_profile);
    $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,
    );
    $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']['google_analytics_counter_hd'] = array(
      '#type' => 'textfield',
      '#title' => t('Google Apps for Business Domain (optional)'),
      '#description' => t('Provide the domain name (example.com) if your domain is registered with Google Apps for Business. Otherwise, leave blank.'),
      '#default_value' => variable_get('google_analytics_counter_hd', ''),
    );
    $form['setup']['setup_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Start setup and authorize account'),
    );
  }
  return $form;
}