3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This code is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * A PHP cron script to format all the addresses in the database. Currently
14 * it only does geocoding if the geocode values are not set. At a later
15 * stage we will also handle USPS address cleanup and other formatting
20 define('THROTTLE_REQUESTS', 0);
24 require_once '../civicrm.config.php';
25 require_once 'CRM/Core/Config.php';
27 $config = CRM_Core_Config
::singleton();
29 require_once 'Console/Getopt.php';
30 $shortOptions = "n:p:k:pre";
31 $longOptions = ['name=', 'pass=', 'key=', 'prefix='];
33 $getopt = new Console_Getopt();
34 $args = $getopt->readPHPArgv();
37 list($valid, $dontCare) = $getopt->getopt2($args, $shortOptions, $longOptions);
46 foreach ($vars as $var => $short) {
48 foreach ($valid as $v) {
49 if ($v[0] == $short ||
$v[0] == "--$var") {
55 $
$var = $_REQUEST[$var] ??
NULL;
57 $_REQUEST[$var] = $
$var;
60 // this does not return on failure
61 // require_once 'CRM/Utils/System.php';
62 CRM_Utils_System
::authenticateScript(TRUE, $name, $pass);
64 //log the execution of script
65 CRM_Core_Error
::debug_log_message('NormalizePhone.php');
68 processPhones($config, $prefix);
75 function processPhones(&$config, $prefix = NULL) {
76 // ignore null phones and phones that already match what we are doing
80 WHERE phone IS NOT NULL
81 AND phone NOT REGEXP '^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$'
84 $dao = &CRM_Core_DAO
::executeQuery($query);
86 $updateQuery = "UPDATE civicrm_phone SET phone = %1 where id = %2";
91 $totalPhone = $validPhone = $nonPrefixedPhone = 0;
92 while ($dao->fetch()) {
93 $newPhone = processPhone($dao->phone
, $prefix);
94 echo "$newPhone, {$dao->phone}\n";
95 if ($newPhone !== FALSE) {
96 $params[1][0] = $newPhone;
97 $params[2][0] = $dao->id
;
98 CRM_Core_DAO
::executeQuery($updateQuery, $params);
99 echo "{$dao->phone}, $newPhone\n";
107 * @param null $prefix
109 * @return bool|string
111 function processPhone($phone, $prefix = NULL) {
112 // eliminate all white space and non numeric charaters
113 $cleanPhone = preg_replace('/[^\d]+/s', '', $phone);
115 $len = strlen($cleanPhone);
119 $cleanPhone = $prefix . $cleanPhone;
121 elseif ($len != 10) {
125 // now we have a 10 character string, lets return it as
127 $cleanPhone = substr($cleanPhone, 0, 3) . "-" . substr($cleanPhone, 3, 3) . "-" . substr($cleanPhone, 6, 4);
129 return ($cleanPhone == $phone) ?
FALSE : $cleanPhone;