You are here

public function YandexMetricsReportsController::report in Yandex.Metrics 8.2

Menu callback; displays a Summary page containing reports and charts.

1 string reference to 'YandexMetricsReportsController::report'
yandex_metrics_reports.routing.yml in yandex_metrics_reports/yandex_metrics_reports.routing.yml
yandex_metrics_reports/yandex_metrics_reports.routing.yml

File

yandex_metrics_reports/src/Controller/YandexMetricsReportsController.php, line 53
Contains \Drupal\action\Controller\YandexMetricsReportsController

Class

YandexMetricsReportsController
Universal controller for yandex_metrics_reports module.

Namespace

Drupal\yandex_metrics_reports\Controller

Code

public function report($filter = 'week') {
  $counter_id = yandex_metrics_reports_get_counter_for_current_site();
  if (empty($counter_id)) {
    drupal_set_message(t('Please create Yandex.Metrics counter for the site first. See more details !link.', array(
      '!link' => l(t('here'), 'admin/config/system/yandex_metrics'),
    )), 'error');
    return '';
  }
  $counter_code = \Drupal::config('yandex_metrics.settings')
    ->get('counter_code');
  if (empty($counter_code)) {
    drupal_set_message(t('Perhaps you have not yet placed Yandex.Metrics counter code on the site. You can do this !link.', array(
      '!link' => l(t('here'), 'admin/config/system/yandex_metrics'),
    )), 'notice');
  }
  $authorisation_token = yandex_services_auth_info('token');
  if (empty($authorisation_token)) {
    drupal_set_message(t('Please make sure that your application is authorized !link.', array(
      '!link' => l(t('here'), 'admin/config/system/yandex_metrics/authorization'),
    )), 'error');
    return '';
  }
  $output = '';
  $form = \Drupal::formBuilder()
    ->getForm('\\Drupal\\yandex_metrics_reports\\Form\\YandexMetricsReportsFilterForm');
  $form['#attached']['css'][] = drupal_get_path('module', 'yandex_metrics_reports') . '/css/yandex_metrics_reports.css';
  $reports = yandex_metrics_reports_get_active_list();
  $clean_urls = TRUE;
  try {
    $clean_urls = \Drupal::request()->attributes
      ->get('clean_urls');
  } catch (ServiceNotFoundException $e) {
  }
  $form['#attached']['js'][] = array(
    'data' => array(
      'yandex_metrics_reports' => array(
        'modulePath' => drupal_get_path('module', 'yandex_metrics_reports'),
        'cleanUrls' => (int) $clean_urls,
        'reportList' => array_keys($reports),
      ),
    ),
    'type' => 'setting',
  );
  $form['#attached']['js'][] = array(
    'data' => drupal_get_path('module', 'yandex_metrics_reports') . '/js/yandex_metrics_reports.js',
  );
  $output .= drupal_render($form);
  $output .= '<input type="hidden" id="yandex_metrics_reports_counter_id" value="' . $counter_id . '" />';
  $output .= '<input type="hidden" id="yandex_metrics_reports_filter" value="' . $filter . '" />';
  $reportsHandler = new Reports($counter_id, $filter);
  foreach ($reports as $report_name => $report_data) {
    $ported_chars = array(
      'visits_chart',
      'sources_chart',
      'geo_chart',
      'hourly_chart',
      'gender_chart',
    );
    if (in_array($report_name, $ported_chars)) {

      // @TODO Remove this condition after charts code is ported.
      $output .= $reportsHandler
        ->{$report_name}();
    }
    else {
      $output .= '<div class="yandex_metrics_reports-report" id="yandex_metrics_reports_' . $report_name . '"></div>';
    }
  }
  return $output;
}