You are here

public function WebformSubmissionStorage::purge in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformSubmissionStorage.php \Drupal\webform\WebformSubmissionStorage::purge()

Purge webform submissions.

Parameters

int $count: Amount of webform submissions to purge.

Overrides WebformSubmissionStorageInterface::purge

File

src/WebformSubmissionStorage.php, line 1230

Class

WebformSubmissionStorage
Defines the webform submission storage.

Namespace

Drupal\webform

Code

public function purge($count) {
  $days_to_seconds = 60 * 60 * 24;
  $query = $this
    ->getWebformStorage()
    ->getQuery();
  $query
    ->accessCheck(FALSE);
  $query
    ->condition('settings.purge', [
    WebformSubmissionStorageInterface::PURGE_DRAFT,
    WebformSubmissionStorageInterface::PURGE_COMPLETED,
    WebformSubmissionStorageInterface::PURGE_ALL,
  ], 'IN');
  $query
    ->condition('settings.purge_days', 0, '>');
  $webform_ids = array_values($query
    ->execute());
  $remaining = $count;
  if (!empty($webform_ids)) {
    $webforms = $this
      ->getWebformStorage()
      ->loadMultiple($webform_ids);
    foreach ($webforms as $webform) {
      $query = $this
        ->getQuery();

      // Since results of this query are never displayed to the user and we
      // actually need to query the entire dataset of webform submissions, we
      // are disabling access check.
      $query
        ->accessCheck(FALSE);
      $query
        ->condition('created', $this->time
        ->getRequestTime() - $webform
        ->getSetting('purge_days') * $days_to_seconds, '<');
      $query
        ->condition('webform_id', $webform
        ->id());
      switch ($webform
        ->getSetting('purge')) {
        case WebformSubmissionStorageInterface::PURGE_DRAFT:
          $query
            ->condition('in_draft', 1);
          break;
        case WebformSubmissionStorageInterface::PURGE_COMPLETED:
          $query
            ->condition('in_draft', 0);
          break;
      }
      $query
        ->range(0, $remaining);
      $sids = array_values($query
        ->execute());
      if (empty($sids)) {
        continue;
      }
      $remaining -= count($sids);
      $webform_submissions = $this
        ->loadMultiple($sids);
      $webform
        ->invokeHandlers('prePurge', $webform_submissions);
      $this
        ->moduleHandler()
        ->invokeAll('webform_submissions_pre_purge', [
        $webform_submissions,
      ]);
      $this
        ->delete($webform_submissions);
      $webform
        ->invokeHandlers('postPurge', $webform_submissions);
      $this
        ->moduleHandler()
        ->invokeAll('webform_submissions_post_purge', [
        $webform_submissions,
      ]);
      if ($remaining === 0) {

        // We've collected enough webform submissions for purging in this run.
        break;
      }
    }
  }
}