You are here

protected function ScheduleEmailWebformHandler::scheduleMessage in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_scheduled_email/src/Plugin/WebformHandler/ScheduleEmailWebformHandler.php \Drupal\webform_scheduled_email\Plugin\WebformHandler\ScheduleEmailWebformHandler::scheduleMessage()

Schedule the sending of an email.

Parameters

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

Return value

bool|string The status of scheduled email. FALSE is email was not scheduled.

1 call to ScheduleEmailWebformHandler::scheduleMessage()
ScheduleEmailWebformHandler::postSave in modules/webform_scheduled_email/src/Plugin/WebformHandler/ScheduleEmailWebformHandler.php
Acts on a saved webform submission before the insert or update hook is invoked.

File

modules/webform_scheduled_email/src/Plugin/WebformHandler/ScheduleEmailWebformHandler.php, line 390

Class

ScheduleEmailWebformHandler
Schedules a webform submission's email.

Namespace

Drupal\webform_scheduled_email\Plugin\WebformHandler

Code

protected function scheduleMessage(WebformSubmissionInterface $webform_submission) {

  /** @var \Drupal\webform_scheduled_email\WebformScheduledEmailManagerInterface $webform_scheduled_email_manager */
  $webform_scheduled_email_manager = \Drupal::service('webform_scheduled_email.manager');
  $t_args = [
    '%submission' => $webform_submission
      ->label(),
    '%handler' => $this
      ->label(),
    '%send' => $this->configuration['send'],
  ];

  // Get message to make sure there is a destination.
  $message = $this
    ->getMessage($webform_submission);

  // Don't send the message if empty (aka To, CC, and BCC is empty).
  if (!$this
    ->hasRecipient($webform_submission, $message)) {
    if ($this->configuration['debug']) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('%submission: Email <b>not sent</b> for %handler handler because a <em>To</em>, <em>CC</em>, or <em>BCC</em> email was not provided.', $t_args));
    }
    return FALSE;
  }

  // When testing send email immediately.
  if ($this
    ->getWebform()
    ->isTest() && !empty($this->configuration['test_send'])) {
    $this
      ->sendMessage($webform_submission, $message);
    return TRUE;
  }

  // Get send date.
  $send_iso_date = $webform_scheduled_email_manager
    ->getSendDate($webform_submission, $this->handler_id);
  $t_args['%date'] = $send_iso_date;

  // Log and exit when we are unable to schedule an email due to an invalid
  // date.
  if (!$send_iso_date) {
    if ($this->configuration['debug']) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('%submission: Email <b>not scheduled</b> for %handler handler because %send is not a valid date/token.', $t_args), TRUE);
    }
    $context = $t_args + [
      'link' => $this
        ->getWebform()
        ->toLink($this
        ->t('Edit'), 'handlers')
        ->toString(),
    ];
    $this
      ->getLogger()
      ->warning('%submission: Email <b>not scheduled</b> for %handler handler because %send is not a valid date/token.', $context);
    return FALSE;
  }

  // Finally, schedule the email, which also writes to the submission log
  // and watchdog.
  $status = $webform_scheduled_email_manager
    ->schedule($webform_submission, $this
    ->getHandlerId());

  // Debug by displaying schedule message onscreen.
  if ($this->configuration['debug']) {
    $statuses = [
      WebformScheduledEmailManagerInterface::EMAIL_ALREADY_SCHEDULED => $this
        ->t('Already Scheduled'),
      WebformScheduledEmailManagerInterface::EMAIL_SCHEDULED => $this
        ->t('Scheduled'),
      WebformScheduledEmailManagerInterface::EMAIL_RESCHEDULED => $this
        ->t('Rescheduled'),
      WebformScheduledEmailManagerInterface::EMAIL_UNSCHEDULED => $this
        ->t('Unscheduled'),
      WebformScheduledEmailManagerInterface::EMAIL_IGNORED => $this
        ->t('Ignored'),
    ];
    $t_args['@action'] = mb_strtolower($statuses[$status]);
    $this
      ->messenger()
      ->addWarning($this
      ->t('%submission: Email <b>@action</b> by %handler handler to be sent on %date.', $t_args), TRUE);
    $debug_message = $this
      ->buildDebugMessage($webform_submission, $message);
    $debug_message['status'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Status'),
      '#markup' => $statuses[$status],
      '#wrapper_attributes' => [
        'class' => [
          'container-inline',
        ],
        'style' => 'margin: 0',
      ],
      '#weight' => -10,
    ];
    $debug_message['send'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Send on'),
      '#markup' => $send_iso_date,
      '#wrapper_attributes' => [
        'class' => [
          'container-inline',
        ],
        'style' => 'margin: 0',
      ],
      '#weight' => -10,
    ];
    $this
      ->messenger()
      ->addWarning(\Drupal::service('renderer')
      ->renderPlain($debug_message), TRUE);
  }
  return $status;
}