You are here

public function WebformScheduledEmailManager::getSendDate in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_scheduled_email/src/WebformScheduledEmailManager.php \Drupal\webform_scheduled_email\WebformScheduledEmailManager::getSendDate()

Get a webform submission's send date.

Parameters

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

string $handler_id: A webform handler id.

Return value

string|bool A send date using ISO date (YYYY-MM-DD) or datetime format (YYYY-MM-DD HH:MM:SS) or FALSE if the send date is invalid.

Overrides WebformScheduledEmailManagerInterface::getSendDate

1 call to WebformScheduledEmailManager::getSendDate()
WebformScheduledEmailManager::schedule in modules/webform_scheduled_email/src/WebformScheduledEmailManager.php
Scheduled an email to be send at a later date.

File

modules/webform_scheduled_email/src/WebformScheduledEmailManager.php, line 165

Class

WebformScheduledEmailManager
Defines the webform scheduled email manager.

Namespace

Drupal\webform_scheduled_email

Code

public function getSendDate(WebformSubmissionInterface $webform_submission, $handler_id) {
  $webform = $webform_submission
    ->getWebform();

  /** @var \Drupal\webform_scheduled_email\Plugin\WebformHandler\ScheduleEmailWebformHandler $handler */
  $handler = $webform
    ->getHandler($handler_id);
  $send = $handler
    ->getSetting('send');
  if (empty($send)) {
    return FALSE;
  }

  // Get send +/- days.
  $days = $handler
    ->getSetting('days') ?: 0;

  // ISSUE:
  // [webform_submission:completed:html_date] token is not being replaced
  // during tests.
  //
  // WORKAROUND:
  // Convert [*:html_date] to [*:custom:Y-m-d].
  $send = preg_replace('/^\\[(date|webform_submission:(?:[^:]+)):html_date\\]$/', '[\\1:custom:Y-m-d]', $send);

  // Convert [*:html_datetime] to [*:custom:Y-m-d H:i:s].
  $send = preg_replace('/^\\[(date|webform_submission:(?:[^:]+)):html_datetime\\]$/', '[\\1:custom:Y-m-d H:i:s]', $send);

  // Replace tokens.
  $send = $this->tokenManager
    ->replace($send, $webform_submission);
  $time = strtotime($send);
  if (!$time) {
    return FALSE;
  }
  $date = new \DateTime();
  $date
    ->setTimestamp($time);
  if ($days) {
    date_add($date, date_interval_create_from_date_string("{$days} days"));
  }
  return date_format($date, $this
    ->getDateFormat());
}