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