You are here

public function WebformSubmissionLogManager::getQuery in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_submission_log/src/WebformSubmissionLogManager.php \Drupal\webform_submission_log\WebformSubmissionLogManager::getQuery()

Get webform submission log query.

Parameters

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

\Drupal\Core\Entity\EntityInterface|null $source_entity: (optional) A webform submission source entity.

\Drupal\Core\Session\AccountInterface $account: The current user account.

array $options: (optional) Additional options and query conditions.

Return value

\Drupal\Core\Database\Query\SelectInterface A webform submission log select query.

Overrides WebformSubmissionLogManagerInterface::getQuery

File

modules/webform_submission_log/src/WebformSubmissionLogManager.php, line 59

Class

WebformSubmissionLogManager
Webform submission log manager.

Namespace

Drupal\webform_submission_log

Code

public function getQuery(EntityInterface $webform_entity = NULL, EntityInterface $source_entity = NULL, AccountInterface $account = NULL, array $options = []) {

  // Default options.
  $options += [
    'header' => NULL,
    'limit' => NULL,
  ];
  $query = $this->database
    ->select(WebformSubmissionLogManagerInterface::TABLE, 'log');

  // Log fields.
  $query
    ->fields('log', [
    'lid',
    'uid',
    'webform_id',
    'sid',
    'handler_id',
    'operation',
    'message',
    'variables',
    'timestamp',
    'data',
  ]);

  // User fields.
  $query
    ->leftJoin('users_field_data', 'user', 'log.uid = user.uid');

  // Submission fields.
  $query
    ->leftJoin('webform_submission', 'submission', 'log.sid = submission.sid');
  $query
    ->fields('submission', [
    'entity_type',
    'entity_id',
  ]);

  // Webform condition.
  if ($webform_entity instanceof WebformInterface) {
    $query
      ->condition('log.webform_id', $webform_entity
      ->id());
  }
  elseif ($webform_entity instanceof WebformSubmissionInterface) {
    $query
      ->condition('log.webform_id', $webform_entity
      ->getWebform()
      ->id());
    $query
      ->condition('log.sid', $webform_entity
      ->id());
  }

  // Source entity conditions.
  if ($source_entity) {
    $query
      ->condition('submission.entity_type', $source_entity
      ->getEntityTypeId());
    $query
      ->condition('submission.entity_id', $source_entity
      ->id());
  }

  // User account condition.
  if ($account) {
    $query
      ->condition('log.uid', $account
      ->id());
  }

  // Set header sorting.
  if ($options['header']) {
    $query = $query
      ->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender')
      ->orderByHeader($options['header']);
  }

  // Set limit pager.
  if ($options['limit']) {
    $query = $query
      ->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')
      ->limit($options['limit']);
  }
  return $query;
}