Merge pull request #3231 from monishdeb/CRM-14665
[civicrm-core.git] / CRM / Utils / Address / BatchUpdate.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
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
32 * issues
33 *
34 */
35class CRM_Utils_Address_BatchUpdate {
36
37 var $start = NULL;
38 var $end = NULL;
39 var $geocoding = 1;
40 var $parse = 1;
41 var $throttle = 0;
42
43 var $returnMessages = array();
44 var $returnError = 0;
45
46 public function __construct($params) {
47
48 foreach ($params as $name => $value) {
49 $this->$name = $value;
50 }
51
52 // fixme: more params verification
53 }
54
55 public function run() {
56
57 $config = &CRM_Core_Config::singleton();
58
59 // do check for geocoding.
60 $processGeocode = FALSE;
61 if (empty($config->geocodeMethod)) {
62 if ($this->geocoding == 'true') {
a08aa8e8 63 $this->returnMessages[] = ts('Error: You need to set a mapping provider under Administer > System Settings > Mapping and Geocoding');
6a488035
TO
64 $this->returnError = 1;
65 $this->returnResult();
66 }
67 }
68 else {
69 $processGeocode = TRUE;
70 // user might want to over-ride.
71 if ($this->geocoding == 'false') {
72 $processGeocode = FALSE;
73 }
74 }
75
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,
80 'address_options'
81 ),
82 FALSE
83 );
84 $parseStreetAddress = FALSE;
85 if (!$parseAddress) {
86 if ($this->parse == 'true') {
a08aa8e8 87 $this->returnMessages[] = ts('Error: You need to enable Street Address Parsing under Administer > Localization > Address Settings.');
6a488035
TO
88 $this->returnError = 1;
89 return $this->returnResult();
90 }
91 }
92 else {
93 $parseStreetAddress = TRUE;
94 // user might want to over-ride.
95 if ($this->parse == 'false') {
96 $parseStreetAddress = FALSE;
97 }
98 }
99
100 // don't process.
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();
105 }
106
107 // do check for parse street address.
108 return $this->processContacts($config, $processGeocode, $parseStreetAddress);
109 }
110
111 function processContacts(&$config, $processGeocode, $parseStreetAddress) {
112 // build where clause.
113 $clause = array('( c.id = a.contact_id )');
18b8253b 114 $params = array();
6a488035 115 if ($this->start) {
18b8253b
DL
116 $clause[] = "( c.id >= %1 )";
117 $params[1] = array($this->start, 'Integer');
6a488035
TO
118 }
119
120 if ($this->end) {
18b8253b
DL
121 $clause[] = "( c.id <= %2 )";
122 $params[2] = array($this->end, 'Integer');
6a488035
TO
123 }
124
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 )';
129 }
130
131 $whereClause = implode(' AND ', $clause);
132
133 $query = "
134 SELECT c.id,
135 a.id as address_id,
136 a.street_address,
137 a.city,
138 a.postal_code,
139 s.name as state,
140 o.name as country
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
145 WHERE {$whereClause}
146 ORDER BY a.id
147 ";
148
149 $totalGeocoded = $totalAddresses = $totalAddressParsed = 0;
150
18b8253b 151 $dao = CRM_Core_DAO::executeQuery($query, $params);
6a488035
TO
152 if ($processGeocode) {
153 require_once (str_replace('_', DIRECTORY_SEPARATOR, $config->geocodeMethod) . '.php');
154 }
155
156
157 $unparseableContactAddress = array();
158 while ($dao->fetch()) {
159 $totalAddresses++;
160 $params = array(
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,
166 );
167
168 $addressParams = array();
169
170 // process geocode.
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
175
176 $maxTries = 5;
177 do {
178 if ($this->throttle) {
179 usleep(5000000);
180 }
181
0e6e8724
DL
182 $className = $config->geocodeMethod;
183 $className::format( $params, true );
79f1148d
DL
184
185 // see if we got a geocode error, in this case we'll trigger a fatal
186 // CRM-13760
187 if (
188 isset($params['geo_code_error']) &&
189 $params['geo_code_error'] == 'OVER_QUERY_LIMIT'
190 ) {
191 CRM_Core_Error::fatal('Aborting batch geocoding. Hit the over query limit on geocoder.');
192 }
193
6a488035
TO
194 array_shift($params);
195 $maxTries--;
79f1148d
DL
196 } while (
197 (!isset($params['geo_code_1']) || $params['geo_code_1'] == 'null') &&
6a488035
TO
198 ($maxTries > 1)
199 );
200
79f1148d 201 if (isset($params['geo_code_1']) && $params['geo_code_1'] != 'null') {
6a488035
TO
202 $totalGeocoded++;
203 $addressParams['geo_code_1'] = $params['geo_code_1'];
204 $addressParams['geo_code_2'] = $params['geo_code_2'];
d40fd297 205 $addressParams['postal_code'] = $params['postal_code'];
206 $addressParams['postal_code_suffix'] = $params['postal_code_suffix'];
6a488035
TO
207 }
208 }
209
210 // parse street address
211 if ($parseStreetAddress) {
212 $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($dao->street_address);
213 $success = TRUE;
214 // consider address is automatically parseable,
215 // when we should found street_number and street_name
8cc574cf 216 if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
6a488035
TO
217 $success = FALSE;
218 }
219
220 // do check for all elements.
221 if ($success) {
222 $totalAddressParsed++;
223 }
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), '');
231 }
232 $addressParams = array_merge($addressParams, $parsedFields);
233 }
234
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);
240 $address->save();
241 $address->free();
242 }
243 }
244
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";
250 }
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";
258 }
259 }
260 }
261
262 return $this->returnResult();
263 }
264
265 function returnResult() {
266 $result = array();
267 $result['is_error'] = $this->returnError;
268 $result['messages'] = implode("", $this->returnMessages);
269 return $result;
270 }
271}
272