3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
36 define('THROTTLE_REQUESTS', 0);
40 require_once '../civicrm.config.php';
41 require_once 'CRM/Core/Config.php';
43 $config = CRM_Core_Config
::singleton();
45 require_once 'Console/Getopt.php';
46 $shortOptions = "n:p:s:e:k:g:parse";
47 $longOptions = array('name=', 'pass=', 'key=', 'start=', 'end=', 'geocoding=', 'parse=');
49 $getopt = new Console_Getopt();
50 $args = $getopt->readPHPArgv();
53 list($valid, $dontCare) = $getopt->getopt2($args, $shortOptions, $longOptions);
65 foreach ($vars as $var => $short) {
67 foreach ($valid as $v) {
68 if ($v[0] == $short ||
$v[0] == "--$var") {
74 $
$var = CRM_Utils_Array
::value($var, $_REQUEST);
76 $_REQUEST[$var] = $
$var;
79 // this does not return on failure
80 // require_once 'CRM/Utils/System.php';
81 CRM_Utils_System
::authenticateScript(TRUE, $name, $pass);
83 //log the execution of script
84 CRM_Core_Error
::debug_log_message('UpdateAddress.php');
86 // do check for geocoding.
87 $processGeocode = FALSE;
88 if (empty($config->geocodeMethod
)) {
89 if ($geocoding == 'true') {
90 echo ts('Error: You need to set a mapping provider under Global Settings');
95 $processGeocode = TRUE;
96 // user might want to over-ride.
97 if ($geocoding == 'false') {
98 $processGeocode = FALSE;
102 // do check for parse street address.
103 require_once 'CRM/Core/BAO/Setting.php';
104 $parseAddress = CRM_Utils_Array
::value('street_address_parsing',
105 CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
110 $parseStreetAddress = FALSE;
111 if (!$parseAddress) {
112 if ($parse == 'true') {
113 echo ts('Error: You need to enable Street Address Parsing under Global Settings >> Address Settings.');
118 $parseStreetAddress = TRUE;
119 // user might want to over-ride.
120 if ($parse == 'false') {
121 $parseStreetAddress = FALSE;
126 if (!$parseStreetAddress && !$processGeocode) {
127 echo ts('Error: Both Geocode mapping as well as Street Address Parsing are disabled. You must configure one or both options to use this script.');
131 // we have an exclusive lock - run the mail queue
132 processContacts($config, $processGeocode, $parseStreetAddress, $start, $end);
137 * @param $processGeocode
138 * @param $parseStreetAddress
142 function processContacts(&$config, $processGeocode, $parseStreetAddress, $start = NULL, $end = NULL) {
143 // build where clause.
144 $clause = array('( c.id = a.contact_id )');
146 $clause[] = "( c.id >= $start )";
149 $clause[] = "( c.id <= $end )";
151 if ($processGeocode) {
152 $clause[] = '( a.geo_code_1 is null OR a.geo_code_1 = 0 )';
153 $clause[] = '( a.geo_code_2 is null OR a.geo_code_2 = 0 )';
154 $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, CRM_Core_DAO
::$_nullArray);
178 if ($processGeocode) {
179 require_once str_replace('_', DIRECTORY_SEPARATOR
, $config->geocodeMethod
) . '.php';
182 require_once 'CRM/Core/DAO/Address.php';
183 require_once 'CRM/Core/BAO/Address.php';
185 $unparseableContactAddress = array();
186 while ($dao->fetch()) {
189 'street_address' => $dao->street_address
,
190 'postal_code' => $dao->postal_code
,
191 'city' => $dao->city
,
192 'state_province' => $dao->state
,
193 'country' => $dao->country
,
196 $addressParams = array();
199 if ($processGeocode) {
200 // loop through the address removing more information
201 // so we can get some geocode for a partial address
202 // i.e. city -> state -> country
206 if (defined('THROTTLE_REQUESTS') &&
212 eval($config->geocodeMethod
. '::format( $params, true );');
213 array_shift($params);
215 } while ((!isset($params['geo_code_1'])) &&
219 if (isset($params['geo_code_1']) &&
220 $params['geo_code_1'] != 'null'
223 $addressParams['geo_code_1'] = $params['geo_code_1'];
224 $addressParams['geo_code_2'] = $params['geo_code_2'];
228 // parse street address
229 if ($parseStreetAddress) {
230 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($dao->street_address
);
232 // consider address is automatically parseable,
233 // when we should found street_number and street_name
234 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
238 // do check for all elements.
240 $totalAddressParsed++
;
242 elseif ($dao->street_address
) {
243 //build contact edit url,
244 //so that user can manually fill the street address fields if the street address is not parsed, CRM-5886
245 $url = CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$dao->id}");
246 $unparseableContactAddress[] = " Contact ID: " . $dao->id
. " <a href =\"$url\"> " . $dao->street_address
. " </a> ";
247 // reset element values.
248 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
250 $addressParams = array_merge($addressParams, $parsedFields);
253 // finally update address object.
254 if (!empty($addressParams)) {
255 $address = new CRM_Core_DAO_Address();
256 $address->id
= $dao->address_id
;
257 $address->copyValues($addressParams);
263 echo ts("Addresses Evaluated: $totalAddresses\n");
264 if ($processGeocode) {
265 echo ts("Addresses Geocoded : $totalGeocoded\n");
267 if ($parseStreetAddress) {
268 echo ts("Street Address Parsed : $totalAddressParsed\n");
269 if ($unparseableContactAddress) {
270 echo ts("<br />\nFollowing is the list of contacts whose address is not parsed :<br />\n");
271 foreach ($unparseableContactAddress as $contactLink) {
272 echo ts("%1<br />\n", array(1 => $contactLink));