You are here

public function WebformSubmissionExporter::getQuery in Webform 6.x

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

Get webform submission query for specified YAMl webform and export options.

Return value

\Drupal\Core\Entity\Query\QueryInterface A webform submission entity query.

Overrides WebformSubmissionExporterInterface::getQuery

File

src/WebformSubmissionExporter.php, line 899

Class

WebformSubmissionExporter
Webform submission exporter.

Namespace

Drupal\webform

Code

public function getQuery() {
  $export_options = $this
    ->getExportOptions();
  $webform = $this
    ->getWebform();
  $source_entity = $this
    ->getSourceEntity();
  $query = $this
    ->getSubmissionStorage()
    ->getQuery()
    ->condition('webform_id', $webform
    ->id());

  // Filter by source entity or submitted to.
  if ($source_entity) {
    $query
      ->condition('entity_type', $source_entity
      ->getEntityTypeId());
    $query
      ->condition('entity_id', $source_entity
      ->id());
  }
  elseif ($export_options['entity_type']) {
    $query
      ->condition('entity_type', $export_options['entity_type']);
    if ($export_options['entity_id']) {
      $query
        ->condition('entity_id', $export_options['entity_id']);
    }
  }

  // Filter by sid or date range.
  switch ($export_options['range_type']) {
    case 'serial':
      if ($export_options['range_start']) {
        $query
          ->condition('serial', $export_options['range_start'], '>=');
      }
      if ($export_options['range_end']) {
        $query
          ->condition('serial', $export_options['range_end'], '<=');
      }
      break;
    case 'sid':
      if ($export_options['range_start']) {
        $query
          ->condition('sid', $export_options['range_start'], '>=');
      }
      if ($export_options['range_end']) {
        $query
          ->condition('sid', $export_options['range_end'], '<=');
      }
      break;
    case 'date':
    case 'date_completed':
    case 'date_changed':
      $date_field = preg_match('/date_(completed|changed)/', $export_options['range_type'], $match) ? $match[1] : 'created';
      if ($export_options['range_start']) {
        $query
          ->condition($date_field, strtotime($export_options['range_start']), '>=');
      }
      if ($export_options['range_end']) {
        $query
          ->condition($date_field, strtotime('+1 day', strtotime($export_options['range_end'])), '<');
      }
      break;
  }

  // Filter by UID.
  if ($export_options['uid'] !== '') {
    $query
      ->condition('uid', $export_options['uid'], '=');
  }

  // Filter by (completion) state.
  switch ($export_options['state']) {
    case 'draft':
      $query
        ->condition('in_draft', 1);
      break;
    case 'completed':
      $query
        ->condition('in_draft', 0);
      break;
  }

  // Filter by sticky.
  if ($export_options['sticky']) {
    $query
      ->condition('sticky', 1);
  }

  // Filter by latest.
  if ($export_options['range_type'] === 'latest' && $export_options['range_latest']) {

    // Clone the query and use it to get latest sid starting sid.
    $latest_query = clone $query;
    $latest_query
      ->sort('created', 'DESC');
    $latest_query
      ->sort('sid', 'DESC');
    $latest_query
      ->range(0, (int) $export_options['range_latest']);
    if ($latest_query_entity_ids = $latest_query
      ->execute()) {
      $query
        ->condition('sid', end($latest_query_entity_ids), '>=');
    }
  }
  else {

    // Sort by created and sid in ASC or DESC order.
    $query
      ->sort('created', isset($export_options['order']) ? $export_options['order'] : 'ASC');
    $query
      ->sort('sid', isset($export_options['order']) ? $export_options['order'] : 'ASC');
  }

  // Do not check access to submission since the exporter UI and Drush
  // already have access checking.
  // @see webform_query_webform_submission_access_alter()
  $query
    ->accessCheck(FALSE);
  return $query;
}