3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 class CRM_Utils_Migrate_ExportJSON
{
34 const CHUNK_SIZE
= 128;
36 protected $_contactIDs;
38 protected $_allContactIDs;
42 protected $_discoverContacts = FALSE;
44 protected $_renameGroups = 1;
46 protected $_renameTags = 1;
48 protected $_sitePrefix = 'Site 1';
51 * @param array $params
53 public function __construct(&$params) {
54 foreach ($params as $name => $value) {
55 $varName = '_' . $name;
56 $this->$varName = $value;
61 * Split a large array of contactIDs into more manageable smaller chunks.
63 * @param array $contactIDs
67 public function &splitContactIDs(&$contactIDs) {
68 // contactIDs could be a real large array, so we split it up into
69 // smaller chunks and then general xml for each chunk
72 $chunks[$current] = array();
75 foreach ($contactIDs as $k => $v) {
76 $chunks[$current][$k] = $v;
79 if ($count == self
::CHUNK_SIZE
) {
81 $chunks[$current] = array();
86 if (empty($chunks[$current])) {
87 unset($chunks[$current]);
94 * Given a set of contact IDs get the values.
96 * @param array $contactIDs
97 * @param array $additionalContactIDs
99 public function getValues(&$contactIDs, &$additionalContactIDs) {
101 $this->contact($contactIDs);
102 $this->address($contactIDs);
103 $this->phone($contactIDs);
104 $this->email($contactIDs);
105 $this->im($contactIDs);
106 $this->website($contactIDs);
107 $this->note($contactIDs);
109 $this->group($contactIDs);
110 $this->groupContact($contactIDs);
111 $this->savedSearch($contactIDs);
113 $this->tag($contactIDs);
114 $this->entityTag($contactIDs);
116 $this->relationship($contactIDs, $additionalContactIDs);
117 $this->activity($contactIDs, $additionalContactIDs);
120 public function metaData() {
121 $optionGroupVars = array(
122 'prefix_id' => 'individual_prefix',
123 'suffix_id' => 'individual_suffix',
124 'gender_id' => 'gender',
125 'mobile_provider' => 'mobile_provider',
126 'phone_type' => 'phone_type',
127 'activity_type' => 'activity_type',
128 'status_id' => 'activity_status_id',
129 'priority_id' => 'activity_priority_id',
130 'medium_id' => 'encounter_medium',
131 'communication_style_id' => 'communication_style',
132 'email_greeting' => 'email_greeting',
133 'postal_greeting' => 'postal_greeting',
134 'addressee_id' => 'addressee',
136 $this->optionGroup($optionGroupVars);
138 $auxilaryTables = array(
139 'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
140 'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
142 $this->auxTable($auxilaryTables);
148 public function auxTable($tables) {
149 foreach ($tables as $tableName => $daoName) {
150 $fields = &$this->dbFields($daoName, TRUE);
152 $sql = "SELECT * from $tableName";
153 $this->sql($sql, $tableName, $fields);
158 * @param $optionGroupVars
160 public function optionGroup($optionGroupVars) {
161 $names = array_values($optionGroupVars);
163 foreach ($names as $name) {
166 $nameString = implode(",", $str);
170 FROM civicrm_option_group
171 WHERE name IN ( $nameString )
173 $fields = &$this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
174 $this->sql($sql, 'civicrm_option_group', $fields);
178 FROM civicrm_option_value v
179 INNER JOIN civicrm_option_group g ON v.option_group_id = g.id
180 WHERE g.name IN ( $nameString )
182 $fields = &$this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
183 $this->sql($sql, 'civicrm_option_value', $fields);
188 * @param string $tableName
191 * @param null $additionalWhereCond
193 public function table(
198 $additionalWhereCond = NULL
204 $idString = implode(',', $ids);
209 WHERE $whereField IN ( $idString )
212 if ($additionalWhereCond) {
213 $sql .= " AND $additionalWhereCond";
216 $this->sql($sql, $tableName, $fields);
221 * @param string $tableName
224 public function sql($sql, $tableName, &$fields) {
225 $dao = &CRM_Core_DAO
::executeQuery($sql);
227 while ($dao->fetch()) {
229 foreach ($fields as $name) {
230 if (empty($dao->$name)) {
231 $value[$name] = NULL;
234 $value[$name] = $dao->$name;
237 $this->appendValue($dao->id
, $tableName, $value);
245 public function contact(&$contactIDs) {
246 $fields = &$this->dbFields('CRM_Contact_DAO_Contact', TRUE);
247 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
253 public function note(&$contactIDs) {
254 $fields = &$this->dbFields('CRM_Core_DAO_Note', TRUE);
255 $this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
261 public function phone(&$contactIDs) {
262 $fields = &$this->dbFields('CRM_Core_DAO_Phone', TRUE);
263 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
269 public function email(&$contactIDs) {
270 $fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
271 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
277 public function im(&$contactIDs) {
278 $fields = &$this->dbFields('CRM_Core_DAO_IM', TRUE);
279 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
285 public function website(&$contactIDs) {
286 $fields = &$this->dbFields('CRM_Core_DAO_Website', TRUE);
287 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
293 public function address(&$contactIDs) {
294 $fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
295 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
301 public function groupContact(&$contactIDs) {
302 $fields = &$this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
303 $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
307 * @todo support group inheritance
309 * Parent child group ids are encoded in a text string
313 public function group(&$contactIDs) {
314 // handle groups only once
315 static $_groupsHandled = array();
317 $ids = implode(',', $contactIDs);
320 SELECT DISTINCT group_id
321 FROM civicrm_group_contact
322 WHERE contact_id IN ( $ids )
324 $dao = CRM_Core_DAO
::executeQuery($sql);
326 while ($dao->fetch()) {
327 if (!isset($_groupsHandled[$dao->group_id
])) {
328 $groupIDs[] = $dao->group_id
;
329 $_groupsHandled[$dao->group_id
] = 1;
333 $fields = &$this->dbFields('CRM_Contact_DAO_Group', TRUE);
334 $this->table($groupIDs, 'civicrm_group', $fields, 'id');
336 $this->savedSearch($groupIDs);
340 * @todo support search builder and custom saved searches
343 public function savedSearch(&$groupIDs) {
344 if (empty($groupIDs)) {
348 $idString = implode(",", $groupIDs);
351 FROM civicrm_saved_search s
352 INNER JOIN civicrm_group g on g.saved_search_id = s.id
353 WHERE g.id IN ( $idString )
356 $fields = &$this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
357 $this->sql($sql, 'civicrm_saved_search', $fields);
363 public function entityTag(&$contactIDs) {
364 $fields = &$this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
365 $this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
371 public function tag(&$contactIDs) {
372 // handle tags only once
373 static $_tagsHandled = array();
375 $ids = implode(',', $contactIDs);
378 SELECT DISTINCT tag_id
379 FROM civicrm_entity_tag
380 WHERE entity_id IN ( $ids )
381 AND entity_table = 'civicrm_contact'
383 $dao = CRM_Core_DAO
::executeQuery($sql);
385 while ($dao->fetch()) {
386 if (!isset($_tagsHandled[$dao->tag_id
])) {
387 $tagIDs[] = $dao->tag_id
;
388 $_tagsHandled[$dao->tag_id
] = 1;
392 $fields = &$this->dbFields('CRM_Core_DAO_Tag', TRUE);
393 $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
398 * @param $additionalContacts
400 public function relationship(&$contactIDs, &$additionalContacts) {
401 // handle relationships only once
402 static $_relationshipsHandled = array();
404 $ids = implode(',', $contactIDs);
408 FROM civicrm_relationship r
409 WHERE r.contact_id_a IN ( $ids )
412 FROM civicrm_relationship r
413 WHERE r.contact_id_b IN ( $ids )
417 $fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
418 $dao = &CRM_Core_DAO
::executeQuery($sql);
419 while ($dao->fetch()) {
420 if (isset($_relationshipsHandled[$dao->id
])) {
423 $_relationshipsHandled[$dao->id
] = $dao->id
;
425 $relationship = array();
426 foreach ($fields as $fld) {
427 if (empty($dao->$fld)) {
428 $relationship[$fld] = NULL;
431 $relationship[$fld] = $dao->$fld;
434 $this->appendValue($dao->id
, 'civicrm_relationship', $relationship);
436 $this->addAdditionalContacts(array(
448 * @param $additionalContacts
450 public function activity(&$contactIDs, &$additionalContacts) {
451 static $_activitiesHandled = array();
452 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
453 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
454 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
455 $ids = implode(',', $contactIDs);
457 // query framing returning all contacts in valid activity
459 SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id
460 FROM civicrm_activity a
461 INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id
462 WHERE ac.contact_id IN ( $ids )
463 AND (a.activity_type_id != 3 AND a.activity_type_id != 20)
466 $fields = &$this->dbFields('CRM_Activity_DAO_Activity', TRUE);
468 $dao = &CRM_Core_DAO
::executeQuery($sql);
469 while ($dao->fetch()) {
470 // adding source, target and assignee contacts in additional contacts array
471 $this->addAdditionalContacts(array($dao->contact_id
),
475 // append values of activity contacts
476 $activityContacts = array(
478 'contact_id' => $dao->contact_id
,
479 'activity_id' => $dao->activity_id
,
480 'record_type_id' => $dao->record_type_id
,
482 $this->appendValue($dao->acID
, 'civicrm_activity_contact', $activityContacts);
484 if (isset($_activitiesHandled[$dao->id
])) {
487 $_activitiesHandled[$dao->id
] = $dao->id
;
490 foreach ($fields as $fld) {
491 if (empty($dao->$fld)) {
492 $activity[$fld] = NULL;
495 $activity[$fld] = $dao->$fld;
499 // append activity value
500 $this->appendValue($dao->id
, 'civicrm_activity', $activity);
507 * @param string $name
510 public function appendValue($id, $name, $value) {
515 if (!isset($this->_values
[$name])) {
516 $this->_values
[$name] = array();
517 $this->_values
[$name][] = array_keys($value);
519 $this->_values
[$name][] = array_values($value);
523 * @param string $daoName
524 * @param bool $onlyKeys
528 public function dbFields($daoName, $onlyKeys = FALSE) {
529 static $_fieldsRetrieved = array();
531 if (!isset($_fieldsRetrieved[$daoName])) {
532 $_fieldsRetrieved[$daoName] = array();
533 $daoFile = str_replace('_',
537 include_once $daoFile;
539 $daoFields = &$daoName::fields();
541 foreach ($daoFields as $key => & $value) {
542 $_fieldsRetrieved[$daoName][$value['name']] = array(
543 'uniqueName' => $key,
544 'type' => $value['type'],
545 'title' => CRM_Utils_Array
::value('title', $value, NULL),
551 return array_keys($_fieldsRetrieved[$daoName]);
554 return $_fieldsRetrieved[$daoName];
560 * @param $additionalContacts
562 public function addAdditionalContacts($contactIDs, &$additionalContacts) {
563 if (!$this->_discoverContacts
) {
567 foreach ($contactIDs as $cid) {
569 !isset($this->_allContactIDs
[$cid]) &&
570 !isset($additionalContacts[$cid])
572 $additionalContacts[$cid] = $cid;
580 public function export(&$contactIDs) {
581 $chunks = &$this->splitContactIDs($contactIDs);
583 $additionalContactIDs = array();
585 foreach ($chunks as $chunk) {
586 $this->getValues($chunk, $additionalContactIDs);
589 if (!empty($additionalContactIDs)) {
590 $this->_allContactIDs
= $this->_allContactIDs +
$additionalContactIDs;
591 $this->export($additionalContactIDs);
596 * @param string $fileName
597 * @param null $lastExportTime
598 * @param bool $discoverContacts
602 $lastExportTime = NULL,
603 $discoverContacts = FALSE
605 $this->_discoverContacts
= $discoverContacts;
607 if (!$lastExportTime) {
615 SELECT DISTINCT entity_id
617 WHERE entity_table = 'civicrm_contact'
618 AND modified_date >= $lastExportTime
620 SELECT DISTINCT contact_id
621 FROM civicrm_subscription_history
622 WHERE date >= $lastExportTime
627 $dao = &CRM_Core_DAO
::executeQuery($sql);
629 $contactIDs = array();
630 while ($dao->fetch()) {
631 $contactIDs[$dao->id
] = $dao->id
;
634 $this->_allContactIDs
= $contactIDs;
635 $this->_values
= array();
639 $this->export($contactIDs);
641 $json = json_encode($this->_values
, JSON_NUMERIC_CHECK
);
642 file_put_contents($fileName,
646 // print_r( json_decode( $json ) );