View source
<?php
namespace Drupal\geolocation\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\geolocation\LocationInputManager;
use Drupal\views\Plugin\views\filter\NumericFilter;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\geolocation\ProximityTrait;
class ProximityFilter extends NumericFilter implements ContainerFactoryPluginInterface {
use ProximityTrait;
protected $locationInputManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LocationInputManager $location_input_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->locationInputManager = $location_input_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.geolocation.locationinput'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['location_input'] = [
'default' => [],
];
$options['unit'] = [
'default' => 'km',
];
$options['value']['contains']['center'] = [
'default' => [],
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['unit'] = [
'#title' => $this
->t('Distance unit'),
'#description' => $this
->t('Unit to use for conversion of input value to proximity distance.'),
'#type' => 'select',
'#default_value' => $this->options['unit'],
'#weight' => 6,
'#options' => [
'km' => $this
->t('Kilometers'),
'mi' => $this
->t('Miles'),
'nm' => $this
->t('Nautical Miles'),
],
];
$form['location_input'] = $this->locationInputManager
->getOptionsForm($this->options['location_input'], $this);
}
public function groupForm(&$form, FormStateInterface $form_state) {
parent::groupForm($form, $form_state);
$center_form = $this->locationInputManager
->getForm($this->options['location_input'], $this, empty($this->value['center']) ? NULL : $this->value['center']);
if (!empty($center_form)) {
$form['center'] = $center_form;
}
}
public function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
$form['#tree'] = TRUE;
if (!isset($form['value']['value'])) {
$form['value'] = array_replace($form['value'], [
'#type' => 'number',
'#min' => 0,
'#step' => 0.1,
'#title' => $this
->t('Distance'),
'#description' => $this
->t('Distance in %unit', [
'%unit' => $this->options['unit'] === 'km' ? $this
->t('Kilometers') : $this
->t('Miles'),
]),
'#default_value' => $form['value']['#default_value'],
]);
}
else {
$form['value']['value'] = array_replace($form['value']['value'], [
'#type' => 'number',
'#min' => 0,
'#step' => 0.1,
'#title' => $this
->t('Distance'),
'#description' => $this
->t('Distance in %unit', [
'%unit' => $this->options['unit'] === 'km' ? $this
->t('Kilometers') : $this
->t('Miles'),
]),
'#default_value' => $form['value']['value']['#default_value'],
]);
}
$form['center'] = $this->locationInputManager
->getForm($this->options['location_input'], $this, empty($this->value['center']) ? NULL : $this->value['center']);
}
protected function valueSubmit($form, FormStateInterface $form_state) {
$value = $form_state
->getValue([
'options',
'value',
'value',
]);
$distance = (double) $value;
$form_state
->setValue([
'options',
'value',
'value',
], $distance);
$form_state
->setValue([
'options',
'value',
'center',
], $form_state
->getValue([
'options',
'value',
'center',
], []));
parent::valueSubmit($form, $form_state);
}
public function acceptExposedInput($input) {
parent::acceptExposedInput($input);
if (array_key_exists('lat', $input) && $input['lat'] !== '' && array_key_exists('lng', $input) && $input['lng'] !== '') {
$this->value['lat'] = (double) $input['lat'];
$this->value['lng'] = (double) $input['lng'];
}
if (!empty($input['center'])) {
$this->value['center'] = $input['center'];
}
else {
$this->value['center'] = [];
}
return TRUE;
}
public function query() {
$table = $this
->ensureMyTable();
$this->value['value'] = self::convertDistance($this->value['value'], $this->options['unit']);
if (array_key_exists('lat', $this->value) && array_key_exists('lng', $this->value)) {
$center = [
'lat' => (double) $this->value['lat'],
'lng' => (double) $this->value['lng'],
];
}
else {
$center = $this->locationInputManager
->getCoordinates((array) $this->value['center'], $this->options['location_input'], $this);
}
if (empty($center) || !is_numeric($center['lat']) || !is_numeric($center['lng']) || empty($this->value['value'])) {
return;
}
$expression = self::getProximityQueryFragment($table, $this->realField, $center['lat'], $center['lng']);
$info = $this
->operators();
if (!empty($info[$this->operator]['method']) && method_exists($this, $info[$this->operator]['method'])) {
$this
->{$info[$this->operator]['method']}($expression);
}
}
protected function opBetween($expression) {
$query = $this->query;
if ($this->operator == 'between') {
$query
->addWhereExpression($this->options['group'], $expression . ' BETWEEN ' . $this->value['min'] . ' AND ' . $this->value['max']);
}
else {
$query
->addWhereExpression($this->options['group'], $expression . ' NOT BETWEEN ' . $this->value['min'] . ' AND ' . $this->value['max']);
}
}
protected function opSimple($expression) {
$query = $this->query;
$query
->addWhereExpression($this->options['group'], $expression . ' ' . $this->operator . ' ' . $this->value['value']);
}
protected function opEmpty($expression) {
$query = $this->query;
if ($this->operator == 'empty') {
$operator = "IS NULL";
}
else {
$operator = "IS NOT NULL";
}
$query
->addWhereExpression($this->options['group'], $expression . ' ' . $operator);
}
protected function opRegex($expression) {
$query = $this->query;
$query
->addWhereExpression($this->options['group'], $expression . ' ~* ' . $this->value['value']);
}
}