Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Utils_Migrate_Import { | |
5bc392e6 | 34 | /** |
b8c71ffa | 35 | * Class constructor. |
5bc392e6 | 36 | */ |
00be9182 | 37 | public function __construct() { |
3302658e | 38 | } |
6a488035 | 39 | |
32963c28 | 40 | /** |
b8c71ffa | 41 | * Import custom-data from an XML file. |
32963c28 | 42 | * |
77855840 TO |
43 | * @param string $file |
44 | * Path to an XML file. | |
b8c71ffa | 45 | * |
32963c28 | 46 | * @throws CRM_Core_Exception |
32963c28 | 47 | */ |
00be9182 | 48 | public function run($file) { |
6a488035 | 49 | // read xml file |
8080d1e0 | 50 | $dom = new DomDocument(); |
f9857c59 JP |
51 | $xmlString = file_get_contents($file); |
52 | $load = $dom->loadXML($xmlString); | |
6498e0de | 53 | if (!$load) { |
8080d1e0 TO |
54 | throw new CRM_Core_Exception("Failed to parse XML file \"$file\""); |
55 | } | |
6a488035 TO |
56 | $dom->xinclude(); |
57 | $xml = simplexml_import_dom($dom); | |
32963c28 TO |
58 | return $this->runXmlElement($xml); |
59 | } | |
6a488035 | 60 | |
32963c28 | 61 | /** |
b8c71ffa | 62 | * Import custom-data from an XML element. |
32963c28 TO |
63 | * |
64 | * @param SimpleXMLElement $xml | |
32963c28 | 65 | */ |
00be9182 | 66 | public function runXmlElement($xml) { |
3302658e TO |
67 | $idMap = array( |
68 | 'custom_group' => array(), | |
6a488035 TO |
69 | 'option_group' => array(), |
70 | ); | |
71 | ||
72 | // first create option groups and values if any | |
73 | $this->optionGroups($xml, $idMap); | |
74 | $this->optionValues($xml, $idMap); | |
75 | ||
76 | $this->relationshipTypes($xml); | |
77 | $this->contributionTypes($xml); | |
78 | ||
79 | // now create custom groups | |
80 | $this->customGroups($xml, $idMap); | |
81 | $this->customFields($xml, $idMap); | |
82 | ||
83 | // now create profile groups | |
84 | $this->profileGroups($xml, $idMap); | |
85 | $this->profileFields($xml, $idMap); | |
86 | $this->profileJoins($xml, $idMap); | |
87 | ||
50bfb460 | 88 | // create DB Template String sample data |
6a488035 TO |
89 | $this->dbTemplateString($xml, $idMap); |
90 | ||
91 | // clean up all caches etc | |
92 | CRM_Core_Config::clearDBCache(); | |
93 | } | |
94 | ||
5bc392e6 | 95 | /** |
c490a46a | 96 | * @param CRM_Core_DAO $dao |
5bc392e6 EM |
97 | * @param $xml |
98 | * @param bool $save | |
99 | * @param null $keyName | |
100 | * | |
101 | * @return bool | |
102 | */ | |
00be9182 | 103 | public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) { |
6a488035 TO |
104 | if ($keyName) { |
105 | if (isset($xml->$keyName)) { | |
106 | $dao->$keyName = (string ) $xml->$keyName; | |
107 | if ($dao->find(TRUE)) { | |
108 | CRM_Core_Session::setStatus(ts("Found %1, %2, %3", | |
3302658e TO |
109 | array( |
110 | 1 => $keyName, | |
111 | 2 => $dao->$keyName, | |
21dfd5f5 | 112 | 3 => $dao->__table, |
3302658e TO |
113 | ) |
114 | ), '', 'info'); | |
6a488035 TO |
115 | return FALSE; |
116 | } | |
117 | } | |
118 | } | |
119 | ||
353ffa53 | 120 | $fields = &$dao->fields(); |
6a488035 TO |
121 | foreach ($fields as $name => $dontCare) { |
122 | if (isset($xml->$name)) { | |
123 | $value = (string ) $xml->$name; | |
4f652853 | 124 | $value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR, |
6a488035 TO |
125 | CRM_Core_DAO::VALUE_SEPARATOR, |
126 | $value | |
127 | ); | |
128 | $dao->$name = $value; | |
129 | } | |
130 | } | |
131 | if ($save) { | |
132 | $dao->save(); | |
133 | } | |
134 | return TRUE; | |
135 | } | |
136 | ||
5bc392e6 EM |
137 | /** |
138 | * @param $xml | |
139 | * @param $idMap | |
140 | */ | |
00be9182 | 141 | public function optionGroups(&$xml, &$idMap) { |
6a488035 TO |
142 | foreach ($xml->OptionGroups as $optionGroupsXML) { |
143 | foreach ($optionGroupsXML->OptionGroup as $optionGroupXML) { | |
144 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
145 | $this->copyData($optionGroup, $optionGroupXML, TRUE, 'name'); | |
146 | $idMap['option_group'][$optionGroup->name] = $optionGroup->id; | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
5bc392e6 EM |
151 | /** |
152 | * @param $xml | |
153 | * @param $idMap | |
154 | */ | |
00be9182 | 155 | public function optionValues(&$xml, &$idMap) { |
6a488035 TO |
156 | foreach ($xml->OptionValues as $optionValuesXML) { |
157 | foreach ($optionValuesXML->OptionValue as $optionValueXML) { | |
158 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
159 | $optionValue->option_group_id = $idMap['option_group'][(string ) $optionValueXML->option_group_name]; | |
24f4fd77 | 160 | if (empty($optionValue->option_group_id)) { |
161 | //CRM-17410 check if option group already exist. | |
162 | $optionValue->option_group_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $optionValueXML->option_group_name, 'id', 'name'); | |
163 | } | |
6a488035 TO |
164 | $this->copyData($optionValue, $optionValueXML, FALSE, 'label'); |
165 | if (!isset($optionValue->value)) { | |
166 | $sql = " | |
167 | SELECT MAX(ROUND(v.value)) + 1 | |
168 | FROM civicrm_option_value v | |
169 | WHERE v.option_group_id = %1 | |
170 | "; | |
171 | $params = array(1 => array($optionValue->option_group_id, 'Integer')); | |
172 | $optionValue->value = CRM_Core_DAO::singleValueQuery($sql, $params); | |
173 | } | |
174 | $optionValue->save(); | |
175 | } | |
176 | } | |
177 | } | |
178 | ||
5bc392e6 EM |
179 | /** |
180 | * @param $xml | |
181 | */ | |
00be9182 | 182 | public function relationshipTypes(&$xml) { |
6a488035 TO |
183 | |
184 | foreach ($xml->RelationshipTypes as $relationshipTypesXML) { | |
185 | foreach ($relationshipTypesXML->RelationshipType as $relationshipTypeXML) { | |
186 | $relationshipType = new CRM_Contact_DAO_RelationshipType(); | |
187 | $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b'); | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
5bc392e6 EM |
192 | /** |
193 | * @param $xml | |
194 | */ | |
00be9182 | 195 | public function contributionTypes(&$xml) { |
6a488035 TO |
196 | |
197 | foreach ($xml->ContributionTypes as $contributionTypesXML) { | |
198 | foreach ($contributionTypesXML->ContributionType as $contributionTypeXML) { | |
3302658e | 199 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
6a488035 TO |
200 | $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name'); |
201 | } | |
202 | } | |
203 | } | |
204 | ||
5bc392e6 EM |
205 | /** |
206 | * @param $xml | |
207 | * @param $idMap | |
208 | */ | |
00be9182 | 209 | public function customGroups(&$xml, &$idMap) { |
6a488035 TO |
210 | foreach ($xml->CustomGroups as $customGroupsXML) { |
211 | foreach ($customGroupsXML->CustomGroup as $customGroupXML) { | |
212 | $customGroup = new CRM_Core_DAO_CustomGroup(); | |
213 | if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) { | |
214 | $idMap['custom_group'][$customGroup->name] = $customGroup->id; | |
215 | continue; | |
216 | } | |
217 | ||
218 | $saveAgain = FALSE; | |
219 | if (!isset($customGroup->table_name) || | |
220 | empty($customGroup->table_name) | |
221 | ) { | |
222 | // fix table name | |
223 | $customGroup->table_name = "civicrm_value_" . strtolower(CRM_Utils_String::munge($customGroup->title, '_', 32)) . "_{$customGroup->id}"; | |
224 | ||
225 | $saveAgain = TRUE; | |
226 | } | |
227 | ||
228 | // fix extends stuff if it exists | |
229 | if (isset($customGroupXML->extends_entity_column_value_option_group) && | |
353ffa53 TO |
230 | isset($customGroupXML->extends_entity_column_value) |
231 | ) { | |
9d1c4909 | 232 | $valueIDs = array(); |
0f2ea47d RN |
233 | $optionValues = explode(",", $customGroupXML->extends_entity_column_value); |
234 | $optValues = implode("','", $optionValues); | |
6a488035 | 235 | if (trim($customGroup->extends) != 'Participant') { |
0f2ea47d RN |
236 | if ($customGroup->extends == 'Relationship') { |
237 | foreach ($optionValues as $key => $value) { | |
238 | $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b'); | |
239 | $valueIDs[] = $relTypeId; | |
240 | } | |
241 | } | |
9d1c4909 RN |
242 | elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) { |
243 | $valueIDs = $optionValues; | |
244 | } | |
87c28cdd | 245 | elseif (in_array($customGroup->extends, array('Contribution', 'ContributionRecur'))) { |
246 | $sql = "SELECT id | |
247 | FROM civicrm_financial_type | |
248 | WHERE name IN ('{$optValues}')"; | |
249 | $dao = &CRM_Core_DAO::executeQuery($sql); | |
250 | while ($dao->fetch()) { | |
251 | $valueIDs[] = $dao->id; | |
252 | } | |
253 | } | |
0f2ea47d RN |
254 | else { |
255 | $sql = " | |
6a488035 TO |
256 | SELECT v.value |
257 | FROM civicrm_option_value v | |
258 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
259 | WHERE g.name = %1 | |
0f2ea47d | 260 | AND v.name IN ('$optValues') |
6a488035 | 261 | "; |
0f2ea47d RN |
262 | $params = array( |
263 | 1 => array( | |
264 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
265 | 'String', | |
266 | ), | |
267 | ); | |
353ffa53 | 268 | $dao = &CRM_Core_DAO::executeQuery($sql, $params); |
6a488035 | 269 | |
0f2ea47d RN |
270 | while ($dao->fetch()) { |
271 | $valueIDs[] = $dao->value; | |
272 | } | |
6a488035 TO |
273 | } |
274 | if (!empty($valueIDs)) { | |
275 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
276 | $valueIDs |
277 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 | 278 | |
0f2ea47d RN |
279 | unset($valueIDs); |
280 | ||
6a488035 TO |
281 | // Note: No need to set extends_entity_column_id here. |
282 | ||
283 | $saveAgain = TRUE; | |
284 | } | |
285 | } | |
286 | else { | |
287 | // when custom group extends 'Participant' | |
288 | $sql = " | |
289 | SELECT v.value | |
290 | FROM civicrm_option_value v | |
291 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
292 | WHERE g.name = 'custom_data_type' | |
293 | AND v.name = %1 | |
294 | "; | |
295 | $params = array( | |
3302658e TO |
296 | 1 => array( |
297 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
6a488035 | 298 | 'String', |
21dfd5f5 | 299 | ), |
3302658e | 300 | ); |
6a488035 TO |
301 | $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params); |
302 | if ($valueID) { | |
303 | $customGroup->extends_entity_column_id = $valueID; | |
304 | } | |
305 | ||
306 | $optionIDs = array(); | |
307 | switch ($valueID) { | |
308 | case 1: | |
309 | // ParticipantRole | |
310 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
311 | $optionIDs = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name'); | |
312 | break; | |
313 | ||
314 | case 2: | |
315 | // ParticipantEventName | |
316 | $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )"; | |
317 | $optionIDs = CRM_Event_PseudoConstant::event(NULL, FALSE, $condition); | |
318 | break; | |
319 | ||
320 | case 3: | |
321 | // ParticipantEventType | |
322 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
323 | $optionIDs = CRM_Core_OptionGroup::values('event_type', FALSE, FALSE, FALSE, $condition, 'name'); | |
324 | break; | |
325 | } | |
326 | ||
327 | if (is_array($optionIDs) && !empty($optionIDs)) { | |
328 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
329 | array_keys($optionIDs) |
330 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
331 | |
332 | $saveAgain = TRUE; | |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | if ($saveAgain) { | |
338 | $customGroup->save(); | |
339 | } | |
340 | ||
341 | CRM_Core_BAO_CustomGroup::createTable($customGroup); | |
342 | $idMap['custom_group'][$customGroup->name] = $customGroup->id; | |
343 | } | |
344 | } | |
345 | } | |
346 | ||
5bc392e6 EM |
347 | /** |
348 | * @param $xml | |
349 | * @param $idMap | |
350 | */ | |
00be9182 | 351 | public function customFields(&$xml, &$idMap) { |
6a488035 TO |
352 | // Re-index by group id so we can build out the custom fields one table |
353 | // at a time, and then rebuild the table triggers at the end, rather than | |
354 | // rebuilding the table triggers after each field is added (which is | |
355 | // painfully slow). | |
356 | $fields_indexed_by_group_id = array(); | |
357 | foreach ($xml->CustomFields as $customFieldsXML) { | |
358 | $total = count($customFieldsXML->CustomField); | |
359 | foreach ($customFieldsXML->CustomField as $customFieldXML) { | |
360 | $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name]; | |
361 | $fields_indexed_by_group_id[$id][] = $customFieldXML; | |
362 | } | |
363 | } | |
e2087a11 | 364 | foreach ($fields_indexed_by_group_id as $group_id => $fields) { |
6a488035 TO |
365 | $total = count($fields); |
366 | $count = 0; | |
e2087a11 | 367 | foreach ($fields as $customFieldXML) { |
6a488035 TO |
368 | $count++; |
369 | $customField = new CRM_Core_DAO_CustomField(); | |
370 | $customField->custom_group_id = $group_id; | |
371 | $skipStore = FALSE; | |
372 | if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) { | |
373 | $skipStore = TRUE; | |
374 | } | |
375 | ||
376 | if (empty($customField->option_group_id) && | |
377 | isset($customFieldXML->option_group_name) | |
378 | ) { | |
379 | $customField->option_group_id = $idMap['option_group'][(string ) $customFieldXML->option_group_name]; | |
380 | } | |
381 | if ($skipStore) { | |
382 | continue; | |
383 | } | |
384 | $customField->save(); | |
385 | ||
386 | // Only rebuild the table's trigger on the last field added to avoid un-necessary | |
387 | // and slow rebuilds when adding many fields at the same time. | |
388 | $triggerRebuild = FALSE; | |
3302658e | 389 | if ($count == $total) { |
6a488035 | 390 | $triggerRebuild = TRUE; |
3302658e | 391 | } |
6a488035 TO |
392 | $indexExist = FALSE; |
393 | CRM_Core_BAO_CustomField::createField($customField, 'add', $indexExist, $triggerRebuild); | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
5bc392e6 EM |
398 | /** |
399 | * @param $xml | |
400 | * @param $idMap | |
401 | */ | |
00be9182 | 402 | public function dbTemplateString(&$xml, &$idMap) { |
6a488035 TO |
403 | foreach ($xml->Persistent as $persistentXML) { |
404 | foreach ($persistentXML->Persistent as $persistent) { | |
405 | $persistentObj = new CRM_Core_DAO_Persistent(); | |
406 | ||
407 | if ($persistent->is_config == 1) { | |
408 | $persistent->data = serialize(explode(',', $persistent->data)); | |
409 | } | |
410 | $this->copyData($persistentObj, $persistent, TRUE, 'context'); | |
411 | } | |
412 | } | |
413 | } | |
414 | ||
5bc392e6 EM |
415 | /** |
416 | * @param $xml | |
417 | * @param $idMap | |
418 | */ | |
00be9182 | 419 | public function profileGroups(&$xml, &$idMap) { |
6a488035 TO |
420 | foreach ($xml->ProfileGroups as $profileGroupsXML) { |
421 | foreach ($profileGroupsXML->ProfileGroup as $profileGroupXML) { | |
422 | $profileGroup = new CRM_Core_DAO_UFGroup(); | |
423 | $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title'); | |
424 | $idMap['profile_group'][$profileGroup->name] = $profileGroup->id; | |
425 | $idMap['profile_group'][$profileGroup->title] = $profileGroup->id; | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
5bc392e6 EM |
430 | /** |
431 | * @param $xml | |
432 | * @param $idMap | |
433 | * | |
434 | * @throws Exception | |
435 | */ | |
00be9182 | 436 | public function profileFields(&$xml, &$idMap) { |
6a488035 TO |
437 | foreach ($xml->ProfileFields as $profileFieldsXML) { |
438 | foreach ($profileFieldsXML->ProfileField as $profileFieldXML) { | |
439 | $profileField = new CRM_Core_DAO_UFField(); | |
440 | $profileField->uf_group_id = $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name]; | |
441 | $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name'); | |
442 | ||
443 | // fix field name | |
444 | if (substr($profileField->field_name, 0, 7) == 'custom.') { | |
445 | list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name); | |
446 | $sql = " | |
447 | SELECT f.id | |
448 | FROM civicrm_custom_field f | |
449 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id | |
450 | WHERE g.table_name = %1 | |
451 | AND f.column_name = %2 | |
452 | "; | |
3302658e TO |
453 | $params = array( |
454 | 1 => array($tableName, 'String'), | |
6a488035 TO |
455 | 2 => array($columnName, 'String'), |
456 | ); | |
457 | $cfID = CRM_Core_DAO::singleValueQuery($sql, $params); | |
458 | if (!$cfID) { | |
459 | CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3", | |
353ffa53 TO |
460 | array( |
461 | 1 => $profileField->field_name, | |
462 | 2 => $tableName, | |
463 | 3 => $columnName, | |
464 | ) | |
465 | ) . "<br />"); | |
6a488035 TO |
466 | } |
467 | $profileField->field_name = "custom_{$cfID}"; | |
468 | } | |
469 | $profileField->save(); | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
5bc392e6 EM |
474 | /** |
475 | * @param $xml | |
476 | * @param $idMap | |
477 | */ | |
00be9182 | 478 | public function profileJoins(&$xml, &$idMap) { |
6a488035 TO |
479 | foreach ($xml->ProfileJoins as $profileJoinsXML) { |
480 | foreach ($profileJoinsXML->ProfileJoin as $profileJoinXML) { | |
481 | $profileJoin = new CRM_Core_DAO_UFJoin(); | |
482 | $profileJoin->uf_group_id = $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name]; | |
483 | $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module'); | |
484 | $profileJoin->save(); | |
485 | } | |
486 | } | |
487 | } | |
96025800 | 488 | |
6a488035 | 489 | } |