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
19 if (!(php_sapi_name() == 'cli' ||
(is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))) {
20 header("HTTP/1.0 404 Not Found");
23 define('THROTTLE_REQUESTS', 0);
27 require_once '../civicrm.config.php';
28 require_once 'CRM/Core/Config.php';
30 $config = CRM_Core_Config
::singleton();
32 require_once 'Console/Getopt.php';
33 $shortOptions = "n:p:k:pre";
34 $longOptions = ['name=', 'pass=', 'key=', 'prefix='];
36 $getopt = new Console_Getopt();
37 $args = $getopt->readPHPArgv();
40 list($valid, $dontCare) = $getopt->getopt2($args, $shortOptions, $longOptions);
49 foreach ($vars as $var => $short) {
51 foreach ($valid as $v) {
52 if ($v[0] == $short ||
$v[0] == "--$var") {
58 $
$var = $_REQUEST[$var] ??
NULL;
60 $_REQUEST[$var] = $
$var;
63 // this does not return on failure
64 // require_once 'CRM/Utils/System.php';
65 CRM_Utils_System
::authenticateScript(TRUE, $name, $pass);
67 //log the execution of script
68 CRM_Core_Error
::debug_log_message('NormalizePhone.php');
71 processPhones($config, $prefix);
78 function processPhones(&$config, $prefix = NULL) {
79 // ignore null phones and phones that already match what we are doing
83 WHERE phone IS NOT NULL
84 AND phone NOT REGEXP '^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$'
87 $dao = &CRM_Core_DAO
::executeQuery($query);
89 $updateQuery = "UPDATE civicrm_phone SET phone = %1 where id = %2";
94 $totalPhone = $validPhone = $nonPrefixedPhone = 0;
95 while ($dao->fetch()) {
96 $newPhone = processPhone($dao->phone
, $prefix);
97 echo "$newPhone, {$dao->phone}\n";
98 if ($newPhone !== FALSE) {
99 $params[1][0] = $newPhone;
100 $params[2][0] = $dao->id
;
101 CRM_Core_DAO
::executeQuery($updateQuery, $params);
102 echo "{$dao->phone}, $newPhone\n";
110 * @param null $prefix
112 * @return bool|string
114 function processPhone($phone, $prefix = NULL) {
115 // eliminate all white space and non numeric charaters
116 $cleanPhone = preg_replace('/[^\d]+/s', '', $phone);
118 $len = strlen($cleanPhone);
122 $cleanPhone = $prefix . $cleanPhone;
124 elseif ($len != 10) {
128 // now we have a 10 character string, lets return it as
130 $cleanPhone = substr($cleanPhone, 0, 3) . "-" . substr($cleanPhone, 3, 3) . "-" . substr($cleanPhone, 6, 4);
132 return ($cleanPhone == $phone) ?
FALSE : $cleanPhone;