Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Utils_Migrate_Import { | |
6714d8d2 | 18 | |
5bc392e6 | 19 | /** |
b8c71ffa | 20 | * Class constructor. |
5bc392e6 | 21 | */ |
00be9182 | 22 | public function __construct() { |
3302658e | 23 | } |
6a488035 | 24 | |
32963c28 | 25 | /** |
b8c71ffa | 26 | * Import custom-data from an XML file. |
32963c28 | 27 | * |
77855840 TO |
28 | * @param string $file |
29 | * Path to an XML file. | |
b8c71ffa | 30 | * |
32963c28 | 31 | * @throws CRM_Core_Exception |
32963c28 | 32 | */ |
00be9182 | 33 | public function run($file) { |
6a488035 | 34 | // read xml file |
8080d1e0 | 35 | $dom = new DomDocument(); |
f9857c59 JP |
36 | $xmlString = file_get_contents($file); |
37 | $load = $dom->loadXML($xmlString); | |
6498e0de | 38 | if (!$load) { |
8080d1e0 TO |
39 | throw new CRM_Core_Exception("Failed to parse XML file \"$file\""); |
40 | } | |
6a488035 TO |
41 | $dom->xinclude(); |
42 | $xml = simplexml_import_dom($dom); | |
32963c28 TO |
43 | return $this->runXmlElement($xml); |
44 | } | |
6a488035 | 45 | |
32963c28 | 46 | /** |
b8c71ffa | 47 | * Import custom-data from an XML element. |
32963c28 TO |
48 | * |
49 | * @param SimpleXMLElement $xml | |
32963c28 | 50 | */ |
00be9182 | 51 | public function runXmlElement($xml) { |
be2fb01f CW |
52 | $idMap = [ |
53 | 'custom_group' => [], | |
54 | 'option_group' => [], | |
55 | ]; | |
6a488035 TO |
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 | ||
6a488035 TO |
73 | // clean up all caches etc |
74 | CRM_Core_Config::clearDBCache(); | |
75 | } | |
76 | ||
5bc392e6 | 77 | /** |
c490a46a | 78 | * @param CRM_Core_DAO $dao |
5bc392e6 EM |
79 | * @param $xml |
80 | * @param bool $save | |
81 | * @param null $keyName | |
82 | * | |
83 | * @return bool | |
84 | */ | |
00be9182 | 85 | public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) { |
6a488035 TO |
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", | |
be2fb01f | 91 | [ |
3302658e TO |
92 | 1 => $keyName, |
93 | 2 => $dao->$keyName, | |
21dfd5f5 | 94 | 3 => $dao->__table, |
be2fb01f | 95 | ] |
3302658e | 96 | ), '', 'info'); |
6a488035 TO |
97 | return FALSE; |
98 | } | |
99 | } | |
100 | } | |
101 | ||
353ffa53 | 102 | $fields = &$dao->fields(); |
6a488035 TO |
103 | foreach ($fields as $name => $dontCare) { |
104 | if (isset($xml->$name)) { | |
105 | $value = (string ) $xml->$name; | |
4f652853 | 106 | $value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR, |
6a488035 TO |
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 | ||
5bc392e6 EM |
119 | /** |
120 | * @param $xml | |
121 | * @param $idMap | |
122 | */ | |
00be9182 | 123 | public function optionGroups(&$xml, &$idMap) { |
6a488035 TO |
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 | ||
5bc392e6 EM |
133 | /** |
134 | * @param $xml | |
135 | * @param $idMap | |
136 | */ | |
00be9182 | 137 | public function optionValues(&$xml, &$idMap) { |
6a488035 TO |
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]; | |
24f4fd77 | 142 | if (empty($optionValue->option_group_id)) { |
143 | //CRM-17410 check if option group already exist. | |
45a8391e | 144 | $optionValue->option_group_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', (string) $optionValueXML->option_group_name, 'id', 'name'); |
24f4fd77 | 145 | } |
6a488035 TO |
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 | "; | |
be2fb01f | 153 | $params = [1 => [$optionValue->option_group_id, 'Integer']]; |
6a488035 TO |
154 | $optionValue->value = CRM_Core_DAO::singleValueQuery($sql, $params); |
155 | } | |
156 | $optionValue->save(); | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
5bc392e6 EM |
161 | /** |
162 | * @param $xml | |
163 | */ | |
00be9182 | 164 | public function relationshipTypes(&$xml) { |
6a488035 TO |
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 | ||
5bc392e6 EM |
174 | /** |
175 | * @param $xml | |
176 | */ | |
00be9182 | 177 | public function contributionTypes(&$xml) { |
6a488035 TO |
178 | |
179 | foreach ($xml->ContributionTypes as $contributionTypesXML) { | |
180 | foreach ($contributionTypesXML->ContributionType as $contributionTypeXML) { | |
3302658e | 181 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
6a488035 TO |
182 | $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name'); |
183 | } | |
184 | } | |
185 | } | |
186 | ||
5bc392e6 EM |
187 | /** |
188 | * @param $xml | |
189 | * @param $idMap | |
190 | */ | |
00be9182 | 191 | public function customGroups(&$xml, &$idMap) { |
6a488035 TO |
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) && | |
353ffa53 TO |
212 | isset($customGroupXML->extends_entity_column_value) |
213 | ) { | |
be2fb01f | 214 | $valueIDs = []; |
0f2ea47d RN |
215 | $optionValues = explode(",", $customGroupXML->extends_entity_column_value); |
216 | $optValues = implode("','", $optionValues); | |
6a488035 | 217 | if (trim($customGroup->extends) != 'Participant') { |
0f2ea47d RN |
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 | } | |
be2fb01f | 224 | elseif (in_array($customGroup->extends, ['Individual', 'Organization', 'Household'])) { |
9d1c4909 RN |
225 | $valueIDs = $optionValues; |
226 | } | |
be2fb01f | 227 | elseif (in_array($customGroup->extends, ['Contribution', 'ContributionRecur'])) { |
87c28cdd | 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 | } | |
0f2ea47d RN |
236 | else { |
237 | $sql = " | |
6a488035 TO |
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 | |
0f2ea47d | 242 | AND v.name IN ('$optValues') |
6a488035 | 243 | "; |
be2fb01f CW |
244 | $params = [ |
245 | 1 => [ | |
0f2ea47d RN |
246 | (string ) $customGroupXML->extends_entity_column_value_option_group, |
247 | 'String', | |
be2fb01f CW |
248 | ], |
249 | ]; | |
353ffa53 | 250 | $dao = &CRM_Core_DAO::executeQuery($sql, $params); |
6a488035 | 251 | |
0f2ea47d RN |
252 | while ($dao->fetch()) { |
253 | $valueIDs[] = $dao->value; | |
254 | } | |
6a488035 TO |
255 | } |
256 | if (!empty($valueIDs)) { | |
257 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
258 | $valueIDs |
259 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 | 260 | |
0f2ea47d RN |
261 | unset($valueIDs); |
262 | ||
6a488035 TO |
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 | "; | |
be2fb01f CW |
277 | $params = [ |
278 | 1 => [ | |
3302658e | 279 | (string ) $customGroupXML->extends_entity_column_value_option_group, |
6a488035 | 280 | 'String', |
be2fb01f CW |
281 | ], |
282 | ]; | |
6a488035 TO |
283 | $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params); |
284 | if ($valueID) { | |
285 | $customGroup->extends_entity_column_id = $valueID; | |
286 | } | |
287 | ||
be2fb01f | 288 | $optionIDs = []; |
6a488035 TO |
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, | |
353ffa53 TO |
311 | array_keys($optionIDs) |
312 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
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 | ||
5bc392e6 EM |
329 | /** |
330 | * @param $xml | |
331 | * @param $idMap | |
332 | */ | |
00be9182 | 333 | public function customFields(&$xml, &$idMap) { |
6a488035 TO |
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). | |
be2fb01f | 338 | $fields_indexed_by_group_id = []; |
6a488035 TO |
339 | foreach ($xml->CustomFields as $customFieldsXML) { |
340 | $total = count($customFieldsXML->CustomField); | |
341 | foreach ($customFieldsXML->CustomField as $customFieldXML) { | |
170f25ff CR |
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 | ||
6a488035 TO |
346 | $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name]; |
347 | $fields_indexed_by_group_id[$id][] = $customFieldXML; | |
348 | } | |
349 | } | |
170f25ff | 350 | |
e2087a11 | 351 | foreach ($fields_indexed_by_group_id as $group_id => $fields) { |
fe806431 | 352 | \Civi\Api4\CustomField::save(FALSE) |
170f25ff CR |
353 | ->setDefaults(['custom_group_id' => $group_id]) |
354 | ->setRecords(json_decode(json_encode($fields), TRUE)) | |
355 | ->execute(); | |
6a488035 TO |
356 | } |
357 | } | |
358 | ||
170f25ff CR |
359 | /** |
360 | * Get Option Group ID. | |
361 | * | |
362 | * Returns an option group's ID, given its name. | |
363 | * | |
364 | * @param $groupName | |
365 | * @param $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 | ||
5bc392e6 EM |
381 | /** |
382 | * @param $xml | |
383 | * @param $idMap | |
384 | */ | |
00be9182 | 385 | public function profileGroups(&$xml, &$idMap) { |
6a488035 TO |
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 | ||
5bc392e6 EM |
396 | /** |
397 | * @param $xml | |
398 | * @param $idMap | |
399 | * | |
ee3db087 | 400 | * @throws CRM_Core_Exception |
5bc392e6 | 401 | */ |
00be9182 | 402 | public function profileFields(&$xml, &$idMap) { |
6a488035 TO |
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 | "; | |
be2fb01f CW |
419 | $params = [ |
420 | 1 => [$tableName, 'String'], | |
421 | 2 => [$columnName, 'String'], | |
422 | ]; | |
6a488035 TO |
423 | $cfID = CRM_Core_DAO::singleValueQuery($sql, $params); |
424 | if (!$cfID) { | |
ee3db087 | 425 | throw new CRM_Core_Exception(ts("Could not find custom field for %1, %2, %3", |
be2fb01f | 426 | [ |
353ffa53 TO |
427 | 1 => $profileField->field_name, |
428 | 2 => $tableName, | |
429 | 3 => $columnName, | |
be2fb01f | 430 | ] |
353ffa53 | 431 | ) . "<br />"); |
6a488035 TO |
432 | } |
433 | $profileField->field_name = "custom_{$cfID}"; | |
434 | } | |
435 | $profileField->save(); | |
436 | } | |
437 | } | |
438 | } | |
439 | ||
5bc392e6 EM |
440 | /** |
441 | * @param $xml | |
442 | * @param $idMap | |
443 | */ | |
00be9182 | 444 | public function profileJoins(&$xml, &$idMap) { |
6a488035 TO |
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 | } | |
96025800 | 454 | |
6a488035 | 455 | } |