You are here

class GoogleAnalyticsReportsApiAdminSettingsForm in Google Analytics Reports 8.3

Represents the admin settings form for google_analytics_reports_api.

Hierarchy

Expanded class hierarchy of GoogleAnalyticsReportsApiAdminSettingsForm

1 file declares its use of GoogleAnalyticsReportsApiAdminSettingsForm
GoogleAnalyticsReportsAdminSettingsForm.php in src/Form/GoogleAnalyticsReportsAdminSettingsForm.php
1 string reference to 'GoogleAnalyticsReportsApiAdminSettingsForm'
google_analytics_reports_api.routing.yml in google_analytics_reports_api/google_analytics_reports_api.routing.yml
google_analytics_reports_api/google_analytics_reports_api.routing.yml

File

google_analytics_reports_api/src/Form/GoogleAnalyticsReportsApiAdminSettingsForm.php, line 19

Namespace

Drupal\google_analytics_reports_api\Form
View source
class GoogleAnalyticsReportsApiAdminSettingsForm extends FormBase {

  /**
   * The config factory used by the config entity query.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The RequestStack service.
   *
   * @var Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructs a new Google Analytics Reports Api Admin Settings Form.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The Date formatter.
   * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, DateFormatterInterface $date_formatter, RequestStack $request_stack) {
    $this->configFactory = $config_factory;
    $this->dateFormatter = $date_formatter;
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('date.formatter'), $container
      ->get('request_stack'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'google_analytics_reports_api_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'google_analytics_reports_api.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // TODO move to a separate controller/url? Problematic could be that
    // users have set this url as the allowed redirect url in google console.
    // We could use a event listener?
    $code = $this
      ->getRequest()->query
      ->get('code');
    if ($code) {
      google_analytics_reports_api_authenticate($code);
      $redirect_uri = Url::fromRoute('google_analytics_reports_api.settings')
        ->setAbsolute()
        ->toString();
      $redirect = new RedirectResponse($redirect_uri);
      $redirect
        ->send();
      exit;
    }
    $account = google_analytics_reports_api_gafeed();
    $config = $this
      ->config('google_analytics_reports_api.settings');

    // There are no profiles, and we should just leave it at setup.
    if (!$account || !$account
      ->isAuthenticated()) {
      $dev_console_url = Url::fromUri('https://console.developers.google.com');
      $dev_console_link = Link::fromTextAndUrl($this
        ->t('Google Developers Console'), $dev_console_url)
        ->toRenderable();
      $dev_console_link['#attributes']['target'] = '_blank';
      $current_path_uri = $this->requestStack
        ->getCurrentRequest()
        ->getUri();
      $current_path_url = Url::fromUri($current_path_uri, [
        'absolute' => TRUE,
      ]);
      $setup_help = $this
        ->t('To access data from Google Analytics you have to create a new project in Google Developers Console.');
      $setup_help .= '<ol>';
      $setup_help .= ' <li>' . $this
        ->t('Open %google_developers_console.', [
        '%google_developers_console' => render($dev_console_link),
      ]) . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Along the toolbar click the pull down arrow and the press <strong>Create a Project</strong> button, enter project name and press <strong>Create</strong>.') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Click <strong>Enable and manage APIs</strong>.') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('In the search box type <strong>Analytics</strong> and then press <strong>Analytics API</strong>, this opens the API page, press <strong>Enable</strong>.') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Click on <strong>Go to Credentials</strong>') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Under <strong>Where will you be calling the API from?</strong> select <strong>Web Browser Javascript</strong> and then select <strong>User Data</strong>') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Hit <strong>What credentials do I need</strong>, edit the name if necessary.') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Leave empty <strong>Authorized JavaScript origins</strong>, fill in <strong>Authorized redirect URIs</strong> with <code>@url</code> and press <strong>Create Client ID</strong> button.', [
        '@url' => $current_path_url
          ->toString(),
      ]) . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Type a Product name to show to users and hit <strong>Continue</strong> and then <strong>Done</strong>') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Click on the name of your new client ID to be shown both the <strong>Client ID</strong> and <strong>Client Secret</strong>.') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Copy <strong>Client ID</strong> and <strong>Client secret</strong> from opened page to the form below.') . '</li>';
      $setup_help .= ' <li>' . $this
        ->t('Press <strong>Start setup and authorize account</strong> in the form below and allow the project access to Google Analytics data.') . '</li>';
      $setup_help .= '</ol>';
      $form['setup'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Initial setup'),
        '#description' => $setup_help,
        '#open' => TRUE,
      ];
      $form['setup']['client_id'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Client ID'),
        '#default_value' => $config
          ->get('client_id'),
        '#size' => 75,
        '#description' => $this
          ->t('Client ID from your project in Google Developers Console.'),
        '#required' => TRUE,
      ];
      $form['setup']['client_secret'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Client Secret'),
        '#default_value' => $config
          ->get('client_secret'),
        '#size' => 30,
        '#description' => $this
          ->t('Client Secret from your project in Google Developers Console'),
        '#required' => TRUE,
      ];
      $form['setup']['setup_submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Start setup and authorize account'),
        '#submit' => [
          '::adminSubmitSetup',
        ],
      ];
    }
    elseif ($account
      ->isAuthenticated()) {

      // Load profiles list.
      $profile_list = google_analytics_reports_api_profiles_list();
      $form['settings'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Settings'),
        '#open' => TRUE,
      ];
      $profile_info = '';
      if (isset($profile_list['current_profile'])) {
        $profile_info = parse_url($profile_list['current_profile']->websiteUrl, PHP_URL_HOST) . ' - ' . $profile_list['current_profile']->name . ' (' . $profile_list['current_profile']->id . ')';
      }
      $form['settings']['profile_id'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Reports profile'),
        '#options' => $profile_list['options'],
        '#default_value' => $profile_list['profile_id'],
        '#description' => $this
          ->t('Choose your Google Analytics profile.  The currently active profile is: %profile.', [
          '%profile' => $profile_info,
        ]),
      ];

      // Default cache periods.
      $times = [];

      // 1-6 days.
      for ($days = 1; $days <= 6; $days++) {
        $times[] = $days * 60 * 60 * 24;
      }

      // 1-4 weeks.
      for ($weeks = 1; $weeks <= 4; $weeks++) {
        $times[] = $weeks * 60 * 60 * 24 * 7;
      }
      $options = array_map([
        $this->dateFormatter,
        'formatInterval',
      ], array_combine($times, $times));
      $form['settings']['cache_length'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Query cache'),
        '#description' => $this
          ->t('The <a href="@link">Google Analytics Quota Policy</a> restricts the number of queries made per day. This limits the creation of new reports on your site.  We recommend setting this cache option to at least three days.', [
          '@link' => Url::fromUri('https://developers.google.com/analytics/devguides/reporting/core/v3/limits-quotas', [
            'fragment' => 'core_reporting',
          ])
            ->toString(),
        ]),
        '#options' => $options,
        '#default_value' => $config
          ->get('cache_length'),
      ];
      $form['settings']['settings_submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Save settings'),
        '#submit' => [
          '::adminSubmitSettings',
        ],
      ];
      $form['revoke'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Revoke access and logout'),
        '#description' => $this
          ->t('Revoke your access token from Google Analytics. This action will log you out of your Google Analytics account and stop all reports from displaying on your site.'),
      ];
      $form['revoke']['revoke_submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Revoke access token'),
        '#submit' => [
          '::adminSubmitRevoke',
        ],
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * Save Google Analytics Reports API admin setup.
   */
  public function adminSubmitSetup(array &$form, FormStateInterface $form_state) {
    $config = $this->configFactory
      ->getEditable('google_analytics_reports_api.settings');
    $config
      ->set('client_id', $form_state
      ->getValue('client_id'))
      ->set('client_secret', $form_state
      ->getValue('client_secret'))
      ->save();
    $redirect_uri = Url::fromRoute('google_analytics_reports_api.settings')
      ->setAbsolute()
      ->toString();
    $google_analytics_reports_api_feed = new GoogleAnalyticsReportsApiFeed();
    $response = $google_analytics_reports_api_feed
      ->beginAuthentication($form_state
      ->getValue('client_id'), $redirect_uri);
    $form_state
      ->setResponse($response);
  }

  /**
   * Save Google Analytics Reports API settings.
   */
  public function adminSubmitSettings(array &$form, FormStateInterface $form_state) {
    $config = $this->configFactory
      ->getEditable('google_analytics_reports_api.settings');
    $config
      ->set('profile_id', $form_state
      ->getValue('profile_id'))
      ->set('cache_length', $form_state
      ->getValue('cache_length'))
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Settings have been saved successfully.'));
  }

  /**
   * Revoke Google Analytics access token.
   */
  public function adminSubmitRevoke(array &$form, FormStateInterface $form_state) {
    google_analytics_reports_api_revoke();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Access token has been successfully revoked.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
GoogleAnalyticsReportsApiAdminSettingsForm::$configFactory protected property The config factory used by the config entity query. Overrides FormBase::$configFactory
GoogleAnalyticsReportsApiAdminSettingsForm::$dateFormatter protected property The date formatter service. 1
GoogleAnalyticsReportsApiAdminSettingsForm::$requestStack protected property The RequestStack service. Overrides FormBase::$requestStack
GoogleAnalyticsReportsApiAdminSettingsForm::adminSubmitRevoke public function Revoke Google Analytics access token.
GoogleAnalyticsReportsApiAdminSettingsForm::adminSubmitSettings public function Save Google Analytics Reports API settings.
GoogleAnalyticsReportsApiAdminSettingsForm::adminSubmitSetup public function Save Google Analytics Reports API admin setup.
GoogleAnalyticsReportsApiAdminSettingsForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
GoogleAnalyticsReportsApiAdminSettingsForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create 1
GoogleAnalyticsReportsApiAdminSettingsForm::getEditableConfigNames protected function
GoogleAnalyticsReportsApiAdminSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
GoogleAnalyticsReportsApiAdminSettingsForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
GoogleAnalyticsReportsApiAdminSettingsForm::__construct public function Constructs a new Google Analytics Reports Api Admin Settings Form. 1
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.