3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
33 class CRM_Utils_Migrate_Import
{
38 public function __construct() {
42 * Import custom-data from an XML file.
45 * Path to an XML file.
47 * @throws CRM_Core_Exception
49 public function run($file) {
51 $dom = new DomDocument();
52 $xmlString = file_get_contents($file);
53 $load = $dom->loadXML($xmlString);
55 throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
58 $xml = simplexml_import_dom($dom);
59 return $this->runXmlElement($xml);
63 * Import custom-data from an XML element.
65 * @param SimpleXMLElement $xml
67 public function runXmlElement($xml) {
73 // first create option groups and values if any
74 $this->optionGroups($xml, $idMap);
75 $this->optionValues($xml, $idMap);
77 $this->relationshipTypes($xml);
78 $this->contributionTypes($xml);
80 // now create custom groups
81 $this->customGroups($xml, $idMap);
82 $this->customFields($xml, $idMap);
84 // now create profile groups
85 $this->profileGroups($xml, $idMap);
86 $this->profileFields($xml, $idMap);
87 $this->profileJoins($xml, $idMap);
89 // create DB Template String sample data
90 $this->dbTemplateString($xml, $idMap);
92 // clean up all caches etc
93 CRM_Core_Config
::clearDBCache();
97 * @param CRM_Core_DAO $dao
100 * @param null $keyName
104 public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
106 if (isset($xml->$keyName)) {
107 $dao->$keyName = (string ) $xml->$keyName;
108 if ($dao->find(TRUE)) {
109 CRM_Core_Session
::setStatus(ts("Found %1, %2, %3",
121 $fields = &$dao->fields();
122 foreach ($fields as $name => $dontCare) {
123 if (isset($xml->$name)) {
124 $value = (string ) $xml->$name;
125 $value = str_replace(CRM_Utils_Migrate_Export
::XML_VALUE_SEPARATOR
,
126 CRM_Core_DAO
::VALUE_SEPARATOR
,
129 $dao->$name = $value;
142 public function optionGroups(&$xml, &$idMap) {
143 foreach ($xml->OptionGroups
as $optionGroupsXML) {
144 foreach ($optionGroupsXML->OptionGroup
as $optionGroupXML) {
145 $optionGroup = new CRM_Core_DAO_OptionGroup();
146 $this->copyData($optionGroup, $optionGroupXML, TRUE, 'name');
147 $idMap['option_group'][$optionGroup->name
] = $optionGroup->id
;
156 public function optionValues(&$xml, &$idMap) {
157 foreach ($xml->OptionValues
as $optionValuesXML) {
158 foreach ($optionValuesXML->OptionValue
as $optionValueXML) {
159 $optionValue = new CRM_Core_DAO_OptionValue();
160 $optionValue->option_group_id
= $idMap['option_group'][(string ) $optionValueXML->option_group_name
];
161 if (empty($optionValue->option_group_id
)) {
162 //CRM-17410 check if option group already exist.
163 $optionValue->option_group_id
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $optionValueXML->option_group_name
, 'id', 'name');
165 $this->copyData($optionValue, $optionValueXML, FALSE, 'label');
166 if (!isset($optionValue->value
)) {
168 SELECT MAX(ROUND(v.value)) + 1
169 FROM civicrm_option_value v
170 WHERE v.option_group_id = %1
172 $params = [1 => [$optionValue->option_group_id
, 'Integer']];
173 $optionValue->value
= CRM_Core_DAO
::singleValueQuery($sql, $params);
175 $optionValue->save();
183 public function relationshipTypes(&$xml) {
185 foreach ($xml->RelationshipTypes
as $relationshipTypesXML) {
186 foreach ($relationshipTypesXML->RelationshipType
as $relationshipTypeXML) {
187 $relationshipType = new CRM_Contact_DAO_RelationshipType();
188 $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b');
196 public function contributionTypes(&$xml) {
198 foreach ($xml->ContributionTypes
as $contributionTypesXML) {
199 foreach ($contributionTypesXML->ContributionType
as $contributionTypeXML) {
200 $contributionType = new CRM_Financial_DAO_FinancialType();
201 $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
210 public function customGroups(&$xml, &$idMap) {
211 foreach ($xml->CustomGroups
as $customGroupsXML) {
212 foreach ($customGroupsXML->CustomGroup
as $customGroupXML) {
213 $customGroup = new CRM_Core_DAO_CustomGroup();
214 if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) {
215 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
220 if (!isset($customGroup->table_name
) ||
221 empty($customGroup->table_name
)
224 $customGroup->table_name
= "civicrm_value_" . strtolower(CRM_Utils_String
::munge($customGroup->title
, '_', 32)) . "_{$customGroup->id}";
229 // fix extends stuff if it exists
230 if (isset($customGroupXML->extends_entity_column_value_option_group
) &&
231 isset($customGroupXML->extends_entity_column_value
)
234 $optionValues = explode(",", $customGroupXML->extends_entity_column_value
);
235 $optValues = implode("','", $optionValues);
236 if (trim($customGroup->extends) != 'Participant') {
237 if ($customGroup->extends == 'Relationship') {
238 foreach ($optionValues as $key => $value) {
239 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b');
240 $valueIDs[] = $relTypeId;
243 elseif (in_array($customGroup->extends, ['Individual', 'Organization', 'Household'])) {
244 $valueIDs = $optionValues;
246 elseif (in_array($customGroup->extends, ['Contribution', 'ContributionRecur'])) {
248 FROM civicrm_financial_type
249 WHERE name IN ('{$optValues}')";
250 $dao = &CRM_Core_DAO
::executeQuery($sql);
251 while ($dao->fetch()) {
252 $valueIDs[] = $dao->id
;
258 FROM civicrm_option_value v
259 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
261 AND v.name IN ('$optValues')
265 (string ) $customGroupXML->extends_entity_column_value_option_group
,
269 $dao = &CRM_Core_DAO
::executeQuery($sql, $params);
271 while ($dao->fetch()) {
272 $valueIDs[] = $dao->value
;
275 if (!empty($valueIDs)) {
276 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
278 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
282 // Note: No need to set extends_entity_column_id here.
288 // when custom group extends 'Participant'
291 FROM civicrm_option_value v
292 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
293 WHERE g.name = 'custom_data_type'
298 (string ) $customGroupXML->extends_entity_column_value_option_group
,
302 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
304 $customGroup->extends_entity_column_id
= $valueID;
311 $condition = "AND v.name IN ( '{$optValues}' )";
312 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
316 // ParticipantEventName
317 $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
318 $optionIDs = CRM_Event_PseudoConstant
::event(NULL, FALSE, $condition);
322 // ParticipantEventType
323 $condition = "AND v.name IN ( '{$optValues}' )";
324 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
328 if (is_array($optionIDs) && !empty($optionIDs)) {
329 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
330 array_keys($optionIDs)
331 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
339 $customGroup->save();
342 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
343 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
352 public function customFields(&$xml, &$idMap) {
353 // Re-index by group id so we can build out the custom fields one table
354 // at a time, and then rebuild the table triggers at the end, rather than
355 // rebuilding the table triggers after each field is added (which is
357 $fields_indexed_by_group_id = [];
358 foreach ($xml->CustomFields
as $customFieldsXML) {
359 $total = count($customFieldsXML->CustomField
);
360 foreach ($customFieldsXML->CustomField
as $customFieldXML) {
361 $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name
];
362 $fields_indexed_by_group_id[$id][] = $customFieldXML;
365 foreach ($fields_indexed_by_group_id as $group_id => $fields) {
366 CRM_Core_BAO_CustomField
::bulkSave(json_decode(json_encode($fields), TRUE), ['custom_group_id' => $group_id]);
374 public function dbTemplateString(&$xml, &$idMap) {
375 foreach ($xml->Persistent
as $persistentXML) {
376 foreach ($persistentXML->Persistent
as $persistent) {
377 $persistentObj = new CRM_Core_DAO_Persistent();
379 if ($persistent->is_config
== 1) {
380 $persistent->data
= serialize(explode(',', $persistent->data
));
382 $this->copyData($persistentObj, $persistent, TRUE, 'context');
391 public function profileGroups(&$xml, &$idMap) {
392 foreach ($xml->ProfileGroups
as $profileGroupsXML) {
393 foreach ($profileGroupsXML->ProfileGroup
as $profileGroupXML) {
394 $profileGroup = new CRM_Core_DAO_UFGroup();
395 $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
396 $idMap['profile_group'][$profileGroup->name
] = $profileGroup->id
;
397 $idMap['profile_group'][$profileGroup->title
] = $profileGroup->id
;
408 public function profileFields(&$xml, &$idMap) {
409 foreach ($xml->ProfileFields
as $profileFieldsXML) {
410 foreach ($profileFieldsXML->ProfileField
as $profileFieldXML) {
411 $profileField = new CRM_Core_DAO_UFField();
412 $profileField->uf_group_id
= $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name
];
413 $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
416 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
417 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
420 FROM civicrm_custom_field f
421 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
422 WHERE g.table_name = %1
423 AND f.column_name = %2
426 1 => [$tableName, 'String'],
427 2 => [$columnName, 'String'],
429 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
431 CRM_Core_Error
::fatal(ts("Could not find custom field for %1, %2, %3",
433 1 => $profileField->field_name
,
439 $profileField->field_name
= "custom_{$cfID}";
441 $profileField->save();
450 public function profileJoins(&$xml, &$idMap) {
451 foreach ($xml->ProfileJoins
as $profileJoinsXML) {
452 foreach ($profileJoinsXML->ProfileJoin
as $profileJoinXML) {
453 $profileJoin = new CRM_Core_DAO_UFJoin();
454 $profileJoin->uf_group_id
= $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name
];
455 $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
456 $profileJoin->save();