3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
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
40 class CRM_Utils_Address_BatchUpdate
{
48 var $returnMessages = array();
54 * @param array $params
56 public function __construct($params) {
58 foreach ($params as $name => $value) {
59 $this->$name = $value;
62 // fixme: more params verification
70 public function run() {
72 $config = &CRM_Core_Config
::singleton();
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();
84 $processGeocode = TRUE;
85 // user might want to over-ride.
86 if (CRM_Utils_String
::strtobool($this->geocoding
) === FALSE) {
87 $processGeocode = FALSE;
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
,
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();
108 $parseStreetAddress = TRUE;
109 // user might want to over-ride.
110 if (CRM_Utils_String
::strtobool($this->parse
) === FALSE) {
111 $parseStreetAddress = FALSE;
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();
122 // do check for parse street address.
123 return $this->processContacts($config, $processGeocode, $parseStreetAddress);
129 * @param CRM_Core_Config $config
130 * @param bool $processGeocode
131 * @param bool $parseStreetAddress
136 public function processContacts(&$config, $processGeocode, $parseStreetAddress) {
137 // build where clause.
138 $clause = array('( c.id = a.contact_id )');
141 $clause[] = "( c.id >= %1 )";
142 $params[1] = array($this->start
, 'Integer');
146 $clause[] = "( c.id <= %2 )";
147 $params[2] = array($this->end
, 'Integer');
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 )';
156 $whereClause = implode(' AND ', $clause);
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
174 $totalGeocoded = $totalAddresses = $totalAddressParsed = 0;
176 $dao = CRM_Core_DAO
::executeQuery($query, $params);
177 if ($processGeocode) {
178 require_once str_replace('_', DIRECTORY_SEPARATOR
, $config->geocodeMethod
) . '.php';
181 $unparseableContactAddress = array();
182 while ($dao->fetch()) {
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
,
192 $addressParams = array();
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
202 if ($this->throttle
) {
206 $className = $config->geocodeMethod
;
207 $className::format($params, TRUE);
209 // see if we got a geocode error, in this case we'll trigger a fatal
212 isset($params['geo_code_error']) &&
213 $params['geo_code_error'] == 'OVER_QUERY_LIMIT'
215 CRM_Core_Error
::fatal('Aborting batch geocoding. Hit the over query limit on geocoder.');
218 array_shift($params);
221 (!isset($params['geo_code_1']) ||
$params['geo_code_1'] == 'null') &&
225 if (isset($params['geo_code_1']) && $params['geo_code_1'] != 'null') {
227 $addressParams = $params;
231 // parse street address
232 if ($parseStreetAddress) {
233 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($dao->street_address
);
235 // consider address is automatically parseable,
236 // when we should found street_number and street_name
237 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
241 // do check for all elements.
243 $totalAddressParsed++
;
245 elseif ($dao->street_address
) {
246 //build contact edit url,
247 //so that user can manually fill the street address fields if the street address is not parsed, CRM-5886
248 $url = CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$dao->id}");
249 $unparseableContactAddress[] = " Contact ID: " . $dao->id
. " <a href =\"$url\"> " . $dao->street_address
. " </a> ";
250 // reset element values.
251 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
253 $addressParams = array_merge($addressParams, $parsedFields);
256 // finally update address object.
257 if (!empty($addressParams)) {
258 $address = new CRM_Core_DAO_Address();
259 $address->id
= $dao->address_id
;
260 $address->copyValues($addressParams);
266 $this->returnMessages
[] = ts("Addresses Evaluated: %1", array(
267 1 => $totalAddresses,
269 if ($processGeocode) {
270 $this->returnMessages
[] = ts("Addresses Geocoded: %1", array(
274 if ($parseStreetAddress) {
275 $this->returnMessages
[] = ts("Street Addresses Parsed: %1", array(
276 1 => $totalAddressParsed,
278 if ($unparseableContactAddress) {
279 $this->returnMessages
[] = "<br />\n" . ts("Following is the list of contacts whose address is not parsed:") . "<br />\n";
280 foreach ($unparseableContactAddress as $contactLink) {
281 $this->returnMessages
[] = $contactLink . "<br />\n";
286 return $this->returnResult();
294 public function returnResult() {
296 $result['is_error'] = $this->returnError
;
297 $result['messages'] = implode("", $this->returnMessages
);