Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | ||
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 | ||
a1a55b61 EM |
87 | /** |
88 | * @param $config | |
89 | * @param null $prefix | |
90 | */ | |
6a488035 TO |
91 | function processPhones(&$config, $prefix = NULL) { |
92 | // ignore null phones and phones that already match what we are doing | |
93 | $query = " | |
94 | SELECT id, phone | |
95 | FROM civicrm_phone | |
96 | WHERE phone IS NOT NULL | |
97 | AND 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"; | |
103 | $params = array(1 => array('', 'String'), | |
104 | 2 => array(0, 'Integer'), | |
105 | ); | |
106 | $totalPhone = $validPhone = $nonPrefixedPhone = 0; | |
107 | while ($dao->fetch()) { | |
108 | $newPhone = processPhone($dao->phone, $prefix); | |
109 | echo "$newPhone, {$dao->phone}\n"; | |
110 | if ($newPhone !== FALSE) { | |
111 | $params[1][0] = $newPhone; | |
112 | $params[2][0] = $dao->id; | |
113 | CRM_Core_DAO::executeQuery($updateQuery, $params); | |
114 | echo "{$dao->phone}, $newPhone\n"; | |
115 | } | |
116 | $totalPhone++; | |
117 | } | |
118 | } | |
119 | ||
a1a55b61 EM |
120 | /** |
121 | * @param $phone | |
122 | * @param null $prefix | |
123 | * | |
124 | * @return bool|string | |
125 | */ | |
6a488035 TO |
126 | function processPhone($phone, $prefix = NULL) { |
127 | // eliminate all white space and non numeric charaters | |
128 | $cleanPhone = preg_replace('/[^\d]+/s', '', $phone); | |
129 | ||
130 | $len = strlen($cleanPhone); | |
131 | if ($prefix && | |
132 | $len == 7 | |
133 | ) { | |
134 | $cleanPhone = $prefix . $cleanPhone; | |
135 | } | |
136 | elseif ($len != 10) { | |
137 | return FALSE; | |
138 | } | |
139 | ||
140 | // now we have a 10 character string, lets return it as | |
141 | // ABC-DEF-GHIJ | |
142 | $cleanPhone = substr($cleanPhone, 0, 3) . "-" . substr($cleanPhone, 3, 3) . "-" . substr($cleanPhone, 6, 4); | |
143 | ||
144 | return ($cleanPhone == $phone) ? FALSE : $cleanPhone; | |
145 | } | |
146 | ||
147 | run(); | |
148 |