You are here

public function WebformScheduledEmailManager::unschedule 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::unschedule()

Unscheduled an email that is waiting to sent.

Parameters

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

string|null $handler_id: The webform handler ID.

Overrides WebformScheduledEmailManagerInterface::unschedule

2 calls to WebformScheduledEmailManager::unschedule()
WebformScheduledEmailManager::cronSchedule in modules/webform_scheduled_email/src/WebformScheduledEmailManager.php
Schedule emails.
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 343

Class

WebformScheduledEmailManager
Defines the webform scheduled email manager.

Namespace

Drupal\webform_scheduled_email

Code

public function unschedule(EntityInterface $entity, $handler_id = NULL) {

  // NOTE: Handler ID is required to unscheduled to prevent accidental
  // deletion of all schedule emails for a submission.
  if ($entity instanceof WebformSubmissionInterface && $handler_id) {
    $webform_submission = $entity;
    $webform = $webform_submission
      ->getWebform();
    $source_entity = $webform_submission
      ->getSourceEntity();
    $handler = $webform
      ->getHandler($handler_id);

    // Remove scheduled email.
    $query = $this->database
      ->delete('webform_scheduled_email');
    $this
      ->addQueryConditions($query, $webform, $webform_submission, $source_entity, $handler_id);
    $query
      ->execute();

    // Log message in submission's log.
    $channel = $webform
      ->hasSubmissionLog() ? 'webform_submission' : 'webform';
    $context = [
      '@title' => $webform_submission
        ->label(),
      '@handler' => $handler
        ->label(),
      'link' => $webform_submission
        ->toLink($this
        ->t('View'))
        ->toString(),
      'webform_submission' => $webform_submission,
      'handler_id' => $handler_id,
      'operation' => 'email unscheduled',
    ];
    $this
      ->getLogger($channel)
      ->notice("@title: Email unscheduled for '@handler' handler.", $context);
  }
  elseif ($entity instanceof WebformInterface) {
    $webform = $entity;
    $query = $this->database
      ->update('webform_scheduled_email')
      ->fields([
      'state' => WebformScheduledEmailManagerInterface::SUBMISSION_UNSCHEDULE,
    ])
      ->condition('webform_id', $webform
      ->id());
    if ($handler_id) {
      $query
        ->condition('handler_id', $handler_id);
    }
    $query
      ->execute();
  }

  // Since webform and submissions can also be used as a source entity,
  // include them in unscheduling.
  $query = $this->database
    ->update('webform_scheduled_email')
    ->fields([
    'state' => WebformScheduledEmailManagerInterface::SUBMISSION_UNSCHEDULE,
  ])
    ->condition('entity_type', $entity
    ->getEntityTypeId())
    ->condition('entity_id', $entity
    ->id());
  if ($handler_id) {
    $query
      ->condition('handler_id', $handler_id);
  }
  $query
    ->execute();
}