3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
35 class CRM_Utils_Migrate_Import
{
36 function __construct() {
40 * Import custom-data from an XML file
42 * @param string $file path to an XML file
43 * @throws CRM_Core_Exception
48 $dom = new DomDocument();
49 if (! $dom->load($file)) {
50 throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
53 $xml = simplexml_import_dom($dom);
54 return $this->runXmlElement($xml);
58 * Import custom-data from an XML element
60 * @param SimpleXMLElement $xml
63 function runXmlElement($xml) {
65 'custom_group' => array(),
66 'option_group' => array(),
69 // first create option groups and values if any
70 $this->optionGroups($xml, $idMap);
71 $this->optionValues($xml, $idMap);
73 $this->relationshipTypes($xml);
74 $this->contributionTypes($xml);
76 // now create custom groups
77 $this->customGroups($xml, $idMap);
78 $this->customFields($xml, $idMap);
80 // now create profile groups
81 $this->profileGroups($xml, $idMap);
82 $this->profileFields($xml, $idMap);
83 $this->profileJoins($xml, $idMap);
85 //create DB Template String sample data
86 $this->dbTemplateString($xml, $idMap);
88 // clean up all caches etc
89 CRM_Core_Config
::clearDBCache();
92 function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
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",
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
,
117 $dao->$name = $value;
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
;
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
)) {
144 SELECT MAX(ROUND(v.value)) + 1
145 FROM civicrm_option_value v
146 WHERE v.option_group_id = %1
148 $params = array(1 => array($optionValue->option_group_id
, 'Integer'));
149 $optionValue->value
= CRM_Core_DAO
::singleValueQuery($sql, $params);
151 $optionValue->save();
156 function relationshipTypes(&$xml) {
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');
166 function contributionTypes(&$xml) {
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');
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
;
186 if (!isset($customGroup->table_name
) ||
187 empty($customGroup->table_name
)
190 $customGroup->table_name
= "civicrm_value_" . strtolower(CRM_Utils_String
::munge($customGroup->title
, '_', 32)) . "_{$customGroup->id}";
195 // fix extends stuff if it exists
196 if (isset($customGroupXML->extends_entity_column_value_option_group
) &&
197 isset($customGroupXML->extends_entity_column_value
)) {
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;
208 elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) {
209 $valueIDs = $optionValues;
214 FROM civicrm_option_value v
215 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
217 AND v.name IN ('$optValues')
221 (string ) $customGroupXML->extends_entity_column_value_option_group
,
225 $dao = & CRM_Core_DAO
::executeQuery($sql, $params);
227 while ($dao->fetch()) {
228 $valueIDs[] = $dao->value
;
231 if (!empty($valueIDs)) {
232 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
234 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
238 // Note: No need to set extends_entity_column_id here.
244 // when custom group extends 'Participant'
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'
254 (string ) $customGroupXML->extends_entity_column_value_option_group
,
258 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
260 $customGroup->extends_entity_column_id
= $valueID;
263 $optionIDs = array();
267 $condition = "AND v.name IN ( '{$optValues}' )";
268 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
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);
278 // ParticipantEventType
279 $condition = "AND v.name IN ( '{$optValues}' )";
280 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
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
;
295 $customGroup->save();
298 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
299 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
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
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;
317 while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
318 $total = count($fields);
320 while (list(, $customFieldXML) = each($fields)) {
322 $customField = new CRM_Core_DAO_CustomField();
323 $customField->custom_group_id
= $group_id;
325 if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
329 if (empty($customField->option_group_id
) &&
330 isset($customFieldXML->option_group_name
)
332 $customField->option_group_id
= $idMap['option_group'][(string ) $customFieldXML->option_group_name
];
337 $customField->save();
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;
346 CRM_Core_BAO_CustomField
::createField($customField, 'add', $indexExist, $triggerRebuild);
351 function dbTemplateString(&$xml, &$idMap) {
352 foreach ($xml->Persistent
as $persistentXML) {
353 foreach ($persistentXML->Persistent
as $persistent) {
354 $persistentObj = new CRM_Core_DAO_Persistent();
356 if ($persistent->is_config
== 1) {
357 $persistent->data
= serialize(explode(',', $persistent->data
));
359 $this->copyData($persistentObj, $persistent, TRUE, 'context');
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
;
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');
383 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
384 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
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
393 1 => array($tableName, 'String'),
394 2 => array($columnName, 'String'),
396 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
398 CRM_Core_Error
::fatal(ts("Could not find custom field for %1, %2, %3",
400 1 => $profileField->field_name
,
406 $profileField->field_name
= "custom_{$cfID}";
408 $profileField->save();
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();