Merge pull request #14326 from civicrm/5.14
[civicrm-core.git] / tools / bin / scripts / NormalizePhone.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
5b71fd5f 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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
36define('THROTTLE_REQUESTS', 0);
37function 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";
b7c0a88f 47 $longOptions = ['name=', 'pass=', 'key=', 'prefix='];
6a488035
TO
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
b7c0a88f 55 $vars = [
6a488035
TO
56 'name' => 'n',
57 'pass' => 'p',
58 'key' => 'k',
59 'prefix' => 'pre',
b7c0a88f 60 ];
6a488035
TO
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
a1a55b61
EM
87/**
88 * @param $config
89 * @param null $prefix
90 */
6a488035
TO
91function processPhones(&$config, $prefix = NULL) {
92 // ignore null phones and phones that already match what we are doing
93 $query = "
94SELECT id, phone
95FROM civicrm_phone
96WHERE phone IS NOT NULL
97AND phone NOT REGEXP '^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$'
98";
99
100 $dao = &CRM_Core_DAO::executeQuery($query);
101
102 $updateQuery = "UPDATE civicrm_phone SET phone = %1 where id = %2";
b7c0a88f 103 $params = [
104 1 => ['', 'String'],
105 2 => [0, 'Integer'],
106 ];
6a488035
TO
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";
116 }
117 $totalPhone++;
118 }
119}
120
a1a55b61
EM
121/**
122 * @param $phone
123 * @param null $prefix
124 *
125 * @return bool|string
126 */
6a488035
TO
127function processPhone($phone, $prefix = NULL) {
128 // eliminate all white space and non numeric charaters
129 $cleanPhone = preg_replace('/[^\d]+/s', '', $phone);
130
131 $len = strlen($cleanPhone);
132 if ($prefix &&
133 $len == 7
134 ) {
135 $cleanPhone = $prefix . $cleanPhone;
136 }
137 elseif ($len != 10) {
138 return FALSE;
139 }
140
141 // now we have a 10 character string, lets return it as
142 // ABC-DEF-GHIJ
143 $cleanPhone = substr($cleanPhone, 0, 3) . "-" . substr($cleanPhone, 3, 3) . "-" . substr($cleanPhone, 6, 4);
144
145 return ($cleanPhone == $phone) ? FALSE : $cleanPhone;
146}
147
148run();
149