3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 * Create a xml file for a set of contact ID's in a format digestible
32 require_once '../../../civicrm.settings.php';
33 require_once 'CRM/Core/Config.php';
35 define('CHUNK_SIZE', 128);
38 * Split a large array of contactIDs into more manageable smaller chunks
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
47 $chunks[$current] = array();
50 foreach ($contactIDs as $k => $v) {
51 $chunks[$current][$k] = $v;
54 if ($count == CHUNK_SIZE
) {
56 $chunks[$current] = array();
61 if (empty($chunks[$current])) {
62 unset($chunks[$current]);
69 * Given a set of contact IDs get the values
72 * @param $allContactIDs
73 * @param $addditionalContactIDs
76 function getValues(&$contactIDs, &$values, &$allContactIDs, &$addditionalContactIDs) {
79 getContactInfo($contactIDs, $values);
80 getAddressInfo($contactIDs, $values);
81 getPhoneInfo($contactIDs, $values);
82 getEmailInfo($contactIDs, $values);
83 getNoteInfo($contactIDs, $values);
85 getRelationshipInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs);
87 getActivityInfo($contactIDs, $values, $allContactIDs, $addditionalContactIDs);
89 // got to do groups, tags
91 // got to do meta data
102 * @param null $additionalWhereCond
105 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields,
106 $whereField, $additionalWhereCond = NULL,
109 $selectString = implode(',', array_keys($fields));
110 $idString = implode(',', $contactIDs);
113 SELECT $selectString, $whereField as contact_id
115 WHERE $whereField IN ( $idString )
118 if ($additionalWhereCond) {
119 $sql .= " AND $additionalWhereCond";
122 $dao = &CRM_Core_DAO
::executeQuery($sql);
123 while ($dao->fetch()) {
125 foreach ($fields as $fld => $name) {
126 $name = $name ?
$name : $fld;
127 if (empty($dao->$fld)) {
128 $contact[$name] = NULL;
131 $contact[$name] = $dao->$fld;
134 appendValue($values, $dao->contact_id
, 'contact', $contact, $flat);
143 function getContactInfo(&$contactIDs, &$values) {
144 $fields = array('id' => NULL,
146 'display_name' => NULL,
147 'contact_type' => NULL,
148 'legal_identifier' => NULL,
149 'external_identifier' => NULL,
150 'first_name' => NULL,
152 'middle_name' => NULL,
153 'household_name' => NULL,
154 'organization_name' => NULL,
155 'legal_name' => NULL,
158 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id', NULL, TRUE);
165 function getNoteInfo(&$contactIDs, &$values) {
166 $ids = implode(',', $contactIDs);
171 entity_id as contact_id,
172 note as note, subject as subject
174 WHERE entity_id IN ( $ids )
175 AND entity_table = 'civicrm_contact'
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
,
186 appendValue($values, $dao->id
, 'note', $note);
195 function getPhoneInfo(&$contactIDs, &$values) {
196 $ids = implode(',', $contactIDs);
202 l.name as location_type,
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
210 WHERE c.id IN ( $ids )
211 AND p.phone IS NOT NULL
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
,
223 appendValue($values, $dao->id
, 'phone', $phone);
232 function getEmailInfo(&$contactIDs, &$values) {
233 $ids = implode(',', $contactIDs);
239 l.name as location_type,
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
244 WHERE c.id IN ( $ids )
245 AND e.email IS NOT NULL
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
,
255 appendValue($values, $dao->id
, 'email', $email);
264 function getAddressInfo(&$contactIDs, &$values) {
265 $ids = implode(',', $contactIDs);
269 c.id as contact_id, l.name as location_type,
270 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
271 a.supplemental_address_3,
272 a.city, a.postal_code,
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 )
282 $fields = array('id', 'contact_id',
283 'location_type', 'street_address', 'supplemental_address_1',
284 'supplemental_address_2', 'supplemental_address_3', 'city', 'postal_code',
287 $dao = &CRM_Core_DAO
::executeQuery($sql);
288 while ($dao->fetch()) {
290 foreach ($fields as $fld) {
291 if (empty($dao->$fld)) {
292 $address[$fld] = NULL;
295 $address[$fld] = $dao->$fld;
298 appendValue($values, $dao->id
, 'address', $address);
306 * @param $allContactIDs
307 * @param $additionalContacts
309 function getRelationshipInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
310 // handle relationships only once
311 static $_relationshipsHandled = array();
313 $ids = implode(',', $contactIDs);
317 FROM civicrm_relationship r
318 WHERE r.contact_id_a IN ( $ids )
321 FROM civicrm_relationship r
322 WHERE r.contact_id_b IN ( $ids )
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
])) {
333 $_relationshipsHandled[$dao->id
] = $dao->id
;
335 $relationship = array();
336 foreach ($fields as $fld) {
337 if (empty($dao->$fld)) {
338 $relationship[$fld] = NULL;
341 $relationship[$fld] = $dao->$fld;
344 appendValue($values, $dao->id
, 'relationship', $relationship);
346 addAdditionalContacts(array($dao->contact_id_a
,
349 $allContactIDs, $additionalContacts
358 * @param $allContactIDs
359 * @param $additionalContacts
361 function getActivityInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
362 static $_activitiesHandled = array();
364 $ids = implode(',', $contactIDs);
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 )
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 )
381 $activityFields = &getDBFields('CRM_Activity_DAO_Activity');
382 $fields = array_keys($activityFields);
384 $activityIDs = array();
385 $dao = &CRM_Core_DAO
::executeQuery($sql);
386 while ($dao->fetch()) {
387 if (isset($_activitiesHandled[$dao->id
])) {
390 $_activitiesHandled[$dao->id
] = $dao->id
;
391 $activityIDs[] = $dao->id
;
394 foreach ($fields as $fld) {
395 if (empty($dao->$fld)) {
396 $activity[$fld] = NULL;
399 $activity[$fld] = $dao->$fld;
403 appendValue($values, $dao->id
, 'activity', $activity);
404 addAdditionalContacts(array($dao->source_contact_id
),
405 $allContactIDs, $additionalContacts
410 if (empty($activityIDs)) {
414 $activityIDString = implode(",", $activityIDs);
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
,
425 appendValue($values, $aaDAO->id
, 'activity_assignment', $activityAssignee);
426 $activityContacts[] = $aaDAO->assignee_contact_id
;
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
,
437 appendValue($values, $atDAO->id
, 'activity_target', $activityTarget);
438 $activityContacts[] = $atDAO->target_contact_id
;
442 addAdditionalContacts($activityContacts, $allContactIDs, $additionalContacts);
450 * @param bool $ignored
452 function appendValue(&$values, $id, $name, $value, $ignored = FALSE) {
457 if (!isset($values[$name])) {
458 $values[$name] = array();
459 $values[$name][] = array_keys($value);
461 $values[$name][] = array_values($value);
465 * @param string $daoName
469 function getDBFields($daoName) {
470 static $_fieldsRetrieved = array();
472 if (!isset($_fieldsRetrieved[$daoName])) {
473 $_fieldsRetrieved[$daoName] = array();
474 $daoFile = str_replace('_',
478 include_once ($daoFile);
480 $daoFields = &$daoName::fields();
481 require_once 'CRM/Utils/Array.php';
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),
490 return $_fieldsRetrieved[$daoName];
495 * @param $allContactIDs
496 * @param $additionalContacts
498 function addAdditionalContacts($contactIDs, &$allContactIDs, &$additionalContacts) {
499 foreach ($contactIDs as $cid) {
501 !isset($allContactIDs[$cid]) &&
502 !isset($additionalContacts[$cid])
504 $additionalContacts[$cid] = $cid;
512 * @param $allContactIDs
514 function run(&$values, &$contactIDs, &$allContactIDs) {
515 $chunks = &splitContactIDs($contactIDs);
517 $additionalContactIDs = array();
519 foreach ($chunks as $chunk) {
520 getValues($chunk, $values, $allContactIDs, $additionalContactIDs);
523 if (!empty($additionalContactIDs)) {
524 $allContactIDs = $allContactIDs +
$additionalContactIDs;
525 run($values, $additionalContactIDs, $allContactIDs);
529 $config = &CRM_Core_Config
::singleton();
530 $config->userFramework
= 'Soap';
531 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
532 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
539 $dao = &CRM_Core_DAO
::executeQuery($sql);
542 $contactIDs = array();
543 while ($dao->fetch()) {
544 $contactIDs[$dao->id
] = $dao->id
;
548 run($values, $contactIDs, $contactIDs);
550 $json = json_encode($values);
552 // print_r( json_decode( $json ) );