First commit
This commit is contained in:
commit
c6e2478c40
13918 changed files with 2303184 additions and 0 deletions
301
sites/all/modules/civicrm/CRM/Utils/Address/BatchUpdate.php
Normal file
301
sites/all/modules/civicrm/CRM/Utils/Address/BatchUpdate.php
Normal file
|
@ -0,0 +1,301 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
|
||||
/**
|
||||
* A PHP cron script to format all the addresses in the database. Currently
|
||||
* it only does geocoding if the geocode values are not set. At a later
|
||||
* stage we will also handle USPS address cleanup and other formatting
|
||||
* issues
|
||||
*/
|
||||
class CRM_Utils_Address_BatchUpdate {
|
||||
|
||||
var $start = NULL;
|
||||
var $end = NULL;
|
||||
var $geocoding = 1;
|
||||
var $parse = 1;
|
||||
var $throttle = 0;
|
||||
|
||||
var $returnMessages = array();
|
||||
var $returnError = 0;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function __construct($params) {
|
||||
|
||||
foreach ($params as $name => $value) {
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
// fixme: more params verification
|
||||
}
|
||||
|
||||
/**
|
||||
* Run batch update.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function run() {
|
||||
|
||||
$config = &CRM_Core_Config::singleton();
|
||||
|
||||
// do check for geocoding.
|
||||
$processGeocode = FALSE;
|
||||
if (empty($config->geocodeMethod)) {
|
||||
if (CRM_Utils_String::strtobool($this->geocoding) === TRUE) {
|
||||
$this->returnMessages[] = ts('Error: You need to set a mapping provider under Administer > System Settings > Mapping and Geocoding');
|
||||
$this->returnError = 1;
|
||||
$this->returnResult();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$processGeocode = TRUE;
|
||||
// user might want to over-ride.
|
||||
if (CRM_Utils_String::strtobool($this->geocoding) === FALSE) {
|
||||
$processGeocode = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// do check for parse street address.
|
||||
$parseAddress = FALSE;
|
||||
$parseAddress = CRM_Utils_Array::value('street_address_parsing',
|
||||
CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
|
||||
'address_options'
|
||||
),
|
||||
FALSE
|
||||
);
|
||||
$parseStreetAddress = FALSE;
|
||||
if (!$parseAddress) {
|
||||
if (CRM_Utils_String::strtobool($this->parse) === TRUE) {
|
||||
$this->returnMessages[] = ts('Error: You need to enable Street Address Parsing under Administer > Localization > Address Settings.');
|
||||
$this->returnError = 1;
|
||||
return $this->returnResult();
|
||||
}
|
||||
}
|
||||
else {
|
||||
$parseStreetAddress = TRUE;
|
||||
// user might want to over-ride.
|
||||
if (CRM_Utils_String::strtobool($this->parse) === FALSE) {
|
||||
$parseStreetAddress = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
// don't process.
|
||||
if (!$parseStreetAddress && !$processGeocode) {
|
||||
$this->returnMessages[] = ts('Error: Both Geocode mapping as well as Street Address Parsing are disabled. You must configure one or both options to use this script.');
|
||||
$this->returnError = 1;
|
||||
return $this->returnResult();
|
||||
}
|
||||
|
||||
// do check for parse street address.
|
||||
return $this->processContacts($config, $processGeocode, $parseStreetAddress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process contacts.
|
||||
*
|
||||
* @param CRM_Core_Config $config
|
||||
* @param bool $processGeocode
|
||||
* @param bool $parseStreetAddress
|
||||
*
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function processContacts(&$config, $processGeocode, $parseStreetAddress) {
|
||||
// build where clause.
|
||||
$clause = array('( c.id = a.contact_id )');
|
||||
$params = array();
|
||||
if ($this->start) {
|
||||
$clause[] = "( c.id >= %1 )";
|
||||
$params[1] = array($this->start, 'Integer');
|
||||
}
|
||||
|
||||
if ($this->end) {
|
||||
$clause[] = "( c.id <= %2 )";
|
||||
$params[2] = array($this->end, 'Integer');
|
||||
}
|
||||
|
||||
if ($processGeocode) {
|
||||
$clause[] = '( a.geo_code_1 is null OR a.geo_code_1 = 0 )';
|
||||
$clause[] = '( a.geo_code_2 is null OR a.geo_code_2 = 0 )';
|
||||
$clause[] = '( a.country_id is not null )';
|
||||
}
|
||||
|
||||
$whereClause = implode(' AND ', $clause);
|
||||
|
||||
$query = "
|
||||
SELECT c.id,
|
||||
a.id as address_id,
|
||||
a.street_address,
|
||||
a.city,
|
||||
a.postal_code,
|
||||
s.name as state,
|
||||
o.name as country
|
||||
FROM civicrm_contact c
|
||||
INNER JOIN civicrm_address a ON a.contact_id = c.id
|
||||
LEFT JOIN civicrm_country o ON a.country_id = o.id
|
||||
LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
|
||||
WHERE {$whereClause}
|
||||
ORDER BY a.id
|
||||
";
|
||||
|
||||
$totalGeocoded = $totalAddresses = $totalAddressParsed = 0;
|
||||
|
||||
$dao = CRM_Core_DAO::executeQuery($query, $params);
|
||||
if ($processGeocode) {
|
||||
require_once str_replace('_', DIRECTORY_SEPARATOR, $config->geocodeMethod) . '.php';
|
||||
}
|
||||
|
||||
$unparseableContactAddress = array();
|
||||
while ($dao->fetch()) {
|
||||
$totalAddresses++;
|
||||
$params = array(
|
||||
'street_address' => $dao->street_address,
|
||||
'postal_code' => $dao->postal_code,
|
||||
'city' => $dao->city,
|
||||
'state_province' => $dao->state,
|
||||
'country' => $dao->country,
|
||||
);
|
||||
|
||||
$addressParams = array();
|
||||
|
||||
// process geocode.
|
||||
if ($processGeocode) {
|
||||
// loop through the address removing more information
|
||||
// so we can get some geocode for a partial address
|
||||
// i.e. city -> state -> country
|
||||
|
||||
$maxTries = 5;
|
||||
do {
|
||||
if ($this->throttle) {
|
||||
usleep(5000000);
|
||||
}
|
||||
|
||||
$className = $config->geocodeMethod;
|
||||
$className::format($params, TRUE);
|
||||
|
||||
// see if we got a geocode error, in this case we'll trigger a fatal
|
||||
// CRM-13760
|
||||
if (
|
||||
isset($params['geo_code_error']) &&
|
||||
$params['geo_code_error'] == 'OVER_QUERY_LIMIT'
|
||||
) {
|
||||
CRM_Core_Error::fatal('Aborting batch geocoding. Hit the over query limit on geocoder.');
|
||||
}
|
||||
|
||||
array_shift($params);
|
||||
$maxTries--;
|
||||
} while (
|
||||
(!isset($params['geo_code_1']) || $params['geo_code_1'] == 'null') &&
|
||||
($maxTries > 1)
|
||||
);
|
||||
|
||||
if (isset($params['geo_code_1']) && $params['geo_code_1'] != 'null') {
|
||||
$totalGeocoded++;
|
||||
$addressParams = $params;
|
||||
}
|
||||
}
|
||||
|
||||
// parse street address
|
||||
if ($parseStreetAddress) {
|
||||
$parsedFields = CRM_Core_BAO_Address::parseStreetAddress($dao->street_address);
|
||||
$success = TRUE;
|
||||
// consider address is automatically parseable,
|
||||
// when we should found street_number and street_name
|
||||
if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
|
||||
$success = FALSE;
|
||||
}
|
||||
|
||||
// do check for all elements.
|
||||
if ($success) {
|
||||
$totalAddressParsed++;
|
||||
}
|
||||
elseif ($dao->street_address) {
|
||||
//build contact edit url,
|
||||
//so that user can manually fill the street address fields if the street address is not parsed, CRM-5886
|
||||
$url = CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$dao->id}");
|
||||
$unparseableContactAddress[] = " Contact ID: " . $dao->id . " <a href =\"$url\"> " . $dao->street_address . " </a> ";
|
||||
// reset element values.
|
||||
$parsedFields = array_fill_keys(array_keys($parsedFields), '');
|
||||
}
|
||||
$addressParams = array_merge($addressParams, $parsedFields);
|
||||
}
|
||||
|
||||
// finally update address object.
|
||||
if (!empty($addressParams)) {
|
||||
$address = new CRM_Core_DAO_Address();
|
||||
$address->id = $dao->address_id;
|
||||
$address->copyValues($addressParams);
|
||||
$address->save();
|
||||
$address->free();
|
||||
}
|
||||
}
|
||||
|
||||
$this->returnMessages[] = ts("Addresses Evaluated: %1", array(
|
||||
1 => $totalAddresses,
|
||||
)) . "\n";
|
||||
if ($processGeocode) {
|
||||
$this->returnMessages[] = ts("Addresses Geocoded: %1", array(
|
||||
1 => $totalGeocoded,
|
||||
)) . "\n";
|
||||
}
|
||||
if ($parseStreetAddress) {
|
||||
$this->returnMessages[] = ts("Street Addresses Parsed: %1", array(
|
||||
1 => $totalAddressParsed,
|
||||
)) . "\n";
|
||||
if ($unparseableContactAddress) {
|
||||
$this->returnMessages[] = "<br />\n" . ts("Following is the list of contacts whose address is not parsed:") . "<br />\n";
|
||||
foreach ($unparseableContactAddress as $contactLink) {
|
||||
$this->returnMessages[] = $contactLink . "<br />\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->returnResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return result.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function returnResult() {
|
||||
$result = array();
|
||||
$result['is_error'] = $this->returnError;
|
||||
$result['messages'] = implode("", $this->returnMessages);
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
139
sites/all/modules/civicrm/CRM/Utils/Address/USPS.php
Normal file
139
sites/all/modules/civicrm/CRM/Utils/Address/USPS.php
Normal file
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
|
||||
/**
|
||||
* Address utilities.
|
||||
*/
|
||||
class CRM_Utils_Address_USPS {
|
||||
|
||||
/**
|
||||
* Whether USPS validation should be disabled during import.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static $_disabled = FALSE;
|
||||
|
||||
/**
|
||||
* Disable the USPS validation.
|
||||
*
|
||||
* @param bool $disable
|
||||
*/
|
||||
public static function disable($disable = TRUE) {
|
||||
self::$_disabled = $disable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check address against USPS.
|
||||
*
|
||||
* @param array $values
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function checkAddress(&$values) {
|
||||
if (self::$_disabled) {
|
||||
return FALSE;
|
||||
}
|
||||
if (!isset($values['street_address']) ||
|
||||
(!isset($values['city']) &&
|
||||
!isset($values['state_province']) &&
|
||||
!isset($values['postal_code'])
|
||||
)
|
||||
) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$userID = Civi::settings()->get('address_standardization_userid');
|
||||
$url = Civi::settings()->get('address_standardization_url');
|
||||
|
||||
if (empty($userID) ||
|
||||
empty($url)
|
||||
) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$address2 = str_replace(',', '', $values['street_address']);
|
||||
|
||||
$XMLQuery = '<AddressValidateRequest USERID="' . $userID . '"><Address ID="0"><Address1>' . CRM_Utils_Array::value('supplemental_address_1', $values, '') . '</Address1><Address2>' . $address2 . '</Address2><City>' . $values['city'] . '</City><State>' . $values['state_province'] . '</State><Zip5>' . $values['postal_code'] . '</Zip5><Zip4>' . CRM_Utils_Array::value('postal_code_suffix', $values, '') . '</Zip4></Address></AddressValidateRequest>';
|
||||
|
||||
require_once 'HTTP/Request.php';
|
||||
$request = new HTTP_Request();
|
||||
|
||||
$request->setURL($url);
|
||||
|
||||
$request->addQueryString('API', 'Verify');
|
||||
$request->addQueryString('XML', $XMLQuery);
|
||||
|
||||
$response = $request->sendRequest();
|
||||
|
||||
$session = CRM_Core_Session::singleton();
|
||||
|
||||
$code = $request->getResponseCode();
|
||||
if ($code != 200) {
|
||||
$session->setStatus(ts('USPS Address Lookup Failed with HTTP status code: %1',
|
||||
array(1 => $code)
|
||||
));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$responseBody = $request->getResponseBody();
|
||||
|
||||
$xml = simplexml_load_string($responseBody);
|
||||
|
||||
if (is_null($xml) || is_null($xml->Address)) {
|
||||
$session->setStatus(ts('Your USPS API Lookup has Failed.'));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if ($xml->Number == '80040b1a') {
|
||||
$session->setStatus(ts('Your USPS API Authorization has Failed.'));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (array_key_exists('Error', $xml->Address)) {
|
||||
$session->setStatus(ts('Address not found in USPS database.'));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$values['street_address'] = (string) $xml->Address->Address2;
|
||||
$values['city'] = (string) $xml->Address->City;
|
||||
$values['state_province'] = (string) $xml->Address->State;
|
||||
$values['postal_code'] = (string) $xml->Address->Zip5;
|
||||
$values['postal_code_suffix'] = (string) $xml->Address->Zip4;
|
||||
|
||||
if (array_key_exists('Address1', $xml->Address)) {
|
||||
$values['supplemental_address_1'] = (string) $xml->Address->Address1;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue