Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
6b7eb9df | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
6b7eb9df TO |
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 | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
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 | |
16 | * issues | |
17 | * | |
18 | */ | |
5c5ed6db | 19 | if (!(php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0))) { |
a9f3bf65 SL |
20 | header("HTTP/1.0 404 Not Found"); |
21 | return; | |
22 | } | |
6a488035 TO |
23 | define('THROTTLE_REQUESTS', 0); |
24 | function run() { | |
25 | session_start(); | |
26 | ||
27 | require_once '../civicrm.config.php'; | |
28 | require_once 'CRM/Core/Config.php'; | |
29 | ||
30 | $config = CRM_Core_Config::singleton(); | |
31 | ||
32 | require_once 'Console/Getopt.php'; | |
33 | $shortOptions = "n:p:k:pre"; | |
b7c0a88f | 34 | $longOptions = ['name=', 'pass=', 'key=', 'prefix=']; |
6a488035 TO |
35 | |
36 | $getopt = new Console_Getopt(); | |
37 | $args = $getopt->readPHPArgv(); | |
38 | ||
39 | array_shift($args); | |
40 | list($valid, $dontCare) = $getopt->getopt2($args, $shortOptions, $longOptions); | |
41 | ||
b7c0a88f | 42 | $vars = [ |
6a488035 TO |
43 | 'name' => 'n', |
44 | 'pass' => 'p', | |
45 | 'key' => 'k', | |
46 | 'prefix' => 'pre', | |
b7c0a88f | 47 | ]; |
6a488035 TO |
48 | |
49 | foreach ($vars as $var => $short) { | |
50 | $$var = NULL; | |
51 | foreach ($valid as $v) { | |
52 | if ($v[0] == $short || $v[0] == "--$var") { | |
53 | $$var = $v[1]; | |
54 | break; | |
55 | } | |
56 | } | |
57 | if (!$$var) { | |
9c1bc317 | 58 | $$var = $_REQUEST[$var] ?? NULL; |
6a488035 TO |
59 | } |
60 | $_REQUEST[$var] = $$var; | |
61 | } | |
62 | ||
63 | // this does not return on failure | |
64 | // require_once 'CRM/Utils/System.php'; | |
65 | CRM_Utils_System::authenticateScript(TRUE, $name, $pass); | |
66 | ||
67 | //log the execution of script | |
68 | CRM_Core_Error::debug_log_message('NormalizePhone.php'); | |
69 | ||
70 | // process all phones | |
71 | processPhones($config, $prefix); | |
72 | } | |
73 | ||
a1a55b61 EM |
74 | /** |
75 | * @param $config | |
76 | * @param null $prefix | |
77 | */ | |
6a488035 TO |
78 | function processPhones(&$config, $prefix = NULL) { |
79 | // ignore null phones and phones that already match what we are doing | |
80 | $query = " | |
81 | SELECT id, phone | |
82 | FROM civicrm_phone | |
83 | WHERE phone IS NOT NULL | |
84 | AND phone NOT REGEXP '^[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$' | |
85 | "; | |
86 | ||
87 | $dao = &CRM_Core_DAO::executeQuery($query); | |
88 | ||
89 | $updateQuery = "UPDATE civicrm_phone SET phone = %1 where id = %2"; | |
b7c0a88f | 90 | $params = [ |
91 | 1 => ['', 'String'], | |
92 | 2 => [0, 'Integer'], | |
93 | ]; | |
6a488035 TO |
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"; | |
103 | } | |
104 | $totalPhone++; | |
105 | } | |
106 | } | |
107 | ||
a1a55b61 EM |
108 | /** |
109 | * @param $phone | |
110 | * @param null $prefix | |
111 | * | |
112 | * @return bool|string | |
113 | */ | |
6a488035 TO |
114 | function processPhone($phone, $prefix = NULL) { |
115 | // eliminate all white space and non numeric charaters | |
116 | $cleanPhone = preg_replace('/[^\d]+/s', '', $phone); | |
117 | ||
118 | $len = strlen($cleanPhone); | |
119 | if ($prefix && | |
120 | $len == 7 | |
121 | ) { | |
122 | $cleanPhone = $prefix . $cleanPhone; | |
123 | } | |
124 | elseif ($len != 10) { | |
125 | return FALSE; | |
126 | } | |
127 | ||
128 | // now we have a 10 character string, lets return it as | |
129 | // ABC-DEF-GHIJ | |
130 | $cleanPhone = substr($cleanPhone, 0, 3) . "-" . substr($cleanPhone, 3, 3) . "-" . substr($cleanPhone, 6, 4); | |
131 | ||
132 | return ($cleanPhone == $phone) ? FALSE : $cleanPhone; | |
133 | } | |
134 | ||
135 | run(); | |
136 |