commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Utils / Migrate / ExportJSON.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 /**
53 * @param array $params
54 */
55 public function __construct(&$params) {
56 foreach ($params as $name => $value) {
57 $varName = '_' . $name;
58 $this->$varName = $value;
59 }
60 }
61
62 /**
63 * Split a large array of contactIDs into more manageable smaller chunks.
64 */
65 public function &splitContactIDs(&$contactIDs) {
66 // contactIDs could be a real large array, so we split it up into
67 // smaller chunks and then general xml for each chunk
68 $chunks = array();
69 $current = 0;
70 $chunks[$current] = array();
71 $count = 0;
72
73 foreach ($contactIDs as $k => $v) {
74 $chunks[$current][$k] = $v;
75 $count++;
76
77 if ($count == self::CHUNK_SIZE) {
78 $current++;
79 $chunks[$current] = array();
80 $count = 0;
81 }
82 }
83
84 if (empty($chunks[$current])) {
85 unset($chunks[$current]);
86 }
87
88 return $chunks;
89 }
90
91 /**
92 * Given a set of contact IDs get the values.
93 */
94 public function getValues(&$contactIDs, &$additionalContactIDs) {
95
96 $this->contact($contactIDs);
97 $this->address($contactIDs);
98 $this->phone($contactIDs);
99 $this->email($contactIDs);
100 $this->im($contactIDs);
101 $this->website($contactIDs);
102 $this->note($contactIDs);
103
104 $this->group($contactIDs);
105 $this->groupContact($contactIDs);
106 $this->savedSearch($contactIDs);
107
108 $this->tag($contactIDs);
109 $this->entityTag($contactIDs);
110
111 $this->relationship($contactIDs, $additionalContactIDs);
112 $this->activity($contactIDs, $additionalContactIDs);
113 }
114
115 public function metaData() {
116 $optionGroupVars = array(
117 'prefix_id' => 'individual_prefix',
118 'suffix_id' => 'individual_suffix',
119 'gender_id' => 'gender',
120 'mobile_provider' => 'mobile_provider',
121 'phone_type' => 'phone_type',
122 'activity_type' => 'activity_type',
123 'status_id' => 'activity_status_id',
124 'priority_id' => 'activity_priority_id',
125 'medium_id' => 'encounter_medium',
126 'communication_style_id' => 'communication_style',
127 'email_greeting' => 'email_greeting',
128 'postal_greeting' => 'postal_greeting',
129 'addressee_id' => 'addressee',
130 );
131 $this->optionGroup($optionGroupVars);
132
133 $auxilaryTables = array(
134 'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
135 'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
136 );
137 $this->auxTable($auxilaryTables);
138 }
139
140 /**
141 * @param $tables
142 */
143 public function auxTable($tables) {
144 foreach ($tables as $tableName => $daoName) {
145 $fields = &$this->dbFields($daoName, TRUE);
146
147 $sql = "SELECT * from $tableName";
148 $this->sql($sql, $tableName, $fields);
149 }
150 }
151
152 /**
153 * @param $optionGroupVars
154 */
155 public function optionGroup($optionGroupVars) {
156 $names = array_values($optionGroupVars);
157 $str = array();
158 foreach ($names as $name) {
159 $str[] = "'$name'";
160 }
161 $nameString = implode(",", $str);
162
163 $sql = "
164 SELECT *
165 FROM civicrm_option_group
166 WHERE name IN ( $nameString )
167 ";
168 $fields = &$this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
169 $this->sql($sql, 'civicrm_option_group', $fields);
170
171 $sql = "
172 SELECT v.*
173 FROM civicrm_option_value v
174 INNER JOIN civicrm_option_group g ON v.option_group_id = g.id
175 WHERE g.name IN ( $nameString )
176 ";
177 $fields = &$this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
178 $this->sql($sql, 'civicrm_option_value', $fields);
179 }
180
181 /**
182 * @param $ids
183 * @param string $tableName
184 * @param $fields
185 * @param $whereField
186 * @param null $additionalWhereCond
187 */
188 public function table(
189 &$ids,
190 $tableName,
191 &$fields,
192 $whereField,
193 $additionalWhereCond = NULL
194 ) {
195 if (empty($ids)) {
196 return;
197 }
198
199 $idString = implode(',', $ids);
200
201 $sql = "
202 SELECT *
203 FROM $tableName
204 WHERE $whereField IN ( $idString )
205 ";
206
207 if ($additionalWhereCond) {
208 $sql .= " AND $additionalWhereCond";
209 }
210
211 $this->sql($sql, $tableName, $fields);
212 }
213
214 /**
215 * @param $sql
216 * @param string $tableName
217 * @param $fields
218 */
219 public function sql($sql, $tableName, &$fields) {
220 $dao = &CRM_Core_DAO::executeQuery($sql);
221
222 while ($dao->fetch()) {
223 $value = array();
224 foreach ($fields as $name) {
225 if (empty($dao->$name)) {
226 $value[$name] = NULL;
227 }
228 else {
229 $value[$name] = $dao->$name;
230 }
231 }
232 $this->appendValue($dao->id, $tableName, $value);
233 }
234 $dao->free();
235 }
236
237 /**
238 * @param $contactIDs
239 */
240 public function contact(&$contactIDs) {
241 $fields = &$this->dbFields('CRM_Contact_DAO_Contact', TRUE);
242 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
243 }
244
245 /**
246 * @param $contactIDs
247 */
248 public function note(&$contactIDs) {
249 $fields = &$this->dbFields('CRM_Core_DAO_Note', TRUE);
250 $this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
251 }
252
253 /**
254 * @param $contactIDs
255 */
256 public function phone(&$contactIDs) {
257 $fields = &$this->dbFields('CRM_Core_DAO_Phone', TRUE);
258 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
259 }
260
261 /**
262 * @param $contactIDs
263 */
264 public function email(&$contactIDs) {
265 $fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
266 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
267 }
268
269 /**
270 * @param $contactIDs
271 */
272 public function im(&$contactIDs) {
273 $fields = &$this->dbFields('CRM_Core_DAO_IM', TRUE);
274 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
275 }
276
277 /**
278 * @param $contactIDs
279 */
280 public function website(&$contactIDs) {
281 $fields = &$this->dbFields('CRM_Core_DAO_Website', TRUE);
282 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
283 }
284
285 /**
286 * @param $contactIDs
287 */
288 public function address(&$contactIDs) {
289 $fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
290 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
291 }
292
293 /**
294 * @param $contactIDs
295 */
296 public function groupContact(&$contactIDs) {
297 $fields = &$this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
298 $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
299 }
300
301 /**
302 * TODO - support group inheritance
303 * Parent child group ids are encoded in a text string
304 * @param $contactIDs
305 */
306 public function group(&$contactIDs) {
307 // handle groups only once
308 static $_groupsHandled = array();
309
310 $ids = implode(',', $contactIDs);
311
312 $sql = "
313 SELECT DISTINCT group_id
314 FROM civicrm_group_contact
315 WHERE contact_id IN ( $ids )
316 ";
317 $dao = CRM_Core_DAO::executeQuery($sql);
318 $groupIDs = array();
319 while ($dao->fetch()) {
320 if (!isset($_groupsHandled[$dao->group_id])) {
321 $groupIDs[] = $dao->group_id;
322 $_groupsHandled[$dao->group_id] = 1;
323 }
324 }
325
326 $fields = &$this->dbFields('CRM_Contact_DAO_Group', TRUE);
327 $this->table($groupIDs, 'civicrm_group', $fields, 'id');
328
329 $this->savedSearch($groupIDs);
330 }
331
332 /**
333 * TODO - support search builder and custom saved searches
334 * @param $groupIDs
335 */
336 public function savedSearch(&$groupIDs) {
337 if (empty($groupIDs)) {
338 return;
339 }
340
341 $idString = implode(",", $groupIDs);
342 $sql = "
343 SELECT s.*
344 FROM civicrm_saved_search s
345 INNER JOIN civicrm_group g on g.saved_search_id = s.id
346 WHERE g.id IN ( $idString )
347 ";
348
349 $fields = &$this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
350 $this->sql($sql, 'civicrm_saved_search', $fields);
351 }
352
353 /**
354 * @param $contactIDs
355 */
356 public function entityTag(&$contactIDs) {
357 $fields = &$this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
358 $this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
359 }
360
361 /**
362 * @param $contactIDs
363 */
364 public function tag(&$contactIDs) {
365 // handle tags only once
366 static $_tagsHandled = array();
367
368 $ids = implode(',', $contactIDs);
369
370 $sql = "
371 SELECT DISTINCT tag_id
372 FROM civicrm_entity_tag
373 WHERE entity_id IN ( $ids )
374 AND entity_table = 'civicrm_contact'
375 ";
376 $dao = CRM_Core_DAO::executeQuery($sql);
377 $tagIDs = array();
378 while ($dao->fetch()) {
379 if (!isset($_tagsHandled[$dao->tag_id])) {
380 $tagIDs[] = $dao->tag_id;
381 $_tagsHandled[$dao->tag_id] = 1;
382 }
383 }
384
385 $fields = &$this->dbFields('CRM_Core_DAO_Tag', TRUE);
386 $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
387 }
388
389 /**
390 * @param $contactIDs
391 * @param $additionalContacts
392 */
393 public function relationship(&$contactIDs, &$additionalContacts) {
394 // handle relationships only once
395 static $_relationshipsHandled = array();
396
397 $ids = implode(',', $contactIDs);
398
399 $sql = "(
400 SELECT r.*
401 FROM civicrm_relationship r
402 WHERE r.contact_id_a IN ( $ids )
403 ) UNION (
404 SELECT r.*
405 FROM civicrm_relationship r
406 WHERE r.contact_id_b IN ( $ids )
407 )
408 ";
409
410 $fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
411 $dao = &CRM_Core_DAO::executeQuery($sql);
412 while ($dao->fetch()) {
413 if (isset($_relationshipsHandled[$dao->id])) {
414 continue;
415 }
416 $_relationshipsHandled[$dao->id] = $dao->id;
417
418 $relationship = array();
419 foreach ($fields as $fld) {
420 if (empty($dao->$fld)) {
421 $relationship[$fld] = NULL;
422 }
423 else {
424 $relationship[$fld] = $dao->$fld;
425 }
426 }
427 $this->appendValue($dao->id, 'civicrm_relationship', $relationship);
428
429 $this->addAdditionalContacts(array(
430 $dao->contact_id_a,
431 $dao->contact_id_b,
432 ),
433 $additionalContacts
434 );
435 }
436 $dao->free();
437 }
438
439 /**
440 * @param $contactIDs
441 * @param $additionalContacts
442 */
443 public function activity(&$contactIDs, &$additionalContacts) {
444 static $_activitiesHandled = array();
445 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
446 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
447 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
448 $ids = implode(',', $contactIDs);
449
450 // query framing returning all contacts in valid activity
451 $sql = "
452 SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id
453 FROM civicrm_activity a
454 INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id
455 WHERE ac.contact_id IN ( $ids )
456 AND (a.activity_type_id != 3 AND a.activity_type_id != 20)
457 ";
458
459 $fields = &$this->dbFields('CRM_Activity_DAO_Activity', TRUE);
460
461 $dao = &CRM_Core_DAO::executeQuery($sql);
462 while ($dao->fetch()) {
463 // adding source, target and assignee contacts in additional contacts array
464 $this->addAdditionalContacts(array($dao->contact_id),
465 $additionalContacts
466 );
467
468 // append values of activity contacts
469 $activityContacts = array(
470 'id' => $dao->acID,
471 'contact_id' => $dao->contact_id,
472 'activity_id' => $dao->activity_id,
473 'record_type_id' => $dao->record_type_id,
474 );
475 $this->appendValue($dao->acID, 'civicrm_activity_contact', $activityContacts);
476
477 if (isset($_activitiesHandled[$dao->id])) {
478 continue;
479 }
480 $_activitiesHandled[$dao->id] = $dao->id;
481
482 $activity = array();
483 foreach ($fields as $fld) {
484 if (empty($dao->$fld)) {
485 $activity[$fld] = NULL;
486 }
487 else {
488 $activity[$fld] = $dao->$fld;
489 }
490 }
491
492 // append activity value
493 $this->appendValue($dao->id, 'civicrm_activity', $activity);
494 }
495 $dao->free();
496 }
497
498 /**
499 * @param int $id
500 * @param string $name
501 * @param $value
502 */
503 public function appendValue($id, $name, $value) {
504 if (empty($value)) {
505 return;
506 }
507
508 if (!isset($this->_values[$name])) {
509 $this->_values[$name] = array();
510 $this->_values[$name][] = array_keys($value);
511 }
512 $this->_values[$name][] = array_values($value);
513 }
514
515 /**
516 * @param string $daoName
517 * @param bool $onlyKeys
518 *
519 * @return array
520 */
521 public function dbFields($daoName, $onlyKeys = FALSE) {
522 static $_fieldsRetrieved = array();
523
524 if (!isset($_fieldsRetrieved[$daoName])) {
525 $_fieldsRetrieved[$daoName] = array();
526 $daoFile = str_replace('_',
527 DIRECTORY_SEPARATOR,
528 $daoName
529 ) . '.php';
530 include_once $daoFile;
531
532 $daoFields = &$daoName::fields();
533
534 foreach ($daoFields as $key => & $value) {
535 $_fieldsRetrieved[$daoName][$value['name']] = array(
536 'uniqueName' => $key,
537 'type' => $value['type'],
538 'title' => CRM_Utils_Array::value('title', $value, NULL),
539 );
540 }
541 }
542
543 if ($onlyKeys) {
544 return array_keys($_fieldsRetrieved[$daoName]);
545 }
546 else {
547 return $_fieldsRetrieved[$daoName];
548 }
549 }
550
551 /**
552 * @param $contactIDs
553 * @param $additionalContacts
554 */
555 public function addAdditionalContacts($contactIDs, &$additionalContacts) {
556 if (!$this->_discoverContacts) {
557 return;
558 }
559
560 foreach ($contactIDs as $cid) {
561 if ($cid &&
562 !isset($this->_allContactIDs[$cid]) &&
563 !isset($additionalContacts[$cid])
564 ) {
565 $additionalContacts[$cid] = $cid;
566 }
567 }
568 }
569
570 /**
571 * @param $contactIDs
572 */
573 public function export(&$contactIDs) {
574 $chunks = &$this->splitContactIDs($contactIDs);
575
576 $additionalContactIDs = array();
577
578 foreach ($chunks as $chunk) {
579 $this->getValues($chunk, $additionalContactIDs);
580 }
581
582 if (!empty($additionalContactIDs)) {
583 $this->_allContactIDs = $this->_allContactIDs + $additionalContactIDs;
584 $this->export($additionalContactIDs);
585 }
586 }
587
588 /**
589 * @param string $fileName
590 * @param null $lastExportTime
591 * @param bool $discoverContacts
592 */
593 public function run(
594 $fileName,
595 $lastExportTime = NULL,
596 $discoverContacts = FALSE
597 ) {
598 $this->_discoverContacts = $discoverContacts;
599
600 if (!$lastExportTime) {
601 $sql = "
602 SELECT id
603 FROM civicrm_contact
604 ";
605 }
606 else {
607 $sql = "(
608 SELECT DISTINCT entity_id
609 FROM civicrm_log
610 WHERE entity_table = 'civicrm_contact'
611 AND modified_date >= $lastExportTime
612 ) UNION (
613 SELECT DISTINCT contact_id
614 FROM civicrm_subscription_history
615 WHERE date >= $lastExportTime
616 )
617 ";
618 }
619
620 $dao = &CRM_Core_DAO::executeQuery($sql);
621
622 $contactIDs = array();
623 while ($dao->fetch()) {
624 $contactIDs[$dao->id] = $dao->id;
625 }
626
627 $this->_allContactIDs = $contactIDs;
628 $this->_values = array();
629
630 $this->metaData();
631
632 $this->export($contactIDs);
633
634 $json = json_encode($this->_values, JSON_NUMERIC_CHECK);
635 file_put_contents($fileName,
636 $json
637 );
638
639 // print_r( json_decode( $json ) );
640 }
641
642 }