Commit | Line | Data |
---|---|---|
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. | | |
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 our Sync scripts | |
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 | |
d7c8cf03 EM |
39 | * @param $contactIDs |
40 | * @return array | |
6a488035 TO |
41 | */ |
42 | function &splitContactIDs(&$contactIDs) { | |
43 | // contactIDs could be a real large array, so we split it up into | |
44 | // smaller chunks and then general xml for each chunk | |
45 | $chunks = array(); | |
46 | $current = 0; | |
47 | $chunks[$current] = array(); | |
48 | $count = 0; | |
49 | ||
50 | foreach ($contactIDs as $k => $v) { | |
51 | $chunks[$current][$k] = $v; | |
52 | $count++; | |
53 | ||
54 | if ($count == CHUNK_SIZE) { | |
55 | $current++; | |
56 | $chunks[$current] = array(); | |
57 | $count = 0; | |
58 | } | |
59 | } | |
60 | ||
61 | if (empty($chunks[$current])) { | |
62 | unset($chunks[$current]); | |
63 | } | |
64 | ||
65 | return $chunks; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Given a set of contact IDs get the values | |
d7c8cf03 EM |
70 | * @param $contactIDs |
71 | * @param $values | |
72 | * @param $allContactIDs | |
73 | * @param $addditionalContactIDs | |
74 | * @return array | |
6a488035 TO |
75 | */ |
76 | function getValues(&$contactIDs, &$values, &$allContactIDs, &$addditionalContactIDs) { | |
77 | $values = array(); | |
78 | ||
79 | getContactInfo($contactIDs, $values); | |
80 | getAddressInfo($contactIDs, $values); | |
81 | getPhoneInfo($contactIDs, $values); | |
82 | getEmailInfo($contactIDs, $values); | |
83 | getNoteInfo($contactIDs, $values); | |
84 | ||
85 | getRelationshipInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs); | |
86 | ||
87 | getActivityInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs); | |
88 | ||
89 | // got to do groups, tags | |
90 | ||
91 | // got to do meta data | |
92 | ||
93 | return $values; | |
94 | } | |
95 | ||
a1a55b61 EM |
96 | /** |
97 | * @param $contactIDs | |
98 | * @param $values | |
99 | * @param $tableName | |
100 | * @param $fields | |
101 | * @param $whereField | |
102 | * @param null $additionalWhereCond | |
103 | * @param bool $flat | |
104 | */ | |
6a488035 TO |
105 | function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, |
106 | $whereField, $additionalWhereCond = NULL, | |
107 | $flat = FALSE | |
108 | ) { | |
109 | $selectString = implode(',', array_keys($fields)); | |
110 | $idString = implode(',', $contactIDs); | |
111 | ||
112 | $sql = " | |
113 | SELECT $selectString, $whereField as contact_id | |
114 | FROM $tableName | |
115 | WHERE $whereField IN ( $idString ) | |
116 | "; | |
117 | ||
118 | if ($additionalWhereCond) { | |
119 | $sql .= " AND $additionalWhereCond"; | |
120 | } | |
121 | ||
122 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
123 | while ($dao->fetch()) { | |
124 | $contact = array(); | |
125 | foreach ($fields as $fld => $name) { | |
126 | $name = $name ? $name : $fld; | |
127 | if (empty($dao->$fld)) { | |
128 | $contact[$name] = NULL; | |
129 | } | |
130 | else { | |
131 | $contact[$name] = $dao->$fld; | |
132 | } | |
133 | } | |
134 | appendValue($values, $dao->contact_id, 'contact', $contact, $flat); | |
135 | } | |
136 | $dao->free(); | |
137 | } | |
138 | ||
a1a55b61 EM |
139 | /** |
140 | * @param $contactIDs | |
141 | * @param $values | |
142 | */ | |
6a488035 TO |
143 | function getContactInfo(&$contactIDs, &$values) { |
144 | $fields = array('id' => NULL, | |
145 | 'sort_name' => NULL, | |
146 | 'display_name' => NULL, | |
147 | 'contact_type' => NULL, | |
148 | 'legal_identifier' => NULL, | |
149 | 'external_identifier' => NULL, | |
150 | 'first_name' => NULL, | |
151 | 'last_name' => NULL, | |
152 | 'middle_name' => NULL, | |
153 | 'household_name' => NULL, | |
154 | 'organization_name' => NULL, | |
155 | 'legal_name' => NULL, | |
156 | 'job_title' => NULL, | |
157 | ); | |
158 | getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id', NULL, TRUE); | |
159 | } | |
160 | ||
a1a55b61 EM |
161 | /** |
162 | * @param $contactIDs | |
163 | * @param $values | |
164 | */ | |
6a488035 TO |
165 | function getNoteInfo(&$contactIDs, &$values) { |
166 | $ids = implode(',', $contactIDs); | |
167 | ||
168 | $sql = " | |
169 | SELECT | |
170 | id, | |
171 | entity_id as contact_id, | |
172 | note as note, subject as subject | |
173 | FROM civicrm_note | |
174 | WHERE entity_id IN ( $ids ) | |
175 | AND entity_table = 'civicrm_contact' | |
176 | "; | |
177 | ||
178 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
179 | while ($dao->fetch()) { | |
180 | $note = array('id' => $dao->id, | |
181 | 'contact_id' => $dao->contact_id, | |
182 | 'subject' => empty($dao->subject) ? NULL : $dao->subject, | |
183 | 'note' => empty($dao->note) ? NULL : $dao->note, | |
184 | ); | |
185 | ||
186 | appendValue($values, $dao->id, 'note', $note); | |
187 | } | |
188 | $dao->free(); | |
189 | } | |
190 | ||
a1a55b61 EM |
191 | /** |
192 | * @param $contactIDs | |
193 | * @param $values | |
194 | */ | |
6a488035 TO |
195 | function getPhoneInfo(&$contactIDs, &$values) { |
196 | $ids = implode(',', $contactIDs); | |
197 | ||
198 | $sql = " | |
199 | SELECT | |
200 | p.id as id, | |
201 | c.id as contact_id, | |
202 | l.name as location_type, | |
203 | p.phone as phone, | |
204 | v.label as phone_type | |
205 | FROM civicrm_contact c | |
206 | INNER JOIN civicrm_phone p ON p.contact_id = c.id | |
207 | LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id | |
208 | LEFT JOIN civicrm_option_group g ON g.name = 'phone_type' | |
209 | LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value | |
a1a55b61 | 210 | WHERE c.id IN ( $ids ) |
6a488035 TO |
211 | AND p.phone IS NOT NULL |
212 | "; | |
213 | ||
214 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
215 | while ($dao->fetch()) { | |
216 | $phone = array('id' => $dao->id, | |
217 | 'contact_id' => $dao->contact_id, | |
218 | 'location_type' => empty($dao->location_type) ? NULL : $dao->location_type, | |
219 | 'phone' => $dao->phone, | |
220 | 'phone_type' => empty($dao->phone_type) ? NULL : $dao->phone_type, | |
221 | ); | |
222 | ||
223 | appendValue($values, $dao->id, 'phone', $phone); | |
224 | } | |
225 | $dao->free(); | |
226 | } | |
227 | ||
a1a55b61 EM |
228 | /** |
229 | * @param $contactIDs | |
230 | * @param $values | |
231 | */ | |
6a488035 TO |
232 | function getEmailInfo(&$contactIDs, &$values) { |
233 | $ids = implode(',', $contactIDs); | |
234 | ||
235 | $sql = " | |
236 | SELECT | |
237 | e.id as id, | |
238 | c.id as contact_id, | |
239 | l.name as location_type, | |
240 | e.email as email | |
241 | FROM civicrm_contact c | |
242 | INNER JOIN civicrm_email e ON e.contact_id = c.id | |
243 | LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id | |
a1a55b61 | 244 | WHERE c.id IN ( $ids ) |
6a488035 TO |
245 | AND e.email IS NOT NULL |
246 | "; | |
247 | ||
248 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
249 | while ($dao->fetch()) { | |
250 | $email = array('id' => $dao->id, | |
251 | 'contact_id' => $dao->contact_id, | |
252 | 'location_type' => empty($dao->location_type) ? NULL : $dao->location_type, | |
253 | 'email' => $dao->email, | |
254 | ); | |
255 | appendValue($values, $dao->id, 'email', $email); | |
256 | } | |
257 | $dao->free(); | |
258 | } | |
259 | ||
a1a55b61 EM |
260 | /** |
261 | * @param $contactIDs | |
262 | * @param $values | |
263 | */ | |
6a488035 TO |
264 | function getAddressInfo(&$contactIDs, &$values) { |
265 | $ids = implode(',', $contactIDs); | |
266 | ||
267 | $sql = " | |
268 | SELECT a.id as id, | |
269 | c.id as contact_id, l.name as location_type, | |
270 | a.street_address, a.supplemental_address_1, a.supplemental_address_2, | |
207f62c6 | 271 | a.supplemental_address_3, |
a1a55b61 | 272 | a.city, a.postal_code, |
6a488035 TO |
273 | s.name as state, co.name as country |
274 | FROM civicrm_contact c | |
275 | INNER JOIN civicrm_address a ON a.contact_id = c.id | |
276 | LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id | |
277 | LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id | |
278 | LEFT JOIN civicrm_country co ON a.country_id = co.id | |
279 | WHERE c.id IN ( $ids ) | |
280 | "; | |
281 | ||
282 | $fields = array('id', 'contact_id', | |
283 | 'location_type', 'street_address', 'supplemental_address_1', | |
207f62c6 | 284 | 'supplemental_address_2', 'supplemental_address_3', 'city', 'postal_code', |
6a488035 TO |
285 | 'state', 'country', |
286 | ); | |
287 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
288 | while ($dao->fetch()) { | |
289 | $address = array(); | |
290 | foreach ($fields as $fld) { | |
291 | if (empty($dao->$fld)) { | |
292 | $address[$fld] = NULL; | |
293 | } | |
294 | else { | |
295 | $address[$fld] = $dao->$fld; | |
296 | } | |
297 | } | |
298 | appendValue($values, $dao->id, 'address', $address); | |
299 | } | |
300 | $dao->free(); | |
301 | } | |
302 | ||
a1a55b61 EM |
303 | /** |
304 | * @param $contactIDs | |
305 | * @param $values | |
306 | * @param $allContactIDs | |
307 | * @param $additionalContacts | |
308 | */ | |
6a488035 TO |
309 | function getRelationshipInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) { |
310 | // handle relationships only once | |
311 | static $_relationshipsHandled = array(); | |
312 | ||
313 | $ids = implode(',', $contactIDs); | |
314 | ||
315 | $sql = "( | |
316 | SELECT r.* | |
317 | FROM civicrm_relationship r | |
318 | WHERE r.contact_id_a IN ( $ids ) | |
319 | ) UNION ( | |
320 | SELECT r.* | |
321 | FROM civicrm_relationship r | |
322 | WHERE r.contact_id_b IN ( $ids ) | |
323 | ) | |
324 | "; | |
325 | ||
326 | $relationshipFields = getDBFields('CRM_Contact_DAO_Relationship'); | |
327 | $fields = array_keys($relationshipFields); | |
328 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
329 | while ($dao->fetch()) { | |
330 | if (isset($_relationshipsHandled[$dao->id])) { | |
331 | continue; | |
332 | } | |
333 | $_relationshipsHandled[$dao->id] = $dao->id; | |
334 | ||
335 | $relationship = array(); | |
336 | foreach ($fields as $fld) { | |
337 | if (empty($dao->$fld)) { | |
338 | $relationship[$fld] = NULL; | |
339 | } | |
340 | else { | |
341 | $relationship[$fld] = $dao->$fld; | |
342 | } | |
343 | } | |
344 | appendValue($values, $dao->id, 'relationship', $relationship); | |
345 | ||
346 | addAdditionalContacts(array($dao->contact_id_a, | |
347 | $dao->contact_id_b, | |
348 | ), | |
349 | $allContactIDs, $additionalContacts | |
350 | ); | |
351 | } | |
352 | $dao->free(); | |
353 | } | |
354 | ||
a1a55b61 EM |
355 | /** |
356 | * @param $contactIDs | |
357 | * @param $values | |
358 | * @param $allContactIDs | |
359 | * @param $additionalContacts | |
360 | */ | |
6a488035 TO |
361 | function getActivityInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) { |
362 | static $_activitiesHandled = array(); | |
363 | ||
364 | $ids = implode(',', $contactIDs); | |
365 | ||
366 | $sql = "( | |
367 | SELECT a.* | |
368 | FROM civicrm_activity a | |
369 | INNER JOIN civicrm_activity_assignment aa ON aa.activity_id = a.id | |
370 | WHERE aa.assignee_contact_id IN ( $ids ) | |
371 | AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 ) | |
372 | ) UNION ( | |
373 | SELECT a.* | |
374 | FROM civicrm_activity a | |
375 | INNER JOIN civicrm_activity_target at ON at.activity_id = a.id | |
376 | WHERE at.target_contact_id IN ( $ids ) | |
377 | AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 ) | |
378 | ) | |
379 | "; | |
380 | ||
381 | $activityFields = &getDBFields('CRM_Activity_DAO_Activity'); | |
382 | $fields = array_keys($activityFields); | |
383 | ||
384 | $activityIDs = array(); | |
385 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
386 | while ($dao->fetch()) { | |
387 | if (isset($_activitiesHandled[$dao->id])) { | |
388 | continue; | |
389 | } | |
390 | $_activitiesHandled[$dao->id] = $dao->id; | |
391 | $activityIDs[] = $dao->id; | |
392 | ||
393 | $activity = array(); | |
394 | foreach ($fields as $fld) { | |
395 | if (empty($dao->$fld)) { | |
396 | $activity[$fld] = NULL; | |
397 | } | |
398 | else { | |
399 | $activity[$fld] = $dao->$fld; | |
400 | } | |
401 | } | |
402 | ||
403 | appendValue($values, $dao->id, 'activity', $activity); | |
404 | addAdditionalContacts(array($dao->source_contact_id), | |
405 | $allContactIDs, $additionalContacts | |
406 | ); | |
407 | } | |
408 | $dao->free(); | |
409 | ||
410 | if (empty($activityIDs)) { | |
411 | return; | |
412 | } | |
413 | ||
414 | $activityIDString = implode(",", $activityIDs); | |
415 | ||
416 | // now get all assignee contact ids and target contact ids for this activity | |
417 | $sql = "SELECT * FROM civicrm_activity_assignment WHERE activity_id IN ($activityIDString)"; | |
418 | $aaDAO = &CRM_Core_DAO::executeQuery($sql); | |
419 | $activityContacts = array(); | |
420 | while ($aaDAO->fetch()) { | |
421 | $activityAssignee = array('id' => $aaDAO->id, | |
422 | 'assignee_contact_id' => $aaDAO->assignee_contact_id, | |
423 | 'activity_id' => $aaDAO->activity_id, | |
424 | ); | |
425 | appendValue($values, $aaDAO->id, 'activity_assignment', $activityAssignee); | |
426 | $activityContacts[] = $aaDAO->assignee_contact_id; | |
427 | } | |
428 | $aaDAO->free(); | |
429 | ||
430 | $sql = "SELECT * FROM civicrm_activity_target WHERE activity_id IN ($activityIDString)"; | |
431 | $atDAO = &CRM_Core_DAO::executeQuery($sql); | |
432 | while ($atDAO->fetch()) { | |
433 | $activityTarget = array('id' => $atDAO->id, | |
434 | 'target_contact_id' => $atDAO->target_contact_id, | |
435 | 'activity_id' => $atDAO->activity_id, | |
436 | ); | |
437 | appendValue($values, $atDAO->id, 'activity_target', $activityTarget); | |
438 | $activityContacts[] = $atDAO->target_contact_id; | |
439 | } | |
440 | $atDAO->free(); | |
441 | ||
442 | addAdditionalContacts($activityContacts, $allContactIDs, $additionalContacts); | |
443 | } | |
444 | ||
a1a55b61 EM |
445 | /** |
446 | * @param $values | |
447 | * @param $id | |
448 | * @param $name | |
449 | * @param $value | |
450 | * @param bool $ignored | |
451 | */ | |
6a488035 TO |
452 | function appendValue(&$values, $id, $name, $value, $ignored = FALSE) { |
453 | if (empty($value)) { | |
454 | return; | |
455 | } | |
456 | ||
457 | if (!isset($values[$name])) { | |
458 | $values[$name] = array(); | |
459 | $values[$name][] = array_keys($value); | |
460 | } | |
461 | $values[$name][] = array_values($value); | |
462 | } | |
463 | ||
a1a55b61 | 464 | /** |
c490a46a | 465 | * @param string $daoName |
a1a55b61 EM |
466 | * |
467 | * @return mixed | |
468 | */ | |
6a488035 TO |
469 | function getDBFields($daoName) { |
470 | static $_fieldsRetrieved = array(); | |
471 | ||
472 | if (!isset($_fieldsRetrieved[$daoName])) { | |
473 | $_fieldsRetrieved[$daoName] = array(); | |
474 | $daoFile = str_replace('_', | |
475 | DIRECTORY_SEPARATOR, | |
476 | $daoName | |
477 | ) . '.php'; | |
478 | include_once ($daoFile); | |
479 | ||
480 | $daoFields = &$daoName::fields(); | |
481 | require_once 'CRM/Utils/Array.php'; | |
482 | ||
483 | foreach ($daoFields as $key => & $value) { | |
484 | $_fieldsRetrieved[$daoName][$value['name']] = array('uniqueName' => $key, | |
485 | 'type' => $value['type'], | |
486 | 'title' => CRM_Utils_Array::value('title', $value, NULL), | |
487 | ); | |
488 | } | |
489 | } | |
490 | return $_fieldsRetrieved[$daoName]; | |
491 | } | |
492 | ||
a1a55b61 EM |
493 | /** |
494 | * @param $contactIDs | |
495 | * @param $allContactIDs | |
496 | * @param $additionalContacts | |
497 | */ | |
6a488035 TO |
498 | function addAdditionalContacts($contactIDs, &$allContactIDs, &$additionalContacts) { |
499 | foreach ($contactIDs as $cid) { | |
500 | if ($cid && | |
501 | !isset($allContactIDs[$cid]) && | |
502 | !isset($additionalContacts[$cid]) | |
503 | ) { | |
504 | $additionalContacts[$cid] = $cid; | |
505 | } | |
506 | } | |
507 | } | |
508 | ||
a1a55b61 EM |
509 | /** |
510 | * @param $values | |
511 | * @param $contactIDs | |
512 | * @param $allContactIDs | |
513 | */ | |
6a488035 TO |
514 | function run(&$values, &$contactIDs, &$allContactIDs) { |
515 | $chunks = &splitContactIDs($contactIDs); | |
516 | ||
517 | $additionalContactIDs = array(); | |
518 | ||
519 | foreach ($chunks as $chunk) { | |
520 | getValues($chunk, $values, $allContactIDs, $additionalContactIDs); | |
521 | } | |
522 | ||
523 | if (!empty($additionalContactIDs)) { | |
524 | $allContactIDs = $allContactIDs + $additionalContactIDs; | |
525 | run($values, $additionalContactIDs, $allContactIDs); | |
526 | } | |
527 | } | |
528 | ||
529 | $config = &CRM_Core_Config::singleton(); | |
530 | $config->userFramework = 'Soap'; | |
531 | $config->userFrameworkClass = 'CRM_Utils_System_Soap'; | |
532 | $config->userHookClass = 'CRM_Utils_Hook_Soap'; | |
533 | ||
534 | $sql = " | |
a1a55b61 | 535 | SELECT id |
6a488035 TO |
536 | FROM civicrm_contact |
537 | LIMIT 10 | |
538 | "; | |
539 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
540 | ||
541 | ||
542 | $contactIDs = array(); | |
543 | while ($dao->fetch()) { | |
544 | $contactIDs[$dao->id] = $dao->id; | |
545 | } | |
546 | ||
547 | $values = array(); | |
548 | run($values, $contactIDs, $contactIDs); | |
549 | ||
550 | $json = json_encode($values); | |
551 | echo $json; | |
552 | // print_r( json_decode( $json ) ); | |
553 |