GeofieldProximitySourceBase.php in Geofield 8
File
src/Plugin/GeofieldProximitySourceBase.php
View source
<?php
namespace Drupal\geofield\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\geofield\Exception\HaversineUnavailableException;
use Drupal\geofield\Exception\InvalidPointException;
use Drupal\geofield\Exception\ProximityUnavailableException;
use Drupal\views\Plugin\views\HandlerBase;
abstract class GeofieldProximitySourceBase extends PluginBase implements GeofieldProximitySourceInterface {
use StringTranslationTrait;
protected $units;
protected $viewHandler;
protected $origin;
public function isValidLocation($lat, $lon) {
return is_numeric($lat) && is_numeric($lon);
}
public function isEmptyLocation($lat, $lon) {
return empty($lat) && empty($lon);
}
public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) {
}
public function validateOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents) {
}
public function getOrigin() {
return $this->origin;
}
public function setOrigin(array $origin) {
return $this->origin = $origin;
}
public function setUnits($units) {
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;
}
public function getUnits() {
return $this->units;
}
public function getUnitsOptions() {
return array_keys(geofield_radius_options());
}
public function setViewHandler(HandlerBase $view_handler) {
$this->viewHandler = $view_handler;
}
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),
]));
}
$radius = constant($this->units);
$origin = $this
->getOrigin();
if (!isset($origin['lat']) || !isset($origin['lon']) || $this
->isEmptyLocation($origin['lat'], $origin['lon'])) {
return NULL;
}
$origin_latitude = deg2rad($origin['lat']);
$origin_longitude = deg2rad($origin['lon']);
$destination_latitude = deg2rad($lat);
$destination_longitude = deg2rad($lon);
$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;
}
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),
];
}
}