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() {
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_option_value
)
199 $optValues = explode(",", $customGroupXML->extends_entity_column_value_option_value
);
200 $optValues = implode("','", $optValues);
201 if (trim($customGroup->extends) != 'Participant') {
204 FROM civicrm_option_value v
205 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
211 (string ) $customGroupXML->extends_entity_column_value_option_group
,
214 2 => array((string ) $optValues, 'String'),
216 $dao = & CRM_Core_DAO
::executeQuery($sql, $params);
219 while ($dao->fetch()) {
220 $valueIDs[] = $dao->value
;
222 if (!empty($valueIDs)) {
223 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
225 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
227 // Note: No need to set extends_entity_column_id here.
233 // when custom group extends 'Participant'
236 FROM civicrm_option_value v
237 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
238 WHERE g.name = 'custom_data_type'
243 (string ) $customGroupXML->extends_entity_column_value_option_group
,
247 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
249 $customGroup->extends_entity_column_id
= $valueID;
252 $optionIDs = array();
256 $condition = "AND v.name IN ( '{$optValues}' )";
257 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
261 // ParticipantEventName
262 $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
263 $optionIDs = CRM_Event_PseudoConstant
::event(NULL, FALSE, $condition);
267 // ParticipantEventType
268 $condition = "AND v.name IN ( '{$optValues}' )";
269 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
273 if (is_array($optionIDs) && !empty($optionIDs)) {
274 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
275 array_keys($optionIDs)
276 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
284 $customGroup->save();
287 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
288 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
293 function customFields(&$xml, &$idMap) {
294 // Re-index by group id so we can build out the custom fields one table
295 // at a time, and then rebuild the table triggers at the end, rather than
296 // rebuilding the table triggers after each field is added (which is
298 $fields_indexed_by_group_id = array();
299 foreach ($xml->CustomFields
as $customFieldsXML) {
300 $total = count($customFieldsXML->CustomField
);
301 foreach ($customFieldsXML->CustomField
as $customFieldXML) {
302 $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name
];
303 $fields_indexed_by_group_id[$id][] = $customFieldXML;
306 while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
307 $total = count($fields);
309 while (list(, $customFieldXML) = each($fields)) {
311 $customField = new CRM_Core_DAO_CustomField();
312 $customField->custom_group_id
= $group_id;
314 if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
318 if (empty($customField->option_group_id
) &&
319 isset($customFieldXML->option_group_name
)
321 $customField->option_group_id
= $idMap['option_group'][(string ) $customFieldXML->option_group_name
];
326 $customField->save();
328 // Only rebuild the table's trigger on the last field added to avoid un-necessary
329 // and slow rebuilds when adding many fields at the same time.
330 $triggerRebuild = FALSE;
331 if ($count == $total) {
332 $triggerRebuild = TRUE;
335 CRM_Core_BAO_CustomField
::createField($customField, 'add', $indexExist, $triggerRebuild);
340 function dbTemplateString(&$xml, &$idMap) {
341 foreach ($xml->Persistent
as $persistentXML) {
342 foreach ($persistentXML->Persistent
as $persistent) {
343 $persistentObj = new CRM_Core_DAO_Persistent();
345 if ($persistent->is_config
== 1) {
346 $persistent->data
= serialize(explode(',', $persistent->data
));
348 $this->copyData($persistentObj, $persistent, TRUE, 'context');
353 function profileGroups(&$xml, &$idMap) {
354 foreach ($xml->ProfileGroups
as $profileGroupsXML) {
355 foreach ($profileGroupsXML->ProfileGroup
as $profileGroupXML) {
356 $profileGroup = new CRM_Core_DAO_UFGroup();
357 $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
358 $idMap['profile_group'][$profileGroup->name
] = $profileGroup->id
;
359 $idMap['profile_group'][$profileGroup->title
] = $profileGroup->id
;
364 function profileFields(&$xml, &$idMap) {
365 foreach ($xml->ProfileFields
as $profileFieldsXML) {
366 foreach ($profileFieldsXML->ProfileField
as $profileFieldXML) {
367 $profileField = new CRM_Core_DAO_UFField();
368 $profileField->uf_group_id
= $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name
];
369 $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
372 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
373 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
376 FROM civicrm_custom_field f
377 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
378 WHERE g.table_name = %1
379 AND f.column_name = %2
382 1 => array($tableName, 'String'),
383 2 => array($columnName, 'String'),
385 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
387 CRM_Core_Error
::fatal(ts("Could not find custom field for %1, %2, %3",
389 1 => $profileField->field_name
,
395 $profileField->field_name
= "custom_{$cfID}";
397 $profileField->save();
402 function profileJoins(&$xml, &$idMap) {
403 foreach ($xml->ProfileJoins
as $profileJoinsXML) {
404 foreach ($profileJoinsXML->ProfileJoin
as $profileJoinXML) {
405 $profileJoin = new CRM_Core_DAO_UFJoin();
406 $profileJoin->uf_group_id
= $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name
];
407 $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
408 $profileJoin->save();