Commit | Line | Data |
---|---|---|
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 | */ | |
35 | class CRM_Utils_Migrate_Import { | |
3302658e TO |
36 | function __construct() { |
37 | } | |
6a488035 | 38 | |
32963c28 TO |
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 | */ | |
6a488035 | 46 | function run($file) { |
6a488035 | 47 | // read xml file |
8080d1e0 TO |
48 | $dom = new DomDocument(); |
49 | if (! $dom->load($file)) { | |
50 | throw new CRM_Core_Exception("Failed to parse XML file \"$file\""); | |
51 | } | |
6a488035 TO |
52 | $dom->xinclude(); |
53 | $xml = simplexml_import_dom($dom); | |
32963c28 TO |
54 | return $this->runXmlElement($xml); |
55 | } | |
6a488035 | 56 | |
32963c28 TO |
57 | /** |
58 | * Import custom-data from an XML element | |
59 | * | |
60 | * @param SimpleXMLElement $xml | |
61 | * @return void | |
62 | */ | |
63 | function runXmlElement($xml) { | |
3302658e TO |
64 | $idMap = array( |
65 | 'custom_group' => array(), | |
6a488035 TO |
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", | |
3302658e TO |
98 | array( |
99 | 1 => $keyName, | |
100 | 2 => $dao->$keyName, | |
101 | 3 => $dao->__table | |
102 | ) | |
103 | ), '', 'info'); | |
6a488035 TO |
104 | return FALSE; |
105 | } | |
106 | } | |
107 | } | |
108 | ||
3302658e | 109 | $fields = & $dao->fields(); |
6a488035 TO |
110 | foreach ($fields as $name => $dontCare) { |
111 | if (isset($xml->$name)) { | |
112 | $value = (string ) $xml->$name; | |
4f652853 | 113 | $value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR, |
6a488035 TO |
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) { | |
3302658e | 170 | $contributionType = new CRM_Financial_DAO_FinancialType(); |
6a488035 TO |
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) && | |
0f2ea47d RN |
197 | isset($customGroupXML->extends_entity_column_value) |
198 | ) { | |
199 | $optionValues = explode(",", $customGroupXML->extends_entity_column_value); | |
200 | $optValues = implode("','", $optionValues); | |
6a488035 | 201 | if (trim($customGroup->extends) != 'Participant') { |
0f2ea47d RN |
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 | else { | |
209 | $sql = " | |
6a488035 TO |
210 | SELECT v.value |
211 | FROM civicrm_option_value v | |
212 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
213 | WHERE g.name = %1 | |
0f2ea47d | 214 | AND v.name IN ('$optValues') |
6a488035 | 215 | "; |
0f2ea47d RN |
216 | $params = array( |
217 | 1 => array( | |
218 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
219 | 'String', | |
220 | ), | |
221 | ); | |
222 | $dao = & CRM_Core_DAO::executeQuery($sql, $params); | |
6a488035 | 223 | |
0f2ea47d RN |
224 | $valueIDs = array(); |
225 | while ($dao->fetch()) { | |
226 | $valueIDs[] = $dao->value; | |
227 | } | |
6a488035 TO |
228 | } |
229 | if (!empty($valueIDs)) { | |
230 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
231 | $valueIDs | |
232 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
233 | ||
0f2ea47d RN |
234 | unset($valueIDs); |
235 | ||
6a488035 TO |
236 | // Note: No need to set extends_entity_column_id here. |
237 | ||
238 | $saveAgain = TRUE; | |
239 | } | |
240 | } | |
241 | else { | |
242 | // when custom group extends 'Participant' | |
243 | $sql = " | |
244 | SELECT v.value | |
245 | FROM civicrm_option_value v | |
246 | INNER JOIN civicrm_option_group g ON g.id = v.option_group_id | |
247 | WHERE g.name = 'custom_data_type' | |
248 | AND v.name = %1 | |
249 | "; | |
250 | $params = array( | |
3302658e TO |
251 | 1 => array( |
252 | (string ) $customGroupXML->extends_entity_column_value_option_group, | |
6a488035 | 253 | 'String', |
3302658e TO |
254 | ) |
255 | ); | |
6a488035 TO |
256 | $valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params); |
257 | if ($valueID) { | |
258 | $customGroup->extends_entity_column_id = $valueID; | |
259 | } | |
260 | ||
261 | $optionIDs = array(); | |
262 | switch ($valueID) { | |
263 | case 1: | |
264 | // ParticipantRole | |
265 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
266 | $optionIDs = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name'); | |
267 | break; | |
268 | ||
269 | case 2: | |
270 | // ParticipantEventName | |
271 | $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )"; | |
272 | $optionIDs = CRM_Event_PseudoConstant::event(NULL, FALSE, $condition); | |
273 | break; | |
274 | ||
275 | case 3: | |
276 | // ParticipantEventType | |
277 | $condition = "AND v.name IN ( '{$optValues}' )"; | |
278 | $optionIDs = CRM_Core_OptionGroup::values('event_type', FALSE, FALSE, FALSE, $condition, 'name'); | |
279 | break; | |
280 | } | |
281 | ||
282 | if (is_array($optionIDs) && !empty($optionIDs)) { | |
283 | $customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
284 | array_keys($optionIDs) | |
285 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
286 | ||
287 | $saveAgain = TRUE; | |
288 | } | |
289 | } | |
290 | } | |
291 | ||
292 | if ($saveAgain) { | |
293 | $customGroup->save(); | |
294 | } | |
295 | ||
296 | CRM_Core_BAO_CustomGroup::createTable($customGroup); | |
297 | $idMap['custom_group'][$customGroup->name] = $customGroup->id; | |
298 | } | |
299 | } | |
300 | } | |
301 | ||
302 | function customFields(&$xml, &$idMap) { | |
303 | // Re-index by group id so we can build out the custom fields one table | |
304 | // at a time, and then rebuild the table triggers at the end, rather than | |
305 | // rebuilding the table triggers after each field is added (which is | |
306 | // painfully slow). | |
307 | $fields_indexed_by_group_id = array(); | |
308 | foreach ($xml->CustomFields as $customFieldsXML) { | |
309 | $total = count($customFieldsXML->CustomField); | |
310 | foreach ($customFieldsXML->CustomField as $customFieldXML) { | |
311 | $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name]; | |
312 | $fields_indexed_by_group_id[$id][] = $customFieldXML; | |
313 | } | |
314 | } | |
3302658e | 315 | while (list($group_id, $fields) = each($fields_indexed_by_group_id)) { |
6a488035 TO |
316 | $total = count($fields); |
317 | $count = 0; | |
3302658e | 318 | while (list(, $customFieldXML) = each($fields)) { |
6a488035 TO |
319 | $count++; |
320 | $customField = new CRM_Core_DAO_CustomField(); | |
321 | $customField->custom_group_id = $group_id; | |
322 | $skipStore = FALSE; | |
323 | if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) { | |
324 | $skipStore = TRUE; | |
325 | } | |
326 | ||
327 | if (empty($customField->option_group_id) && | |
328 | isset($customFieldXML->option_group_name) | |
329 | ) { | |
330 | $customField->option_group_id = $idMap['option_group'][(string ) $customFieldXML->option_group_name]; | |
331 | } | |
332 | if ($skipStore) { | |
333 | continue; | |
334 | } | |
335 | $customField->save(); | |
336 | ||
337 | // Only rebuild the table's trigger on the last field added to avoid un-necessary | |
338 | // and slow rebuilds when adding many fields at the same time. | |
339 | $triggerRebuild = FALSE; | |
3302658e | 340 | if ($count == $total) { |
6a488035 | 341 | $triggerRebuild = TRUE; |
3302658e | 342 | } |
6a488035 TO |
343 | $indexExist = FALSE; |
344 | CRM_Core_BAO_CustomField::createField($customField, 'add', $indexExist, $triggerRebuild); | |
345 | } | |
346 | } | |
347 | } | |
348 | ||
349 | function dbTemplateString(&$xml, &$idMap) { | |
350 | foreach ($xml->Persistent as $persistentXML) { | |
351 | foreach ($persistentXML->Persistent as $persistent) { | |
352 | $persistentObj = new CRM_Core_DAO_Persistent(); | |
353 | ||
354 | if ($persistent->is_config == 1) { | |
355 | $persistent->data = serialize(explode(',', $persistent->data)); | |
356 | } | |
357 | $this->copyData($persistentObj, $persistent, TRUE, 'context'); | |
358 | } | |
359 | } | |
360 | } | |
361 | ||
362 | function profileGroups(&$xml, &$idMap) { | |
363 | foreach ($xml->ProfileGroups as $profileGroupsXML) { | |
364 | foreach ($profileGroupsXML->ProfileGroup as $profileGroupXML) { | |
365 | $profileGroup = new CRM_Core_DAO_UFGroup(); | |
366 | $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title'); | |
367 | $idMap['profile_group'][$profileGroup->name] = $profileGroup->id; | |
368 | $idMap['profile_group'][$profileGroup->title] = $profileGroup->id; | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | function profileFields(&$xml, &$idMap) { | |
374 | foreach ($xml->ProfileFields as $profileFieldsXML) { | |
375 | foreach ($profileFieldsXML->ProfileField as $profileFieldXML) { | |
376 | $profileField = new CRM_Core_DAO_UFField(); | |
377 | $profileField->uf_group_id = $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name]; | |
378 | $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name'); | |
379 | ||
380 | // fix field name | |
381 | if (substr($profileField->field_name, 0, 7) == 'custom.') { | |
382 | list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name); | |
383 | $sql = " | |
384 | SELECT f.id | |
385 | FROM civicrm_custom_field f | |
386 | INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id | |
387 | WHERE g.table_name = %1 | |
388 | AND f.column_name = %2 | |
389 | "; | |
3302658e TO |
390 | $params = array( |
391 | 1 => array($tableName, 'String'), | |
6a488035 TO |
392 | 2 => array($columnName, 'String'), |
393 | ); | |
394 | $cfID = CRM_Core_DAO::singleValueQuery($sql, $params); | |
395 | if (!$cfID) { | |
396 | CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3", | |
3302658e TO |
397 | array( |
398 | 1 => $profileField->field_name, | |
399 | 2 => $tableName, | |
400 | 3 => $columnName | |
401 | ) | |
402 | ) . "<br />"); | |
6a488035 TO |
403 | } |
404 | $profileField->field_name = "custom_{$cfID}"; | |
405 | } | |
406 | $profileField->save(); | |
407 | } | |
408 | } | |
409 | } | |
410 | ||
411 | function profileJoins(&$xml, &$idMap) { | |
412 | foreach ($xml->ProfileJoins as $profileJoinsXML) { | |
413 | foreach ($profileJoinsXML->ProfileJoin as $profileJoinXML) { | |
414 | $profileJoin = new CRM_Core_DAO_UFJoin(); | |
415 | $profileJoin->uf_group_id = $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name]; | |
416 | $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module'); | |
417 | $profileJoin->save(); | |
418 | } | |
419 | } | |
420 | } | |
421 | } | |
422 |