3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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';
52 function __construct(&$params) {
53 foreach ($params as $name => $value) {
54 $varName = '_' . $name;
55 $this->$varName = $value;
60 * Split a large array of contactIDs into more manageable smaller chunks
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
67 $chunks[$current] = array();
70 foreach ($contactIDs as $k => $v) {
71 $chunks[$current][$k] = $v;
74 if ($count == self
::CHUNK_SIZE
) {
76 $chunks[$current] = array();
81 if (empty($chunks[$current])) {
82 unset($chunks[$current]);
89 * Given a set of contact IDs get the values
91 function getValues(&$contactIDs, &$additionalContactIDs) {
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);
101 $this->group($contactIDs);
102 $this->groupContact($contactIDs);
103 $this->savedSearch($contactIDs);
105 $this->tag($contactIDs);
106 $this->entityTag($contactIDs);
108 $this->relationship($contactIDs, $additionalContactIDs);
109 $this->activity($contactIDs, $additionalContactIDs);
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',
127 $this->optionGroup($optionGroupVars);
129 $auxilaryTables = array(
130 'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
131 'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
133 $this->auxTable($auxilaryTables);
136 function auxTable($tables) {
137 foreach ($tables as $tableName => $daoName) {
138 $fields = & $this->dbFields($daoName, TRUE);
140 $sql = "SELECT * from $tableName";
141 $this->sql($sql, $tableName, $fields);
145 function optionGroup($optionGroupVars) {
146 $names = array_values($optionGroupVars);
148 foreach ($names as $name) {
151 $nameString = implode(",", $str);
155 FROM civicrm_option_group
156 WHERE name IN ( $nameString )
158 $fields = & $this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
159 $this->sql($sql, 'civicrm_option_group', $fields);
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 )
167 $fields = & $this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
168 $this->sql($sql, 'civicrm_option_value', $fields);
171 function table(&$ids,
175 $additionalWhereCond = NULL
181 $idString = implode(',', $ids);
186 WHERE $whereField IN ( $idString )
189 if ($additionalWhereCond) {
190 $sql .= " AND $additionalWhereCond";
193 $this->sql($sql, $tableName, $fields);
196 function sql($sql, $tableName, &$fields) {
197 $dao = & CRM_Core_DAO
::executeQuery($sql);
199 while ($dao->fetch()) {
201 foreach ($fields as $name) {
202 if (empty($dao->$name)) {
203 $value[$name] = NULL;
206 $value[$name] = $dao->$name;
209 $this->appendValue($dao->id
, $tableName, $value);
214 function contact(&$contactIDs) {
215 $fields = & $this->dbFields('CRM_Contact_DAO_Contact', TRUE);
216 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
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'");
224 function phone(&$contactIDs) {
225 $fields = & $this->dbFields('CRM_Core_DAO_Phone', TRUE);
226 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
229 function email(&$contactIDs) {
230 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
231 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
234 function im(&$contactIDs) {
235 $fields = & $this->dbFields('CRM_Core_DAO_IM', TRUE);
236 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
239 function website(&$contactIDs) {
240 $fields = & $this->dbFields('CRM_Core_DAO_Website', TRUE);
241 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
244 function address(&$contactIDs) {
245 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
246 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
249 function groupContact(&$contactIDs) {
250 $fields = & $this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
251 $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
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();
260 $ids = implode(',', $contactIDs);
263 SELECT DISTINCT group_id
264 FROM civicrm_group_contact
265 WHERE contact_id IN ( $ids )
267 $dao = CRM_Core_DAO
::executeQuery($sql);
269 while ($dao->fetch()) {
270 if (!isset($_groupsHandled[$dao->group_id
])) {
271 $groupIDs[] = $dao->group_id
;
272 $_groupsHandled[$dao->group_id
] = 1;
276 $fields = & $this->dbFields('CRM_Contact_DAO_Group', TRUE);
277 $this->table($groupIDs, 'civicrm_group', $fields, 'id');
279 $this->savedSearch($groupIDs);
282 // TODO - support search builder and custom saved searches
283 function savedSearch(&$groupIDs) {
284 if (empty($groupIDs)) {
288 $idString = implode(",", $groupIDs);
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 )
296 $fields = & $this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
297 $this->sql($sql, 'civicrm_saved_search', $fields);
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'");
305 function tag(&$contactIDs) {
306 // handle tags only once
307 static $_tagsHandled = array();
309 $ids = implode(',', $contactIDs);
312 SELECT DISTINCT tag_id
313 FROM civicrm_entity_tag
314 WHERE entity_id IN ( $ids )
315 AND entity_table = 'civicrm_contact'
317 $dao = CRM_Core_DAO
::executeQuery($sql);
319 while ($dao->fetch()) {
320 if (!isset($_tagsHandled[$dao->tag_id
])) {
321 $tagIDs[] = $dao->tag_id
;
322 $_tagsHandled[$dao->tag_id
] = 1;
326 $fields = & $this->dbFields('CRM_Core_DAO_Tag', TRUE);
327 $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
330 function relationship(&$contactIDs, &$additionalContacts) {
331 // handle relationships only once
332 static $_relationshipsHandled = array();
334 $ids = implode(',', $contactIDs);
338 FROM civicrm_relationship r
339 WHERE r.contact_id_a IN ( $ids )
342 FROM civicrm_relationship r
343 WHERE r.contact_id_b IN ( $ids )
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
])) {
353 $_relationshipsHandled[$dao->id
] = $dao->id
;
355 $relationship = array();
356 foreach ($fields as $fld) {
357 if (empty($dao->$fld)) {
358 $relationship[$fld] = NULL;
361 $relationship[$fld] = $dao->$fld;
364 $this->appendValue($dao->id
, 'civicrm_relationship', $relationship);
366 $this->addAdditionalContacts(array(
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);
383 // query framing returning all contacts in valid activity
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)
392 $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE);
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
),
401 // append values of activity contacts
402 $activityContacts = array(
404 'contact_id' => $dao->contact_id
,
405 'activity_id' => $dao->activity_id
,
406 'record_type_id' => $dao->record_type_id
408 $this->appendValue($dao->acID
, 'civicrm_activity_contact', $activityContacts);
410 if (isset($_activitiesHandled[$dao->id
])) {
413 $_activitiesHandled[$dao->id
] = $dao->id
;
416 foreach ($fields as $fld) {
417 if (empty($dao->$fld)) {
418 $activity[$fld] = NULL;
421 $activity[$fld] = $dao->$fld;
425 // append activity value
426 $this->appendValue($dao->id
, 'civicrm_activity', $activity);
431 function appendValue($id, $name, $value) {
436 if (!isset($this->_values
[$name])) {
437 $this->_values
[$name] = array();
438 $this->_values
[$name][] = array_keys($value);
440 $this->_values
[$name][] = array_values($value);
443 function dbFields($daoName, $onlyKeys = FALSE) {
444 static $_fieldsRetrieved = array();
446 if (!isset($_fieldsRetrieved[$daoName])) {
447 $_fieldsRetrieved[$daoName] = array();
448 $daoFile = str_replace('_',
452 include_once ($daoFile);
454 $daoFields = & $daoName::fields();
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),
466 return array_keys($_fieldsRetrieved[$daoName]);
469 return $_fieldsRetrieved[$daoName];
473 function addAdditionalContacts($contactIDs, &$additionalContacts) {
474 if (!$this->_discoverContacts
) {
478 foreach ($contactIDs as $cid) {
480 !isset($this->_allContactIDs
[$cid]) &&
481 !isset($additionalContacts[$cid])
483 $additionalContacts[$cid] = $cid;
488 function export(&$contactIDs) {
489 $chunks = & $this->splitContactIDs($contactIDs);
491 $additionalContactIDs = array();
493 foreach ($chunks as $chunk) {
494 $this->getValues($chunk, $additionalContactIDs);
497 if (!empty($additionalContactIDs)) {
498 $this->_allContactIDs
= $this->_allContactIDs +
$additionalContactIDs;
499 $this->export($additionalContactIDs);
503 function run($fileName,
504 $lastExportTime = NULL,
505 $discoverContacts = FALSE
507 $this->_discoverContacts
= $discoverContacts;
509 if (!$lastExportTime) {
517 SELECT DISTINCT entity_id
519 WHERE entity_table = 'civicrm_contact'
520 AND modified_date >= $lastExportTime
522 SELECT DISTINCT contact_id
523 FROM civicrm_subscription_history
524 WHERE date >= $lastExportTime
530 $dao = & CRM_Core_DAO
::executeQuery($sql);
532 $contactIDs = array();
533 while ($dao->fetch()) {
534 $contactIDs[$dao->id
] = $dao->id
;
537 $this->_allContactIDs
= $contactIDs;
538 $this->_values
= array();
542 $this->export($contactIDs);
544 $json = json_encode($this->_values
, JSON_NUMERIC_CHECK
);
545 file_put_contents($fileName,
549 // print_r( json_decode( $json ) );