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