3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * A PHP cron script to format all the addresses in the database. Currently
30 * it only does geocoding if the geocode values are not set. At a later
31 * stage we will also handle USPS address cleanup and other formatting
35 class CRM_Utils_Address_BatchUpdate
{
43 var $returnMessages = array();
46 public function __construct($params) {
48 foreach ($params as $name => $value) {
49 $this->$name = $value;
52 // fixme: more params verification
55 public function run() {
57 $config = &CRM_Core_Config
::singleton();
59 // do check for geocoding.
60 $processGeocode = FALSE;
61 if (empty($config->geocodeMethod
)) {
62 if ($this->geocoding
== 'true') {
63 $this->returnMessages
[] = ts('Error: You need to set a mapping provider under Administer > System Settings > Mapping and Geocoding');
64 $this->returnError
= 1;
65 $this->returnResult();
69 $processGeocode = TRUE;
70 // user might want to over-ride.
71 if ($this->geocoding
== 'false') {
72 $processGeocode = FALSE;
76 // do check for parse street address.
77 $parseAddress = FALSE;
78 $parseAddress = CRM_Utils_Array
::value('street_address_parsing',
79 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
84 $parseStreetAddress = FALSE;
86 if ($this->parse
== 'true') {
87 $this->returnMessages
[] = ts('Error: You need to enable Street Address Parsing under Administer > Localization > Address Settings.');
88 $this->returnError
= 1;
89 return $this->returnResult();
93 $parseStreetAddress = TRUE;
94 // user might want to over-ride.
95 if ($this->parse
== 'false') {
96 $parseStreetAddress = FALSE;
101 if (!$parseStreetAddress && !$processGeocode) {
102 $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.');
103 $this->returnError
= 1;
104 return $this->returnResult();
107 // do check for parse street address.
108 return $this->processContacts($config, $processGeocode, $parseStreetAddress);
111 function processContacts(&$config, $processGeocode, $parseStreetAddress) {
112 // build where clause.
113 $clause = array('( c.id = a.contact_id )');
116 $clause[] = "( c.id >= %1 )";
117 $params[1] = array($this->start
, 'Integer');
121 $clause[] = "( c.id <= %2 )";
122 $params[2] = array($this->end
, 'Integer');
125 if ($processGeocode) {
126 $clause[] = '( a.geo_code_1 is null OR a.geo_code_1 = 0 )';
127 $clause[] = '( a.geo_code_2 is null OR a.geo_code_2 = 0 )';
128 $clause[] = '( a.country_id is not null )';
131 $whereClause = implode(' AND ', $clause);
141 FROM civicrm_contact c
142 INNER JOIN civicrm_address a ON a.contact_id = c.id
143 LEFT JOIN civicrm_country o ON a.country_id = o.id
144 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
149 $totalGeocoded = $totalAddresses = $totalAddressParsed = 0;
151 $dao = CRM_Core_DAO
::executeQuery($query, $params);
152 if ($processGeocode) {
153 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $config->geocodeMethod
) . '.php');
157 $unparseableContactAddress = array();
158 while ($dao->fetch()) {
161 'street_address' => $dao->street_address
,
162 'postal_code' => $dao->postal_code
,
163 'city' => $dao->city
,
164 'state_province' => $dao->state
,
165 'country' => $dao->country
,
168 $addressParams = array();
171 if ($processGeocode) {
172 // loop through the address removing more information
173 // so we can get some geocode for a partial address
174 // i.e. city -> state -> country
178 if ($this->throttle
) {
182 $className = $config->geocodeMethod
;
183 $className::format( $params, true );
185 // see if we got a geocode error, in this case we'll trigger a fatal
188 isset($params['geo_code_error']) &&
189 $params['geo_code_error'] == 'OVER_QUERY_LIMIT'
191 CRM_Core_Error
::fatal('Aborting batch geocoding. Hit the over query limit on geocoder.');
194 array_shift($params);
197 (!isset($params['geo_code_1']) ||
$params['geo_code_1'] == 'null') &&
201 if (isset($params['geo_code_1']) && $params['geo_code_1'] != 'null') {
203 $addressParams['geo_code_1'] = $params['geo_code_1'];
204 $addressParams['geo_code_2'] = $params['geo_code_2'];
205 $addressParams['postal_code'] = $params['postal_code'];
206 $addressParams['postal_code_suffix'] = $params['postal_code_suffix'];
210 // parse street address
211 if ($parseStreetAddress) {
212 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($dao->street_address
);
214 // consider address is automatically parseable,
215 // when we should found street_number and street_name
216 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
220 // do check for all elements.
222 $totalAddressParsed++
;
224 elseif ($dao->street_address
) {
225 //build contact edit url,
226 //so that user can manually fill the street address fields if the street address is not parsed, CRM-5886
227 $url = CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$dao->id}");
228 $unparseableContactAddress[] = " Contact ID: " . $dao->id
. " <a href =\"$url\"> " . $dao->street_address
. " </a> ";
229 // reset element values.
230 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
232 $addressParams = array_merge($addressParams, $parsedFields);
235 // finally update address object.
236 if (!empty($addressParams)) {
237 $address = new CRM_Core_DAO_Address();
238 $address->id
= $dao->address_id
;
239 $address->copyValues($addressParams);
245 $this->returnMessages
[] = ts("Addresses Evaluated: %1", array(
246 1 => $totalAddresses)) . "\n";
247 if ($processGeocode) {
248 $this->returnMessages
[] = ts("Addresses Geocoded: %1", array(
249 1 => $totalGeocoded)) . "\n";
251 if ($parseStreetAddress) {
252 $this->returnMessages
[] = ts("Street Addresses Parsed: %1", array(
253 1 => $totalAddressParsed)) . "\n";
254 if ($unparseableContactAddress) {
255 $this->returnMessages
[] = "<br />\n" . ts("Following is the list of contacts whose address is not parsed:") . "<br />\n";
256 foreach ($unparseableContactAddress as $contactLink) {
257 $this->returnMessages
[] = $contactLink . "<br />\n";
262 return $this->returnResult();
265 function returnResult() {
267 $result['is_error'] = $this->returnError
;
268 $result['messages'] = implode("", $this->returnMessages
);