function __construct(&$formValues) { parent::__construct($formValues); $this->_earthFlattening = 1.0 / 298.257223563; $this->_earthRadiusSemiMajor = 6378137.0; $this->_earthRadiusSemiMinor = $this->_earthRadiusSemiMajor * (1.0 - $this->_earthFlattening); $this->_earthEccentricitySQ = 2 * $this->_earthFlattening - pow($this->_earthFlattening, 2); // unset search profile if set unset($this->_formValues['uf_group_id']); if (!empty($this->_formValues)) { // add the country and state if (CRM_Utils_Array::value('country_id', $this->_formValues)) { $this->_formValues['country'] = CRM_Core_PseudoConstant::country($this->_formValues['country_id']); } if (CRM_Utils_Array::value('state_province_id', $this->_formValues)) { $this->_formValues['state_province'] = CRM_Core_PseudoConstant::stateProvince($this->_formValues['state_province_id']); } // use the address to get the latitude and longitude require_once 'CRM/Utils/Geocode/Google.php'; CRM_Utils_Geocode_Google::format($this->_formValues); if (!isset($this->_formValues['geo_code_1']) || !isset($this->_formValues['geo_code_2']) || !isset($this->_formValues['distance'])) { CRM_Core_Error::fatal(ts('Could not geocode input')); } $this->_latitude = $this->_formValues['geo_code_1']; $this->_longitude = $this->_formValues['geo_code_2']; $this->_distance = $this->_formValues['distance'] * 1000; } $this->_earthDistanceSQL = $this->earthDistanceSQL($this->_latitude, $this->_longitude); $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues); $this->_columns = array(ts('Name') => 'sort_name', ts('Street Address') => 'street_address', ts('City') => 'city', ts('Postal Code') => 'postal_code', ts('State') => 'state_province', ts('Country') => 'country'); }
/** * @param $formValues * * @throws Exception */ public function __construct(&$formValues) { parent::__construct($formValues); // unset search profile and other search params if set unset($this->_formValues['uf_group_id']); unset($this->_formValues['component_mode']); unset($this->_formValues['operator']); if (!empty($this->_formValues)) { // add the country and state if (!empty($this->_formValues['country_id'])) { $this->_formValues['country'] = CRM_Core_PseudoConstant::country($this->_formValues['country_id']); } if (!empty($this->_formValues['state_province_id'])) { $this->_formValues['state_province'] = CRM_Core_PseudoConstant::stateProvince($this->_formValues['state_province_id']); } // use the address to get the latitude and longitude CRM_Utils_Geocode_Google::format($this->_formValues); if (!is_numeric(CRM_Utils_Array::value('geo_code_1', $this->_formValues)) || !is_numeric(CRM_Utils_Array::value('geo_code_2', $this->_formValues)) || !isset($this->_formValues['distance'])) { CRM_Core_Error::fatal(ts('Could not geocode input')); } $this->_latitude = $this->_formValues['geo_code_1']; $this->_longitude = $this->_formValues['geo_code_2']; if ($this->_formValues['prox_distance_unit'] == "miles") { $conversionFactor = 1609.344; } else { $conversionFactor = 1000; } $this->_distance = $this->_formValues['distance'] * $conversionFactor; } $this->_group = CRM_Utils_Array::value('group', $this->_formValues); $this->_tag = CRM_Utils_Array::value('tag', $this->_formValues); $this->_columns = array(ts('Name') => 'sort_name', ts('Street Address') => 'street_address', ts('City') => 'city', ts('Postal Code') => 'postal_code', ts('State') => 'state_province', ts('Country') => 'country'); }
/** * Helper method to filter Projects by location. * * @param array $params * <ol> * <li>string city - optional. Not used in this function, just passed along for geocoding.</li> * <li>mixed country - required if lat/lon not provided. Can be country_id or string.</li> * <li>float lat - required if country not provided</li> * <li>float lon - required if country not provided</li> * <li>string postal_code - optional. Not used in this function, just passed along for geocoding.</li> * <li>float radius - required</li> * <li>string street_address - optional. Not used in this function, just passed along for geocoding.</li> * <li>string unit - optional, defaults to meters unless 'mile' is specified</li> * </ol> * @return string * SQL fragment (partial where clause) * @throws Exception */ private static function buildProximityWhere(array $params) { $country = $lat = $lon = $radius = $unit = NULL; extract($params, EXTR_IF_EXISTS); // ensure that radius is a float if (!CRM_Utils_Rule::numeric($radius)) { throw new Exception(ts('Radius should exist and be numeric')); } if (!CRM_Utils_Rule::numeric($lat) || !CRM_Utils_Rule::numeric($lon)) { if (empty($country)) { throw new Exception(ts('Either Country or both Latitude and Longitude are required')); } // TODO: I think CRM_Utils_Geocode_*::format should be responsible for this if (CRM_Utils_Type::validate($country, 'Positive', FALSE)) { $country = civicrm_api3('Country', 'getvalue', array('id' => $country, 'return' => 'name')); } // TODO: support other geocoders $geocodeSuccess = CRM_Utils_Geocode_Google::format($params); if (!$geocodeSuccess) { // this is intentionally a string; a query like "SELECT * FROM foo WHERE FALSE" // will return an empty set, which is what we should do if the provided address // can't be geocoded return 'FALSE'; } // $params is passed to the geocoder by reference; on success, these values // will be available $lat = $params['geo_code_1']; $lon = $params['geo_code_2']; } $conversionFactor = $unit == "mile" ? 1609.344 : 1000; //radius in meters $radius = $radius * $conversionFactor; return CRM_Contact_BAO_ProximityQuery::where($lat, $lon, $radius); }