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