3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * Create a xml file for a set of contact ID's in a format digestible
17 require_once '../../../civicrm.settings.php';
18 require_once 'CRM/Core/Config.php';
20 define('CHUNK_SIZE', 128);
23 * Split a large array of contactIDs into more manageable smaller chunks
27 function &splitContactIDs(&$contactIDs) {
28 // contactIDs could be a real large array, so we split it up into
29 // smaller chunks and then general xml for each chunk
32 $chunks[$current] = [];
35 foreach ($contactIDs as $k => $v) {
36 $chunks[$current][$k] = $v;
39 if ($count == CHUNK_SIZE
) {
41 $chunks[$current] = [];
46 if (empty($chunks[$current])) {
47 unset($chunks[$current]);
54 * Given a set of contact IDs get the values
57 * @param $allContactIDs
58 * @param $addditionalContactIDs
61 function getValues(&$contactIDs, &$values, &$allContactIDs, &$addditionalContactIDs) {
64 getContactInfo($contactIDs, $values);
65 getAddressInfo($contactIDs, $values);
66 getPhoneInfo($contactIDs, $values);
67 getEmailInfo($contactIDs, $values);
68 getNoteInfo($contactIDs, $values);
70 getRelationshipInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs);
72 getActivityInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs);
74 // got to do groups, tags
76 // got to do meta data
87 * @param null $additionalWhereCond
90 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields,
91 $whereField, $additionalWhereCond = NULL,
94 $selectString = implode(',', array_keys($fields));
95 $idString = implode(',', $contactIDs);
98 SELECT $selectString, $whereField as contact_id
100 WHERE $whereField IN ( $idString )
103 if ($additionalWhereCond) {
104 $sql .= " AND $additionalWhereCond";
107 $dao = &CRM_Core_DAO
::executeQuery($sql);
108 while ($dao->fetch()) {
110 foreach ($fields as $fld => $name) {
111 $name = $name ?
$name : $fld;
112 if (empty($dao->$fld)) {
113 $contact[$name] = NULL;
116 $contact[$name] = $dao->$fld;
119 appendValue($values, $dao->contact_id
, 'contact', $contact, $flat);
127 function getContactInfo(&$contactIDs, &$values) {
131 'display_name' => NULL,
132 'contact_type' => NULL,
133 'legal_identifier' => NULL,
134 'external_identifier' => NULL,
135 'first_name' => NULL,
137 'middle_name' => NULL,
138 'household_name' => NULL,
139 'organization_name' => NULL,
140 'legal_name' => NULL,
143 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id', NULL, TRUE);
150 function getNoteInfo(&$contactIDs, &$values) {
151 $ids = implode(',', $contactIDs);
156 entity_id as contact_id,
157 note as note, subject as subject
159 WHERE entity_id IN ( $ids )
160 AND entity_table = 'civicrm_contact'
163 $dao = &CRM_Core_DAO
::executeQuery($sql);
164 while ($dao->fetch()) {
167 'contact_id' => $dao->contact_id
,
168 'subject' => empty($dao->subject
) ?
NULL : $dao->subject
,
169 'note' => empty($dao->note
) ?
NULL : $dao->note
,
172 appendValue($values, $dao->id
, 'note', $note);
180 function getPhoneInfo(&$contactIDs, &$values) {
181 $ids = implode(',', $contactIDs);
187 l.name as location_type,
189 v.label as phone_type
190 FROM civicrm_contact c
191 INNER JOIN civicrm_phone p ON p.contact_id = c.id
192 LEFT JOIN civicrm_location_type l ON p.location_type_id = l.id
193 LEFT JOIN civicrm_option_group g ON g.name = 'phone_type'
194 LEFT JOIN civicrm_option_value v ON v.option_group_id = g.id AND p.phone_type_id = v.value
195 WHERE c.id IN ( $ids )
196 AND p.phone IS NOT NULL
199 $dao = &CRM_Core_DAO
::executeQuery($sql);
200 while ($dao->fetch()) {
203 'contact_id' => $dao->contact_id
,
204 'location_type' => empty($dao->location_type
) ?
NULL : $dao->location_type
,
205 'phone' => $dao->phone
,
206 'phone_type' => empty($dao->phone_type
) ?
NULL : $dao->phone_type
,
209 appendValue($values, $dao->id
, 'phone', $phone);
217 function getEmailInfo(&$contactIDs, &$values) {
218 $ids = implode(',', $contactIDs);
224 l.name as location_type,
226 FROM civicrm_contact c
227 INNER JOIN civicrm_email e ON e.contact_id = c.id
228 LEFT JOIN civicrm_location_type l ON e.location_type_id = l.id
229 WHERE c.id IN ( $ids )
230 AND e.email IS NOT NULL
233 $dao = &CRM_Core_DAO
::executeQuery($sql);
234 while ($dao->fetch()) {
237 'contact_id' => $dao->contact_id
,
238 'location_type' => empty($dao->location_type
) ?
NULL : $dao->location_type
,
239 'email' => $dao->email
,
241 appendValue($values, $dao->id
, 'email', $email);
249 function getAddressInfo(&$contactIDs, &$values) {
250 $ids = implode(',', $contactIDs);
254 c.id as contact_id, l.name as location_type,
255 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
256 a.supplemental_address_3,
257 a.city, a.postal_code,
258 s.name as state, co.name as country
259 FROM civicrm_contact c
260 INNER JOIN civicrm_address a ON a.contact_id = c.id
261 LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id
262 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
263 LEFT JOIN civicrm_country co ON a.country_id = co.id
264 WHERE c.id IN ( $ids )
272 'supplemental_address_1',
273 'supplemental_address_2',
274 'supplemental_address_3',
280 $dao = &CRM_Core_DAO
::executeQuery($sql);
281 while ($dao->fetch()) {
283 foreach ($fields as $fld) {
284 if (empty($dao->$fld)) {
285 $address[$fld] = NULL;
288 $address[$fld] = $dao->$fld;
291 appendValue($values, $dao->id
, 'address', $address);
298 * @param $allContactIDs
299 * @param $additionalContacts
301 function getRelationshipInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
302 // handle relationships only once
303 static $_relationshipsHandled = [];
305 $ids = implode(',', $contactIDs);
309 FROM civicrm_relationship r
310 WHERE r.contact_id_a IN ( $ids )
313 FROM civicrm_relationship r
314 WHERE r.contact_id_b IN ( $ids )
318 $relationshipFields = getDBFields('CRM_Contact_DAO_Relationship');
319 $fields = array_keys($relationshipFields);
320 $dao = &CRM_Core_DAO
::executeQuery($sql);
321 while ($dao->fetch()) {
322 if (isset($_relationshipsHandled[$dao->id
])) {
325 $_relationshipsHandled[$dao->id
] = $dao->id
;
328 foreach ($fields as $fld) {
329 if (empty($dao->$fld)) {
330 $relationship[$fld] = NULL;
333 $relationship[$fld] = $dao->$fld;
336 appendValue($values, $dao->id
, 'relationship', $relationship);
338 addAdditionalContacts([
342 $allContactIDs, $additionalContacts
350 * @param $allContactIDs
351 * @param $additionalContacts
353 function getActivityInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
354 static $_activitiesHandled = [];
356 $ids = implode(',', $contactIDs);
360 FROM civicrm_activity a
361 INNER JOIN civicrm_activity_assignment aa ON aa.activity_id = a.id
362 WHERE aa.assignee_contact_id IN ( $ids )
363 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
366 FROM civicrm_activity a
367 INNER JOIN civicrm_activity_target at ON at.activity_id = a.id
368 WHERE at.target_contact_id IN ( $ids )
369 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
373 $activityFields = &getDBFields('CRM_Activity_DAO_Activity');
374 $fields = array_keys($activityFields);
377 $dao = &CRM_Core_DAO
::executeQuery($sql);
378 while ($dao->fetch()) {
379 if (isset($_activitiesHandled[$dao->id
])) {
382 $_activitiesHandled[$dao->id
] = $dao->id
;
383 $activityIDs[] = $dao->id
;
386 foreach ($fields as $fld) {
387 if (empty($dao->$fld)) {
388 $activity[$fld] = NULL;
391 $activity[$fld] = $dao->$fld;
395 appendValue($values, $dao->id
, 'activity', $activity);
396 addAdditionalContacts([$dao->source_contact_id
],
397 $allContactIDs, $additionalContacts
401 if (empty($activityIDs)) {
405 $activityIDString = implode(",", $activityIDs);
407 // now get all assignee contact ids and target contact ids for this activity
408 $sql = "SELECT * FROM civicrm_activity_assignment WHERE activity_id IN ($activityIDString)";
409 $aaDAO = &CRM_Core_DAO
::executeQuery($sql);
410 $activityContacts = [];
411 while ($aaDAO->fetch()) {
412 $activityAssignee = [
414 'assignee_contact_id' => $aaDAO->assignee_contact_id
,
415 'activity_id' => $aaDAO->activity_id
,
417 appendValue($values, $aaDAO->id
, 'activity_assignment', $activityAssignee);
418 $activityContacts[] = $aaDAO->assignee_contact_id
;
421 $sql = "SELECT * FROM civicrm_activity_target WHERE activity_id IN ($activityIDString)";
422 $atDAO = &CRM_Core_DAO
::executeQuery($sql);
423 while ($atDAO->fetch()) {
426 'target_contact_id' => $atDAO->target_contact_id
,
427 'activity_id' => $atDAO->activity_id
,
429 appendValue($values, $atDAO->id
, 'activity_target', $activityTarget);
430 $activityContacts[] = $atDAO->target_contact_id
;
433 addAdditionalContacts($activityContacts, $allContactIDs, $additionalContacts);
441 * @param bool $ignored
443 function appendValue(&$values, $id, $name, $value, $ignored = FALSE) {
448 if (!isset($values[$name])) {
450 $values[$name][] = array_keys($value);
452 $values[$name][] = array_values($value);
456 * @param string $daoName
460 function getDBFields($daoName) {
461 static $_fieldsRetrieved = [];
463 if (!isset($_fieldsRetrieved[$daoName])) {
464 $_fieldsRetrieved[$daoName] = [];
465 $daoFile = str_replace('_',
469 include_once($daoFile);
471 $daoFields = &$daoName::fields();
472 require_once 'CRM/Utils/Array.php';
474 foreach ($daoFields as $key => & $value) {
475 $_fieldsRetrieved[$daoName][$value['name']] = [
476 'uniqueName' => $key,
477 'type' => $value['type'],
478 'title' => CRM_Utils_Array
::value('title', $value, NULL),
482 return $_fieldsRetrieved[$daoName];
487 * @param $allContactIDs
488 * @param $additionalContacts
490 function addAdditionalContacts($contactIDs, &$allContactIDs, &$additionalContacts) {
491 foreach ($contactIDs as $cid) {
493 !isset($allContactIDs[$cid]) &&
494 !isset($additionalContacts[$cid])
496 $additionalContacts[$cid] = $cid;
504 * @param $allContactIDs
506 function run(&$values, &$contactIDs, &$allContactIDs) {
507 $chunks = &splitContactIDs($contactIDs);
509 $additionalContactIDs = [];
511 foreach ($chunks as $chunk) {
512 getValues($chunk, $values, $allContactIDs, $additionalContactIDs);
515 if (!empty($additionalContactIDs)) {
516 $allContactIDs = $allContactIDs +
$additionalContactIDs;
517 run($values, $additionalContactIDs, $allContactIDs);
521 $config = CRM_Core_Config
::singleton();
522 $config->userFramework
= 'Soap';
523 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
524 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
531 $dao = &CRM_Core_DAO
::executeQuery($sql);
535 while ($dao->fetch()) {
536 $contactIDs[$dao->id
] = $dao->id
;
540 run($values, $contactIDs, $contactIDs);
542 $json = json_encode($values);
544 // print_r( json_decode( $json ) );