You are here

public static function MailSafetyController::load in Mail Safety 8

Load one or more mails.

Parameters

optional $mail_id: If mail_id is not given it will load all the mails.

Return value

array|bool Returns an array of one ore more mails.

2 calls to MailSafetyController::load()
ClearForm::submitForm in src/Form/ClearForm.php
Form submission handler.
MailSafetyParamConverter::convert in src/ParamConverter/MailSafetyParamConverter.php
Converts path variables to their corresponding objects.

File

src/Controller/MailSafetyController.php, line 51

Class

MailSafetyController
Class MailSafetyController.

Namespace

Drupal\mail_safety\Controller

Code

public static function load($mail_id = NULL) {
  $mails = [];
  $connection = \Drupal::database();
  $query = $connection
    ->select('mail_safety_dashboard', 'msd');
  $query
    ->fields('msd', [
    'mail_id',
    'sent',
    'mail',
  ]);

  // Add a condition for the mail id is given.
  if (!is_null($mail_id)) {
    $query
      ->condition('mail_id', $mail_id);
  }
  $query
    ->orderBy('sent', 'DESC');
  $result = $query
    ->execute();
  while ($row = $result
    ->fetchAssoc()) {
    $mails[$row['mail_id']] = [
      'mail' => unserialize($row['mail']),
      'sent' => $row['sent'],
      'mail_id' => $row['mail_id'],
    ];
  }

  // Let other modules respond before a mail is loaded.
  // E.g. attachments that were saved with the mail.
  $modules = \Drupal::moduleHandler()
    ->getImplementations('mail_safety_load');
  foreach ($mails as $key => $mail) {
    foreach ($modules as $module) {
      $mail['mail'] = \Drupal::moduleHandler()
        ->invoke($module, 'mail_safety_load', $mail['mail']);
    }
    $mails[$key] = $mail;
  }
  if (!is_null($mail_id) && !empty($mails[$mail_id])) {
    return $mails[$mail_id];
  }
  elseif (!empty($mails)) {
    return $mails;
  }
  return $mails;
}