View source
<?php
namespace Drupal\civicrm_entity\Plugin\views\filter;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Condition;
use Drupal\civicrm_entity\CiviCrmApiInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\InOperator as BaseInOperator;
use Drupal\views\ViewExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InOperator extends BaseInOperator {
protected $civicrmApi;
protected $connection;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, CiviCrmApiInterface $civicrm_api, Connection $connection) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->civicrmApi = $civicrm_api;
$this->connection = $connection;
}
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->civicrmApi
->civicrmInitialize();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('civicrm_entity.api'), $container
->get('database'));
}
public function getValueOptions() {
if (isset($this->valueOptions)) {
return $this->valueOptions;
}
if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
if (isset($this->definition['options arguments'])) {
$this->valueOptions = call_user_func_array($this->definition['options callback'], [
$this->definition['options arguments'],
]);
}
else {
$this->valueOptions = call_user_func($this->definition['options callback']);
}
}
else {
$this->valueOptions = [
$this
->t('Yes'),
$this
->t('No'),
];
}
return $this->valueOptions;
}
protected function opSimple() {
if (empty($this->value)) {
return;
}
$this
->ensureMyTable();
$values = array_values($this->value);
$field = "{$this->tableAlias}.{$this->realField}";
if (!isset($this->definition['multi']) || !$this->definition['multi']) {
$this->query
->addWhere($this->options['group'], $field, $values, $this->operator);
}
else {
switch ($this->operator) {
case 'in':
$values = array_map(function ($value) {
return \CRM_Core_DAO::VALUE_SEPARATOR . $value . \CRM_Core_DAO::VALUE_SEPARATOR;
}, $values);
$this->query
->addWhereExpression($this->options['group'], "CAST({$field} AS BINARY) RLIKE BINARY :arg1", [
':arg1' => implode('|', $values),
]);
break;
case 'not in':
$condition = new Condition('AND');
foreach ($values as $value) {
$condition
->condition($field, '%' . $this->connection
->escapeLike($value) . '%', 'NOT LIKE');
}
$this->query
->addWhere($this->options['group'], $condition);
break;
}
}
}
}