diff --git a/google_analytics_reports_api/google_analytics_reports_api.admin.inc b/google_analytics_reports_api/google_analytics_reports_api.admin.inc index add4886..ff4bf44 100644 --- a/google_analytics_reports_api/google_analytics_reports_api.admin.inc +++ b/google_analytics_reports_api/google_analytics_reports_api.admin.inc @@ -65,52 +65,9 @@ function google_analytics_reports_api_admin() { } elseif ($account->isAuthenticated()) { - $web_properties = $account->queryWebProperties()->results->items; - $profiles = $account->queryProfiles()->results->items; - $options = array(); - $profile_id = variable_get('google_analytics_reports_api_profile_id', 0); - $set_default = FALSE; - - // Add optgroups for each web property. - if (!empty($profiles)) { - foreach ($profiles as $profile) { - $web_property = NULL; - foreach ($web_properties as $web_property_value) { - if ($web_property_value->id == $profile->webPropertyId) { - $web_property = $web_property_value; - break; - } - } - - $options[$web_property->name][$profile->id] = theme('google_analytics_reports_api_profile_label', array('profile' => $profile)); - // Rough attempt to see if the current site is in the account list. - if (empty($profile_id) && (parse_url($web_property->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_reports_api_profile_id', $profile_id); - } - $current_profile = NULL; - - // Load current profile object. - foreach ($profiles as $profile) { - if ($profile->id == $profile_id) { - $current_profile = $profile; - variable_set('google_analytics_reports_api_default_page', isset($current_profile->defaultPage) ? '/' . $current_profile->defaultPage : '/'); - break; - } - } + // Load profiles list. + $profile_list = google_analytics_reports_api_profiles_list(); $form['settings'] = array( '#type' => 'fieldset', @@ -122,11 +79,11 @@ function google_analytics_reports_api_admin() { $form['settings']['google_analytics_reports_api_profile_id'] = array( '#type' => 'select', '#title' => t('Reports profile'), - '#options' => $options, - '#default_value' => $profile_id, + '#options' => $profile_list['options'], + '#default_value' => $profile_list['profile_id'], '#description' => t('Choose your Google Analytics profile. The currently active profile is: %profile.', array( '%profile' => theme('google_analytics_reports_api_profile_label', - array('profile' => $current_profile)), + array('profile' => $profile_list['current_profile'])), )), ); @@ -175,6 +132,72 @@ function google_analytics_reports_api_admin() { } /** + * Google Analytics reports profiles for current authorized user. + * + * @return array + * An associative array containing: + * - options: list of current available profiles. + * - profile_id: current default profile id. + * - current_profile: current default profile object. + */ +function google_analytics_reports_api_profiles_list() { + $account = google_analytics_reports_api_gafeed(); + $web_properties = $account->queryWebProperties()->results->items; + $profiles = $account->queryProfiles()->results->items; + $options = array(); + $profile_id = variable_get('google_analytics_reports_api_profile_id', 0); + $set_default = FALSE; + + // Add optgroups for each web property. + if (!empty($profiles)) { + foreach ($profiles as $profile) { + $web_property = NULL; + foreach ($web_properties as $web_property_value) { + if ($web_property_value->id == $profile->webPropertyId) { + $web_property = $web_property_value; + break; + } + } + + $options[$web_property->name][$profile->id] = theme('google_analytics_reports_api_profile_label', array('profile' => $profile)); + // Rough attempt to see if the current site is in the account list. + if (empty($profile_id) && (parse_url($web_property->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_reports_api_profile_id', $profile_id); + } + + $current_profile = NULL; + + // Load current profile object. + foreach ($profiles as $profile) { + if ($profile->id == $profile_id) { + $current_profile = $profile; + variable_set('google_analytics_reports_api_default_page', isset($current_profile->defaultPage) ? '/' . $current_profile->defaultPage : '/'); + break; + } + } + $return = array( + 'options' => $options, + 'profile_id' => $profile_id, + 'current_profile' => $current_profile, + ); + + return $return; +} + +/** * Start authorization process with Google Analytics API. */ function google_analytics_reports_api_admin_submit_setup(&$form, &$form_state) { diff --git a/plugins/google_analytics_reports_plugin_query_google_analytics.inc b/plugins/google_analytics_reports_plugin_query_google_analytics.inc index 4483d4b..b9626ec 100644 --- a/plugins/google_analytics_reports_plugin_query_google_analytics.inc +++ b/plugins/google_analytics_reports_plugin_query_google_analytics.inc @@ -189,6 +189,11 @@ class google_analytics_reports_plugin_query_google_analytics extends views_plugi } } + // Change reports profile. + if (($this->options['reports_profile']) && (!empty($this->options['profile_id']))) { + $query['profile_id'] = $this->options['profile_id']; + } + return $query; } @@ -268,4 +273,55 @@ class google_analytics_reports_plugin_query_google_analytics extends views_plugi $view->build_info['query'] = print_r($feed->results->query, TRUE); } + /** + * Definition of views options. + */ + function option_definition() { + $options = parent::option_definition(); + $options['reports_profile'] = array( + 'default' => FALSE, + 'translatable' => FALSE, + 'bool' => TRUE, + ); + + // Load profiles list. + module_load_include('inc', 'google_analytics_reports_api', 'google_analytics_reports_api.admin'); + $profile_list = google_analytics_reports_api_profiles_list(); + + $options['profile_id'] = array( + 'default' => $profile_list['profile_id'], + ); + + return $options; + } + + /** + * Add settings for the ui. + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + + // Load profiles list. + module_load_include('inc', 'google_analytics_reports_api', 'google_analytics_reports_api.admin'); + $profile_list = google_analytics_reports_api_profiles_list(); + + $form['reports_profile'] = array( + '#title' => t('Use another reports profile'), + '#description' => t('This view will use another reports profile rather than system default profile: %profile.', array( + '%profile' => theme('google_analytics_reports_api_profile_label', + array('profile' => $profile_list['current_profile'])), + )), + '#type' => 'checkbox', + '#default_value' => !empty($this->options['reports_profile']), + ); + $form['profile_id'] = array( + '#type' => 'select', + '#title' => t('Reports profile'), + '#options' => $profile_list['options'], + '#description' => t('Choose your Google Analytics profile.'), + '#default_value' => $this->options['profile_id'], + '#dependency' => array('edit-query-options-reports-profile' => '1'), + ); + } + }