3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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);
135 function processContacts(&$config, $processGeocode, $parseStreetAddress, $start = NULL, $end = NULL) {
136 // build where clause.
137 $clause = array('( c.id = a.contact_id )');
139 $clause[] = "( c.id >= $start )";
142 $clause[] = "( c.id <= $end )";
144 if ($processGeocode) {
145 $clause[] = '( a.geo_code_1 is null OR a.geo_code_1 = 0 )';
146 $clause[] = '( a.geo_code_2 is null OR a.geo_code_2 = 0 )';
147 $clause[] = '( a.country_id is not null )';
149 $whereClause = implode(' AND ', $clause);
159 FROM civicrm_contact c
160 INNER JOIN civicrm_address a ON a.contact_id = c.id
161 LEFT JOIN civicrm_country o ON a.country_id = o.id
162 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
167 $totalGeocoded = $totalAddresses = $totalAddressParsed = 0;
169 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
171 if ($processGeocode) {
172 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $config->geocodeMethod
) . '.php');
175 require_once 'CRM/Core/DAO/Address.php';
176 require_once 'CRM/Core/BAO/Address.php';
178 $unparseableContactAddress = array();
179 while ($dao->fetch()) {
182 'street_address' => $dao->street_address
,
183 'postal_code' => $dao->postal_code
,
184 'city' => $dao->city
,
185 'state_province' => $dao->state
,
186 'country' => $dao->country
,
189 $addressParams = array();
192 if ($processGeocode) {
193 // loop through the address removing more information
194 // so we can get some geocode for a partial address
195 // i.e. city -> state -> country
199 if (defined('THROTTLE_REQUESTS') &&
205 eval($config->geocodeMethod
. '::format( $params, true );');
206 array_shift($params);
208 } while ((!isset($params['geo_code_1'])) &&
212 if (isset($params['geo_code_1']) &&
213 $params['geo_code_1'] != 'null'
216 $addressParams['geo_code_1'] = $params['geo_code_1'];
217 $addressParams['geo_code_2'] = $params['geo_code_2'];
221 // parse street address
222 if ($parseStreetAddress) {
223 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($dao->street_address
);
225 // consider address is automatically parseable,
226 // when we should found street_number and street_name
227 if (!CRM_Utils_Array
::value('street_name', $parsedFields) ||
228 !CRM_Utils_Array
::value('street_number', $parsedFields)
233 // do check for all elements.
235 $totalAddressParsed++
;
237 elseif ($dao->street_address
) {
238 //build contact edit url,
239 //so that user can manually fill the street address fields if the street address is not parsed, CRM-5886
240 $url = CRM_Utils_System
::url('civicrm/contact/add', "reset=1&action=update&cid={$dao->id}");
241 $unparseableContactAddress[] = " Contact ID: " . $dao->id
. " <a href =\"$url\"> " . $dao->street_address
. " </a> ";
242 // reset element values.
243 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
245 $addressParams = array_merge($addressParams, $parsedFields);
248 // finally update address object.
249 if (!empty($addressParams)) {
250 $address = new CRM_Core_DAO_Address();
251 $address->id
= $dao->address_id
;
252 $address->copyValues($addressParams);
258 echo ts("Addresses Evaluated: $totalAddresses\n");
259 if ($processGeocode) {
260 echo ts("Addresses Geocoded : $totalGeocoded\n");
262 if ($parseStreetAddress) {
263 echo ts("Street Address Parsed : $totalAddressParsed\n");
264 if ($unparseableContactAddress) {
265 echo ts("<br />\nFollowing is the list of contacts whose address is not parsed :<br />\n");
266 foreach ($unparseableContactAddress as $contactLink) {
267 echo ts("%1<br />\n", array(1 => $contactLink));