3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_Migrate_Export
{
19 const XML_VALUE_SEPARATOR
= ":;:;:;";
22 * @var array description of export field mapping
25 * 'exampleEntityMappingName' => array(
26 * 'data' => array(), // placeholder; this will get filled-in during execution
27 * 'name' => 'CustomGroup', // per-item XML tag name
28 * 'scope' => 'CustomGroups', // container XML tag name
29 * 'required' => FALSE, // whether we *must* find records of this type
30 * 'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
31 * 'idNameMap' => array(), // placeholder; this will get filled-in during execution
40 public function __construct() {
44 'name' => 'CustomGroup',
45 'scope' => 'CustomGroups',
47 'idNameFields' => ['id', 'name'],
52 'name' => 'CustomField',
53 'scope' => 'CustomFields',
55 'idNameFields' => ['id', 'column_name'],
58 ['optionGroup', 'option_group_id', 'option_group_name'],
59 ['customGroup', 'custom_group_id', 'custom_group_name'],
64 'name' => 'OptionGroup',
65 'scope' => 'OptionGroups',
68 'idNameFields' => ['id', 'name'],
70 'relationshipType' => [
72 'name' => 'RelationshipType',
73 'scope' => 'RelationshipTypes',
75 'idNameFields' => ['id', 'name_a_b'],
80 'name' => 'LocationType',
81 'scope' => 'LocationTypes',
83 'idNameFields' => ['id', 'name'],
88 'name' => 'OptionValue',
89 'scope' => 'OptionValues',
92 'idNameFields' => ['value', 'name', 'prefix'],
94 ['optionGroup', 'option_group_id', 'option_group_name'],
99 'name' => 'ProfileGroup',
100 'scope' => 'ProfileGroups',
102 'idNameFields' => ['id', 'title'],
107 'name' => 'ProfileField',
108 'scope' => 'ProfileFields',
112 ['profileGroup', 'uf_group_id', 'profile_group_name'],
117 'name' => 'ProfileJoin',
118 'scope' => 'ProfileJoins',
122 ['profileGroup', 'uf_group_id', 'profile_group_name'],
127 'name' => 'MappingGroup',
128 'scope' => 'MappingGroups',
130 'idNameFields' => ['id', 'name'],
133 ['optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'],
138 'name' => 'MappingField',
139 'scope' => 'MappingFields',
143 ['mappingGroup', 'mapping_id', 'mapping_group_name'],
144 ['locationType', 'location_type_id', 'location_type_name'],
145 ['relationshipType', 'relationship_type_id', 'relationship_type_name'],
152 * Scan local customizations and build an in-memory representation.
154 public function build() {
155 // fetch the option group / values for
156 // activity type and event_type
158 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
161 SELECT distinct(g.id), g.*
162 FROM civicrm_option_group g
163 WHERE g.name IN $optionGroups
165 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
168 SELECT distinct(g.id), g.*
169 FROM civicrm_option_group g,
170 civicrm_custom_field f,
171 civicrm_custom_group cg
172 WHERE f.option_group_id = g.id
173 AND f.custom_group_id = cg.id
176 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
179 SELECT v.*, g.name as prefix
180 FROM civicrm_option_value v,
181 civicrm_option_group g
182 WHERE v.option_group_id = g.id
183 AND g.name IN $optionGroups
186 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
189 SELECT distinct(v.id), v.*, g.name as prefix
190 FROM civicrm_option_value v,
191 civicrm_option_group g,
192 civicrm_custom_field f,
193 civicrm_custom_group cg
194 WHERE v.option_group_id = g.id
195 AND f.option_group_id = g.id
196 AND f.custom_group_id = cg.id
200 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
204 FROM civicrm_relationship_type rt
205 WHERE rt.is_active = 1
207 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
211 FROM civicrm_location_type lt
212 WHERE lt.is_active = 1
214 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
218 FROM civicrm_custom_group cg
219 WHERE cg.is_active = 1
221 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
225 FROM civicrm_custom_field f,
226 civicrm_custom_group cg
227 WHERE f.custom_group_id = cg.id
230 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
232 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
234 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
239 WHERE entity_table IS NULL
240 AND entity_id IS NULL
242 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
244 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
246 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
250 * Build custom groups.
252 * @param array $customGroupIds
253 * List of custom groups to export.
255 public function buildCustomGroups($customGroupIds) {
256 $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
257 if (empty($customGroupIdsSql)) {
262 SELECT distinct(g.id), g.*
263 FROM civicrm_option_group g,
264 civicrm_custom_field f,
265 civicrm_custom_group cg
266 WHERE f.option_group_id = g.id
267 AND f.custom_group_id = cg.id
268 AND cg.id in ($customGroupIdsSql)
270 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
273 SELECT distinct(v.id), v.*, g.name as prefix
274 FROM civicrm_option_value v,
275 civicrm_option_group g,
276 civicrm_custom_field f,
277 civicrm_custom_group cg
278 WHERE v.option_group_id = g.id
279 AND f.option_group_id = g.id
280 AND f.custom_group_id = cg.id
281 AND cg.id in ($customGroupIdsSql)
284 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
288 FROM civicrm_custom_group cg
289 WHERE cg.id in ($customGroupIdsSql)
292 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
296 FROM civicrm_custom_field f,
297 civicrm_custom_group cg
298 WHERE f.custom_group_id = cg.id
299 AND cg.id in ($customGroupIdsSql)
301 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
305 * @param array $ufGroupIds
306 * List of custom groups to export.
308 public function buildUFGroups($ufGroupIds) {
309 $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
310 if (empty($ufGroupIdsSql)) {
316 FROM civicrm_uf_group cg
317 WHERE cg.id IN ($ufGroupIdsSql)
320 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
324 FROM civicrm_uf_field f,
326 WHERE f.uf_group_id = cg.id
327 AND cg.id IN ($ufGroupIdsSql)
329 $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
334 WHERE entity_table IS NULL
335 AND entity_id IS NULL
336 AND uf_group_id IN ($ufGroupIdsSql)
338 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
342 * Render the in-memory representation as XML
347 public function toXML() {
348 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
349 $buffer .= "\n\n<CustomData>\n";
350 foreach (array_keys($this->_xml
) as $key) {
351 if (!empty($this->_xml
[$key]['data'])) {
352 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
353 foreach ($this->_xml
[$key]['data'] as $item) {
354 $buffer .= $this->renderKeyValueXML($this->_xml
[$key]['name'], $item);
356 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
358 elseif ($this->_xml
[$key]['required']) {
359 CRM_Core_Error
::fatal("No records in DB for $key");
362 $buffer .= "</CustomData>\n";
367 * Generate an array-tree representation of the exported elements.
371 public function toArray() {
373 foreach (array_keys($this->_xml
) as $key) {
374 if (!empty($this->_xml
[$key]['data'])) {
375 $result[$this->_xml
[$key]['name']] = array_values($this->_xml
[$key]['data']);
382 * @param string $groupName
383 * @param string $daoName
386 public function fetch($groupName, $daoName, $sql = NULL) {
387 $idNameFields = isset($this->_xml
[$groupName]['idNameFields']) ?
$this->_xml
[$groupName]['idNameFields'] : NULL;
388 $mappedFields = isset($this->_xml
[$groupName]['mappedFields']) ?
$this->_xml
[$groupName]['mappedFields'] : NULL;
390 $dao = new $daoName();
398 while ($dao->fetch()) {
399 $this->_xml
[$groupName]['data'][$dao->id
] = $this->exportDAO($this->_xml
[$groupName]['name'], $dao, $mappedFields);
401 // index the id/name fields so that we can translate from FK ids to FK names
402 if (isset($idNameFields[2])) {
403 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
406 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
413 * Compute any fields of the entity defined by the $mappedFields specification
415 * @param array $mappedFields
416 * Each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix).
417 * @param CRM_Core_DAO $dao
418 * The entity for which we want to prepare mapped fields.
422 public function computeMappedFields($mappedFields, $dao) {
425 foreach ($mappedFields as $mappedField) {
426 if (isset($dao->{$mappedField[1]})) {
427 if (isset($mappedField[3])) {
428 $label = $this->_xml
[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
431 $label = $this->_xml
[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
433 $keyValues[$mappedField[2]] = $label;
441 * @param string $objectName
442 * Business-entity/xml-tag name.
443 * @param CRM_Core_DAO $object
444 * @param $mappedFields
448 public function exportDAO($objectName, $object, $mappedFields) {
449 $dbFields = &$object->fields();
451 // Filter the list of keys and values so that we only export interesting stuff
453 foreach ($dbFields as $name => $dontCare) {
455 if ($name == 'id' ||
substr($name, -3, 3) == '_id') {
458 if (isset($object->$name) && $object->$name !== NULL) {
459 // hack for extends_entity_column_value
460 if ($name == 'extends_entity_column_value') {
461 if (in_array($object->extends, [
470 if ($object->extends == 'Event') {
473 elseif ($object->extends == 'Activity') {
474 $key = 'activity_type';
476 elseif ($object->extends == 'Relationship') {
477 $key = 'relationship_type';
479 elseif ($object->extends == 'Case') {
482 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($object->$name, 1, -1));
484 if (in_array($object->extends, ['Individual', 'Organization', 'Household'])) {
485 $key = 'contact_type';
489 foreach ($types as $type) {
490 if (in_array($key, ['activity_type', 'event_type', 'case_type'])) {
491 $ogID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
492 $ovParams = ['option_group_id' => $ogID, 'value' => $type];
493 CRM_Core_BAO_OptionValue
::retrieve($ovParams, $oValue);
494 $values[] = $oValue['name'];
497 $relTypeName = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
498 $values[] = $relTypeName;
502 $keyValues['extends_entity_column_value_option_group'] = $key;
503 $value = implode(',', $values);
504 $object->extends_entity_column_value
= $value;
507 echo "This extension: {$object->extends} is not yet handled";
512 $value = $object->$name;
513 if ($name == 'field_name') {
514 // hack for profile field_name
515 if (substr($value, 0, 7) == 'custom_') {
516 $cfID = substr($value, 7);
517 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
518 $value = "custom.{$tableName}.{$columnName}";
521 $keyValues[$name] = $value;
525 $keyValues +
= $this->computeMappedFields($mappedFields, $object);
531 * @param string $tagName
532 * @param array $keyValues
537 public function renderKeyValueXML($tagName, $keyValues) {
538 $xml = " <$tagName>";
539 foreach ($keyValues as $k => $v) {
540 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, self
::XML_VALUE_SEPARATOR
, $v));
542 $xml .= "\n </$tagName>\n";
547 * @param string $name
549 * @param string $value
551 * @param string $prefix
557 public function renderTextTag($name, $value, $prefix = '') {
558 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
559 throw new Exception("Malformed tag name: $name");
561 return $prefix . "<$name>" . htmlentities($value) . "</$name>";