3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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.city, a.postal_code,
272 s.name as state, co.name as country
273 FROM civicrm_contact c
274 INNER JOIN civicrm_address a ON a.contact_id = c.id
275 LEFT JOIN civicrm_location_type l ON a.location_type_id = l.id
276 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
277 LEFT JOIN civicrm_country co ON a.country_id = co.id
278 WHERE c.id IN ( $ids )
281 $fields = array('id', 'contact_id',
282 'location_type', 'street_address', 'supplemental_address_1',
283 'supplemental_address_2', 'city', 'postal_code',
286 $dao = &CRM_Core_DAO
::executeQuery($sql);
287 while ($dao->fetch()) {
289 foreach ($fields as $fld) {
290 if (empty($dao->$fld)) {
291 $address[$fld] = NULL;
294 $address[$fld] = $dao->$fld;
297 appendValue($values, $dao->id
, 'address', $address);
305 * @param $allContactIDs
306 * @param $additionalContacts
308 function getRelationshipInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
309 // handle relationships only once
310 static $_relationshipsHandled = array();
312 $ids = implode(',', $contactIDs);
316 FROM civicrm_relationship r
317 WHERE r.contact_id_a IN ( $ids )
320 FROM civicrm_relationship r
321 WHERE r.contact_id_b IN ( $ids )
325 $relationshipFields = getDBFields('CRM_Contact_DAO_Relationship');
326 $fields = array_keys($relationshipFields);
327 $dao = &CRM_Core_DAO
::executeQuery($sql);
328 while ($dao->fetch()) {
329 if (isset($_relationshipsHandled[$dao->id
])) {
332 $_relationshipsHandled[$dao->id
] = $dao->id
;
334 $relationship = array();
335 foreach ($fields as $fld) {
336 if (empty($dao->$fld)) {
337 $relationship[$fld] = NULL;
340 $relationship[$fld] = $dao->$fld;
343 appendValue($values, $dao->id
, 'relationship', $relationship);
345 addAdditionalContacts(array($dao->contact_id_a
,
348 $allContactIDs, $additionalContacts
357 * @param $allContactIDs
358 * @param $additionalContacts
360 function getActivityInfo(&$contactIDs, &$values, &$allContactIDs, &$additionalContacts) {
361 static $_activitiesHandled = array();
363 $ids = implode(',', $contactIDs);
367 FROM civicrm_activity a
368 INNER JOIN civicrm_activity_assignment aa ON aa.activity_id = a.id
369 WHERE aa.assignee_contact_id IN ( $ids )
370 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
373 FROM civicrm_activity a
374 INNER JOIN civicrm_activity_target at ON at.activity_id = a.id
375 WHERE at.target_contact_id IN ( $ids )
376 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
380 $activityFields = &getDBFields('CRM_Activity_DAO_Activity');
381 $fields = array_keys($activityFields);
383 $activityIDs = array();
384 $dao = &CRM_Core_DAO
::executeQuery($sql);
385 while ($dao->fetch()) {
386 if (isset($_activitiesHandled[$dao->id
])) {
389 $_activitiesHandled[$dao->id
] = $dao->id
;
390 $activityIDs[] = $dao->id
;
393 foreach ($fields as $fld) {
394 if (empty($dao->$fld)) {
395 $activity[$fld] = NULL;
398 $activity[$fld] = $dao->$fld;
402 appendValue($values, $dao->id
, 'activity', $activity);
403 addAdditionalContacts(array($dao->source_contact_id
),
404 $allContactIDs, $additionalContacts
409 if (empty($activityIDs)) {
413 $activityIDString = implode(",", $activityIDs);
415 // now get all assignee contact ids and target contact ids for this activity
416 $sql = "SELECT * FROM civicrm_activity_assignment WHERE activity_id IN ($activityIDString)";
417 $aaDAO = &CRM_Core_DAO
::executeQuery($sql);
418 $activityContacts = array();
419 while ($aaDAO->fetch()) {
420 $activityAssignee = array('id' => $aaDAO->id
,
421 'assignee_contact_id' => $aaDAO->assignee_contact_id
,
422 'activity_id' => $aaDAO->activity_id
,
424 appendValue($values, $aaDAO->id
, 'activity_assignment', $activityAssignee);
425 $activityContacts[] = $aaDAO->assignee_contact_id
;
429 $sql = "SELECT * FROM civicrm_activity_target WHERE activity_id IN ($activityIDString)";
430 $atDAO = &CRM_Core_DAO
::executeQuery($sql);
431 while ($atDAO->fetch()) {
432 $activityTarget = array('id' => $atDAO->id
,
433 'target_contact_id' => $atDAO->target_contact_id
,
434 'activity_id' => $atDAO->activity_id
,
436 appendValue($values, $atDAO->id
, 'activity_target', $activityTarget);
437 $activityContacts[] = $atDAO->target_contact_id
;
441 addAdditionalContacts($activityContacts, $allContactIDs, $additionalContacts);
449 * @param bool $ignored
451 function appendValue(&$values, $id, $name, $value, $ignored = FALSE) {
456 if (!isset($values[$name])) {
457 $values[$name] = array();
458 $values[$name][] = array_keys($value);
460 $values[$name][] = array_values($value);
464 * @param string $daoName
468 function getDBFields($daoName) {
469 static $_fieldsRetrieved = array();
471 if (!isset($_fieldsRetrieved[$daoName])) {
472 $_fieldsRetrieved[$daoName] = array();
473 $daoFile = str_replace('_',
477 include_once ($daoFile);
479 $daoFields = &$daoName::fields();
480 require_once 'CRM/Utils/Array.php';
482 foreach ($daoFields as $key => & $value) {
483 $_fieldsRetrieved[$daoName][$value['name']] = array('uniqueName' => $key,
484 'type' => $value['type'],
485 'title' => CRM_Utils_Array
::value('title', $value, NULL),
489 return $_fieldsRetrieved[$daoName];
494 * @param $allContactIDs
495 * @param $additionalContacts
497 function addAdditionalContacts($contactIDs, &$allContactIDs, &$additionalContacts) {
498 foreach ($contactIDs as $cid) {
500 !isset($allContactIDs[$cid]) &&
501 !isset($additionalContacts[$cid])
503 $additionalContacts[$cid] = $cid;
511 * @param $allContactIDs
513 function run(&$values, &$contactIDs, &$allContactIDs) {
514 $chunks = &splitContactIDs($contactIDs);
516 $additionalContactIDs = array();
518 foreach ($chunks as $chunk) {
519 getValues($chunk, $values, $allContactIDs, $additionalContactIDs);
522 if (!empty($additionalContactIDs)) {
523 $allContactIDs = $allContactIDs +
$additionalContactIDs;
524 run($values, $additionalContactIDs, $allContactIDs);
528 $config = &CRM_Core_Config
::singleton();
529 $config->userFramework
= 'Soap';
530 $config->userFrameworkClass
= 'CRM_Utils_System_Soap';
531 $config->userHookClass
= 'CRM_Utils_Hook_Soap';
538 $dao = &CRM_Core_DAO
::executeQuery($sql);
541 $contactIDs = array();
542 while ($dao->fetch()) {
543 $contactIDs[$dao->id
] = $dao->id
;
547 run($values, $contactIDs, $contactIDs);
549 $json = json_encode($values);
551 // print_r( json_decode( $json ) );