3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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:k:pre";
47 $longOptions = ['name=', 'pass=', 'key=', 'prefix='];
49 $getopt = new Console_Getopt();
50 $args = $getopt->readPHPArgv();
53 list($valid, $dontCare) = $getopt->getopt2($args, $shortOptions, $longOptions);
62 foreach ($vars as $var => $short) {
64 foreach ($valid as $v) {
65 if ($v[0] == $short ||
$v[0] == "--$var") {
71 $
$var = CRM_Utils_Array
::value($var, $_REQUEST);
73 $_REQUEST[$var] = $
$var;
76 // this does not return on failure
77 // require_once 'CRM/Utils/System.php';
78 CRM_Utils_System
::authenticateScript(TRUE, $name, $pass);
80 //log the execution of script
81 CRM_Core_Error
::debug_log_message('NormalizePhone.php');
84 processPhones($config, $prefix);
91 function processPhones(&$config, $prefix = NULL) {
92 // ignore null phones and phones that already match what we are doing
96 WHERE phone IS NOT NULL
97 AND phone NOT REGEXP '^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$'
100 $dao = &CRM_Core_DAO
::executeQuery($query);
102 $updateQuery = "UPDATE civicrm_phone SET phone = %1 where id = %2";
107 $totalPhone = $validPhone = $nonPrefixedPhone = 0;
108 while ($dao->fetch()) {
109 $newPhone = processPhone($dao->phone
, $prefix);
110 echo "$newPhone, {$dao->phone}\n";
111 if ($newPhone !== FALSE) {
112 $params[1][0] = $newPhone;
113 $params[2][0] = $dao->id
;
114 CRM_Core_DAO
::executeQuery($updateQuery, $params);
115 echo "{$dao->phone}, $newPhone\n";
123 * @param null $prefix
125 * @return bool|string
127 function processPhone($phone, $prefix = NULL) {
128 // eliminate all white space and non numeric charaters
129 $cleanPhone = preg_replace('/[^\d]+/s', '', $phone);
131 $len = strlen($cleanPhone);
135 $cleanPhone = $prefix . $cleanPhone;
137 elseif ($len != 10) {
141 // now we have a 10 character string, lets return it as
143 $cleanPhone = substr($cleanPhone, 0, 3) . "-" . substr($cleanPhone, 3, 3) . "-" . substr($cleanPhone, 6, 4);
145 return ($cleanPhone == $phone) ?
FALSE : $cleanPhone;