Merge pull request #1932 from mlutfy/4.4-crm13713-fix
[civicrm-core.git] / CRM / Utils / Migrate / ExportJSON.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 */
35 class 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
50 protected $_sitePrefix = 'Site 1';
51
52 function __construct(&$params) {
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
65 $chunks = array();
66 $current = 0;
67 $chunks[$current] = array();
68 $count = 0;
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) {
138 $fields = & $this->dbFields($daoName, TRUE);
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 = "
154 SELECT *
155 FROM civicrm_option_group
156 WHERE name IN ( $nameString )
157 ";
158 $fields = & $this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
159 $this->sql($sql, 'civicrm_option_group', $fields);
160
161 $sql = "
162 SELECT v.*
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 )
166 ";
167 $fields = & $this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
168 $this->sql($sql, 'civicrm_option_value', $fields);
169 }
170
171 function table(&$ids,
172 $tableName,
173 &$fields,
174 $whereField,
175 $additionalWhereCond = NULL
176 ) {
177 if (empty($ids)) {
178 return;
179 }
180
181 $idString = implode(',', $ids);
182
183 $sql = "
184 SELECT *
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) {
197 $dao = & CRM_Core_DAO::executeQuery($sql);
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) {
215 $fields = & $this->dbFields('CRM_Contact_DAO_Contact', TRUE);
216 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
217 }
218
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'");
222 }
223
224 function phone(&$contactIDs) {
225 $fields = & $this->dbFields('CRM_Core_DAO_Phone', TRUE);
226 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
227 }
228
229 function email(&$contactIDs) {
230 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
231 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
232 }
233
234 function im(&$contactIDs) {
235 $fields = & $this->dbFields('CRM_Core_DAO_IM', TRUE);
236 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
237 }
238
239 function website(&$contactIDs) {
240 $fields = & $this->dbFields('CRM_Core_DAO_Website', TRUE);
241 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
242 }
243
244 function address(&$contactIDs) {
245 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
246 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
247 }
248
249 function groupContact(&$contactIDs) {
250 $fields = & $this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
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 = "
263 SELECT DISTINCT group_id
264 FROM civicrm_group_contact
265 WHERE 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
276 $fields = & $this->dbFields('CRM_Contact_DAO_Group', TRUE);
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 = "
290 SELECT s.*
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 )
294 ";
295
296 $fields = & $this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
297 $this->sql($sql, 'civicrm_saved_search', $fields);
298 }
299
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'");
303 }
304
305 function tag(&$contactIDs) {
306 // handle tags only once
307 static $_tagsHandled = array();
308
309 $ids = implode(',', $contactIDs);
310
311 $sql = "
312 SELECT DISTINCT tag_id
313 FROM civicrm_entity_tag
314 WHERE entity_id IN ( $ids )
315 AND 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
326 $fields = & $this->dbFields('CRM_Core_DAO_Tag', TRUE);
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);
348 $dao = & CRM_Core_DAO::executeQuery($sql);
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(
367 $dao->contact_id_a,
368 $dao->contact_id_b,
369 ),
370 $additionalContacts
371 );
372 }
373 $dao->free();
374 }
375
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);
381 $ids = implode(',', $contactIDs);
382
383 // query framing returning all contacts in valid activity
384 $sql = "
385 SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id
386 FROM civicrm_activity a
387 INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id
388 WHERE ac.contact_id IN ( $ids )
389 AND (a.activity_type_id != 3 AND a.activity_type_id != 20)
390 ";
391
392 $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE);
393
394 $dao = & CRM_Core_DAO::executeQuery($sql);
395 while ($dao->fetch()) {
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
410 if (isset($_activitiesHandled[$dao->id])) {
411 continue;
412 }
413 $_activitiesHandled[$dao->id] = $dao->id;
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
425 // append activity value
426 $this->appendValue($dao->id, 'civicrm_activity', $activity);
427 }
428 $dao->free();
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
454 $daoFields = & $daoName::fields();
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) {
489 $chunks = & $this->splitContactIDs($contactIDs);
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,
504 $lastExportTime = NULL,
505 $discoverContacts = FALSE
506 ) {
507 $this->_discoverContacts = $discoverContacts;
508
509 if (!$lastExportTime) {
510 $sql = "
511 SELECT id
512 FROM civicrm_contact
513 ";
514 }
515 else {
516 $sql = "(
517 SELECT DISTINCT entity_id
518 FROM civicrm_log
519 WHERE entity_table = 'civicrm_contact'
520 AND modified_date >= $lastExportTime
521 ) UNION (
522 SELECT DISTINCT contact_id
523 FROM civicrm_subscription_history
524 WHERE date >= $lastExportTime
525 )
526 ";
527 }
528
529
530 $dao = & CRM_Core_DAO::executeQuery($sql);
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
549 // print_r( json_decode( $json ) );
550 }
551 }
552