You are here

class SendGridReportsController in SendGrid Integration 8.2

Same name and namespace in other branches
  1. 8 modules/sendgrid_integration_reports/src/Controller/SendGridReportsController.php \Drupal\sendgrid_integration_reports\Controller\SendGridReportsController

Class for rendering sendgrid reports page.

@package Drupal\sendgrid_integration_reports\Controller

Hierarchy

Expanded class hierarchy of SendGridReportsController

File

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

Namespace

Drupal\sendgrid_integration_reports\Controller
View source
class SendGridReportsController extends ControllerBase {

  /**
   * Include the messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Logger service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactory
   */
  protected $loggerFactory;

  /**
   * Api service.
   *
   * @var \Drupal\sendgrid_integration_reports\Api
   */
  protected $api;

  /**
   * SendGridReportsController constructor.
   *
   * @param \Drupal\sengrid_integration_reports\Api $api
   *   The sendgrid api service.
   */
  public function __construct(Api $api) {
    $this->api = $api;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('sendgrid_integration_reports.api'));
  }

  /**
   * Returns stats categories.
   *
   * @param array $categories
   *   Array of categories.
   * @param string|null $start_date
   *   Start date.
   * @param string|null $end_date
   *   End date.
   * @param bool $refresh
   *   Flag is cache should be refreshed.
   *
   * @return array|bool
   *   Array of stats data.
   */
  public function getStatsCategories(array $categories, $start_date = NULL, $end_date = NULL, $refresh = FALSE) {
    $cid = 'sendgrid_reports_categories';

    // Sanitize the categories array and generate the cache ID.
    if ($categories && is_array($categories)) {
      $categories = array_values($categories);
      $cid .= '_' . implode('_', $categories);
    }
    else {
      $categories = NULL;
    }
    return $this->api
      ->getStats($cid, $categories, $start_date, $end_date, $refresh);
  }

  /**
   * Returns reports.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
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::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
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.
SendGridReportsController::$api protected property Api service.
SendGridReportsController::$loggerFactory protected property Logger service. Overrides LoggerChannelTrait::$loggerFactory
SendGridReportsController::$messenger protected property Include the messenger service. Overrides MessengerTrait::$messenger
SendGridReportsController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
SendGridReportsController::getReports public function Returns reports.
SendGridReportsController::getStatsCategories public function Returns stats categories.
SendGridReportsController::__construct public function SendGridReportsController constructor.
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.