Merge pull request #23461 from eileenmcnaughton/import_woohoo
[civicrm-core.git] / CRM / Utils / Migrate / Import.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Migrate_Import {
18
19 /**
20 * Class constructor.
21 */
22 public function __construct() {
23 }
24
25 /**
26 * Import custom-data from an XML file.
27 *
28 * @param string $file
29 * Path to an XML file.
30 *
31 * @throws CRM_Core_Exception
32 */
33 public function run($file) {
34 // read xml file
35 $dom = new DomDocument();
36 $xmlString = file_get_contents($file);
37 $load = $dom->loadXML($xmlString);
38 if (!$load) {
39 throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
40 }
41 $dom->xinclude();
42 $xml = simplexml_import_dom($dom);
43 return $this->runXmlElement($xml);
44 }
45
46 /**
47 * Import custom-data from an XML element.
48 *
49 * @param SimpleXMLElement $xml
50 */
51 public function runXmlElement($xml) {
52 $idMap = [
53 'custom_group' => [],
54 'option_group' => [],
55 ];
56
57 // first create option groups and values if any
58 $this->optionGroups($xml, $idMap);
59 $this->optionValues($xml, $idMap);
60
61 $this->relationshipTypes($xml);
62 $this->contributionTypes($xml);
63
64 // now create custom groups
65 $this->customGroups($xml, $idMap);
66 $this->customFields($xml, $idMap);
67
68 // now create profile groups
69 $this->profileGroups($xml, $idMap);
70 $this->profileFields($xml, $idMap);
71 $this->profileJoins($xml, $idMap);
72
73 // clean up all caches etc
74 CRM_Core_Config::clearDBCache();
75 }
76
77 /**
78 * @param CRM_Core_DAO $dao
79 * @param SimpleXMLElement $xml
80 * @param bool $save
81 * @param string $keyName
82 *
83 * @return bool
84 */
85 public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
86 if ($keyName) {
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",
91 [
92 1 => $keyName,
93 2 => $dao->$keyName,
94 3 => $dao->__table,
95 ]
96 ), '', 'info');
97 return FALSE;
98 }
99 }
100 }
101
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,
108 $value
109 );
110 $dao->$name = $value;
111 }
112 }
113 if ($save) {
114 $dao->save();
115 }
116 return TRUE;
117 }
118
119 /**
120 * @param SimpleXMLElement $xml
121 * @param array $idMap
122 */
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;
129 }
130 }
131 }
132
133 /**
134 * @param SimpleXMLElement $xml
135 * @param array $idMap
136 */
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');
145 }
146 $this->copyData($optionValue, $optionValueXML, FALSE, 'label');
147 if (!isset($optionValue->value)) {
148 $sql = "
149 SELECT MAX(ROUND(v.value)) + 1
150 FROM civicrm_option_value v
151 WHERE v.option_group_id = %1
152 ";
153 $params = [1 => [$optionValue->option_group_id, 'Integer']];
154 $optionValue->value = CRM_Core_DAO::singleValueQuery($sql, $params);
155 }
156 $optionValue->save();
157 }
158 }
159 }
160
161 /**
162 * @param SimpleXMLElement $xml
163 */
164 public function relationshipTypes(&$xml) {
165
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');
170 }
171 }
172 }
173
174 /**
175 * @param SimpleXMLElement $xml
176 */
177 public function contributionTypes(&$xml) {
178
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');
183 }
184 }
185 }
186
187 /**
188 * @param SimpleXMLElement $xml
189 * @param array $idMap
190 */
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;
197 continue;
198 }
199
200 $saveAgain = FALSE;
201 if (!isset($customGroup->table_name) ||
202 empty($customGroup->table_name)
203 ) {
204 // fix table name
205 $customGroup->table_name = "civicrm_value_" . strtolower(CRM_Utils_String::munge($customGroup->title, '_', 32)) . "_{$customGroup->id}";
206
207 $saveAgain = TRUE;
208 }
209
210 // fix extends stuff if it exists
211 if (isset($customGroupXML->extends_entity_column_value_option_group) &&
212 isset($customGroupXML->extends_entity_column_value)
213 ) {
214 $valueIDs = [];
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;
222 }
223 }
224 elseif (in_array($customGroup->extends, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) {
225 $valueIDs = $optionValues;
226 }
227 elseif (in_array($customGroup->extends, ['Contribution', 'ContributionRecur'])) {
228 $sql = "SELECT id
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;
234 }
235 }
236 else {
237 $sql = "
238 SELECT v.value
239 FROM civicrm_option_value v
240 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
241 WHERE g.name = %1
242 AND v.name IN ('$optValues')
243 ";
244 $params = [
245 1 => [
246 (string ) $customGroupXML->extends_entity_column_value_option_group,
247 'String',
248 ],
249 ];
250 $dao = &CRM_Core_DAO::executeQuery($sql, $params);
251
252 while ($dao->fetch()) {
253 $valueIDs[] = $dao->value;
254 }
255 }
256 if (!empty($valueIDs)) {
257 $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
258 $valueIDs
259 ) . CRM_Core_DAO::VALUE_SEPARATOR;
260
261 unset($valueIDs);
262
263 // Note: No need to set extends_entity_column_id here.
264
265 $saveAgain = TRUE;
266 }
267 }
268 else {
269 // when custom group extends 'Participant'
270 $sql = "
271 SELECT v.value
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'
275 AND v.name = %1
276 ";
277 $params = [
278 1 => [
279 (string ) $customGroupXML->extends_entity_column_value_option_group,
280 'String',
281 ],
282 ];
283 $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params);
284 if ($valueID) {
285 $customGroup->extends_entity_column_id = $valueID;
286 }
287
288 $optionIDs = [];
289 switch ($valueID) {
290 case 1:
291 // ParticipantRole
292 $condition = "AND v.name IN ( '{$optValues}' )";
293 $optionIDs = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
294 break;
295
296 case 2:
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);
300 break;
301
302 case 3:
303 // ParticipantEventType
304 $condition = "AND v.name IN ( '{$optValues}' )";
305 $optionIDs = CRM_Core_OptionGroup::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
306 break;
307 }
308
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;
313
314 $saveAgain = TRUE;
315 }
316 }
317 }
318
319 if ($saveAgain) {
320 $customGroup->save();
321 }
322
323 CRM_Core_BAO_CustomGroup::createTable($customGroup);
324 $idMap['custom_group'][$customGroup->name] = $customGroup->id;
325 }
326 }
327 }
328
329 /**
330 * @param SimpleXMLElement $xml
331 * @param array $idMap
332 */
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
337 // painfully slow).
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);
344 }
345
346 $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name];
347 $fields_indexed_by_group_id[$id][] = $customFieldXML;
348 }
349 }
350
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))
355 ->execute();
356 }
357 }
358
359 /**
360 * Get Option Group ID.
361 *
362 * Returns an option group's ID, given its name.
363 *
364 * @param $groupName
365 * @param array $idMap
366 *
367 * @return int|null
368 */
369 private function getOptionGroupIDFromName($groupName, &$idMap) {
370 if (empty($groupName)) {
371 return NULL;
372 }
373
374 if (!isset($idMap['option_group'][$groupName])) {
375 $idMap['option_group'][$groupName] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $groupName, 'id', 'name');
376 }
377
378 return $idMap['option_group'][$groupName];
379 }
380
381 /**
382 * @param SimpleXMLElement $xml
383 * @param array $idMap
384 */
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;
392 }
393 }
394 }
395
396 /**
397 * @param SimpleXMLElement $xml
398 * @param array $idMap
399 *
400 * @throws CRM_Core_Exception
401 */
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');
408
409 // fix field name
410 if (substr($profileField->field_name, 0, 7) == 'custom.') {
411 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name);
412 $sql = "
413 SELECT f.id
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
418 ";
419 $params = [
420 1 => [$tableName, 'String'],
421 2 => [$columnName, 'String'],
422 ];
423 $cfID = CRM_Core_DAO::singleValueQuery($sql, $params);
424 if (!$cfID) {
425 throw new CRM_Core_Exception(ts("Could not find custom field for %1, %2, %3",
426 [
427 1 => $profileField->field_name,
428 2 => $tableName,
429 3 => $columnName,
430 ]
431 ) . "<br />");
432 }
433 $profileField->field_name = "custom_{$cfID}";
434 }
435 $profileField->save();
436 }
437 }
438 }
439
440 /**
441 * @param SimpleXMLElement $xml
442 * @param array $idMap
443 */
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();
451 }
452 }
453 }
454
455 }