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
= ":;:;:;";
23 * Description of export field mapping
26 * 'exampleEntityMappingName' => array(
27 * 'data' => array(), // placeholder; this will get filled-in during execution
28 * 'name' => 'CustomGroup', // per-item XML tag name
29 * 'scope' => 'CustomGroups', // container XML tag name
30 * 'required' => FALSE, // whether we *must* find records of this type
31 * 'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
32 * 'idNameMap' => array(), // placeholder; this will get filled-in during execution
41 public function __construct() {
45 'name' => 'CustomGroup',
46 'scope' => 'CustomGroups',
48 'idNameFields' => ['id', 'name'],
53 'name' => 'CustomField',
54 'scope' => 'CustomFields',
56 'idNameFields' => ['id', 'column_name'],
59 ['optionGroup', 'option_group_id', 'option_group_name'],
60 ['customGroup', 'custom_group_id', 'custom_group_name'],
65 'name' => 'OptionGroup',
66 'scope' => 'OptionGroups',
69 'idNameFields' => ['id', 'name'],
71 'relationshipType' => [
73 'name' => 'RelationshipType',
74 'scope' => 'RelationshipTypes',
76 'idNameFields' => ['id', 'name_a_b'],
81 'name' => 'LocationType',
82 'scope' => 'LocationTypes',
84 'idNameFields' => ['id', 'name'],
89 'name' => 'OptionValue',
90 'scope' => 'OptionValues',
93 'idNameFields' => ['value', 'name', 'prefix'],
95 ['optionGroup', 'option_group_id', 'option_group_name'],
100 'name' => 'ProfileGroup',
101 'scope' => 'ProfileGroups',
103 'idNameFields' => ['id', 'title'],
108 'name' => 'ProfileField',
109 'scope' => 'ProfileFields',
113 ['profileGroup', 'uf_group_id', 'profile_group_name'],
118 'name' => 'ProfileJoin',
119 'scope' => 'ProfileJoins',
123 ['profileGroup', 'uf_group_id', 'profile_group_name'],
128 'name' => 'MappingGroup',
129 'scope' => 'MappingGroups',
131 'idNameFields' => ['id', 'name'],
134 ['optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'],
139 'name' => 'MappingField',
140 'scope' => 'MappingFields',
144 ['mappingGroup', 'mapping_id', 'mapping_group_name'],
145 ['locationType', 'location_type_id', 'location_type_name'],
146 ['relationshipType', 'relationship_type_id', 'relationship_type_name'],
153 * Scan local customizations and build an in-memory representation.
155 public function build() {
156 // fetch the option group / values for
157 // activity type and event_type
159 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
162 SELECT distinct(g.id), g.*
163 FROM civicrm_option_group g
164 WHERE g.name IN $optionGroups
166 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
169 SELECT distinct(g.id), g.*
170 FROM civicrm_option_group g,
171 civicrm_custom_field f,
172 civicrm_custom_group cg
173 WHERE f.option_group_id = g.id
174 AND f.custom_group_id = cg.id
177 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
180 SELECT v.*, g.name as prefix
181 FROM civicrm_option_value v,
182 civicrm_option_group g
183 WHERE v.option_group_id = g.id
184 AND g.name IN $optionGroups
187 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
190 SELECT distinct(v.id), v.*, g.name as prefix
191 FROM civicrm_option_value v,
192 civicrm_option_group g,
193 civicrm_custom_field f,
194 civicrm_custom_group cg
195 WHERE v.option_group_id = g.id
196 AND f.option_group_id = g.id
197 AND f.custom_group_id = cg.id
201 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
205 FROM civicrm_relationship_type rt
206 WHERE rt.is_active = 1
208 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
212 FROM civicrm_location_type lt
213 WHERE lt.is_active = 1
215 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
219 FROM civicrm_custom_group cg
220 WHERE cg.is_active = 1
222 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
226 FROM civicrm_custom_field f,
227 civicrm_custom_group cg
228 WHERE f.custom_group_id = cg.id
231 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
233 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
235 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
240 WHERE entity_table IS NULL
241 AND entity_id IS NULL
243 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
245 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
247 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
251 * Build custom groups.
253 * @param array $customGroupIds
254 * List of custom groups to export.
256 public function buildCustomGroups($customGroupIds) {
257 $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
258 if (empty($customGroupIdsSql)) {
263 SELECT distinct(g.id), g.*
264 FROM civicrm_option_group g,
265 civicrm_custom_field f,
266 civicrm_custom_group cg
267 WHERE f.option_group_id = g.id
268 AND f.custom_group_id = cg.id
269 AND cg.id in ($customGroupIdsSql)
271 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
274 SELECT distinct(v.id), v.*, g.name as prefix
275 FROM civicrm_option_value v,
276 civicrm_option_group g,
277 civicrm_custom_field f,
278 civicrm_custom_group cg
279 WHERE v.option_group_id = g.id
280 AND f.option_group_id = g.id
281 AND f.custom_group_id = cg.id
282 AND cg.id in ($customGroupIdsSql)
285 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
289 FROM civicrm_custom_group cg
290 WHERE cg.id in ($customGroupIdsSql)
293 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
297 FROM civicrm_custom_field f,
298 civicrm_custom_group cg
299 WHERE f.custom_group_id = cg.id
300 AND cg.id in ($customGroupIdsSql)
302 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
306 * @param array $ufGroupIds
307 * List of custom groups to export.
309 public function buildUFGroups($ufGroupIds) {
310 $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
311 if (empty($ufGroupIdsSql)) {
317 FROM civicrm_uf_group cg
318 WHERE cg.id IN ($ufGroupIdsSql)
321 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
325 FROM civicrm_uf_field f,
327 WHERE f.uf_group_id = cg.id
328 AND cg.id IN ($ufGroupIdsSql)
330 $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
335 WHERE entity_table IS NULL
336 AND entity_id IS NULL
337 AND uf_group_id IN ($ufGroupIdsSql)
339 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
343 * Render the in-memory representation as XML
347 * @throws CRM_Core_Exception
349 public function toXML() {
350 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
351 $buffer .= "\n\n<CustomData>\n";
352 foreach (array_keys($this->_xml
) as $key) {
353 if (!empty($this->_xml
[$key]['data'])) {
354 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
355 foreach ($this->_xml
[$key]['data'] as $item) {
356 $buffer .= $this->renderKeyValueXML($this->_xml
[$key]['name'], $item);
358 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
360 elseif ($this->_xml
[$key]['required']) {
361 throw new CRM_Core_Exception("No records in DB for $key");
364 $buffer .= "</CustomData>\n";
369 * Generate an array-tree representation of the exported elements.
373 public function toArray() {
375 foreach (array_keys($this->_xml
) as $key) {
376 if (!empty($this->_xml
[$key]['data'])) {
377 $result[$this->_xml
[$key]['name']] = array_values($this->_xml
[$key]['data']);
384 * @param string $groupName
385 * @param string $daoName
388 public function fetch($groupName, $daoName, $sql = NULL) {
389 $idNameFields = $this->_xml
[$groupName]['idNameFields'] ??
NULL;
390 $mappedFields = $this->_xml
[$groupName]['mappedFields'] ??
NULL;
392 $dao = new $daoName();
400 while ($dao->fetch()) {
401 $this->_xml
[$groupName]['data'][$dao->id
] = $this->exportDAO($this->_xml
[$groupName]['name'], $dao, $mappedFields);
403 // index the id/name fields so that we can translate from FK ids to FK names
404 if (isset($idNameFields[2])) {
405 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
408 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
415 * Compute any fields of the entity defined by the $mappedFields specification
417 * @param array $mappedFields
418 * Each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix).
419 * @param CRM_Core_DAO $dao
420 * The entity for which we want to prepare mapped fields.
424 public function computeMappedFields($mappedFields, $dao) {
427 foreach ($mappedFields as $mappedField) {
428 if (isset($dao->{$mappedField[1]})) {
429 if (isset($mappedField[3])) {
430 $label = $this->_xml
[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
433 $label = $this->_xml
[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
435 $keyValues[$mappedField[2]] = $label;
443 * @param string $objectName
444 * Business-entity/xml-tag name.
445 * @param CRM_Core_DAO $object
446 * @param array $mappedFields
450 public function exportDAO($objectName, $object, $mappedFields) {
451 $dbFields = &$object->fields();
453 // Filter the list of keys and values so that we only export interesting stuff
455 foreach ($dbFields as $name => $dontCare) {
457 if ($name == 'id' ||
substr($name, -3, 3) == '_id') {
460 if (isset($object->$name) && $object->$name !== NULL) {
461 // hack for extends_entity_column_value
462 if ($name == 'extends_entity_column_value') {
463 if (in_array($object->extends, [
472 if ($object->extends == 'Event') {
475 elseif ($object->extends == 'Activity') {
476 $key = 'activity_type';
478 elseif ($object->extends == 'Relationship') {
479 $key = 'relationship_type';
481 elseif ($object->extends == 'Case') {
484 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($object->$name, 1, -1));
486 if (in_array($object->extends, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE)) {
487 $key = 'contact_type';
491 foreach ($types as $type) {
492 if (in_array($key, ['activity_type', 'event_type', 'case_type'])) {
493 $ogID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
494 $ovParams = ['option_group_id' => $ogID, 'value' => $type];
495 CRM_Core_BAO_OptionValue
::retrieve($ovParams, $oValue);
496 $values[] = $oValue['name'];
499 $relTypeName = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
500 $values[] = $relTypeName;
504 $keyValues['extends_entity_column_value_option_group'] = $key;
505 $value = implode(',', $values);
506 $object->extends_entity_column_value
= $value;
509 echo "This extension: {$object->extends} is not yet handled";
514 $value = $object->$name;
515 if ($name == 'field_name') {
516 // hack for profile field_name
517 if (substr($value, 0, 7) == 'custom_') {
518 $cfID = substr($value, 7);
519 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
520 $value = "custom.{$tableName}.{$columnName}";
523 $keyValues[$name] = $value;
527 $keyValues +
= $this->computeMappedFields($mappedFields, $object);
533 * @param string $tagName
534 * @param array $keyValues
539 public function renderKeyValueXML($tagName, $keyValues) {
540 $xml = " <$tagName>";
541 foreach ($keyValues as $k => $v) {
542 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, self
::XML_VALUE_SEPARATOR
, $v));
544 $xml .= "\n </$tagName>\n";
549 * @param string $name
551 * @param string $value
553 * @param string $prefix
559 public function renderTextTag($name, $value, $prefix = '') {
560 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
561 throw new Exception("Malformed tag name: $name");
563 return $prefix . "<$name>" . htmlentities($value) . "</$name>";