Merge pull request #2081 from eileenmcnaughton/metadata
[civicrm-core.git] / CRM / Utils / Migrate / ExportJSON.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 and the CiviCRM Licensing Exception. |
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 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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35class CRM_Utils_Migrate_ExportJSON {
36 CONST CHUNK_SIZE = 128;
37
38 protected $_contactIDs;
39
40 protected $_allContactIDs;
41
42 protected $_values;
43
44 protected $_discoverContacts = FALSE;
45
46 protected $_renameGroups = 1;
47
48 protected $_renameTags = 1;
49
3302658e
TO
50 protected $_sitePrefix = 'Site 1';
51
52 function __construct(&$params) {
6a488035
TO
53 foreach ($params as $name => $value) {
54 $varName = '_' . $name;
55 $this->$varName = $value;
56 }
57 }
58
59 /**
60 * Split a large array of contactIDs into more manageable smaller chunks
61 */
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
3302658e
TO
65 $chunks = array();
66 $current = 0;
6a488035 67 $chunks[$current] = array();
3302658e 68 $count = 0;
6a488035
TO
69
70 foreach ($contactIDs as $k => $v) {
71 $chunks[$current][$k] = $v;
72 $count++;
73
74 if ($count == self::CHUNK_SIZE) {
75 $current++;
76 $chunks[$current] = array();
77 $count = 0;
78 }
79 }
80
81 if (empty($chunks[$current])) {
82 unset($chunks[$current]);
83 }
84
85 return $chunks;
86 }
87
88 /**
89 * Given a set of contact IDs get the values
90 */
91 function getValues(&$contactIDs, &$additionalContactIDs) {
92
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);
100
101 $this->group($contactIDs);
102 $this->groupContact($contactIDs);
103 $this->savedSearch($contactIDs);
104
105 $this->tag($contactIDs);
106 $this->entityTag($contactIDs);
107
108 $this->relationship($contactIDs, $additionalContactIDs);
109 $this->activity($contactIDs, $additionalContactIDs);
110 }
111
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',
126 );
127 $this->optionGroup($optionGroupVars);
128
129 $auxilaryTables = array(
130 'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
131 'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
132 );
133 $this->auxTable($auxilaryTables);
134 }
135
136 function auxTable($tables) {
137 foreach ($tables as $tableName => $daoName) {
3302658e 138 $fields = & $this->dbFields($daoName, TRUE);
6a488035
TO
139
140 $sql = "SELECT * from $tableName";
141 $this->sql($sql, $tableName, $fields);
142 }
143 }
144
145 function optionGroup($optionGroupVars) {
146 $names = array_values($optionGroupVars);
147 $str = array();
148 foreach ($names as $name) {
149 $str[] = "'$name'";
150 }
151 $nameString = implode(",", $str);
152
153 $sql = "
154SELECT *
155FROM civicrm_option_group
156WHERE name IN ( $nameString )
157";
3302658e 158 $fields = & $this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
6a488035
TO
159 $this->sql($sql, 'civicrm_option_group', $fields);
160
161 $sql = "
162SELECT v.*
163FROM civicrm_option_value v
164INNER JOIN civicrm_option_group g ON v.option_group_id = g.id
165WHERE g.name IN ( $nameString )
166";
3302658e 167 $fields = & $this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
6a488035
TO
168 $this->sql($sql, 'civicrm_option_value', $fields);
169 }
170
171 function table(&$ids,
3302658e
TO
172 $tableName,
173 &$fields,
174 $whereField,
175 $additionalWhereCond = NULL
6a488035
TO
176 ) {
177 if (empty($ids)) {
178 return;
179 }
180
181 $idString = implode(',', $ids);
182
183 $sql = "
184SELECT *
185 FROM $tableName
186 WHERE $whereField IN ( $idString )
187";
188
189 if ($additionalWhereCond) {
190 $sql .= " AND $additionalWhereCond";
191 }
192
193 $this->sql($sql, $tableName, $fields);
194 }
195
196 function sql($sql, $tableName, &$fields) {
3302658e 197 $dao = & CRM_Core_DAO::executeQuery($sql);
6a488035
TO
198
199 while ($dao->fetch()) {
200 $value = array();
201 foreach ($fields as $name) {
202 if (empty($dao->$name)) {
203 $value[$name] = NULL;
204 }
205 else {
206 $value[$name] = $dao->$name;
207 }
208 }
209 $this->appendValue($dao->id, $tableName, $value);
210 }
211 $dao->free();
212 }
213
214 function contact(&$contactIDs) {
3302658e 215 $fields = & $this->dbFields('CRM_Contact_DAO_Contact', TRUE);
6a488035
TO
216 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
217 }
218
219 function note(&$contactIDs) {
3302658e 220 $fields = & $this->dbFields('CRM_Core_DAO_Note', TRUE);
6a488035
TO
221 $this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
222 }
223
224 function phone(&$contactIDs) {
3302658e 225 $fields = & $this->dbFields('CRM_Core_DAO_Phone', TRUE);
6a488035
TO
226 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
227 }
228
229 function email(&$contactIDs) {
3302658e 230 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
6a488035
TO
231 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
232 }
233
234 function im(&$contactIDs) {
3302658e 235 $fields = & $this->dbFields('CRM_Core_DAO_IM', TRUE);
6a488035
TO
236 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
237 }
238
239 function website(&$contactIDs) {
3302658e 240 $fields = & $this->dbFields('CRM_Core_DAO_Website', TRUE);
6a488035
TO
241 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
242 }
243
244 function address(&$contactIDs) {
3302658e 245 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
6a488035
TO
246 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
247 }
248
249 function groupContact(&$contactIDs) {
3302658e 250 $fields = & $this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
6a488035
TO
251 $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
252 }
253
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();
259
260 $ids = implode(',', $contactIDs);
261
262 $sql = "
263SELECT DISTINCT group_id
264FROM civicrm_group_contact
265WHERE contact_id IN ( $ids )
266";
267 $dao = CRM_Core_DAO::executeQuery($sql);
268 $groupIDs = array();
269 while ($dao->fetch()) {
270 if (!isset($_groupsHandled[$dao->group_id])) {
271 $groupIDs[] = $dao->group_id;
272 $_groupsHandled[$dao->group_id] = 1;
273 }
274 }
275
3302658e 276 $fields = & $this->dbFields('CRM_Contact_DAO_Group', TRUE);
6a488035
TO
277 $this->table($groupIDs, 'civicrm_group', $fields, 'id');
278
279 $this->savedSearch($groupIDs);
280 }
281
282 // TODO - support search builder and custom saved searches
283 function savedSearch(&$groupIDs) {
284 if (empty($groupIDs)) {
285 return;
286 }
287
288 $idString = implode(",", $groupIDs);
289 $sql = "
290SELECT s.*
291FROM civicrm_saved_search s
292INNER JOIN civicrm_group g on g.saved_search_id = s.id
293WHERE g.id IN ( $idString )
294";
295
3302658e 296 $fields = & $this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
6a488035
TO
297 $this->sql($sql, 'civicrm_saved_search', $fields);
298 }
299
300 function entityTag(&$contactIDs) {
3302658e 301 $fields = & $this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
6a488035
TO
302 $this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
303 }
304
305 function tag(&$contactIDs) {
306 // handle tags only once
307 static $_tagsHandled = array();
308
309 $ids = implode(',', $contactIDs);
310
311 $sql = "
312SELECT DISTINCT tag_id
313FROM civicrm_entity_tag
314WHERE entity_id IN ( $ids )
315AND entity_table = 'civicrm_contact'
316";
317 $dao = CRM_Core_DAO::executeQuery($sql);
318 $tagIDs = array();
319 while ($dao->fetch()) {
320 if (!isset($_tagsHandled[$dao->tag_id])) {
321 $tagIDs[] = $dao->tag_id;
322 $_tagsHandled[$dao->tag_id] = 1;
323 }
324 }
325
3302658e 326 $fields = & $this->dbFields('CRM_Core_DAO_Tag', TRUE);
6a488035
TO
327 $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
328 }
329
330 function relationship(&$contactIDs, &$additionalContacts) {
331 // handle relationships only once
332 static $_relationshipsHandled = array();
333
334 $ids = implode(',', $contactIDs);
335
336 $sql = "(
337 SELECT r.*
338 FROM civicrm_relationship r
339 WHERE r.contact_id_a IN ( $ids )
340) UNION (
341 SELECT r.*
342 FROM civicrm_relationship r
343 WHERE r.contact_id_b IN ( $ids )
344)
345";
346
347 $fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
3302658e 348 $dao = & CRM_Core_DAO::executeQuery($sql);
6a488035
TO
349 while ($dao->fetch()) {
350 if (isset($_relationshipsHandled[$dao->id])) {
351 continue;
352 }
353 $_relationshipsHandled[$dao->id] = $dao->id;
354
355 $relationship = array();
356 foreach ($fields as $fld) {
357 if (empty($dao->$fld)) {
358 $relationship[$fld] = NULL;
359 }
360 else {
361 $relationship[$fld] = $dao->$fld;
362 }
363 }
364 $this->appendValue($dao->id, 'civicrm_relationship', $relationship);
365
366 $this->addAdditionalContacts(array(
3302658e 367 $dao->contact_id_a,
6a488035
TO
368 $dao->contact_id_b,
369 ),
370 $additionalContacts
371 );
372 }
373 $dao->free();
374 }
375
376 function activity(&$contactIDs, &$additionalContacts) {
377 static $_activitiesHandled = array();
e7e657f0 378 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
9e74e3ce 379 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
380 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
6a488035
TO
381 $ids = implode(',', $contactIDs);
382
42d58fac
PJ
383 // query framing returning all contacts in valid activity
384 $sql = "
385SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id
386FROM civicrm_activity a
387INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id
388WHERE ac.contact_id IN ( $ids )
389 AND (a.activity_type_id != 3 AND a.activity_type_id != 20)
6a488035
TO
390";
391
3302658e 392 $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE);
6a488035 393
3302658e 394 $dao = & CRM_Core_DAO::executeQuery($sql);
6a488035 395 while ($dao->fetch()) {
42d58fac
PJ
396 // adding source, target and assignee contacts in additional contacts array
397 $this->addAdditionalContacts(array($dao->contact_id),
398 $additionalContacts
399 );
400
401 // append values of activity contacts
402 $activityContacts = array(
403 'id' => $dao->acID,
404 'contact_id' => $dao->contact_id,
405 'activity_id' => $dao->activity_id,
406 'record_type_id' => $dao->record_type_id
407 );
408 $this->appendValue($dao->acID, 'civicrm_activity_contact', $activityContacts);
409
6a488035
TO
410 if (isset($_activitiesHandled[$dao->id])) {
411 continue;
412 }
413 $_activitiesHandled[$dao->id] = $dao->id;
6a488035
TO
414
415 $activity = array();
416 foreach ($fields as $fld) {
417 if (empty($dao->$fld)) {
418 $activity[$fld] = NULL;
419 }
420 else {
421 $activity[$fld] = $dao->$fld;
422 }
423 }
424
42d58fac 425 // append activity value
6a488035 426 $this->appendValue($dao->id, 'civicrm_activity', $activity);
6a488035
TO
427 }
428 $dao->free();
6a488035
TO
429 }
430
431 function appendValue($id, $name, $value) {
432 if (empty($value)) {
433 return;
434 }
435
436 if (!isset($this->_values[$name])) {
437 $this->_values[$name] = array();
438 $this->_values[$name][] = array_keys($value);
439 }
440 $this->_values[$name][] = array_values($value);
441 }
442
443 function dbFields($daoName, $onlyKeys = FALSE) {
444 static $_fieldsRetrieved = array();
445
446 if (!isset($_fieldsRetrieved[$daoName])) {
447 $_fieldsRetrieved[$daoName] = array();
448 $daoFile = str_replace('_',
449 DIRECTORY_SEPARATOR,
450 $daoName
451 ) . '.php';
452 include_once ($daoFile);
453
3302658e 454 $daoFields = & $daoName::fields();
6a488035
TO
455
456 foreach ($daoFields as $key => & $value) {
457 $_fieldsRetrieved[$daoName][$value['name']] = array(
458 'uniqueName' => $key,
459 'type' => $value['type'],
460 'title' => CRM_Utils_Array::value('title', $value, NULL),
461 );
462 }
463 }
464
465 if ($onlyKeys) {
466 return array_keys($_fieldsRetrieved[$daoName]);
467 }
468 else {
469 return $_fieldsRetrieved[$daoName];
470 }
471 }
472
473 function addAdditionalContacts($contactIDs, &$additionalContacts) {
474 if (!$this->_discoverContacts) {
475 return;
476 }
477
478 foreach ($contactIDs as $cid) {
479 if ($cid &&
480 !isset($this->_allContactIDs[$cid]) &&
481 !isset($additionalContacts[$cid])
482 ) {
483 $additionalContacts[$cid] = $cid;
484 }
485 }
486 }
487
488 function export(&$contactIDs) {
3302658e 489 $chunks = & $this->splitContactIDs($contactIDs);
6a488035
TO
490
491 $additionalContactIDs = array();
492
493 foreach ($chunks as $chunk) {
494 $this->getValues($chunk, $additionalContactIDs);
495 }
496
497 if (!empty($additionalContactIDs)) {
498 $this->_allContactIDs = $this->_allContactIDs + $additionalContactIDs;
499 $this->export($additionalContactIDs);
500 }
501 }
502
503 function run($fileName,
3302658e
TO
504 $lastExportTime = NULL,
505 $discoverContacts = FALSE
6a488035
TO
506 ) {
507 $this->_discoverContacts = $discoverContacts;
508
509 if (!$lastExportTime) {
510 $sql = "
511SELECT id
512FROM civicrm_contact
513";
514 }
515 else {
516 $sql = "(
517SELECT DISTINCT entity_id
518FROM civicrm_log
519WHERE entity_table = 'civicrm_contact'
520AND modified_date >= $lastExportTime
521) UNION (
522SELECT DISTINCT contact_id
523FROM civicrm_subscription_history
524WHERE date >= $lastExportTime
525)
526";
527 }
528
529
3302658e 530 $dao = & CRM_Core_DAO::executeQuery($sql);
6a488035
TO
531
532 $contactIDs = array();
533 while ($dao->fetch()) {
534 $contactIDs[$dao->id] = $dao->id;
535 }
536
537 $this->_allContactIDs = $contactIDs;
538 $this->_values = array();
539
540 $this->metaData();
541
542 $this->export($contactIDs);
543
544 $json = json_encode($this->_values, JSON_NUMERIC_CHECK);
545 file_put_contents($fileName,
546 $json
547 );
548
676c79ea 549 // print_r( json_decode( $json ) );
6a488035
TO
550 }
551}
552