3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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() {
42 $dom = DomDocument
::load($file);
44 $xml = simplexml_import_dom($dom);
47 'custom_group' => array(),
48 'option_group' => array(),
51 // first create option groups and values if any
52 $this->optionGroups($xml, $idMap);
53 $this->optionValues($xml, $idMap);
55 $this->relationshipTypes($xml);
56 $this->contributionTypes($xml);
58 // now create custom groups
59 $this->customGroups($xml, $idMap);
60 $this->customFields($xml, $idMap);
62 // now create profile groups
63 $this->profileGroups($xml, $idMap);
64 $this->profileFields($xml, $idMap);
65 $this->profileJoins($xml, $idMap);
67 //create DB Template String sample data
68 $this->dbTemplateString($xml, $idMap);
70 // clean up all caches etc
71 CRM_Core_Config
::clearDBCache();
74 function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
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",
91 $fields = & $dao->fields();
92 foreach ($fields as $name => $dontCare) {
93 if (isset($xml->$name)) {
94 $value = (string ) $xml->$name;
95 $value = str_replace(CRM_Utils_Migrate_Export
::XML_VALUE_SEPARATOR
,
96 CRM_Core_DAO
::VALUE_SEPARATOR
,
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
;
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
)) {
126 SELECT MAX(ROUND(v.value)) + 1
127 FROM civicrm_option_value v
128 WHERE v.option_group_id = %1
130 $params = array(1 => array($optionValue->option_group_id
, 'Integer'));
131 $optionValue->value
= CRM_Core_DAO
::singleValueQuery($sql, $params);
133 $optionValue->save();
138 function relationshipTypes(&$xml) {
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');
148 function contributionTypes(&$xml) {
150 foreach ($xml->ContributionTypes
as $contributionTypesXML) {
151 foreach ($contributionTypesXML->ContributionType
as $contributionTypeXML) {
152 $contributionType = new CRM_Financial_DAO_FinancialType();
153 $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
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
;
168 if (!isset($customGroup->table_name
) ||
169 empty($customGroup->table_name
)
172 $customGroup->table_name
= "civicrm_value_" . strtolower(CRM_Utils_String
::munge($customGroup->title
, '_', 32)) . "_{$customGroup->id}";
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
)
181 $optValues = explode(",", $customGroupXML->extends_entity_column_value_option_value
);
182 $optValues = implode("','", $optValues);
183 if (trim($customGroup->extends) != 'Participant') {
186 FROM civicrm_option_value v
187 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
193 (string ) $customGroupXML->extends_entity_column_value_option_group
,
196 2 => array((string ) $optValues, 'String'),
198 $dao = & CRM_Core_DAO
::executeQuery($sql, $params);
201 while ($dao->fetch()) {
202 $valueIDs[] = $dao->value
;
204 if (!empty($valueIDs)) {
205 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
207 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
209 // Note: No need to set extends_entity_column_id here.
215 // when custom group extends 'Participant'
218 FROM civicrm_option_value v
219 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
220 WHERE g.name = 'custom_data_type'
225 (string ) $customGroupXML->extends_entity_column_value_option_group
,
229 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
231 $customGroup->extends_entity_column_id
= $valueID;
234 $optionIDs = array();
238 $condition = "AND v.name IN ( '{$optValues}' )";
239 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
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);
249 // ParticipantEventType
250 $condition = "AND v.name IN ( '{$optValues}' )";
251 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
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
;
266 $customGroup->save();
269 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
270 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
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
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;
288 while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
289 $total = count($fields);
291 while (list(, $customFieldXML) = each($fields)) {
293 $customField = new CRM_Core_DAO_CustomField();
294 $customField->custom_group_id
= $group_id;
296 if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
300 if (empty($customField->option_group_id
) &&
301 isset($customFieldXML->option_group_name
)
303 $customField->option_group_id
= $idMap['option_group'][(string ) $customFieldXML->option_group_name
];
308 $customField->save();
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;
313 if ($count == $total) {
314 $triggerRebuild = TRUE;
317 CRM_Core_BAO_CustomField
::createField($customField, 'add', $indexExist, $triggerRebuild);
322 function dbTemplateString(&$xml, &$idMap) {
323 foreach ($xml->Persistent
as $persistentXML) {
324 foreach ($persistentXML->Persistent
as $persistent) {
325 $persistentObj = new CRM_Core_DAO_Persistent();
327 if ($persistent->is_config
== 1) {
328 $persistent->data
= serialize(explode(',', $persistent->data
));
330 $this->copyData($persistentObj, $persistent, TRUE, 'context');
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
;
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');
354 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
355 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
358 FROM civicrm_custom_field f
359 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
360 WHERE g.table_name = %1
361 AND f.column_name = %2
364 1 => array($tableName, 'String'),
365 2 => array($columnName, 'String'),
367 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
369 CRM_Core_Error
::fatal(ts("Could not find custom field for %1, %2, %3",
371 1 => $profileField->field_name
,
377 $profileField->field_name
= "custom_{$cfID}";
379 $profileField->save();
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();