Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | */ | |
35 | ||
36 | define('THROTTLE_REQUESTS', 0); | |
37 | function run() { | |
38 | session_start(); | |
39 | ||
40 | require_once '../civicrm.config.php'; | |
41 | require_once 'CRM/Core/Config.php'; | |
42 | ||
43 | $config = CRM_Core_Config::singleton(); | |
44 | ||
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='); | |
48 | ||
49 | $getopt = new Console_Getopt(); | |
50 | $args = $getopt->readPHPArgv(); | |
51 | ||
52 | array_shift($args); | |
53 | list($valid, $dontCare) = $getopt->getopt2($args, $shortOptions, $longOptions); | |
54 | ||
55 | $vars = array( | |
56 | 'start' => 's', | |
57 | 'end' => 'e', | |
58 | 'name' => 'n', | |
59 | 'pass' => 'p', | |
60 | 'key' => 'k', | |
61 | 'geocoding' => 'g', | |
62 | 'parse' => 'ap', | |
63 | ); | |
64 | ||
65 | foreach ($vars as $var => $short) { | |
66 | $$var = NULL; | |
67 | foreach ($valid as $v) { | |
68 | if ($v[0] == $short || $v[0] == "--$var") { | |
69 | $$var = $v[1]; | |
70 | break; | |
71 | } | |
72 | } | |
73 | if (!$$var) { | |
74 | $$var = CRM_Utils_Array::value($var, $_REQUEST); | |
75 | } | |
76 | $_REQUEST[$var] = $$var; | |
77 | } | |
78 | ||
79 | // this does not return on failure | |
80 | // require_once 'CRM/Utils/System.php'; | |
81 | CRM_Utils_System::authenticateScript(TRUE, $name, $pass); | |
82 | ||
83 | //log the execution of script | |
84 | CRM_Core_Error::debug_log_message('UpdateAddress.php'); | |
85 | ||
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'); | |
91 | exit(); | |
92 | } | |
93 | } | |
94 | else { | |
95 | $processGeocode = TRUE; | |
96 | // user might want to over-ride. | |
97 | if ($geocoding == 'false') { | |
98 | $processGeocode = FALSE; | |
99 | } | |
100 | } | |
101 | ||
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, | |
106 | 'address_options' | |
107 | ), | |
108 | FALSE | |
109 | ); | |
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.'); | |
114 | exit(); | |
115 | } | |
116 | } | |
117 | else { | |
118 | $parseStreetAddress = TRUE; | |
119 | // user might want to over-ride. | |
120 | if ($parse == 'false') { | |
121 | $parseStreetAddress = FALSE; | |
122 | } | |
123 | } | |
124 | ||
125 | // don't process. | |
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.'); | |
128 | exit(); | |
129 | } | |
130 | ||
131 | // we have an exclusive lock - run the mail queue | |
132 | processContacts($config, $processGeocode, $parseStreetAddress, $start, $end); | |
133 | } | |
134 | ||
135 | function processContacts(&$config, $processGeocode, $parseStreetAddress, $start = NULL, $end = NULL) { | |
136 | // build where clause. | |
137 | $clause = array('( c.id = a.contact_id )'); | |
138 | if ($start) { | |
139 | $clause[] = "( c.id >= $start )"; | |
140 | } | |
141 | if ($end) { | |
142 | $clause[] = "( c.id <= $end )"; | |
143 | } | |
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 )'; | |
148 | } | |
149 | $whereClause = implode(' AND ', $clause); | |
150 | ||
151 | $query = " | |
152 | SELECT c.id, | |
153 | a.id as address_id, | |
154 | a.street_address, | |
155 | a.city, | |
156 | a.postal_code, | |
157 | s.name as state, | |
158 | o.name as country | |
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 | |
163 | WHERE {$whereClause} | |
164 | ORDER BY a.id | |
165 | "; | |
166 | ||
167 | $totalGeocoded = $totalAddresses = $totalAddressParsed = 0; | |
168 | ||
169 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
170 | ||
171 | if ($processGeocode) { | |
172 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $config->geocodeMethod) . '.php'); | |
173 | } | |
174 | ||
175 | require_once 'CRM/Core/DAO/Address.php'; | |
176 | require_once 'CRM/Core/BAO/Address.php'; | |
177 | ||
178 | $unparseableContactAddress = array(); | |
179 | while ($dao->fetch()) { | |
180 | $totalAddresses++; | |
181 | $params = array( | |
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, | |
187 | ); | |
188 | ||
189 | $addressParams = array(); | |
190 | ||
191 | // process geocode. | |
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 | |
196 | ||
197 | $maxTries = 5; | |
198 | do { | |
199 | if (defined('THROTTLE_REQUESTS') && | |
200 | THROTTLE_REQUESTS | |
201 | ) { | |
202 | usleep(50000); | |
203 | } | |
204 | ||
205 | eval($config->geocodeMethod . '::format( $params, true );'); | |
206 | array_shift($params); | |
207 | $maxTries--; | |
208 | } while ((!isset($params['geo_code_1'])) && | |
209 | ($maxTries > 1) | |
210 | ); | |
211 | ||
212 | if (isset($params['geo_code_1']) && | |
213 | $params['geo_code_1'] != 'null' | |
214 | ) { | |
215 | $totalGeocoded++; | |
216 | $addressParams['geo_code_1'] = $params['geo_code_1']; | |
217 | $addressParams['geo_code_2'] = $params['geo_code_2']; | |
218 | } | |
219 | } | |
220 | ||
221 | // parse street address | |
222 | if ($parseStreetAddress) { | |
223 | $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($dao->street_address); | |
224 | $success = TRUE; | |
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) | |
229 | ) { | |
230 | $success = FALSE; | |
231 | } | |
232 | ||
233 | // do check for all elements. | |
234 | if ($success) { | |
235 | $totalAddressParsed++; | |
236 | } | |
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), ''); | |
244 | } | |
245 | $addressParams = array_merge($addressParams, $parsedFields); | |
246 | } | |
247 | ||
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); | |
253 | $address->save(); | |
254 | $address->free(); | |
255 | } | |
256 | } | |
257 | ||
258 | echo ts("Addresses Evaluated: $totalAddresses\n"); | |
259 | if ($processGeocode) { | |
260 | echo ts("Addresses Geocoded : $totalGeocoded\n"); | |
261 | } | |
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)); | |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | return; | |
273 | } | |
274 | ||
275 | run(); | |
276 |