Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.1 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2011 | | |
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. | | |
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 along with this program; if not, contact CiviCRM LLC | | |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
25 | */ | |
26 | ||
27 | /** | |
28 | * Create a xml file for a set of contact ID's in a format digestible | |
29 | * by Solr | |
30 | */ | |
31 | ||
32 | require_once '../../../civicrm.settings.php'; | |
33 | require_once 'CRM/Core/Config.php'; | |
34 | ||
35 | define('CHUNK_SIZE', 128); | |
36 | ||
37 | /** | |
38 | * Split a large array of contactIDs into more manageable smaller chunks | |
39 | */ | |
40 | function &splitContactIDs(&$contactIDs) { | |
41 | // contactIDs could be a real large array, so we split it up into | |
42 | // smaller chunks and then general xml for each chunk | |
43 | $chunks = array(); | |
44 | $current = 0; | |
45 | $chunks[$current] = array(); | |
46 | $count = 0; | |
47 | ||
48 | foreach ($contactIDs as $cid) { | |
49 | $chunks[$current][] = $cid; | |
50 | $count++; | |
51 | ||
52 | if ($count == CHUNK_SIZE) { | |
53 | $current++; | |
54 | $chunks[$current] = array(); | |
55 | $count = 0; | |
56 | } | |
57 | } | |
58 | ||
59 | if (empty($chunks[$current])) { | |
60 | unset($chunks[$current]); | |
61 | } | |
62 | ||
63 | return $chunks; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Given an array of values, generate the JSON in the Solr format | |
68 | */ | |
69 | function &generateSolrJSON($values) { | |
70 | $result = "["; | |
71 | foreach ($values as $cid => $tokens) { | |
72 | if (empty($tokens)) { | |
73 | continue; | |
74 | } | |
75 | ||
76 | $result .= "\n {\n \"contact_id\" : \"$cid\","; | |
77 | ||
78 | foreach ($tokens as $n => $v) { | |
79 | if (is_array($v)) { | |
80 | $str = array(); | |
81 | foreach ($v as $el) { | |
82 | $el = escapeJsonString($el); | |
83 | $str[] = "\"$el\""; | |
84 | } | |
85 | $string = implode(",", $str); | |
86 | $result .= "\n \"{$n}\" : [$string],"; | |
87 | } | |
88 | else { | |
89 | $v = escapeJsonString($v); | |
90 | $result .= "\n \"{$n}\" : \"{$v}\","; | |
91 | } | |
92 | } | |
93 | ||
94 | // remove the last comma | |
95 | $result = rtrim($result, ","); | |
96 | ||
97 | $result .= "\n },"; | |
98 | } | |
99 | // remove the last comma | |
100 | $result = rtrim($result, ","); | |
101 | ||
102 | $result .= "\n]\n"; | |
103 | ||
104 | ||
105 | return $result; | |
106 | } | |
107 | ||
108 | function escapeJsonString($value) { | |
109 | $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); | |
110 | $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); | |
111 | return str_replace($escapers, $replacements, $value); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Given a set of contact IDs get the values | |
116 | */ | |
117 | function getValues(&$contactIDs, &$values) { | |
118 | $values = array(); | |
119 | ||
120 | foreach ($contactIDs as $cid) { | |
121 | $values[$cid] = array(); | |
122 | } | |
123 | ||
124 | getContactInfo($contactIDs, $values); | |
125 | getAddressInfo($contactIDs, $values); | |
126 | getPhoneInfo($contactIDs, $values); | |
127 | getEmailInfo($contactIDs, $values); | |
128 | getNoteInfo($contactIDs, $values); | |
129 | ||
130 | return $values; | |
131 | } | |
132 | ||
133 | function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) { | |
134 | $selectString = implode(',', array_keys($fields)); | |
135 | $idString = implode(',', $contactIDs); | |
136 | ||
137 | $sql = " | |
138 | SELECT $selectString, $whereField as contact_id | |
139 | FROM $tableName | |
140 | WHERE $whereField IN ( $idString ) | |
141 | "; | |
142 | ||
143 | if ($additionalWhereCond) { | |
144 | $sql .= " AND $additionalWhereCond"; | |
145 | } | |
146 | ||
147 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
148 | while ($dao->fetch()) { | |
149 | foreach ($fields as $fld => $name) { | |
150 | $name = $name ? $name : $fld; | |
151 | appendValue($values, $dao->contact_id, $name, $dao->$fld); | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
156 | function getContactInfo(&$contactIDs, &$values) { | |
157 | $fields = array('sort_name' => NULL, | |
158 | 'display_name' => NULL, | |
159 | 'contact_type' => NULL, | |
160 | 'legal_identifier' => NULL, | |
161 | 'external_identifier' => NULL, | |
162 | 'first_name' => NULL, | |
163 | 'last_name' => NULL, | |
164 | 'middle_name' => NULL, | |
165 | 'household_name' => NULL, | |
166 | 'organization_name' => NULL, | |
167 | 'legal_name' => NULL, | |
168 | 'job_title' => NULL, | |
169 | ); | |
170 | getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id'); | |
171 | } | |
172 | ||
173 | function getNoteInfo(&$contactIDs, &$values) { | |
174 | $ids = implode(',', $contactIDs); | |
175 | ||
176 | $sql = " | |
177 | SELECT | |
178 | entity_id as contact_id, | |
179 | note as note, subject as subject | |
180 | FROM civicrm_note | |
181 | WHERE entity_id IN ( $ids ) | |
182 | AND entity_table = 'civicrm_contact' | |
183 | "; | |
184 | ||
185 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
186 | while ($dao->fetch()) { | |
187 | $note = empty($dao->subject) ? '' : "{$dao->subject}: "; | |
188 | $note .= empty($dao->note) ? '' : $dao->note; | |
189 | ||
190 | appendValue($values, $dao->contact_id, 'note', $note); | |
191 | } | |
192 | } | |
193 | ||
194 | function getPhoneInfo(&$contactIDs, &$values) { | |
195 | $ids = implode(',', $contactIDs); | |
196 | ||
197 | $sql = " | |
198 | SELECT | |
199 | c.id as contact_id, | |
200 | l.name as location_type, | |
201 | p.phone as phone, | |
202 | v.label as phone_type | |
203 | FROM civicrm_contact c | |
204 | INNER JOIN civicrm_phone p ON p.contact_id = c.id | |
205 | LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id | |
206 | LEFT JOIN civicrm_option_group g ON g.name = 'phone_type' | |
207 | LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value | |
208 | WHERE c.id IN ( $ids ) | |
209 | AND p.phone IS NOT NULL | |
210 | "; | |
211 | ||
212 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
213 | while ($dao->fetch()) { | |
214 | $phone = ''; | |
215 | ||
216 | if (!empty($dao->location_type)) { | |
217 | $phone = "{$dao->location_type}: "; | |
218 | } | |
219 | ||
220 | $phone .= $dao->phone; | |
221 | ||
222 | if (!empty($dao->phone_type)) { | |
223 | $phone .= " ({$dao->phone_type})"; | |
224 | } | |
225 | ||
226 | appendValue($values, $dao->contact_id, 'phone', $phone); | |
227 | } | |
228 | } | |
229 | ||
230 | function getEmailInfo(&$contactIDs, &$values) { | |
231 | $ids = implode(',', $contactIDs); | |
232 | ||
233 | $sql = " | |
234 | SELECT | |
235 | c.id as contact_id, | |
236 | l.name as location_type, | |
237 | e.email as email | |
238 | FROM civicrm_contact c | |
239 | INNER JOIN civicrm_email e ON e.contact_id = c.id | |
240 | LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id | |
241 | WHERE c.id IN ( $ids ) | |
242 | AND e.email IS NOT NULL | |
243 | "; | |
244 | ||
245 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
246 | while ($dao->fetch()) { | |
247 | $email = ''; | |
248 | ||
249 | if (!empty($dao->location_type)) { | |
250 | $email = "{$dao->location_type}: "; | |
251 | } | |
252 | ||
253 | $email .= $dao->email; | |
254 | appendValue($values, $dao->contact_id, 'email', $email); | |
255 | } | |
256 | } | |
257 | ||
258 | function getAddressInfo(&$contactIDs, &$values) { | |
259 | $ids = implode(',', $contactIDs); | |
260 | ||
261 | $sql = " | |
262 | SELECT c.id as contact_id, l.name as location_type, | |
263 | a.street_address, a.supplemental_address_1, a.supplemental_address_2, | |
264 | a.city, a.postal_code, | |
265 | s.name as state, co.name as country | |
266 | FROM civicrm_contact c | |
267 | INNER JOIN civicrm_address a ON a.contact_id = c.id | |
268 | LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id | |
269 | LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id | |
270 | LEFT JOIN civicrm_country co ON a.country_id = co.id | |
271 | WHERE c.id IN ( $ids ) | |
272 | "; | |
273 | ||
274 | $fields = array('location_type', 'street_address', 'supplemental_address_1', | |
275 | 'supplemental_address_2', 'city', 'postal_code', | |
276 | 'state', 'country', | |
277 | ); | |
278 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
279 | while ($dao->fetch()) { | |
280 | $address = ''; | |
281 | foreach ($fields as $fld) { | |
282 | if (empty($dao->$fld)) { | |
283 | continue; | |
284 | } | |
285 | ||
286 | $address .= ($fld == 'location_type') ? "{$dao->$fld}: " : " {$dao->$fld},"; | |
287 | appendValue($values, $dao->contact_id, $fld, $dao->$fld); | |
288 | } | |
289 | ||
290 | if (!empty($address)) { | |
291 | $address = rtrim($address, ","); | |
292 | appendValue($values, $dao->contact_id, 'address', $address); | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
297 | function appendValue(&$values, $contactID, $name, $value) { | |
298 | if (empty($value)) { | |
299 | return; | |
300 | } | |
301 | ||
302 | if (!isset($values[$contactID][$name])) { | |
303 | $values[$contactID][$name] = $value; | |
304 | } | |
305 | else { | |
306 | if (!is_array($values[$contactID][$name])) { | |
307 | $save = $values[$contactID][$name]; | |
308 | $values[$contactID][$name] = array(); | |
309 | $values[$contactID][$name][] = $save; | |
310 | } | |
311 | $values[$contactID][$name][] = $value; | |
312 | } | |
313 | } | |
314 | ||
315 | function run(&$contactIDs) { | |
316 | $chunks = &splitContactIDs($contactIDs); | |
317 | ||
318 | foreach ($chunks as $chunk) { | |
319 | $values = array(); | |
320 | getValues($chunk, $values); | |
321 | $xml = &generateSolrJSON($values); | |
322 | echo $xml; | |
323 | } | |
324 | } | |
325 | ||
326 | $config = &CRM_Core_Config::singleton(); | |
327 | $config->userFramework = 'Soap'; | |
328 | $config->userFrameworkClass = 'CRM_Utils_System_Soap'; | |
329 | $config->userHookClass = 'CRM_Utils_Hook_Soap'; | |
330 | ||
331 | $sql = <<<EOT | |
332 | SELECT id | |
333 | FROM civicrm_contact | |
334 | EOT; | |
335 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
336 | ||
337 | ||
338 | $contactIDs = array(); | |
339 | while ($dao->fetch()) { | |
340 | $contactIDs[] = $dao->id; | |
341 | } | |
342 | ||
343 | run($contactIDs); | |
344 |