You are here

public function SendGridReportsController::getReports in SendGrid Integration 8

Same name and namespace in other branches
  1. 8.2 modules/sendgrid_integration_reports/src/Controller/SendGridReportsController.php \Drupal\sendgrid_integration_reports\Controller\SendGridReportsController::getReports()

Returns reports.

File

modules/sendgrid_integration_reports/src/Controller/SendGridReportsController.php, line 90

Class

SendGridReportsController
Class for rendering sendgrid reports page.

Namespace

Drupal\sendgrid_integration_reports\Controller

Code

public function getReports() {
  $stats = $this->api
    ->getStats('sendgrid_reports_global');
  $settings = [];
  $stats['global'] = isset($stats['global']) ? $stats['global'] : [];
  foreach ($stats['global'] as $items) {
    $settings['global'][] = [
      'date' => $items['date'],
      'opens' => $items['opens'],
      'clicks' => $items['clicks'],
      'delivered' => $items['delivered'],
      'spam_reports' => $items['spam_reports'],
      'spam_report_drops' => $items['spam_report_drops'],
    ];
  }
  $render = [
    '#attached' => [
      'library' => [
        'sendgrid_integration_reports/googlejsapi',
        'sendgrid_integration_reports/main',
      ],
      'drupalSettings' => [
        'sendgrid_integration_reports' => $settings,
      ],
    ],
    'message' => [
      '#markup' => t('The following reports are the from the Global Statistics provided by SendGrid. For more comprehensive data, please visit your @dashboard. @cache to ensure the data is current. @settings to alter the time frame of this data.', [
        '@dashboard' => Link::fromTextAndUrl(t('SendGrid Dashboard'), Url::fromUri('//app.sendgrid.com/'))
          ->toString(),
        '@cache' => Link::createFromRoute(t('Clear your cache'), 'system.performance_settings')
          ->toString(),
        '@settings' => Link::createFromRoute(t('Change your settings'), 'sendgrid_integration_reports.settings_form')
          ->toString(),
      ]),
    ],
    'volume' => [
      '#prefix' => '<h2>' . t('Sending Volume') . '</h2>',
      '#markup' => '<div id="sendgrid-global-volume-chart"></div>',
    ],
    'spam' => [
      '#prefix' => '<h2>' . t('Spam Reports') . '</h2>',
      '#markup' => '<div id="sendgrid-global-spam-chart"></div>',
    ],
  ];
  $browserstats = $this->api
    ->getStatsBrowser();
  $rows = [];
  foreach ($browserstats as $key => $value) {
    $rows[] = [
      $key,
      $value,
    ];
  }
  $headerbrowser = [
    t('Browser'),
    t('Click Count'),
  ];
  $render['browsers'] = [
    '#prefix' => '<h2>' . t('Browser Statistics') . '</h2>',
    '#theme' => 'table',
    '#header' => $headerbrowser,
    '#rows' => $rows,
    'attributes' => [
      'width' => '75%',
    ],
  ];
  $devicestats = $this->api
    ->getStatsDevices();
  $rowsdevices = [];
  foreach ($devicestats as $key => $value) {
    $rowsdevices[] = [
      $key,
      $value,
    ];
  }
  $headerdevices = [
    t('Device'),
    t('Open Count'),
  ];
  $render['devices'] = [
    '#prefix' => '<h2>' . t('Device Statistics') . '</h2>',
    '#theme' => 'table',
    '#header' => $headerdevices,
    '#rows' => $rowsdevices,
    'attributes' => [
      'width' => '75%',
    ],
  ];
  return $render;
}