3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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';
53 * @param array $params
55 function __construct(&$params) {
56 foreach ($params as $name => $value) {
57 $varName = '_' . $name;
58 $this->$varName = $value;
63 * Split a large array of contactIDs into more manageable smaller chunks
65 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
70 $chunks[$current] = array();
73 foreach ($contactIDs as $k => $v) {
74 $chunks[$current][$k] = $v;
77 if ($count == self
::CHUNK_SIZE
) {
79 $chunks[$current] = array();
84 if (empty($chunks[$current])) {
85 unset($chunks[$current]);
92 * Given a set of contact IDs get the values
94 function getValues(&$contactIDs, &$additionalContactIDs) {
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);
104 $this->group($contactIDs);
105 $this->groupContact($contactIDs);
106 $this->savedSearch($contactIDs);
108 $this->tag($contactIDs);
109 $this->entityTag($contactIDs);
111 $this->relationship($contactIDs, $additionalContactIDs);
112 $this->activity($contactIDs, $additionalContactIDs);
115 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',
131 $this->optionGroup($optionGroupVars);
133 $auxilaryTables = array(
134 'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
135 'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
137 $this->auxTable($auxilaryTables);
143 function auxTable($tables) {
144 foreach ($tables as $tableName => $daoName) {
145 $fields = & $this->dbFields($daoName, TRUE);
147 $sql = "SELECT * from $tableName";
148 $this->sql($sql, $tableName, $fields);
153 * @param $optionGroupVars
155 function optionGroup($optionGroupVars) {
156 $names = array_values($optionGroupVars);
158 foreach ($names as $name) {
161 $nameString = implode(",", $str);
165 FROM civicrm_option_group
166 WHERE name IN ( $nameString )
168 $fields = & $this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
169 $this->sql($sql, 'civicrm_option_group', $fields);
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 )
177 $fields = & $this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
178 $this->sql($sql, 'civicrm_option_value', $fields);
183 * @param string $tableName
186 * @param null $additionalWhereCond
188 function table(&$ids,
192 $additionalWhereCond = NULL
198 $idString = implode(',', $ids);
203 WHERE $whereField IN ( $idString )
206 if ($additionalWhereCond) {
207 $sql .= " AND $additionalWhereCond";
210 $this->sql($sql, $tableName, $fields);
215 * @param string $tableName
218 function sql($sql, $tableName, &$fields) {
219 $dao = & CRM_Core_DAO
::executeQuery($sql);
221 while ($dao->fetch()) {
223 foreach ($fields as $name) {
224 if (empty($dao->$name)) {
225 $value[$name] = NULL;
228 $value[$name] = $dao->$name;
231 $this->appendValue($dao->id
, $tableName, $value);
239 function contact(&$contactIDs) {
240 $fields = & $this->dbFields('CRM_Contact_DAO_Contact', TRUE);
241 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
247 function note(&$contactIDs) {
248 $fields = & $this->dbFields('CRM_Core_DAO_Note', TRUE);
249 $this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
255 function phone(&$contactIDs) {
256 $fields = & $this->dbFields('CRM_Core_DAO_Phone', TRUE);
257 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
263 function email(&$contactIDs) {
264 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
265 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
271 function im(&$contactIDs) {
272 $fields = & $this->dbFields('CRM_Core_DAO_IM', TRUE);
273 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
279 function website(&$contactIDs) {
280 $fields = & $this->dbFields('CRM_Core_DAO_Website', TRUE);
281 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
287 function address(&$contactIDs) {
288 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
289 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
295 function groupContact(&$contactIDs) {
296 $fields = & $this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
297 $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
300 // TODO - support group inheritance
301 // Parent child group ids are encoded in a text string
305 function group(&$contactIDs) {
306 // handle groups only once
307 static $_groupsHandled = array();
309 $ids = implode(',', $contactIDs);
312 SELECT DISTINCT group_id
313 FROM civicrm_group_contact
314 WHERE contact_id IN ( $ids )
316 $dao = CRM_Core_DAO
::executeQuery($sql);
318 while ($dao->fetch()) {
319 if (!isset($_groupsHandled[$dao->group_id
])) {
320 $groupIDs[] = $dao->group_id
;
321 $_groupsHandled[$dao->group_id
] = 1;
325 $fields = & $this->dbFields('CRM_Contact_DAO_Group', TRUE);
326 $this->table($groupIDs, 'civicrm_group', $fields, 'id');
328 $this->savedSearch($groupIDs);
331 // TODO - support search builder and custom saved searches
335 function savedSearch(&$groupIDs) {
336 if (empty($groupIDs)) {
340 $idString = implode(",", $groupIDs);
343 FROM civicrm_saved_search s
344 INNER JOIN civicrm_group g on g.saved_search_id = s.id
345 WHERE g.id IN ( $idString )
348 $fields = & $this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
349 $this->sql($sql, 'civicrm_saved_search', $fields);
355 function entityTag(&$contactIDs) {
356 $fields = & $this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
357 $this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
363 function tag(&$contactIDs) {
364 // handle tags only once
365 static $_tagsHandled = array();
367 $ids = implode(',', $contactIDs);
370 SELECT DISTINCT tag_id
371 FROM civicrm_entity_tag
372 WHERE entity_id IN ( $ids )
373 AND entity_table = 'civicrm_contact'
375 $dao = CRM_Core_DAO
::executeQuery($sql);
377 while ($dao->fetch()) {
378 if (!isset($_tagsHandled[$dao->tag_id
])) {
379 $tagIDs[] = $dao->tag_id
;
380 $_tagsHandled[$dao->tag_id
] = 1;
384 $fields = & $this->dbFields('CRM_Core_DAO_Tag', TRUE);
385 $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
390 * @param $additionalContacts
392 function relationship(&$contactIDs, &$additionalContacts) {
393 // handle relationships only once
394 static $_relationshipsHandled = array();
396 $ids = implode(',', $contactIDs);
400 FROM civicrm_relationship r
401 WHERE r.contact_id_a IN ( $ids )
404 FROM civicrm_relationship r
405 WHERE r.contact_id_b IN ( $ids )
409 $fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
410 $dao = & CRM_Core_DAO
::executeQuery($sql);
411 while ($dao->fetch()) {
412 if (isset($_relationshipsHandled[$dao->id
])) {
415 $_relationshipsHandled[$dao->id
] = $dao->id
;
417 $relationship = array();
418 foreach ($fields as $fld) {
419 if (empty($dao->$fld)) {
420 $relationship[$fld] = NULL;
423 $relationship[$fld] = $dao->$fld;
426 $this->appendValue($dao->id
, 'civicrm_relationship', $relationship);
428 $this->addAdditionalContacts(array(
440 * @param $additionalContacts
442 function activity(&$contactIDs, &$additionalContacts) {
443 static $_activitiesHandled = array();
444 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
445 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
446 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
447 $ids = implode(',', $contactIDs);
449 // query framing returning all contacts in valid activity
451 SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id
452 FROM civicrm_activity a
453 INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id
454 WHERE ac.contact_id IN ( $ids )
455 AND (a.activity_type_id != 3 AND a.activity_type_id != 20)
458 $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE);
460 $dao = & CRM_Core_DAO
::executeQuery($sql);
461 while ($dao->fetch()) {
462 // adding source, target and assignee contacts in additional contacts array
463 $this->addAdditionalContacts(array($dao->contact_id
),
467 // append values of activity contacts
468 $activityContacts = array(
470 'contact_id' => $dao->contact_id
,
471 'activity_id' => $dao->activity_id
,
472 'record_type_id' => $dao->record_type_id
474 $this->appendValue($dao->acID
, 'civicrm_activity_contact', $activityContacts);
476 if (isset($_activitiesHandled[$dao->id
])) {
479 $_activitiesHandled[$dao->id
] = $dao->id
;
482 foreach ($fields as $fld) {
483 if (empty($dao->$fld)) {
484 $activity[$fld] = NULL;
487 $activity[$fld] = $dao->$fld;
491 // append activity value
492 $this->appendValue($dao->id
, 'civicrm_activity', $activity);
499 * @param string $name
502 function appendValue($id, $name, $value) {
507 if (!isset($this->_values
[$name])) {
508 $this->_values
[$name] = array();
509 $this->_values
[$name][] = array_keys($value);
511 $this->_values
[$name][] = array_values($value);
515 * @param string $daoName
516 * @param bool $onlyKeys
520 function dbFields($daoName, $onlyKeys = FALSE) {
521 static $_fieldsRetrieved = array();
523 if (!isset($_fieldsRetrieved[$daoName])) {
524 $_fieldsRetrieved[$daoName] = array();
525 $daoFile = str_replace('_',
529 include_once ($daoFile);
531 $daoFields = & $daoName::fields();
533 foreach ($daoFields as $key => & $value) {
534 $_fieldsRetrieved[$daoName][$value['name']] = array(
535 'uniqueName' => $key,
536 'type' => $value['type'],
537 'title' => CRM_Utils_Array
::value('title', $value, NULL),
543 return array_keys($_fieldsRetrieved[$daoName]);
546 return $_fieldsRetrieved[$daoName];
552 * @param $additionalContacts
554 function addAdditionalContacts($contactIDs, &$additionalContacts) {
555 if (!$this->_discoverContacts
) {
559 foreach ($contactIDs as $cid) {
561 !isset($this->_allContactIDs
[$cid]) &&
562 !isset($additionalContacts[$cid])
564 $additionalContacts[$cid] = $cid;
572 function export(&$contactIDs) {
573 $chunks = & $this->splitContactIDs($contactIDs);
575 $additionalContactIDs = array();
577 foreach ($chunks as $chunk) {
578 $this->getValues($chunk, $additionalContactIDs);
581 if (!empty($additionalContactIDs)) {
582 $this->_allContactIDs
= $this->_allContactIDs +
$additionalContactIDs;
583 $this->export($additionalContactIDs);
588 * @param string $fileName
589 * @param null $lastExportTime
590 * @param bool $discoverContacts
592 function run($fileName,
593 $lastExportTime = NULL,
594 $discoverContacts = FALSE
596 $this->_discoverContacts
= $discoverContacts;
598 if (!$lastExportTime) {
606 SELECT DISTINCT entity_id
608 WHERE entity_table = 'civicrm_contact'
609 AND modified_date >= $lastExportTime
611 SELECT DISTINCT contact_id
612 FROM civicrm_subscription_history
613 WHERE date >= $lastExportTime
619 $dao = & CRM_Core_DAO
::executeQuery($sql);
621 $contactIDs = array();
622 while ($dao->fetch()) {
623 $contactIDs[$dao->id
] = $dao->id
;
626 $this->_allContactIDs
= $contactIDs;
627 $this->_values
= array();
631 $this->export($contactIDs);
633 $json = json_encode($this->_values
, JSON_NUMERIC_CHECK
);
634 file_put_contents($fileName,
638 // print_r( json_decode( $json ) );