You are here

abstract class GeofieldProximitySourceBase in Geofield 8

Base class for Geofield Proximity Source plugins.

Hierarchy

Expanded class hierarchy of GeofieldProximitySourceBase

3 files declare their use of GeofieldProximitySourceBase
ContextProximityFilter.php in src/Plugin/GeofieldProximitySource/ContextProximityFilter.php
ManualOriginDefault.php in src/Plugin/GeofieldProximitySource/ManualOriginDefault.php
OriginFromProximityFilter.php in src/Plugin/GeofieldProximitySource/OriginFromProximityFilter.php

File

src/Plugin/GeofieldProximitySourceBase.php, line 16

Namespace

Drupal\geofield\Plugin
View source
abstract class GeofieldProximitySourceBase extends PluginBase implements GeofieldProximitySourceInterface {
  use StringTranslationTrait;

  /**
   * The name of the constant defining the measurement unit.
   *
   * @var string
   */
  protected $units;

  /**
   * The view handler which uses this proximity plugin.
   *
   * @var \Drupal\views\Plugin\views\HandlerBase
   */
  protected $viewHandler;

  /**
   * The origin point to measure proximity from.
   *
   * @var array
   */
  protected $origin;

  /**
   * {@inheritdoc}
   */
  public function isValidLocation($lat, $lon) {
    return is_numeric($lat) && is_numeric($lon);
  }

  /**
   * {@inheritdoc}
   */
  public function isEmptyLocation($lat, $lon) {
    return empty($lat) && empty($lon);
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) {
  }

  /**
   * {@inheritdoc}
   */
  public function validateOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents) {
  }

  /**
   * {@inheritdoc}
   */
  public function getOrigin() {
    return $this->origin;
  }

  /**
   * {@inheritdoc}
   */
  public function setOrigin(array $origin) {
    return $this->origin = $origin;
  }

  /**
   * {@inheritdoc}
   */
  public function setUnits($units) {

    // If the given value is not a valid option, throw an error.
    if (!in_array($units, $this
      ->getUnitsOptions())) {
      $message = $this
        ->t('Invalid units supplied.');
      \Drupal::logger('geofield')
        ->error($message);
      return FALSE;
    }
    else {
      $this->units = $units;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getUnits() {
    return $this->units;
  }

  /**
   * Get the list of valid options for units.
   *
   * @return array
   *   The list of available unit types.
   */
  public function getUnitsOptions() {
    return array_keys(geofield_radius_options());
  }

  /**
   * {@inheritdoc}
   */
  public function setViewHandler(HandlerBase $view_handler) {
    $this->viewHandler = $view_handler;
  }

  /**
   * {@inheritdoc}
   */
  public function getProximity($lat, $lon) {
    if (!$this
      ->isValidLocation($lat, $lon)) {
      throw new InvalidPointException($this
        ->t('@proximity_handler reports Invalid Point coordinates', [
        '@proximity_handler' => get_class($this),
      ]));
    }

    // Fetch the value of the units that have been set for this class. The
    // constants are defined in the module file.
    $radius = constant($this->units);
    $origin = $this
      ->getOrigin();
    if (!isset($origin['lat']) || !isset($origin['lon']) || $this
      ->isEmptyLocation($origin['lat'], $origin['lon'])) {
      return NULL;
    }

    // Convert degrees to radians.
    $origin_latitude = deg2rad($origin['lat']);
    $origin_longitude = deg2rad($origin['lon']);
    $destination_latitude = deg2rad($lat);
    $destination_longitude = deg2rad($lon);

    // Calculate proximity.
    $proximity = $radius * acos(cos($origin_latitude) * cos($destination_latitude) * cos($destination_longitude - $origin_longitude) + sin($origin_latitude) * sin($destination_latitude));
    if (!is_numeric($proximity)) {
      throw new ProximityUnavailableException($this
        ->t('@proximity_handler not able to calculate valid Proximity value', [
        '@proximity_handler' => get_class($this),
      ]));
    }
    return $proximity;
  }

  /**
   * {@inheritdoc}
   */
  public function getHaversineOptions() {
    $origin = $this
      ->getOrigin();
    if (!$origin || !isset($origin['lat']) || !isset($origin['lon'])) {
      throw new HaversineUnavailableException('Not able to calculate Haversine Options due to invalid Proximity Origin definition.');
    }
    if ($this
      ->isEmptyLocation($origin['lat'], $origin['lon']) || !$this
      ->isValidLocation($origin['lat'], $origin['lon'])) {
      return NULL;
    }
    return [
      'origin_latitude' => $origin['lat'],
      'origin_longitude' => $origin['lon'],
      'earth_radius' => constant($this->units),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeofieldProximitySourceBase::$origin protected property The origin point to measure proximity from.
GeofieldProximitySourceBase::$units protected property The name of the constant defining the measurement unit.
GeofieldProximitySourceBase::$viewHandler protected property The view handler which uses this proximity plugin.
GeofieldProximitySourceBase::buildOptionsForm public function Builds the specific form elements for the geofield proximity plugin. Overrides GeofieldProximitySourceInterface::buildOptionsForm 2
GeofieldProximitySourceBase::getHaversineOptions public function Gets the haversine options. Overrides GeofieldProximitySourceInterface::getHaversineOptions
GeofieldProximitySourceBase::getOrigin public function Gets the proximity distance origin. Overrides GeofieldProximitySourceInterface::getOrigin 2
GeofieldProximitySourceBase::getProximity public function Get the calculated proximity. Overrides GeofieldProximitySourceInterface::getProximity
GeofieldProximitySourceBase::getUnits public function Get the current units. Overrides GeofieldProximitySourceInterface::getUnits
GeofieldProximitySourceBase::getUnitsOptions public function Get the list of valid options for units.
GeofieldProximitySourceBase::isEmptyLocation public function Check if Location is empty. Overrides GeofieldProximitySourceInterface::isEmptyLocation
GeofieldProximitySourceBase::isValidLocation public function Check for a valid couple of latitude and longitude. Overrides GeofieldProximitySourceInterface::isValidLocation
GeofieldProximitySourceBase::setOrigin public function Sets the proximity distance origin. Overrides GeofieldProximitySourceInterface::setOrigin
GeofieldProximitySourceBase::setUnits public function Set the units to perform the calculation in. Overrides GeofieldProximitySourceInterface::setUnits
GeofieldProximitySourceBase::setViewHandler public function Sets view handler which uses this proximity plugin. Overrides GeofieldProximitySourceInterface::setViewHandler
GeofieldProximitySourceBase::validateOptionsForm public function Validates the options form for the geofield proximity plugin. Overrides GeofieldProximitySourceInterface::validateOptionsForm 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.