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