3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Utils_Migrate_ExportJSON
{
36 CONST CHUNK_SIZE
= 128;
38 protected $_contactIDs;
40 protected $_allContactIDs;
44 protected $_discoverContacts = FALSE;
46 protected $_renameGroups = 1;
48 protected $_renameTags = 1;
50 protected $_sitePrefix = 'Site 1';
52 function __construct(&$params) {
53 foreach ($params as $name => $value) {
54 $varName = '_' . $name;
55 $this->$varName = $value;
60 * Split a large array of contactIDs into more manageable smaller chunks
62 function &splitContactIDs(&$contactIDs) {
63 // contactIDs could be a real large array, so we split it up into
64 // smaller chunks and then general xml for each chunk
67 $chunks[$current] = array();
70 foreach ($contactIDs as $k => $v) {
71 $chunks[$current][$k] = $v;
74 if ($count == self
::CHUNK_SIZE
) {
76 $chunks[$current] = array();
81 if (empty($chunks[$current])) {
82 unset($chunks[$current]);
89 * Given a set of contact IDs get the values
91 function getValues(&$contactIDs, &$additionalContactIDs) {
93 $this->contact($contactIDs);
94 $this->address($contactIDs);
95 $this->phone($contactIDs);
96 $this->email($contactIDs);
97 $this->im($contactIDs);
98 $this->website($contactIDs);
99 $this->note($contactIDs);
101 $this->group($contactIDs);
102 $this->groupContact($contactIDs);
103 $this->savedSearch($contactIDs);
105 $this->tag($contactIDs);
106 $this->entityTag($contactIDs);
108 $this->relationship($contactIDs, $additionalContactIDs);
109 $this->activity($contactIDs, $additionalContactIDs);
112 function metaData() {
113 $optionGroupVars = array(
114 'prefix_id' => 'individual_prefix',
115 'suffix_id' => 'individual_suffix',
116 'gender_id' => 'gender',
117 'mobile_provider' => 'mobile_provider',
118 'phone_type' => 'phone_type',
119 'activity_type' => 'activity_type',
120 'status_id' => 'activity_status_id',
121 'priority_id' => 'activity_priority_id',
122 'medium_id' => 'encounter_medium',
123 'email_greeting' => 'email_greeting',
124 'postal_greeting' => 'postal_greeting',
125 'addressee_id' => 'addressee',
127 $this->optionGroup($optionGroupVars);
129 $auxilaryTables = array(
130 'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
131 'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
133 $this->auxTable($auxilaryTables);
136 function auxTable($tables) {
137 foreach ($tables as $tableName => $daoName) {
138 $fields = & $this->dbFields($daoName, TRUE);
140 $sql = "SELECT * from $tableName";
141 $this->sql($sql, $tableName, $fields);
145 function optionGroup($optionGroupVars) {
146 $names = array_values($optionGroupVars);
148 foreach ($names as $name) {
151 $nameString = implode(",", $str);
155 FROM civicrm_option_group
156 WHERE name IN ( $nameString )
158 $fields = & $this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
159 $this->sql($sql, 'civicrm_option_group', $fields);
163 FROM civicrm_option_value v
164 INNER JOIN civicrm_option_group g ON v.option_group_id = g.id
165 WHERE g.name IN ( $nameString )
167 $fields = & $this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
168 $this->sql($sql, 'civicrm_option_value', $fields);
171 function table(&$ids,
175 $additionalWhereCond = NULL
181 $idString = implode(',', $ids);
186 WHERE $whereField IN ( $idString )
189 if ($additionalWhereCond) {
190 $sql .= " AND $additionalWhereCond";
193 $this->sql($sql, $tableName, $fields);
196 function sql($sql, $tableName, &$fields) {
197 $dao = & CRM_Core_DAO
::executeQuery($sql);
199 while ($dao->fetch()) {
201 foreach ($fields as $name) {
202 if (empty($dao->$name)) {
203 $value[$name] = NULL;
206 $value[$name] = $dao->$name;
209 $this->appendValue($dao->id
, $tableName, $value);
214 function contact(&$contactIDs) {
215 $fields = & $this->dbFields('CRM_Contact_DAO_Contact', TRUE);
216 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
219 function note(&$contactIDs) {
220 $fields = & $this->dbFields('CRM_Core_DAO_Note', TRUE);
221 $this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
224 function phone(&$contactIDs) {
225 $fields = & $this->dbFields('CRM_Core_DAO_Phone', TRUE);
226 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
229 function email(&$contactIDs) {
230 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
231 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
234 function im(&$contactIDs) {
235 $fields = & $this->dbFields('CRM_Core_DAO_IM', TRUE);
236 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
239 function website(&$contactIDs) {
240 $fields = & $this->dbFields('CRM_Core_DAO_Website', TRUE);
241 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
244 function address(&$contactIDs) {
245 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
246 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
249 function groupContact(&$contactIDs) {
250 $fields = & $this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
251 $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
254 // TODO - support group inheritance
255 // Parent child group ids are encoded in a text string
256 function group(&$contactIDs) {
257 // handle groups only once
258 static $_groupsHandled = array();
260 $ids = implode(',', $contactIDs);
263 SELECT DISTINCT group_id
264 FROM civicrm_group_contact
265 WHERE contact_id IN ( $ids )
267 $dao = CRM_Core_DAO
::executeQuery($sql);
269 while ($dao->fetch()) {
270 if (!isset($_groupsHandled[$dao->group_id
])) {
271 $groupIDs[] = $dao->group_id
;
272 $_groupsHandled[$dao->group_id
] = 1;
276 $fields = & $this->dbFields('CRM_Contact_DAO_Group', TRUE);
277 $this->table($groupIDs, 'civicrm_group', $fields, 'id');
279 $this->savedSearch($groupIDs);
282 // TODO - support search builder and custom saved searches
283 function savedSearch(&$groupIDs) {
284 if (empty($groupIDs)) {
288 $idString = implode(",", $groupIDs);
291 FROM civicrm_saved_search s
292 INNER JOIN civicrm_group g on g.saved_search_id = s.id
293 WHERE g.id IN ( $idString )
296 $fields = & $this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
297 $this->sql($sql, 'civicrm_saved_search', $fields);
300 function entityTag(&$contactIDs) {
301 $fields = & $this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
302 $this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
305 function tag(&$contactIDs) {
306 // handle tags only once
307 static $_tagsHandled = array();
309 $ids = implode(',', $contactIDs);
312 SELECT DISTINCT tag_id
313 FROM civicrm_entity_tag
314 WHERE entity_id IN ( $ids )
315 AND entity_table = 'civicrm_contact'
317 $dao = CRM_Core_DAO
::executeQuery($sql);
319 while ($dao->fetch()) {
320 if (!isset($_tagsHandled[$dao->tag_id
])) {
321 $tagIDs[] = $dao->tag_id
;
322 $_tagsHandled[$dao->tag_id
] = 1;
326 $fields = & $this->dbFields('CRM_Core_DAO_Tag', TRUE);
327 $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
330 function relationship(&$contactIDs, &$additionalContacts) {
331 // handle relationships only once
332 static $_relationshipsHandled = array();
334 $ids = implode(',', $contactIDs);
338 FROM civicrm_relationship r
339 WHERE r.contact_id_a IN ( $ids )
342 FROM civicrm_relationship r
343 WHERE r.contact_id_b IN ( $ids )
347 $fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
348 $dao = & CRM_Core_DAO
::executeQuery($sql);
349 while ($dao->fetch()) {
350 if (isset($_relationshipsHandled[$dao->id
])) {
353 $_relationshipsHandled[$dao->id
] = $dao->id
;
355 $relationship = array();
356 foreach ($fields as $fld) {
357 if (empty($dao->$fld)) {
358 $relationship[$fld] = NULL;
361 $relationship[$fld] = $dao->$fld;
364 $this->appendValue($dao->id
, 'civicrm_relationship', $relationship);
366 $this->addAdditionalContacts(array(
376 function activity(&$contactIDs, &$additionalContacts) {
377 static $_activitiesHandled = array();
378 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
379 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
380 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
382 $ids = implode(',', $contactIDs);
386 FROM civicrm_activity a
387 INNER JOIN civicrm_activity_contact aa ON aa.activity_id = a.id AND aa.record_type_id = $assigneeID
388 WHERE aa.contact_id IN ( $ids )
389 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
392 FROM civicrm_activity a
393 INNER JOIN civicrm_activity_contact at ON at.activity_id = a.id AND at.record_type_id = $targetID
394 WHERE at.contact_id IN ( $ids )
395 AND ( a.activity_type_id != 3 AND a.activity_type_id != 20 )
399 $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE);
401 $activityIDs = array();
402 $dao = & CRM_Core_DAO
::executeQuery($sql);
403 while ($dao->fetch()) {
404 if (isset($_activitiesHandled[$dao->id
])) {
407 $_activitiesHandled[$dao->id
] = $dao->id
;
408 $activityIDs[] = $dao->id
;
411 foreach ($fields as $fld) {
412 if (empty($dao->$fld)) {
413 $activity[$fld] = NULL;
416 $activity[$fld] = $dao->$fld;
420 $this->appendValue($dao->id
, 'civicrm_activity', $activity);
421 $this->addAdditionalContacts(array($dao->source_contact_id
),
427 if (empty($activityIDs)) {
431 $activityIDString = implode(",", $activityIDs);
433 // now get all assignee contact ids and target contact ids for this activity
434 $sql = "SELECT * FROM civicrm_activity_contact WHERE activity_id IN ($activityIDString) AND record_type = 'Assignee'";
435 $aaDAO = &CRM_Core_DAO
::executeQuery($sql);
437 $activityContacts = array();
438 while ($aaDAO->fetch()) {
439 $activityAssignee = array(
441 'assignee_contact_id' => $aaDAO->assignee_contact_id
,
442 'activity_id' => $aaDAO->activity_id
,
444 $this->appendValue($aaDAO->id
, 'civicrm_activity_assignment', $activityAssignee);
445 $activityContacts[] = $aaDAO->assignee_contact_id
;
449 $sql = "SELECT * FROM civicrm_activity_contact WHERE activity_id IN ($activityIDString) AND record_type = 'Target'";
450 $atDAO = &CRM_Core_DAO
::executeQuery($sql);
452 while ($atDAO->fetch()) {
453 $activityTarget = array(
455 'target_contact_id' => $atDAO->target_contact_id
,
456 'activity_id' => $atDAO->activity_id
,
458 $this->appendValue($atDAO->id
, 'civicrm_activity_target', $activityTarget);
459 $activityContacts[] = $atDAO->target_contact_id
;
463 $this->addAdditionalContacts($activityContacts, $additionalContacts);
466 function appendValue($id, $name, $value) {
471 if (!isset($this->_values
[$name])) {
472 $this->_values
[$name] = array();
473 $this->_values
[$name][] = array_keys($value);
475 $this->_values
[$name][] = array_values($value);
478 function dbFields($daoName, $onlyKeys = FALSE) {
479 static $_fieldsRetrieved = array();
481 if (!isset($_fieldsRetrieved[$daoName])) {
482 $_fieldsRetrieved[$daoName] = array();
483 $daoFile = str_replace('_',
487 include_once ($daoFile);
489 $daoFields = & $daoName::fields();
491 foreach ($daoFields as $key => & $value) {
492 $_fieldsRetrieved[$daoName][$value['name']] = array(
493 'uniqueName' => $key,
494 'type' => $value['type'],
495 'title' => CRM_Utils_Array
::value('title', $value, NULL),
501 return array_keys($_fieldsRetrieved[$daoName]);
504 return $_fieldsRetrieved[$daoName];
508 function addAdditionalContacts($contactIDs, &$additionalContacts) {
509 if (!$this->_discoverContacts
) {
513 foreach ($contactIDs as $cid) {
515 !isset($this->_allContactIDs
[$cid]) &&
516 !isset($additionalContacts[$cid])
518 $additionalContacts[$cid] = $cid;
523 function export(&$contactIDs) {
524 $chunks = & $this->splitContactIDs($contactIDs);
526 $additionalContactIDs = array();
528 foreach ($chunks as $chunk) {
529 $this->getValues($chunk, $additionalContactIDs);
532 if (!empty($additionalContactIDs)) {
533 $this->_allContactIDs
= $this->_allContactIDs +
$additionalContactIDs;
534 $this->export($additionalContactIDs);
538 function run($fileName,
539 $lastExportTime = NULL,
540 $discoverContacts = FALSE
542 $this->_discoverContacts
= $discoverContacts;
544 if (!$lastExportTime) {
552 SELECT DISTINCT entity_id
554 WHERE entity_table = 'civicrm_contact'
555 AND modified_date >= $lastExportTime
557 SELECT DISTINCT contact_id
558 FROM civicrm_subscription_history
559 WHERE date >= $lastExportTime
565 $dao = & CRM_Core_DAO
::executeQuery($sql);
567 $contactIDs = array();
568 while ($dao->fetch()) {
569 $contactIDs[$dao->id
] = $dao->id
;
572 $this->_allContactIDs
= $contactIDs;
573 $this->_values
= array();
577 $this->export($contactIDs);
579 $json = json_encode($this->_values
, JSON_NUMERIC_CHECK
);
580 file_put_contents($fileName,
584 // print_r( json_decode( $json ) );