Merge pull request #13985 from seamuslee001/new_coder_crm_utils
[civicrm-core.git] / CRM / Utils / Migrate / Import.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Utils_Migrate_Import {
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 }
40
41 /**
42 * Import custom-data from an XML file.
43 *
44 * @param string $file
45 * Path to an XML file.
46 *
47 * @throws CRM_Core_Exception
48 */
49 public function run($file) {
50 // read xml file
51 $dom = new DomDocument();
52 $xmlString = file_get_contents($file);
53 $load = $dom->loadXML($xmlString);
54 if (!$load) {
55 throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
56 }
57 $dom->xinclude();
58 $xml = simplexml_import_dom($dom);
59 return $this->runXmlElement($xml);
60 }
61
62 /**
63 * Import custom-data from an XML element.
64 *
65 * @param SimpleXMLElement $xml
66 */
67 public function runXmlElement($xml) {
68 $idMap = [
69 'custom_group' => [],
70 'option_group' => [],
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
89 // create DB Template String sample data
90 $this->dbTemplateString($xml, $idMap);
91
92 // clean up all caches etc
93 CRM_Core_Config::clearDBCache();
94 }
95
96 /**
97 * @param CRM_Core_DAO $dao
98 * @param $xml
99 * @param bool $save
100 * @param null $keyName
101 *
102 * @return bool
103 */
104 public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
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",
110 [
111 1 => $keyName,
112 2 => $dao->$keyName,
113 3 => $dao->__table,
114 ]
115 ), '', 'info');
116 return FALSE;
117 }
118 }
119 }
120
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,
127 $value
128 );
129 $dao->$name = $value;
130 }
131 }
132 if ($save) {
133 $dao->save();
134 }
135 return TRUE;
136 }
137
138 /**
139 * @param $xml
140 * @param $idMap
141 */
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;
148 }
149 }
150 }
151
152 /**
153 * @param $xml
154 * @param $idMap
155 */
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');
164 }
165 $this->copyData($optionValue, $optionValueXML, FALSE, 'label');
166 if (!isset($optionValue->value)) {
167 $sql = "
168 SELECT MAX(ROUND(v.value)) + 1
169 FROM civicrm_option_value v
170 WHERE v.option_group_id = %1
171 ";
172 $params = [1 => [$optionValue->option_group_id, 'Integer']];
173 $optionValue->value = CRM_Core_DAO::singleValueQuery($sql, $params);
174 }
175 $optionValue->save();
176 }
177 }
178 }
179
180 /**
181 * @param $xml
182 */
183 public function relationshipTypes(&$xml) {
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
193 /**
194 * @param $xml
195 */
196 public function contributionTypes(&$xml) {
197
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');
202 }
203 }
204 }
205
206 /**
207 * @param $xml
208 * @param $idMap
209 */
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;
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) &&
231 isset($customGroupXML->extends_entity_column_value)
232 ) {
233 $valueIDs = [];
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;
241 }
242 }
243 elseif (in_array($customGroup->extends, ['Individual', 'Organization', 'Household'])) {
244 $valueIDs = $optionValues;
245 }
246 elseif (in_array($customGroup->extends, ['Contribution', 'ContributionRecur'])) {
247 $sql = "SELECT id
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;
253 }
254 }
255 else {
256 $sql = "
257 SELECT v.value
258 FROM civicrm_option_value v
259 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
260 WHERE g.name = %1
261 AND v.name IN ('$optValues')
262 ";
263 $params = [
264 1 => [
265 (string ) $customGroupXML->extends_entity_column_value_option_group,
266 'String',
267 ],
268 ];
269 $dao = &CRM_Core_DAO::executeQuery($sql, $params);
270
271 while ($dao->fetch()) {
272 $valueIDs[] = $dao->value;
273 }
274 }
275 if (!empty($valueIDs)) {
276 $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
277 $valueIDs
278 ) . CRM_Core_DAO::VALUE_SEPARATOR;
279
280 unset($valueIDs);
281
282 // Note: No need to set extends_entity_column_id here.
283
284 $saveAgain = TRUE;
285 }
286 }
287 else {
288 // when custom group extends 'Participant'
289 $sql = "
290 SELECT v.value
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'
294 AND v.name = %1
295 ";
296 $params = [
297 1 => [
298 (string ) $customGroupXML->extends_entity_column_value_option_group,
299 'String',
300 ],
301 ];
302 $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params);
303 if ($valueID) {
304 $customGroup->extends_entity_column_id = $valueID;
305 }
306
307 $optionIDs = [];
308 switch ($valueID) {
309 case 1:
310 // ParticipantRole
311 $condition = "AND v.name IN ( '{$optValues}' )";
312 $optionIDs = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
313 break;
314
315 case 2:
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);
319 break;
320
321 case 3:
322 // ParticipantEventType
323 $condition = "AND v.name IN ( '{$optValues}' )";
324 $optionIDs = CRM_Core_OptionGroup::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
325 break;
326 }
327
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;
332
333 $saveAgain = TRUE;
334 }
335 }
336 }
337
338 if ($saveAgain) {
339 $customGroup->save();
340 }
341
342 CRM_Core_BAO_CustomGroup::createTable($customGroup);
343 $idMap['custom_group'][$customGroup->name] = $customGroup->id;
344 }
345 }
346 }
347
348 /**
349 * @param $xml
350 * @param $idMap
351 */
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
356 // painfully slow).
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;
363 }
364 }
365 foreach ($fields_indexed_by_group_id as $group_id => $fields) {
366 $total = count($fields);
367 $count = 0;
368 foreach ($fields as $customFieldXML) {
369 $count++;
370 $customField = new CRM_Core_DAO_CustomField();
371 $customField->custom_group_id = $group_id;
372 $skipStore = FALSE;
373 if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
374 $skipStore = TRUE;
375 }
376
377 if (empty($customField->option_group_id) &&
378 isset($customFieldXML->option_group_name)
379 ) {
380 $customField->option_group_id = $idMap['option_group'][(string ) $customFieldXML->option_group_name];
381 }
382 if ($skipStore) {
383 continue;
384 }
385 $customField->save();
386
387 // Only rebuild the table's trigger on the last field added to avoid un-necessary
388 // and slow rebuilds when adding many fields at the same time.
389 $triggerRebuild = FALSE;
390 if ($count == $total) {
391 $triggerRebuild = TRUE;
392 }
393 $indexExist = FALSE;
394 CRM_Core_BAO_CustomField::createField($customField, 'add', $indexExist, $triggerRebuild);
395 }
396 }
397 }
398
399 /**
400 * @param $xml
401 * @param $idMap
402 */
403 public function dbTemplateString(&$xml, &$idMap) {
404 foreach ($xml->Persistent as $persistentXML) {
405 foreach ($persistentXML->Persistent as $persistent) {
406 $persistentObj = new CRM_Core_DAO_Persistent();
407
408 if ($persistent->is_config == 1) {
409 $persistent->data = serialize(explode(',', $persistent->data));
410 }
411 $this->copyData($persistentObj, $persistent, TRUE, 'context');
412 }
413 }
414 }
415
416 /**
417 * @param $xml
418 * @param $idMap
419 */
420 public function profileGroups(&$xml, &$idMap) {
421 foreach ($xml->ProfileGroups as $profileGroupsXML) {
422 foreach ($profileGroupsXML->ProfileGroup as $profileGroupXML) {
423 $profileGroup = new CRM_Core_DAO_UFGroup();
424 $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
425 $idMap['profile_group'][$profileGroup->name] = $profileGroup->id;
426 $idMap['profile_group'][$profileGroup->title] = $profileGroup->id;
427 }
428 }
429 }
430
431 /**
432 * @param $xml
433 * @param $idMap
434 *
435 * @throws Exception
436 */
437 public function profileFields(&$xml, &$idMap) {
438 foreach ($xml->ProfileFields as $profileFieldsXML) {
439 foreach ($profileFieldsXML->ProfileField as $profileFieldXML) {
440 $profileField = new CRM_Core_DAO_UFField();
441 $profileField->uf_group_id = $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name];
442 $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
443
444 // fix field name
445 if (substr($profileField->field_name, 0, 7) == 'custom.') {
446 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name);
447 $sql = "
448 SELECT f.id
449 FROM civicrm_custom_field f
450 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
451 WHERE g.table_name = %1
452 AND f.column_name = %2
453 ";
454 $params = [
455 1 => [$tableName, 'String'],
456 2 => [$columnName, 'String'],
457 ];
458 $cfID = CRM_Core_DAO::singleValueQuery($sql, $params);
459 if (!$cfID) {
460 CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3",
461 [
462 1 => $profileField->field_name,
463 2 => $tableName,
464 3 => $columnName,
465 ]
466 ) . "<br />");
467 }
468 $profileField->field_name = "custom_{$cfID}";
469 }
470 $profileField->save();
471 }
472 }
473 }
474
475 /**
476 * @param $xml
477 * @param $idMap
478 */
479 public function profileJoins(&$xml, &$idMap) {
480 foreach ($xml->ProfileJoins as $profileJoinsXML) {
481 foreach ($profileJoinsXML->ProfileJoin as $profileJoinXML) {
482 $profileJoin = new CRM_Core_DAO_UFJoin();
483 $profileJoin->uf_group_id = $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name];
484 $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
485 $profileJoin->save();
486 }
487 }
488 }
489
490 }