You are here

public function YamlFormSubmissionExporter::getQuery in YAML Form 8

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

Return value

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

Overrides YamlFormSubmissionExporterInterface::getQuery

File

src/YamlFormSubmissionExporter.php, line 698

Class

YamlFormSubmissionExporter
Form submission exporter.

Namespace

Drupal\yamlform

Code

public function getQuery() {
  $export_options = $this
    ->getExportOptions();
  $yamlform = $this
    ->getYamlForm();
  $source_entity = $this
    ->getSourceEntity();
  $query = $this->queryFactory
    ->get('yamlform_submission')
    ->condition('yamlform_id', $yamlform
    ->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':
      if ($export_options['range_start']) {
        $query
          ->condition('created', strtotime($export_options['range_start']), '>=');
      }
      if ($export_options['range_end']) {
        $query
          ->condition('created', strtotime($export_options['range_end']), '<=');
      }
      break;
  }

  // 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('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), '>=');
    }
  }

  // Sort by sid with the oldest one first.
  $query
    ->sort('sid', 'ASC');
  return $query;
}