public function JobState::query

File

src/Plugin/views/filter/JobState.php, line 77

Class

JobState
Filter based on job state.

Namespace

Drupal\tmgmt\Plugin\views\filter

Code

public function query() {
  $state = reset($this->value);
  $field = $this->field;
  $table = $this->table;
  if (strpos($state, 'job_item_') !== FALSE) {
    $job_item_state = str_replace('job_item_', '', $state);
    $table_alias = 'job_item';
    $job_item_field = 'state';
    $state_definitions = JobItem::getStateDefinitions();
    if ($state_definitions[$job_item_state]['type'] == 'translator_state') {
      $job_item_field = 'translator_state';
    }

    // Create a sub query to add the state of job item to the view.
    $sub_query = \Drupal::database()
      ->select('tmgmt_job_item', $table_alias);
    $sub_query
      ->addField($table_alias, 'tjid');
    $sub_query
      ->condition("{$table_alias}.{$job_item_field}", $job_item_state, '=');

    // Select all job items that are not in the sub query.
    $this->query
      ->addWhere($this->options['group'], 'tjid', $sub_query, 'IN');
    $this->query
      ->addWhere($this->options['group'], "{$table}.{$field}", JobInterface::STATE_ACTIVE, 'IN');
  }
  else {
    $operator = '=';
    if ($state == 'open_jobs') {
      $state = [
        JobInterface::STATE_UNPROCESSED,
        JobInterface::STATE_ACTIVE,
        JobInterface::STATE_REJECTED,
        JobInterface::STATE_CONTINUOUS,
      ];
      $operator = 'IN';
    }
    $this->query
      ->addWhere($this->options['group'], "{$table}.{$field}", $state, $operator);
  }
}