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