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