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