Merge pull request #1453 from davecivicrm/CRM-13176
[civicrm-core.git] / tools / bin / scripts / NormalizePhone.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2010 |
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:k:pre";
47 $longOptions = array('name=', 'pass=', 'key=', 'prefix=');
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 'name' => 'n',
57 'pass' => 'p',
58 'key' => 'k',
59 'prefix' => 'pre',
60 );
61
62 foreach ($vars as $var => $short) {
63 $$var = NULL;
64 foreach ($valid as $v) {
65 if ($v[0] == $short || $v[0] == "--$var") {
66 $$var = $v[1];
67 break;
68 }
69 }
70 if (!$$var) {
71 $$var = CRM_Utils_Array::value($var, $_REQUEST);
72 }
73 $_REQUEST[$var] = $$var;
74 }
75
76 // this does not return on failure
77 // require_once 'CRM/Utils/System.php';
78 CRM_Utils_System::authenticateScript(TRUE, $name, $pass);
79
80 //log the execution of script
81 CRM_Core_Error::debug_log_message('NormalizePhone.php');
82
83 // process all phones
84 processPhones($config, $prefix);
85 }
86
87 function processPhones(&$config, $prefix = NULL) {
88 // ignore null phones and phones that already match what we are doing
89 $query = "
90 SELECT id, phone
91 FROM civicrm_phone
92 WHERE phone IS NOT NULL
93 AND phone NOT REGEXP '^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$'
94 ";
95
96 $dao = &CRM_Core_DAO::executeQuery($query);
97
98 $updateQuery = "UPDATE civicrm_phone SET phone = %1 where id = %2";
99 $params = array(1 => array('', 'String'),
100 2 => array(0, 'Integer'),
101 );
102 $totalPhone = $validPhone = $nonPrefixedPhone = 0;
103 while ($dao->fetch()) {
104 $newPhone = processPhone($dao->phone, $prefix);
105 echo "$newPhone, {$dao->phone}\n";
106 if ($newPhone !== FALSE) {
107 $params[1][0] = $newPhone;
108 $params[2][0] = $dao->id;
109 CRM_Core_DAO::executeQuery($updateQuery, $params);
110 echo "{$dao->phone}, $newPhone\n";
111 }
112 $totalPhone++;
113 }
114 }
115
116 function processPhone($phone, $prefix = NULL) {
117 // eliminate all white space and non numeric charaters
118 $cleanPhone = preg_replace('/[^\d]+/s', '', $phone);
119
120 $len = strlen($cleanPhone);
121 if ($prefix &&
122 $len == 7
123 ) {
124 $cleanPhone = $prefix . $cleanPhone;
125 }
126 elseif ($len != 10) {
127 return FALSE;
128 }
129
130 // now we have a 10 character string, lets return it as
131 // ABC-DEF-GHIJ
132 $cleanPhone = substr($cleanPhone, 0, 3) . "-" . substr($cleanPhone, 3, 3) . "-" . substr($cleanPhone, 6, 4);
133
134 return ($cleanPhone == $phone) ? FALSE : $cleanPhone;
135 }
136
137 run();
138