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 'communication_style_id' => 'communication_style',
124 'email_greeting' => 'email_greeting',
125 'postal_greeting' => 'postal_greeting',
126 'addressee_id' => 'addressee',
128 $this->optionGroup($optionGroupVars);
130 $auxilaryTables = array(
131 'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
132 'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
134 $this->auxTable($auxilaryTables);
137 function auxTable($tables) {
138 foreach ($tables as $tableName => $daoName) {
139 $fields = & $this->dbFields($daoName, TRUE);
141 $sql = "SELECT * from $tableName";
142 $this->sql($sql, $tableName, $fields);
146 function optionGroup($optionGroupVars) {
147 $names = array_values($optionGroupVars);
149 foreach ($names as $name) {
152 $nameString = implode(",", $str);
156 FROM civicrm_option_group
157 WHERE name IN ( $nameString )
159 $fields = & $this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
160 $this->sql($sql, 'civicrm_option_group', $fields);
164 FROM civicrm_option_value v
165 INNER JOIN civicrm_option_group g ON v.option_group_id = g.id
166 WHERE g.name IN ( $nameString )
168 $fields = & $this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
169 $this->sql($sql, 'civicrm_option_value', $fields);
172 function table(&$ids,
176 $additionalWhereCond = NULL
182 $idString = implode(',', $ids);
187 WHERE $whereField IN ( $idString )
190 if ($additionalWhereCond) {
191 $sql .= " AND $additionalWhereCond";
194 $this->sql($sql, $tableName, $fields);
197 function sql($sql, $tableName, &$fields) {
198 $dao = & CRM_Core_DAO
::executeQuery($sql);
200 while ($dao->fetch()) {
202 foreach ($fields as $name) {
203 if (empty($dao->$name)) {
204 $value[$name] = NULL;
207 $value[$name] = $dao->$name;
210 $this->appendValue($dao->id
, $tableName, $value);
215 function contact(&$contactIDs) {
216 $fields = & $this->dbFields('CRM_Contact_DAO_Contact', TRUE);
217 $this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
220 function note(&$contactIDs) {
221 $fields = & $this->dbFields('CRM_Core_DAO_Note', TRUE);
222 $this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
225 function phone(&$contactIDs) {
226 $fields = & $this->dbFields('CRM_Core_DAO_Phone', TRUE);
227 $this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
230 function email(&$contactIDs) {
231 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
232 $this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
235 function im(&$contactIDs) {
236 $fields = & $this->dbFields('CRM_Core_DAO_IM', TRUE);
237 $this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
240 function website(&$contactIDs) {
241 $fields = & $this->dbFields('CRM_Core_DAO_Website', TRUE);
242 $this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
245 function address(&$contactIDs) {
246 $fields = & $this->dbFields('CRM_Core_DAO_Email', TRUE);
247 $this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
250 function groupContact(&$contactIDs) {
251 $fields = & $this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
252 $this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
255 // TODO - support group inheritance
256 // Parent child group ids are encoded in a text string
257 function group(&$contactIDs) {
258 // handle groups only once
259 static $_groupsHandled = array();
261 $ids = implode(',', $contactIDs);
264 SELECT DISTINCT group_id
265 FROM civicrm_group_contact
266 WHERE contact_id IN ( $ids )
268 $dao = CRM_Core_DAO
::executeQuery($sql);
270 while ($dao->fetch()) {
271 if (!isset($_groupsHandled[$dao->group_id
])) {
272 $groupIDs[] = $dao->group_id
;
273 $_groupsHandled[$dao->group_id
] = 1;
277 $fields = & $this->dbFields('CRM_Contact_DAO_Group', TRUE);
278 $this->table($groupIDs, 'civicrm_group', $fields, 'id');
280 $this->savedSearch($groupIDs);
283 // TODO - support search builder and custom saved searches
284 function savedSearch(&$groupIDs) {
285 if (empty($groupIDs)) {
289 $idString = implode(",", $groupIDs);
292 FROM civicrm_saved_search s
293 INNER JOIN civicrm_group g on g.saved_search_id = s.id
294 WHERE g.id IN ( $idString )
297 $fields = & $this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
298 $this->sql($sql, 'civicrm_saved_search', $fields);
301 function entityTag(&$contactIDs) {
302 $fields = & $this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
303 $this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
306 function tag(&$contactIDs) {
307 // handle tags only once
308 static $_tagsHandled = array();
310 $ids = implode(',', $contactIDs);
313 SELECT DISTINCT tag_id
314 FROM civicrm_entity_tag
315 WHERE entity_id IN ( $ids )
316 AND entity_table = 'civicrm_contact'
318 $dao = CRM_Core_DAO
::executeQuery($sql);
320 while ($dao->fetch()) {
321 if (!isset($_tagsHandled[$dao->tag_id
])) {
322 $tagIDs[] = $dao->tag_id
;
323 $_tagsHandled[$dao->tag_id
] = 1;
327 $fields = & $this->dbFields('CRM_Core_DAO_Tag', TRUE);
328 $this->table($tagIDs, 'civicrm_tag', $fields, 'id');
331 function relationship(&$contactIDs, &$additionalContacts) {
332 // handle relationships only once
333 static $_relationshipsHandled = array();
335 $ids = implode(',', $contactIDs);
339 FROM civicrm_relationship r
340 WHERE r.contact_id_a IN ( $ids )
343 FROM civicrm_relationship r
344 WHERE r.contact_id_b IN ( $ids )
348 $fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
349 $dao = & CRM_Core_DAO
::executeQuery($sql);
350 while ($dao->fetch()) {
351 if (isset($_relationshipsHandled[$dao->id
])) {
354 $_relationshipsHandled[$dao->id
] = $dao->id
;
356 $relationship = array();
357 foreach ($fields as $fld) {
358 if (empty($dao->$fld)) {
359 $relationship[$fld] = NULL;
362 $relationship[$fld] = $dao->$fld;
365 $this->appendValue($dao->id
, 'civicrm_relationship', $relationship);
367 $this->addAdditionalContacts(array(
377 function activity(&$contactIDs, &$additionalContacts) {
378 static $_activitiesHandled = array();
379 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
380 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
381 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
382 $ids = implode(',', $contactIDs);
384 // query framing returning all contacts in valid activity
386 SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id
387 FROM civicrm_activity a
388 INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id
389 WHERE ac.contact_id IN ( $ids )
390 AND (a.activity_type_id != 3 AND a.activity_type_id != 20)
393 $fields = & $this->dbFields('CRM_Activity_DAO_Activity', TRUE);
395 $dao = & CRM_Core_DAO
::executeQuery($sql);
396 while ($dao->fetch()) {
397 // adding source, target and assignee contacts in additional contacts array
398 $this->addAdditionalContacts(array($dao->contact_id
),
402 // append values of activity contacts
403 $activityContacts = array(
405 'contact_id' => $dao->contact_id
,
406 'activity_id' => $dao->activity_id
,
407 'record_type_id' => $dao->record_type_id
409 $this->appendValue($dao->acID
, 'civicrm_activity_contact', $activityContacts);
411 if (isset($_activitiesHandled[$dao->id
])) {
414 $_activitiesHandled[$dao->id
] = $dao->id
;
417 foreach ($fields as $fld) {
418 if (empty($dao->$fld)) {
419 $activity[$fld] = NULL;
422 $activity[$fld] = $dao->$fld;
426 // append activity value
427 $this->appendValue($dao->id
, 'civicrm_activity', $activity);
432 function appendValue($id, $name, $value) {
437 if (!isset($this->_values
[$name])) {
438 $this->_values
[$name] = array();
439 $this->_values
[$name][] = array_keys($value);
441 $this->_values
[$name][] = array_values($value);
444 function dbFields($daoName, $onlyKeys = FALSE) {
445 static $_fieldsRetrieved = array();
447 if (!isset($_fieldsRetrieved[$daoName])) {
448 $_fieldsRetrieved[$daoName] = array();
449 $daoFile = str_replace('_',
453 include_once ($daoFile);
455 $daoFields = & $daoName::fields();
457 foreach ($daoFields as $key => & $value) {
458 $_fieldsRetrieved[$daoName][$value['name']] = array(
459 'uniqueName' => $key,
460 'type' => $value['type'],
461 'title' => CRM_Utils_Array
::value('title', $value, NULL),
467 return array_keys($_fieldsRetrieved[$daoName]);
470 return $_fieldsRetrieved[$daoName];
474 function addAdditionalContacts($contactIDs, &$additionalContacts) {
475 if (!$this->_discoverContacts
) {
479 foreach ($contactIDs as $cid) {
481 !isset($this->_allContactIDs
[$cid]) &&
482 !isset($additionalContacts[$cid])
484 $additionalContacts[$cid] = $cid;
489 function export(&$contactIDs) {
490 $chunks = & $this->splitContactIDs($contactIDs);
492 $additionalContactIDs = array();
494 foreach ($chunks as $chunk) {
495 $this->getValues($chunk, $additionalContactIDs);
498 if (!empty($additionalContactIDs)) {
499 $this->_allContactIDs
= $this->_allContactIDs +
$additionalContactIDs;
500 $this->export($additionalContactIDs);
504 function run($fileName,
505 $lastExportTime = NULL,
506 $discoverContacts = FALSE
508 $this->_discoverContacts
= $discoverContacts;
510 if (!$lastExportTime) {
518 SELECT DISTINCT entity_id
520 WHERE entity_table = 'civicrm_contact'
521 AND modified_date >= $lastExportTime
523 SELECT DISTINCT contact_id
524 FROM civicrm_subscription_history
525 WHERE date >= $lastExportTime
531 $dao = & CRM_Core_DAO
::executeQuery($sql);
533 $contactIDs = array();
534 while ($dao->fetch()) {
535 $contactIDs[$dao->id
] = $dao->id
;
538 $this->_allContactIDs
= $contactIDs;
539 $this->_values
= array();
543 $this->export($contactIDs);
545 $json = json_encode($this->_values
, JSON_NUMERIC_CHECK
);
546 file_put_contents($fileName,
550 // print_r( json_decode( $json ) );