3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 class CRM_Utils_Migrate_Import
{
37 public function __construct() {
41 * Import custom-data from an XML file.
44 * Path to an XML file.
46 * @throws CRM_Core_Exception
48 public function run($file) {
50 $dom = new DomDocument();
51 $oldValue = libxml_disable_entity_loader(FALSE);
52 $load = $dom->load($file);
53 libxml_disable_entity_loader($oldValue);
55 throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
58 $xml = simplexml_import_dom($dom);
59 return $this->runXmlElement($xml);
63 * Import custom-data from an XML element.
65 * @param SimpleXMLElement $xml
67 public function runXmlElement($xml) {
69 'custom_group' => array(),
70 'option_group' => array(),
73 // first create option groups and values if any
74 $this->optionGroups($xml, $idMap);
75 $this->optionValues($xml, $idMap);
77 $this->relationshipTypes($xml);
78 $this->contributionTypes($xml);
80 // now create custom groups
81 $this->customGroups($xml, $idMap);
82 $this->customFields($xml, $idMap);
84 // now create profile groups
85 $this->profileGroups($xml, $idMap);
86 $this->profileFields($xml, $idMap);
87 $this->profileJoins($xml, $idMap);
89 // create DB Template String sample data
90 $this->dbTemplateString($xml, $idMap);
92 // clean up all caches etc
93 CRM_Core_Config
::clearDBCache();
97 * @param CRM_Core_DAO $dao
100 * @param null $keyName
104 public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
106 if (isset($xml->$keyName)) {
107 $dao->$keyName = (string ) $xml->$keyName;
108 if ($dao->find(TRUE)) {
109 CRM_Core_Session
::setStatus(ts("Found %1, %2, %3",
121 $fields = &$dao->fields();
122 foreach ($fields as $name => $dontCare) {
123 if (isset($xml->$name)) {
124 $value = (string ) $xml->$name;
125 $value = str_replace(CRM_Utils_Migrate_Export
::XML_VALUE_SEPARATOR
,
126 CRM_Core_DAO
::VALUE_SEPARATOR
,
129 $dao->$name = $value;
142 public function optionGroups(&$xml, &$idMap) {
143 foreach ($xml->OptionGroups
as $optionGroupsXML) {
144 foreach ($optionGroupsXML->OptionGroup
as $optionGroupXML) {
145 $optionGroup = new CRM_Core_DAO_OptionGroup();
146 $this->copyData($optionGroup, $optionGroupXML, TRUE, 'name');
147 $idMap['option_group'][$optionGroup->name
] = $optionGroup->id
;
156 public function optionValues(&$xml, &$idMap) {
157 foreach ($xml->OptionValues
as $optionValuesXML) {
158 foreach ($optionValuesXML->OptionValue
as $optionValueXML) {
159 $optionValue = new CRM_Core_DAO_OptionValue();
160 $optionValue->option_group_id
= $idMap['option_group'][(string ) $optionValueXML->option_group_name
];
161 if (empty($optionValue->option_group_id
)) {
162 //CRM-17410 check if option group already exist.
163 $optionValue->option_group_id
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $optionValueXML->option_group_name
, 'id', 'name');
165 $this->copyData($optionValue, $optionValueXML, FALSE, 'label');
166 if (!isset($optionValue->value
)) {
168 SELECT MAX(ROUND(v.value)) + 1
169 FROM civicrm_option_value v
170 WHERE v.option_group_id = %1
172 $params = array(1 => array($optionValue->option_group_id
, 'Integer'));
173 $optionValue->value
= CRM_Core_DAO
::singleValueQuery($sql, $params);
175 $optionValue->save();
183 public function relationshipTypes(&$xml) {
185 foreach ($xml->RelationshipTypes
as $relationshipTypesXML) {
186 foreach ($relationshipTypesXML->RelationshipType
as $relationshipTypeXML) {
187 $relationshipType = new CRM_Contact_DAO_RelationshipType();
188 $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b');
196 public function contributionTypes(&$xml) {
198 foreach ($xml->ContributionTypes
as $contributionTypesXML) {
199 foreach ($contributionTypesXML->ContributionType
as $contributionTypeXML) {
200 $contributionType = new CRM_Financial_DAO_FinancialType();
201 $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
210 public function customGroups(&$xml, &$idMap) {
211 foreach ($xml->CustomGroups
as $customGroupsXML) {
212 foreach ($customGroupsXML->CustomGroup
as $customGroupXML) {
213 $customGroup = new CRM_Core_DAO_CustomGroup();
214 if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) {
215 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
220 if (!isset($customGroup->table_name
) ||
221 empty($customGroup->table_name
)
224 $customGroup->table_name
= "civicrm_value_" . strtolower(CRM_Utils_String
::munge($customGroup->title
, '_', 32)) . "_{$customGroup->id}";
229 // fix extends stuff if it exists
230 if (isset($customGroupXML->extends_entity_column_value_option_group
) &&
231 isset($customGroupXML->extends_entity_column_value
)
234 $optionValues = explode(",", $customGroupXML->extends_entity_column_value
);
235 $optValues = implode("','", $optionValues);
236 if (trim($customGroup->extends) != 'Participant') {
237 if ($customGroup->extends == 'Relationship') {
238 foreach ($optionValues as $key => $value) {
239 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b');
240 $valueIDs[] = $relTypeId;
243 elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) {
244 $valueIDs = $optionValues;
249 FROM civicrm_option_value v
250 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
252 AND v.name IN ('$optValues')
256 (string ) $customGroupXML->extends_entity_column_value_option_group
,
260 $dao = &CRM_Core_DAO
::executeQuery($sql, $params);
262 while ($dao->fetch()) {
263 $valueIDs[] = $dao->value
;
266 if (!empty($valueIDs)) {
267 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
269 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
273 // Note: No need to set extends_entity_column_id here.
279 // when custom group extends 'Participant'
282 FROM civicrm_option_value v
283 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
284 WHERE g.name = 'custom_data_type'
289 (string ) $customGroupXML->extends_entity_column_value_option_group
,
293 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
295 $customGroup->extends_entity_column_id
= $valueID;
298 $optionIDs = array();
302 $condition = "AND v.name IN ( '{$optValues}' )";
303 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
307 // ParticipantEventName
308 $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
309 $optionIDs = CRM_Event_PseudoConstant
::event(NULL, FALSE, $condition);
313 // ParticipantEventType
314 $condition = "AND v.name IN ( '{$optValues}' )";
315 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
319 if (is_array($optionIDs) && !empty($optionIDs)) {
320 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
321 array_keys($optionIDs)
322 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
330 $customGroup->save();
333 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
334 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
343 public function customFields(&$xml, &$idMap) {
344 // Re-index by group id so we can build out the custom fields one table
345 // at a time, and then rebuild the table triggers at the end, rather than
346 // rebuilding the table triggers after each field is added (which is
348 $fields_indexed_by_group_id = array();
349 foreach ($xml->CustomFields
as $customFieldsXML) {
350 $total = count($customFieldsXML->CustomField
);
351 foreach ($customFieldsXML->CustomField
as $customFieldXML) {
352 $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name
];
353 $fields_indexed_by_group_id[$id][] = $customFieldXML;
356 while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
357 $total = count($fields);
359 while (list(, $customFieldXML) = each($fields)) {
361 $customField = new CRM_Core_DAO_CustomField();
362 $customField->custom_group_id
= $group_id;
364 if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
368 if (empty($customField->option_group_id
) &&
369 isset($customFieldXML->option_group_name
)
371 $customField->option_group_id
= $idMap['option_group'][(string ) $customFieldXML->option_group_name
];
376 $customField->save();
378 // Only rebuild the table's trigger on the last field added to avoid un-necessary
379 // and slow rebuilds when adding many fields at the same time.
380 $triggerRebuild = FALSE;
381 if ($count == $total) {
382 $triggerRebuild = TRUE;
385 CRM_Core_BAO_CustomField
::createField($customField, 'add', $indexExist, $triggerRebuild);
394 public function dbTemplateString(&$xml, &$idMap) {
395 foreach ($xml->Persistent
as $persistentXML) {
396 foreach ($persistentXML->Persistent
as $persistent) {
397 $persistentObj = new CRM_Core_DAO_Persistent();
399 if ($persistent->is_config
== 1) {
400 $persistent->data
= serialize(explode(',', $persistent->data
));
402 $this->copyData($persistentObj, $persistent, TRUE, 'context');
411 public function profileGroups(&$xml, &$idMap) {
412 foreach ($xml->ProfileGroups
as $profileGroupsXML) {
413 foreach ($profileGroupsXML->ProfileGroup
as $profileGroupXML) {
414 $profileGroup = new CRM_Core_DAO_UFGroup();
415 $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
416 $idMap['profile_group'][$profileGroup->name
] = $profileGroup->id
;
417 $idMap['profile_group'][$profileGroup->title
] = $profileGroup->id
;
428 public function profileFields(&$xml, &$idMap) {
429 foreach ($xml->ProfileFields
as $profileFieldsXML) {
430 foreach ($profileFieldsXML->ProfileField
as $profileFieldXML) {
431 $profileField = new CRM_Core_DAO_UFField();
432 $profileField->uf_group_id
= $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name
];
433 $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
436 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
437 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
440 FROM civicrm_custom_field f
441 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
442 WHERE g.table_name = %1
443 AND f.column_name = %2
446 1 => array($tableName, 'String'),
447 2 => array($columnName, 'String'),
449 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
451 CRM_Core_Error
::fatal(ts("Could not find custom field for %1, %2, %3",
453 1 => $profileField->field_name
,
459 $profileField->field_name
= "custom_{$cfID}";
461 $profileField->save();
470 public function profileJoins(&$xml, &$idMap) {
471 foreach ($xml->ProfileJoins
as $profileJoinsXML) {
472 foreach ($profileJoinsXML->ProfileJoin
as $profileJoinXML) {
473 $profileJoin = new CRM_Core_DAO_UFJoin();
474 $profileJoin->uf_group_id
= $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name
];
475 $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
476 $profileJoin->save();