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_Import
{
22 public function __construct() {
26 * Import custom-data from an XML file.
29 * Path to an XML file.
31 * @throws CRM_Core_Exception
33 public function run($file) {
35 $dom = new DomDocument();
36 $xmlString = file_get_contents($file);
37 $load = $dom->loadXML($xmlString);
39 throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
42 $xml = simplexml_import_dom($dom);
43 return $this->runXmlElement($xml);
47 * Import custom-data from an XML element.
49 * @param SimpleXMLElement $xml
51 public function runXmlElement($xml) {
57 // first create option groups and values if any
58 $this->optionGroups($xml, $idMap);
59 $this->optionValues($xml, $idMap);
61 $this->relationshipTypes($xml);
62 $this->contributionTypes($xml);
64 // now create custom groups
65 $this->customGroups($xml, $idMap);
66 $this->customFields($xml, $idMap);
68 // now create profile groups
69 $this->profileGroups($xml, $idMap);
70 $this->profileFields($xml, $idMap);
71 $this->profileJoins($xml, $idMap);
73 // clean up all caches etc
74 CRM_Core_Config
::clearDBCache();
78 * @param CRM_Core_DAO $dao
79 * @param SimpleXMLElement $xml
81 * @param string $keyName
85 public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
87 if (isset($xml->$keyName)) {
88 $dao->$keyName = (string ) $xml->$keyName;
89 if ($dao->find(TRUE)) {
90 CRM_Core_Session
::setStatus(ts("Found %1, %2, %3",
102 $fields = &$dao->fields();
103 foreach ($fields as $name => $dontCare) {
104 if (isset($xml->$name)) {
105 $value = (string ) $xml->$name;
106 $value = str_replace(CRM_Utils_Migrate_Export
::XML_VALUE_SEPARATOR
,
107 CRM_Core_DAO
::VALUE_SEPARATOR
,
110 $dao->$name = $value;
120 * @param SimpleXMLElement $xml
121 * @param array $idMap
123 public function optionGroups(&$xml, &$idMap) {
124 foreach ($xml->OptionGroups
as $optionGroupsXML) {
125 foreach ($optionGroupsXML->OptionGroup
as $optionGroupXML) {
126 $optionGroup = new CRM_Core_DAO_OptionGroup();
127 $this->copyData($optionGroup, $optionGroupXML, TRUE, 'name');
128 $idMap['option_group'][$optionGroup->name
] = $optionGroup->id
;
134 * @param SimpleXMLElement $xml
135 * @param array $idMap
137 public function optionValues(&$xml, &$idMap) {
138 foreach ($xml->OptionValues
as $optionValuesXML) {
139 foreach ($optionValuesXML->OptionValue
as $optionValueXML) {
140 $optionValue = new CRM_Core_DAO_OptionValue();
141 $optionValue->option_group_id
= $idMap['option_group'][(string ) $optionValueXML->option_group_name
];
142 if (empty($optionValue->option_group_id
)) {
143 //CRM-17410 check if option group already exist.
144 $optionValue->option_group_id
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', (string) $optionValueXML->option_group_name
, 'id', 'name');
146 $this->copyData($optionValue, $optionValueXML, FALSE, 'label');
147 if (!isset($optionValue->value
)) {
149 SELECT MAX(ROUND(v.value)) + 1
150 FROM civicrm_option_value v
151 WHERE v.option_group_id = %1
153 $params = [1 => [$optionValue->option_group_id
, 'Integer']];
154 $optionValue->value
= CRM_Core_DAO
::singleValueQuery($sql, $params);
156 $optionValue->save();
162 * @param SimpleXMLElement $xml
164 public function relationshipTypes(&$xml) {
166 foreach ($xml->RelationshipTypes
as $relationshipTypesXML) {
167 foreach ($relationshipTypesXML->RelationshipType
as $relationshipTypeXML) {
168 $relationshipType = new CRM_Contact_DAO_RelationshipType();
169 $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b');
175 * @param SimpleXMLElement $xml
177 public function contributionTypes(&$xml) {
179 foreach ($xml->ContributionTypes
as $contributionTypesXML) {
180 foreach ($contributionTypesXML->ContributionType
as $contributionTypeXML) {
181 $contributionType = new CRM_Financial_DAO_FinancialType();
182 $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
188 * @param SimpleXMLElement $xml
189 * @param array $idMap
191 public function customGroups(&$xml, &$idMap) {
192 foreach ($xml->CustomGroups
as $customGroupsXML) {
193 foreach ($customGroupsXML->CustomGroup
as $customGroupXML) {
194 $customGroup = new CRM_Core_DAO_CustomGroup();
195 if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) {
196 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
201 if (!isset($customGroup->table_name
) ||
202 empty($customGroup->table_name
)
205 $customGroup->table_name
= "civicrm_value_" . strtolower(CRM_Utils_String
::munge($customGroup->title
, '_', 32)) . "_{$customGroup->id}";
210 // fix extends stuff if it exists
211 if (isset($customGroupXML->extends_entity_column_value_option_group
) &&
212 isset($customGroupXML->extends_entity_column_value
)
215 $optionValues = explode(",", $customGroupXML->extends_entity_column_value
);
216 $optValues = implode("','", $optionValues);
217 if (trim($customGroup->extends) != 'Participant') {
218 if ($customGroup->extends == 'Relationship') {
219 foreach ($optionValues as $key => $value) {
220 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b');
221 $valueIDs[] = $relTypeId;
224 elseif (in_array($customGroup->extends, CRM_Contact_BAO_ContactType
::basicTypes(TRUE), TRUE)) {
225 $valueIDs = $optionValues;
227 elseif (in_array($customGroup->extends, ['Contribution', 'ContributionRecur'])) {
229 FROM civicrm_financial_type
230 WHERE name IN ('{$optValues}')";
231 $dao = &CRM_Core_DAO
::executeQuery($sql);
232 while ($dao->fetch()) {
233 $valueIDs[] = $dao->id
;
239 FROM civicrm_option_value v
240 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
242 AND v.name IN ('$optValues')
246 (string ) $customGroupXML->extends_entity_column_value_option_group
,
250 $dao = &CRM_Core_DAO
::executeQuery($sql, $params);
252 while ($dao->fetch()) {
253 $valueIDs[] = $dao->value
;
256 if (!empty($valueIDs)) {
257 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
259 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
263 // Note: No need to set extends_entity_column_id here.
269 // when custom group extends 'Participant'
272 FROM civicrm_option_value v
273 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
274 WHERE g.name = 'custom_data_type'
279 (string ) $customGroupXML->extends_entity_column_value_option_group
,
283 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
285 $customGroup->extends_entity_column_id
= $valueID;
292 $condition = "AND v.name IN ( '{$optValues}' )";
293 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
297 // ParticipantEventName
298 $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
299 $optionIDs = CRM_Event_PseudoConstant
::event(NULL, FALSE, $condition);
303 // ParticipantEventType
304 $condition = "AND v.name IN ( '{$optValues}' )";
305 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
309 if (is_array($optionIDs) && !empty($optionIDs)) {
310 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
311 array_keys($optionIDs)
312 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
320 $customGroup->save();
323 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
324 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
330 * @param SimpleXMLElement $xml
331 * @param array $idMap
333 public function customFields(&$xml, &$idMap) {
334 // Re-index by group id so we can build out the custom fields one table
335 // at a time, and then rebuild the table triggers at the end, rather than
336 // rebuilding the table triggers after each field is added (which is
338 $fields_indexed_by_group_id = [];
339 foreach ($xml->CustomFields
as $customFieldsXML) {
340 $total = count($customFieldsXML->CustomField
);
341 foreach ($customFieldsXML->CustomField
as $customFieldXML) {
342 if (empty($customFieldXML->option_group_id
) && isset($customFieldXML->option_group_name
)) {
343 $customFieldXML->option_group_id
= $this->getOptionGroupIDFromName((string) $customFieldXML->option_group_name
, $idMap);
346 $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name
];
347 $fields_indexed_by_group_id[$id][] = $customFieldXML;
351 foreach ($fields_indexed_by_group_id as $group_id => $fields) {
352 \Civi\Api4\CustomField
::save(FALSE)
353 ->setDefaults(['custom_group_id' => $group_id])
354 ->setRecords(json_decode(json_encode($fields), TRUE))
360 * Get Option Group ID.
362 * Returns an option group's ID, given its name.
365 * @param array $idMap
369 private function getOptionGroupIDFromName($groupName, &$idMap) {
370 if (empty($groupName)) {
374 if (!isset($idMap['option_group'][$groupName])) {
375 $idMap['option_group'][$groupName] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $groupName, 'id', 'name');
378 return $idMap['option_group'][$groupName];
382 * @param SimpleXMLElement $xml
383 * @param array $idMap
385 public function profileGroups(&$xml, &$idMap) {
386 foreach ($xml->ProfileGroups
as $profileGroupsXML) {
387 foreach ($profileGroupsXML->ProfileGroup
as $profileGroupXML) {
388 $profileGroup = new CRM_Core_DAO_UFGroup();
389 $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
390 $idMap['profile_group'][$profileGroup->name
] = $profileGroup->id
;
391 $idMap['profile_group'][$profileGroup->title
] = $profileGroup->id
;
397 * @param SimpleXMLElement $xml
398 * @param array $idMap
400 * @throws CRM_Core_Exception
402 public function profileFields(&$xml, &$idMap) {
403 foreach ($xml->ProfileFields
as $profileFieldsXML) {
404 foreach ($profileFieldsXML->ProfileField
as $profileFieldXML) {
405 $profileField = new CRM_Core_DAO_UFField();
406 $profileField->uf_group_id
= $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name
];
407 $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
410 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
411 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
414 FROM civicrm_custom_field f
415 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
416 WHERE g.table_name = %1
417 AND f.column_name = %2
420 1 => [$tableName, 'String'],
421 2 => [$columnName, 'String'],
423 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
425 throw new CRM_Core_Exception(ts("Could not find custom field for %1, %2, %3",
427 1 => $profileField->field_name
,
433 $profileField->field_name
= "custom_{$cfID}";
435 $profileField->save();
441 * @param SimpleXMLElement $xml
442 * @param array $idMap
444 public function profileJoins(&$xml, &$idMap) {
445 foreach ($xml->ProfileJoins
as $profileJoinsXML) {
446 foreach ($profileJoinsXML->ProfileJoin
as $profileJoinXML) {
447 $profileJoin = new CRM_Core_DAO_UFJoin();
448 $profileJoin->uf_group_id
= $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name
];
449 $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
450 $profileJoin->save();