You are here

public function Proximity::query in CiviCRM Entity 8.3

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides FilterPluginBase::query

File

src/Plugin/views/filter/Proximity.php, line 151

Class

Proximity
Filter handler for proximity.

Namespace

Drupal\civicrm_entity\Plugin\views\filter

Code

public function query() {

  // Make sure that postal code and distance are set before altering the
  // query.
  if ((!empty($this->value['value']) || !empty($this->value['city']) || !empty($this->value['state_province_id'])) && !empty($this->value['distance'])) {
    $distance = $this
      ->getCalculatedDistance($this->value['distance'], $this->value['distance_unit']);
    $countries = $this->civicrmApi
      ->get('Country', [
      'sequential' => 1,
      'id' => static::COUNTRY_ID,
      'return' => [
        'name',
      ],
    ]);
    $proximity_address = [
      'postal_code' => $this->value['value'],
      'state_province_id' => $this->value['state_province_id'],
      'city' => $this->value['city'],
      'country' => !empty($countries) ? $countries[0]['name'] : '',
      'country_id' => static::COUNTRY_ID,
      'distance_unit' => $this->value['distance_unit'],
    ];
    $geocoded_address = $this
      ->getGeocodedAddress($proximity_address);
    list($min_longitude, $max_longitude) = \CRM_Contact_BAO_ProximityQuery::earthLongitudeRange($geocoded_address['longitude'], $geocoded_address['latitude'], $distance);
    list($min_latitude, $max_latitude) = \CRM_Contact_BAO_ProximityQuery::earthLatitudeRange($geocoded_address['longitude'], $geocoded_address['latitude'], $distance);
    $this
      ->ensureMyTable();
    $condition = new Condition('AND');
    if (!is_nan($min_latitude)) {
      $condition
        ->condition("{$this->tableAlias}.geo_code_1", $min_latitude, '>=');
    }
    if (!is_nan($max_latitude)) {
      $condition
        ->condition("{$this->tableAlias}.geo_code_1", $max_latitude, '<=');
    }
    if (!is_nan($min_longitude)) {
      $condition
        ->condition("{$this->tableAlias}.geo_code_2", $min_longitude, '>=');
    }
    if (!is_nan($max_longitude)) {
      $condition
        ->condition("{$this->tableAlias}.geo_code_2", $max_longitude, '<=');
    }
    if ($condition
      ->count() > 0) {
      $this->query
        ->addWhere($this->options['group'], $condition);
    }
    $expression = "\n        ACOS(\n          COS(RADIANS({$this->tableAlias}.geo_code_1)) *\n          COS(RADIANS({$geocoded_address['latitude']})) *\n          COS(RADIANS({$this->tableAlias}.geo_code_2) - RADIANS({$geocoded_address['longitude']})) +\n          SIN(RADIANS({$this->tableAlias}.geo_code_1)) *\n          SIN(RADIANS({$geocoded_address['latitude']}))\n        ) * 6378137\n      ";
    $this->query
      ->addWhereExpression($this->options['group'], "{$expression} <= {$distance}");
  }
}