You are here

protected function ProximityFilter::getProximityCenterBySource in Geolocation Field 8

Retrieve proximity center data from configured source.

Return value

array Proximity Center data.

3 calls to ProximityFilter::getProximityCenterBySource()
ProximityFilter::getLatitudeValue in src/Plugin/views/filter/ProximityFilter.php
Retrieve latitude value from configured source.
ProximityFilter::getLongitudeValue in src/Plugin/views/filter/ProximityFilter.php
Retrieve longitude value from configured source.
ProximityFilter::getProximityUnit in src/Plugin/views/filter/ProximityFilter.php
Retrieve proximity unit from configured source.

File

src/Plugin/views/filter/ProximityFilter.php, line 672

Class

ProximityFilter
Filter handler for search keywords.

Namespace

Drupal\geolocation\Plugin\views\filter

Code

protected function getProximityCenterBySource() {
  if (!empty($this->currentProximityCenter)) {
    return $this->currentProximityCenter;
  }
  switch ($this->options['proximity_source']) {
    case 'boundary_filter':
      $filter = $this->view->filter[$this->options['boundary_filter']];

      // See documentation at
      // http://tubalmartin.github.io/spherical-geometry-php/#LatLngBounds
      $proximity_center = [
        'latitude' => ($filter->value['lat_south_west'] + $filter->value['lat_north_east']) / 2,
        'longitude' => ($filter->value['lng_south_west'] + $filter->value['lng_north_east']) / 2,
      ];
      if ($filter->value['lng_south_west'] > $filter->value['lng_north_east']) {
        $proximity_center['longitude'] = $proximity_center['longitude'] == 0 ? 180 : fmod(fmod($proximity_center['longitude'] + 180 - -180, 360) + 360, 360) + -180;
      }
      break;
    case 'argument':

      /** @var \Drupal\geolocation\Plugin\views\argument\ProximityArgument $argument */
      $argument = $this->view->argument[$this->options['proximity_argument']];
      $values = $argument
        ->getParsedReferenceLocation();
      $proximity_center = [
        'latitude' => $values['lat'],
        'longitude' => $values['lng'],
        'units' => $values['units'],
      ];
      break;
    case 'entity_id_argument':
      $argument = $this->view->argument[$this->options['entity_id_argument']];
      if (empty($argument)) {
        return [];
      }
      $entity_id = $argument
        ->getValue();
      if (!ctype_digit($entity_id)) {
        return [];
      }

      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = \Drupal::entityTypeManager()
        ->getStorage($this
        ->getEntityType())
        ->load($entity_id);
      if (!$entity
        ->hasField($this->realField)) {
        return [];
      }
      $field = $entity
        ->get($this->realField);
      if (empty($field)) {
        return [];
      }
      $values = $field
        ->getValue();
      if (empty($values)) {
        return [];
      }
      $values = reset($values);
      $proximity_center = [
        'latitude' => $values['lat'],
        'longitude' => $values['lng'],
      ];
      break;
    case 'client_location':
    case 'exposed':
      $proximity_center = [
        'latitude' => empty($this->value[$this->options['expose']['identifier'] . '-lat']) ? '' : $this->value[$this->options['expose']['identifier'] . '-lat'],
        'longitude' => empty($this->value[$this->options['expose']['identifier'] . '-lng']) ? '' : $this->value[$this->options['expose']['identifier'] . '-lng'],
      ];
      break;
    default:
      $proximity_center = [
        'latitude' => $this->options['proximity_lat'],
        'longitude' => $this->options['proximity_lng'],
      ];
  }
  $this->currentProximityCenter = $proximity_center;
  return $proximity_center;
}