You are here

public function WebformScheduledEmailManager::schedule in Webform 6.x

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

Scheduled an email to be send at a later date.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: A webform or webform submission.

string $handler_id: The webform handler ID.

Return value

string|false The status of scheduled emails. FALSE if send date is invalid. (EMAIL_SCHEDULED, EMAIL_RESCHEDULED, or EMAIL_ALREADY_SCHEDULED)

Overrides WebformScheduledEmailManagerInterface::schedule

1 call to WebformScheduledEmailManager::schedule()
WebformScheduledEmailManager::cronSchedule in modules/webform_scheduled_email/src/WebformScheduledEmailManager.php
Schedule emails.

File

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

Class

WebformScheduledEmailManager
Defines the webform scheduled email manager.

Namespace

Drupal\webform_scheduled_email

Code

public function schedule(EntityInterface $entity, $handler_id) {
  if ($entity instanceof WebformSubmissionInterface) {
    $webform_submission = $entity;
    $webform = $webform_submission
      ->getWebform();

    /** @var \Drupal\webform_scheduled_email\Plugin\WebformHandler\ScheduleEmailWebformHandler $handler */
    $handler = $webform
      ->getHandler($handler_id);
    $handler_settings = $handler
      ->getSettings();

    // Check send date and set timestamp.
    $send_iso_date = $this
      ->getSendDate($webform_submission, $handler_id);
    if ($send_iso_date === FALSE) {
      $this
        ->unschedule($webform_submission, $handler_id);
      return WebformScheduledEmailManagerInterface::EMAIL_UNSCHEDULED;
    }
    $send_timestamp = strtotime($send_iso_date);

    // Check submission state and unschedule.
    $state = $webform_submission
      ->getState();
    if (!in_array($state, $handler_settings['states']) && $handler_settings['unschedule']) {
      $this
        ->unschedule($webform_submission, $handler_id);
      return WebformScheduledEmailManagerInterface::EMAIL_UNSCHEDULED;
    }

    // Check if action should be triggered in the past.
    if (!empty($handler_settings['ignore_past']) && $send_timestamp < $this->time
      ->getRequestTime()) {
      $this
        ->unschedule($webform_submission, $handler_id);
      return WebformScheduledEmailManagerInterface::EMAIL_IGNORED;
    }

    // Check recipient.
    if (!$handler
      ->hasRecipient($webform_submission, $handler
      ->getMessage($webform_submission))) {
      $this
        ->unschedule($webform_submission, $handler_id);
      return WebformScheduledEmailManagerInterface::EMAIL_UNSCHEDULED;
    }

    // See if there is already a scheduled email.
    $scheduled_email = $this
      ->load($webform_submission, $handler_id);

    // Get update or insert $query, reschedule or schedule $action, or skip.
    if (!$scheduled_email) {
      $query = $this->database
        ->insert('webform_scheduled_email');
      $action = $this
        ->t('scheduled');
      $status = WebformScheduledEmailManagerInterface::EMAIL_SCHEDULED;
    }
    else {
      $query = $this->database
        ->update('webform_scheduled_email');
      $query
        ->condition('eid', $scheduled_email->eid);
      if ($scheduled_email->send !== $send_timestamp) {
        $action = $this
          ->t('rescheduled');
        $status = WebformScheduledEmailManagerInterface::EMAIL_RESCHEDULED;
      }
      else {
        $action = NULL;
        $status = WebformScheduledEmailManagerInterface::EMAIL_ALREADY_SCHEDULED;
      }
    }
    $query
      ->fields([
      'webform_id' => $webform_submission
        ->getWebform()
        ->id(),
      'sid' => $webform_submission
        ->id(),
      'entity_type' => $webform_submission->entity_type->value,
      'entity_id' => $webform_submission->entity_id->value,
      'handler_id' => $handler_id,
      'state' => WebformScheduledEmailManagerInterface::SUBMISSION_SEND,
      'send' => $send_timestamp,
    ])
      ->execute();

    // If email is already scheduled when don't need to log anything.
    if ($status === WebformScheduledEmailManagerInterface::EMAIL_ALREADY_SCHEDULED) {
      return $status;
    }
    $channel = $webform
      ->hasSubmissionLog() ? 'webform_submission' : 'webform';
    $context = [
      '@title' => $webform_submission
        ->label(),
      '@action' => $action,
      '@handler' => $handler
        ->label(),
      '@date' => $send_iso_date,
      'link' => $webform_submission
        ->toLink($this
        ->t('View'))
        ->toString(),
      'webform_submission' => $webform_submission,
      'handler_id' => $handler_id,
      'operation' => 'email ' . $action,
    ];
    $this
      ->getLogger($channel)
      ->notice("@title: Email @action by '@handler' handler to be sent on @date.", $context);
    return $status;
  }
  elseif ($entity instanceof WebformInterface) {
    $webform = $entity;

    // Set all existing submissions reviewed.
    $this->database
      ->update('webform_scheduled_email')
      ->fields([
      'state' => WebformScheduledEmailManagerInterface::SUBMISSION_SCHEDULE,
    ])
      ->condition('webform_id', $webform
      ->id())
      ->condition('handler_id', $handler_id)
      ->execute();

    // Set all remaining submissions to be scheduled.
    // Get existing scheduled email sids.
    $query = $this->database
      ->select('webform_scheduled_email', 'w');
    $query
      ->fields('w', [
      'sid',
    ]);
    $query
      ->condition('webform_id', $webform
      ->id());
    $query
      ->condition('handler_id', $handler_id);
    $sids = $query
      ->execute()
      ->fetchCol();

    // Get webform submissions that need to be scheduled.
    $query = $this->database
      ->select('webform_submission', 'w');
    $query
      ->fields('w', [
      'webform_id',
      'entity_type',
      'entity_id',
      'sid',
    ]);
    $query
      ->addExpression("'{$handler_id}'", 'handler_id');
    $query
      ->addExpression("'" . WebformScheduledEmailManagerInterface::SUBMISSION_SCHEDULE . "'", 'state');
    $query
      ->condition('webform_id', $webform
      ->id());
    if ($sids) {
      $query
        ->condition('sid', $sids, 'NOT IN');
    }

    // Perform the bulk insert.
    $this->database
      ->insert('webform_scheduled_email')
      ->from($query)
      ->execute();
  }
  else {
    throw new \Exception('Scheduling email for source entity is not supported');
  }
  return NULL;
}