CRM-18390 - Adding contact summary tab dynamically
[civicrm-core.git] / CRM / Utils / Address / BatchUpdate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
34 /**
35 * A PHP cron script to format all the addresses in the database. Currently
36 * it only does geocoding if the geocode values are not set. At a later
37 * stage we will also handle USPS address cleanup and other formatting
38 * issues
39 */
40 class CRM_Utils_Address_BatchUpdate {
41
42 var $start = NULL;
43 var $end = NULL;
44 var $geocoding = 1;
45 var $parse = 1;
46 var $throttle = 0;
47
48 var $returnMessages = array();
49 var $returnError = 0;
50
51 /**
52 * Class constructor.
53 *
54 * @param array $params
55 */
56 public function __construct($params) {
57
58 foreach ($params as $name => $value) {
59 $this->$name = $value;
60 }
61
62 // fixme: more params verification
63 }
64
65 /**
66 * Run batch update.
67 *
68 * @return array
69 */
70 public function run() {
71
72 $config = &CRM_Core_Config::singleton();
73
74 // do check for geocoding.
75 $processGeocode = FALSE;
76 if (empty($config->geocodeMethod)) {
77 if (CRM_Utils_String::strtobool($this->geocoding) === TRUE) {
78 $this->returnMessages[] = ts('Error: You need to set a mapping provider under Administer > System Settings > Mapping and Geocoding');
79 $this->returnError = 1;
80 $this->returnResult();
81 }
82 }
83 else {
84 $processGeocode = TRUE;
85 // user might want to over-ride.
86 if (CRM_Utils_String::strtobool($this->geocoding) === FALSE) {
87 $processGeocode = FALSE;
88 }
89 }
90
91 // do check for parse street address.
92 $parseAddress = FALSE;
93 $parseAddress = CRM_Utils_Array::value('street_address_parsing',
94 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
95 'address_options'
96 ),
97 FALSE
98 );
99 $parseStreetAddress = FALSE;
100 if (!$parseAddress) {
101 if (CRM_Utils_String::strtobool($this->parse) === TRUE) {
102 $this->returnMessages[] = ts('Error: You need to enable Street Address Parsing under Administer > Localization > Address Settings.');
103 $this->returnError = 1;
104 return $this->returnResult();
105 }
106 }
107 else {
108 $parseStreetAddress = TRUE;
109 // user might want to over-ride.
110 if (CRM_Utils_String::strtobool($this->parse) === FALSE) {
111 $parseStreetAddress = FALSE;
112 }
113 }
114
115 // don't process.
116 if (!$parseStreetAddress && !$processGeocode) {
117 $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.');
118 $this->returnError = 1;
119 return $this->returnResult();
120 }
121
122 // do check for parse street address.
123 return $this->processContacts($config, $processGeocode, $parseStreetAddress);
124 }
125
126 /**
127 * Process contacts.
128 *
129 * @param CRM_Core_Config $config
130 * @param bool $processGeocode
131 * @param bool $parseStreetAddress
132 *
133 * @return array
134 * @throws Exception
135 */
136 public function processContacts(&$config, $processGeocode, $parseStreetAddress) {
137 // build where clause.
138 $clause = array('( c.id = a.contact_id )');
139 $params = array();
140 if ($this->start) {
141 $clause[] = "( c.id >= %1 )";
142 $params[1] = array($this->start, 'Integer');
143 }
144
145 if ($this->end) {
146 $clause[] = "( c.id <= %2 )";
147 $params[2] = array($this->end, 'Integer');
148 }
149
150 if ($processGeocode) {
151 $clause[] = '( a.geo_code_1 is null OR a.geo_code_1 = 0 )';
152 $clause[] = '( a.geo_code_2 is null OR a.geo_code_2 = 0 )';
153 $clause[] = '( a.country_id is not null )';
154 }
155
156 $whereClause = implode(' AND ', $clause);
157
158 $query = "
159 SELECT c.id,
160 a.id as address_id,
161 a.street_address,
162 a.city,
163 a.postal_code,
164 s.name as state,
165 o.name as country
166 FROM civicrm_contact c
167 INNER JOIN civicrm_address a ON a.contact_id = c.id
168 LEFT JOIN civicrm_country o ON a.country_id = o.id
169 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
170 WHERE {$whereClause}
171 ORDER BY a.id
172 ";
173
174 $totalGeocoded = $totalAddresses = $totalAddressParsed = 0;
175
176 $dao = CRM_Core_DAO::executeQuery($query, $params);
177 if ($processGeocode) {
178 require_once str_replace('_', DIRECTORY_SEPARATOR, $config->geocodeMethod) . '.php';
179 }
180
181 $unparseableContactAddress = array();
182 while ($dao->fetch()) {
183 $totalAddresses++;
184 $params = array(
185 'street_address' => $dao->street_address,
186 'postal_code' => $dao->postal_code,
187 'city' => $dao->city,
188 'state_province' => $dao->state,
189 'country' => $dao->country,
190 );
191
192 $addressParams = array();
193
194 // process geocode.
195 if ($processGeocode) {
196 // loop through the address removing more information
197 // so we can get some geocode for a partial address
198 // i.e. city -> state -> country
199
200 $maxTries = 5;
201 do {
202 if ($this->throttle) {
203 usleep(5000000);
204 }
205
206 $className = $config->geocodeMethod;
207 $className::format($params, TRUE);
208
209 // see if we got a geocode error, in this case we'll trigger a fatal
210 // CRM-13760
211 if (
212 isset($params['geo_code_error']) &&
213 $params['geo_code_error'] == 'OVER_QUERY_LIMIT'
214 ) {
215 CRM_Core_Error::fatal('Aborting batch geocoding. Hit the over query limit on geocoder.');
216 }
217
218 array_shift($params);
219 $maxTries--;
220 } while (
221 (!isset($params['geo_code_1']) || $params['geo_code_1'] == 'null') &&
222 ($maxTries > 1)
223 );
224
225 if (isset($params['geo_code_1']) && $params['geo_code_1'] != 'null') {
226 $totalGeocoded++;
227 $addressParams['geo_code_1'] = $params['geo_code_1'];
228 $addressParams['geo_code_2'] = $params['geo_code_2'];
229 $addressParams['postal_code'] = $params['postal_code'];
230 $addressParams['postal_code_suffix'] = CRM_Utils_Array::value('postal_code_suffix', $params);
231 }
232 }
233
234 // parse street address
235 if ($parseStreetAddress) {
236 $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($dao->street_address);
237 $success = TRUE;
238 // consider address is automatically parseable,
239 // when we should found street_number and street_name
240 if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
241 $success = FALSE;
242 }
243
244 // do check for all elements.
245 if ($success) {
246 $totalAddressParsed++;
247 }
248 elseif ($dao->street_address) {
249 //build contact edit url,
250 //so that user can manually fill the street address fields if the street address is not parsed, CRM-5886
251 $url = CRM_Utils_System::url('civicrm/contact/add', "reset=1&action=update&cid={$dao->id}");
252 $unparseableContactAddress[] = " Contact ID: " . $dao->id . " <a href =\"$url\"> " . $dao->street_address . " </a> ";
253 // reset element values.
254 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
255 }
256 $addressParams = array_merge($addressParams, $parsedFields);
257 }
258
259 // finally update address object.
260 if (!empty($addressParams)) {
261 $address = new CRM_Core_DAO_Address();
262 $address->id = $dao->address_id;
263 $address->copyValues($addressParams);
264 $address->save();
265 $address->free();
266 }
267 }
268
269 $this->returnMessages[] = ts("Addresses Evaluated: %1", array(
270 1 => $totalAddresses,
271 )) . "\n";
272 if ($processGeocode) {
273 $this->returnMessages[] = ts("Addresses Geocoded: %1", array(
274 1 => $totalGeocoded,
275 )) . "\n";
276 }
277 if ($parseStreetAddress) {
278 $this->returnMessages[] = ts("Street Addresses Parsed: %1", array(
279 1 => $totalAddressParsed,
280 )) . "\n";
281 if ($unparseableContactAddress) {
282 $this->returnMessages[] = "<br />\n" . ts("Following is the list of contacts whose address is not parsed:") . "<br />\n";
283 foreach ($unparseableContactAddress as $contactLink) {
284 $this->returnMessages[] = $contactLink . "<br />\n";
285 }
286 }
287 }
288
289 return $this->returnResult();
290 }
291
292 /**
293 * Return result.
294 *
295 * @return array
296 */
297 public function returnResult() {
298 $result = array();
299 $result['is_error'] = $this->returnError;
300 $result['messages'] = implode("", $this->returnMessages);
301 return $result;
302 }
303
304 }