Example #1
0
 /**
  * global form rule
  *
  * @param array $fields  the input form values
  * @return true if no errors, else array of errors
  * @access public
  * @static
  */
 static function formRule($fields)
 {
     $errors = array();
     if (!CRM_Utils_System::checkPHPVersion(5, false)) {
         $errors['_qf_default'] = ts('Mapping features require PHP version 5 or greater');
     }
     if (!$fields['mapAPIKey'] && $fields['mapProvider'] != '') {
         $errors['mapAPIKey'] = "API key is a required field";
     }
     return $errors;
 }
Example #2
0
 /**
  * Global form rule.
  *
  * @param array $fields
  *   The input form values.
  *
  * @return bool|array
  *   true if no errors, else array of errors
  */
 public static function formRule($fields)
 {
     $errors = array();
     if (!CRM_Utils_System::checkPHPVersion(5, FALSE)) {
         $errors['_qf_default'] = ts('Mapping features require PHP version 5 or greater');
     }
     if (!$fields['mapAPIKey'] && ($fields['mapProvider'] != '' && $fields['mapProvider'] == 'Yahoo')) {
         $errors['mapAPIKey'] = "Map Provider key is a required field.";
     }
     if ($fields['mapProvider'] == 'OpenStreetMaps' && $fields['geoProvider'] == '') {
         $errors['geoProvider'] = "Please select a Geocoding Provider - Open Street Maps does not provide geocoding.";
     }
     return $errors;
 }
Example #3
0
 /**
  * @param $fields
  *
  * @return bool
  */
 public static function formRule($fields)
 {
     $p = $fields['address_standardization_provider'];
     $u = $fields['address_standardization_userid'];
     $w = $fields['address_standardization_url'];
     // make sure that there is a value for all of them
     // if any of them are set
     if ($p || $u || $w) {
         if (!CRM_Utils_System::checkPHPVersion(5, FALSE)) {
             $errors['_qf_default'] = ts('Address Standardization features require PHP version 5 or greater.');
             return $errors;
         }
         if (!($p && $u && $w)) {
             $errors['_qf_default'] = ts('You must provide values for all three Address Standarization fields.');
             return $errors;
         }
     }
     return TRUE;
 }
Example #4
0
 /**
  * function that takes an address object and gets the latitude / longitude for this
  * address. Note that at a later stage, we could make this function also clean up
  * the address into a more valid format
  *
  * @param object $address
  *
  * @return boolean true if we modified the address, false otherwise
  * @static
  */
 static function format(&$values, $stateName = false)
 {
     CRM_Utils_System::checkPHPVersion(5, true);
     // we need a valid country, else we ignore
     if (!CRM_Utils_Array::value('country', $values)) {
         return false;
     }
     $config =& CRM_Core_Config::singleton();
     $arg = array();
     $arg[] = "appid=" . urlencode($config->mapAPIKey);
     if (CRM_Utils_Array::value('street_address', $values)) {
         $arg[] = "street=" . urlencode($values['street_address']);
     }
     $city = CRM_Utils_Array::value('city', $values);
     if ($city) {
         $arg[] = "city=" . urlencode($city);
     }
     if (CRM_Utils_Array::value('state_province', $values)) {
         if (CRM_Utils_Array::value('state_province_id', $values)) {
             $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']);
         } else {
             if (!$stateName) {
                 $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province'], 'name', 'abbreviation');
             } else {
                 $stateProvince = $values['state_province'];
             }
         }
         // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632)
         if ($stateProvince != $city) {
             $arg[] = "state=" . urlencode($stateProvince);
         }
     }
     if (CRM_Utils_Array::value('country', $values)) {
         $arg[] = "country=" . urlencode($values['country']);
     }
     if (CRM_Utils_Array::value('postal_code', $values)) {
         $arg[] = "zip=" . urlencode($values['postal_code']);
     }
     $args = implode('&', $arg);
     $query = 'http://' . self::$_server . self::$_uri . '?' . $args;
     require_once 'HTTP/Request.php';
     $request =& new HTTP_Request($query);
     $request->sendRequest();
     $string = $request->getResponseBody();
     $xml = simplexml_load_string($string);
     $ret = array();
     $ret['precision'] = (string) $xml->Result['precision'];
     if (is_a($xml->Result, 'SimpleXMLElement')) {
         $result = array();
         $result = get_object_vars($xml->Result);
         foreach ($result as $key => $val) {
             if (strlen($val)) {
                 $ret[(string) $key] = (string) $val;
             }
         }
         $values['geo_code_1'] = $ret['Latitude'];
         $values['geo_code_2'] = $ret['Longitude'];
         return true;
     }
     // reset the geo code values if we did not get any good values
     $values['geo_code_1'] = $values['geo_code_2'] = 'null';
     return false;
 }
Example #5
0
 /**
  * function that takes an address array and gets the latitude / longitude
  * and postal code for this address. Note that at a later stage, we could
  * make this function also clean up the address into a more valid format
  *
  * @param array $values associative array of address data: country, street_address, city, state_province, postal code
  * @param boolean $stateName this parameter currently has no function
  *
  * @return boolean true if we modified the address, false otherwise
  * @static
  */
 static function format(&$values, $stateName = FALSE)
 {
     CRM_Utils_System::checkPHPVersion(5, TRUE);
     $config = CRM_Core_Config::singleton();
     $whereComponents = array();
     if (CRM_Utils_Array::value('street_address', $values)) {
         $whereComponents['street'] = $values['street_address'];
     }
     if ($city = CRM_Utils_Array::value('city', $values)) {
         $whereComponents['city'] = $city;
     }
     if (CRM_Utils_Array::value('state_province', $values)) {
         if (CRM_Utils_Array::value('state_province_id', $values)) {
             $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']);
         } else {
             if (!$stateName) {
                 $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province'], 'name', 'abbreviation');
             } else {
                 $stateProvince = $values['state_province'];
             }
         }
         // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632)
         if ($stateProvince != $city) {
             $whereComponents['state'] = $stateProvince;
         }
     }
     if (CRM_Utils_Array::value('postal_code', $values)) {
         $whereComponents['postal'] = $values['postal_code'];
     }
     if (CRM_Utils_Array::value('country', $values)) {
         $whereComponents['country'] = $values['country'];
     }
     foreach ($whereComponents as $componentName => $componentValue) {
         $whereComponents[$componentName] = urlencode("{$componentName}=\"{$componentValue}\"");
     }
     $add = 'q=' . urlencode('select * from geo.placefinder where ');
     $add .= join(urlencode(' and '), $whereComponents);
     $add .= "&appid=" . urlencode($config->mapAPIKey);
     $query = 'http://' . self::$_server . self::$_uri . '?' . $add;
     require_once 'HTTP/Request.php';
     $request = new HTTP_Request($query);
     $request->sendRequest();
     $string = $request->getResponseBody();
     // see CRM-11359 for why we suppress errors with @
     $xml = @simplexml_load_string($string);
     if ($xml === FALSE) {
         // account blocked maybe?
         CRM_Core_Error::debug_var('Geocoding failed.  Message from Yahoo:', $string);
         return FALSE;
     }
     if ($xml->getName() == 'error') {
         CRM_Core_Error::debug_var('query', $query);
         CRM_Core_Error::debug_log_message('Geocoding failed.  Message from Yahoo: ' . (string) $xml->description);
         return FALSE;
     }
     if (is_a($xml->results->Result, 'SimpleXMLElement')) {
         $result = array();
         $result = get_object_vars($xml->results->Result);
         foreach ($result as $key => $val) {
             if (is_scalar($val) && strlen($val)) {
                 $ret[(string) $key] = (string) $val;
             }
         }
         $values['geo_code_1'] = $ret['latitude'];
         $values['geo_code_2'] = $ret['longitude'];
         if ($ret['postal']) {
             $current_pc = CRM_Utils_Array::value('postal_code', $values);
             $skip_postal = FALSE;
             if ($current_pc) {
                 $current_pc_suffix = CRM_Utils_Array::value('postal_code_suffix', $values);
                 $current_pc_complete = $current_pc . $current_pc_suffix;
                 $new_pc_complete = preg_replace("/[+-]/", '', $ret['postal']);
                 // if a postal code was already entered, don't change it, except to make it more precise
                 if (strpos($new_pc_complete, $current_pc_complete) !== 0) {
                     // Don't bother anonymous users with the message - they can't change a form they just submitted anyway
                     if (CRM_Utils_System::isUserLoggedIn()) {
                         $msg = ts('The Yahoo Geocoding system returned a different postal code (%1) than the one you entered (%2). If you want the Yahoo value, please delete the current postal code and save again.', array(1 => $ret['postal'], 2 => $current_pc_suffix ? "{$current_pc}-{$current_pc_suffix}" : $current_pc));
                         CRM_Core_Session::setStatus($msg, ts('Postal Code Mismatch'), 'error');
                     }
                     $skip_postal = TRUE;
                 }
             }
             if (!$skip_postal) {
                 $values['postal_code'] = $ret['postal'];
                 /* the following logic to split the string was borrowed from
                       CRM/Core/BAO/Address.php -- CRM_Core_BAO_Address::fixAddress.
                       This is actually the function that calls the geocoding
                       script to begin with, but the postal code business takes
                       place before geocoding gets called.
                    */
                 if (preg_match('/^(\\d{4,5})[+-](\\d{4})$/', $ret['postal'], $match)) {
                     $values['postal_code'] = $match[1];
                     $values['postal_code_suffix'] = $match[2];
                 }
             }
         }
         return TRUE;
     }
     // reset the geo code values if we did not get any good values
     $values['geo_code_1'] = $values['geo_code_2'] = 'null';
     return FALSE;
 }
Example #6
0
 /**
  * function that takes an address object and gets the latitude / longitude for this
  * address. Note that at a later stage, we could make this function also clean up
  * the address into a more valid format
  *
  * @param object $address
  *
  * @return boolean true if we modified the address, false otherwise
  * @static
  */
 static function format(&$values, $stateName = false)
 {
     CRM_Utils_System::checkPHPVersion(5, true);
     require_once 'CRM/Utils/Array.php';
     // we need a valid country, else we ignore
     if (!CRM_Utils_Array::value('country', $values)) {
         return false;
     }
     $config =& CRM_Core_Config::singleton();
     // CRM-1439: Google (sometimes?) returns data in ISO-8859-1
     // hence we use oe to ensure we get utf-8
     $arg = "&oe=utf8&output=xml&key=" . urlencode($config->mapAPIKey);
     $add = '';
     if (CRM_Utils_Array::value('street_address', $values)) {
         $add = urlencode(str_replace('', '+', $values['street_address']));
         $add .= ',+';
     }
     $city = CRM_Utils_Array::value('city', $values);
     if ($city) {
         $add .= '+' . urlencode(str_replace('', '+', $city));
         $add .= ',+';
     }
     if (CRM_Utils_Array::value('state_province', $values)) {
         if (CRM_Utils_Array::value('state_province_id', $values)) {
             $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']);
         } else {
             if (!$stateName) {
                 $stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province'], 'name', 'abbreviation');
             } else {
                 $stateProvince = $values['state_province'];
             }
         }
         // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632)
         if ($stateProvince != $city) {
             $add .= '+' . urlencode(str_replace('', '+', $stateProvince));
             $add .= ',+';
         }
     }
     if (CRM_Utils_Array::value('postal_code', $values)) {
         $add .= '+' . urlencode(str_replace('', '+', $values['postal_code']));
         $add .= ',+';
     }
     if (CRM_Utils_Array::value('country', $values)) {
         $add .= '+' . urlencode(str_replace('', '+', $values['country']));
     }
     $query = 'http://' . self::$_server . self::$_uri . $add . $arg;
     require_once 'HTTP/Request.php';
     $request =& new HTTP_Request($query);
     $request->sendRequest();
     $string = $request->getResponseBody();
     libxml_use_internal_errors(true);
     $xml = @simplexml_load_string($string);
     if ($xml === false) {
         // account blocked maybe?
         return false;
     }
     $ret = array();
     $val = array();
     if (is_a($xml->Response->Placemark->Point, 'SimpleXMLElement')) {
         $ret = $xml->Response->Placemark->Point->children();
         $val = explode(',', (string) $ret[0]);
         if ($val[0] && $val[1]) {
             $values['geo_code_1'] = $val[1];
             $values['geo_code_2'] = $val[0];
             return true;
         }
     }
     // reset the geo code values if we did not get any good values
     $values['geo_code_1'] = $values['geo_code_2'] = 'null';
     return false;
 }