You are here

class DashboardForm in Mail Safety 8

Class DashboardForm.

@package Drupal\mail_safety\Form

Hierarchy

Expanded class hierarchy of DashboardForm

1 string reference to 'DashboardForm'
mail_safety.routing.yml in ./mail_safety.routing.yml
mail_safety.routing.yml

File

src/Form/DashboardForm.php, line 19

Namespace

Drupal\mail_safety\Form
View source
class DashboardForm extends FormBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

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

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * DashboardForm constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
   *   The date formatter.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler.
   */
  public function __construct(Connection $database, DateFormatter $date_formatter, ModuleHandler $module_handler) {
    $this->database = $database;
    $this->dateFormatter = $date_formatter;
    $this->moduleHandler = $module_handler;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFormId() {

    // Unique ID of the form.
    return 'mail_safety_dashboard_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $table_structure = [];

    // Create the headers.
    $table_structure['header'] = [
      [
        'data' => $this
          ->t('Subject'),
      ],
      [
        'data' => $this
          ->t('Date sent'),
        'field' => 'sent',
        'sort' => 'desc',
      ],
      [
        'data' => $this
          ->t('To'),
      ],
      [
        'data' => $this
          ->t('CC'),
      ],
      [
        'data' => $this
          ->t('Bcc'),
      ],
      [
        'data' => $this
          ->t('Module'),
      ],
      [
        'data' => $this
          ->t('Key'),
      ],
      [
        'data' => $this
          ->t('Details'),
      ],
      [
        'data' => $this
          ->t('Send to original'),
      ],
      [
        'data' => $this
          ->t('Send to default mail'),
      ],
      [
        'data' => $this
          ->t('Delete'),
      ],
    ];

    // Create the query.

    /** @var \Drupal\Core\Database\Query\SelectInterface $query */
    $query = $this->database
      ->select('mail_safety_dashboard', 'msd')
      ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
      ->limit(50)
      ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
      ->orderByHeader($table_structure['header'])
      ->fields('msd', [
      'mail_id',
      'sent',
      'mail',
    ]);
    $results = $query
      ->execute();

    // Fill the rows for the table.
    $table_structure['rows'] = [];
    foreach ($results as $row) {
      $mail = unserialize($row->mail);

      // Build the links for the row.
      $view_url = Url::fromRoute('mail_safety.view', [
        'mail_safety' => $row->mail_id,
      ]);
      $details_url = Url::fromRoute('mail_safety.details', [
        'mail_safety' => $row->mail_id,
      ]);
      $send_original_url = Url::fromRoute('mail_safety.send_original', [
        'mail_safety' => $row->mail_id,
      ]);
      $send_default_url = Url::fromRoute('mail_safety.send_default', [
        'mail_safety' => $row->mail_id,
      ]);
      $delete_url = Url::fromRoute('mail_safety.delete', [
        'mail_safety' => $row->mail_id,
      ]);
      $cc = $mail['headers']['Cc'] ?? $this
        ->t('none');
      $bcc = $mail['headers']['Bcc'] ?? $this
        ->t('none');
      $table_structure['rows'][$row->mail_id] = [
        'data' => [
          Link::fromTextAndUrl($mail['subject'], $view_url),
          $this->dateFormatter
            ->format((int) $row->sent, 'short'),
          $mail['to'],
          $cc,
          $bcc,
          $mail['module'],
          $mail['key'],
          Link::fromTextAndUrl($this
            ->t('Details'), $details_url),
          Link::fromTextAndUrl($this
            ->t('Send to original'), $send_original_url),
          Link::fromTextAndUrl($this
            ->t('Send to default mail'), $send_default_url),
          Link::fromTextAndUrl($this
            ->t('Delete'), $delete_url),
        ],
      ];
    }

    // Let other modules change the table structure to add or remove
    // information to be shown. E.g. attachments that need to be downloaded.
    $this->moduleHandler
      ->alter('mail_safety_table_structure', $table_structure);
    $form['mails']['table'] = [
      '#theme' => 'table',
      '#header' => $table_structure['header'],
      '#rows' => $table_structure['rows'],
      '#sticky' => TRUE,
      '#empty' => $this
        ->t('No mails found'),
    ];
    $form['mails']['pager'] = [
      '#type' => 'pager',
      '#tags' => [],
    ];
    return $form;
  }

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

    // Validate submitted form data.
  }

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

    // Handle submitted form data.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DashboardForm::$database protected property The database connection.
DashboardForm::$dateFormatter protected property The date formatter.
DashboardForm::$moduleHandler protected property The module handler.
DashboardForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
DashboardForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
DashboardForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
DashboardForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
DashboardForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
DashboardForm::__construct public function DashboardForm constructor.
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::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
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.
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.