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 if (!$dom->load($file)) {
52 throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
55 $xml = simplexml_import_dom($dom);
56 return $this->runXmlElement($xml);
60 * Import custom-data from an XML element.
62 * @param SimpleXMLElement $xml
64 public function runXmlElement($xml) {
66 'custom_group' => array(),
67 'option_group' => array(),
70 // first create option groups and values if any
71 $this->optionGroups($xml, $idMap);
72 $this->optionValues($xml, $idMap);
74 $this->relationshipTypes($xml);
75 $this->contributionTypes($xml);
77 // now create custom groups
78 $this->customGroups($xml, $idMap);
79 $this->customFields($xml, $idMap);
81 // now create profile groups
82 $this->profileGroups($xml, $idMap);
83 $this->profileFields($xml, $idMap);
84 $this->profileJoins($xml, $idMap);
86 // create DB Template String sample data
87 $this->dbTemplateString($xml, $idMap);
89 // clean up all caches etc
90 CRM_Core_Config
::clearDBCache();
94 * @param CRM_Core_DAO $dao
97 * @param null $keyName
101 public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
103 if (isset($xml->$keyName)) {
104 $dao->$keyName = (string ) $xml->$keyName;
105 if ($dao->find(TRUE)) {
106 CRM_Core_Session
::setStatus(ts("Found %1, %2, %3",
118 $fields = &$dao->fields();
119 foreach ($fields as $name => $dontCare) {
120 if (isset($xml->$name)) {
121 $value = (string ) $xml->$name;
122 $value = str_replace(CRM_Utils_Migrate_Export
::XML_VALUE_SEPARATOR
,
123 CRM_Core_DAO
::VALUE_SEPARATOR
,
126 $dao->$name = $value;
139 public function optionGroups(&$xml, &$idMap) {
140 foreach ($xml->OptionGroups
as $optionGroupsXML) {
141 foreach ($optionGroupsXML->OptionGroup
as $optionGroupXML) {
142 $optionGroup = new CRM_Core_DAO_OptionGroup();
143 $this->copyData($optionGroup, $optionGroupXML, TRUE, 'name');
144 $idMap['option_group'][$optionGroup->name
] = $optionGroup->id
;
153 public function optionValues(&$xml, &$idMap) {
154 foreach ($xml->OptionValues
as $optionValuesXML) {
155 foreach ($optionValuesXML->OptionValue
as $optionValueXML) {
156 $optionValue = new CRM_Core_DAO_OptionValue();
157 $optionValue->option_group_id
= $idMap['option_group'][(string ) $optionValueXML->option_group_name
];
158 if (empty($optionValue->option_group_id
)) {
159 //CRM-17410 check if option group already exist.
160 $optionValue->option_group_id
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $optionValueXML->option_group_name
, 'id', 'name');
162 $this->copyData($optionValue, $optionValueXML, FALSE, 'label');
163 if (!isset($optionValue->value
)) {
165 SELECT MAX(ROUND(v.value)) + 1
166 FROM civicrm_option_value v
167 WHERE v.option_group_id = %1
169 $params = array(1 => array($optionValue->option_group_id
, 'Integer'));
170 $optionValue->value
= CRM_Core_DAO
::singleValueQuery($sql, $params);
172 $optionValue->save();
180 public function relationshipTypes(&$xml) {
182 foreach ($xml->RelationshipTypes
as $relationshipTypesXML) {
183 foreach ($relationshipTypesXML->RelationshipType
as $relationshipTypeXML) {
184 $relationshipType = new CRM_Contact_DAO_RelationshipType();
185 $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b');
193 public function contributionTypes(&$xml) {
195 foreach ($xml->ContributionTypes
as $contributionTypesXML) {
196 foreach ($contributionTypesXML->ContributionType
as $contributionTypeXML) {
197 $contributionType = new CRM_Financial_DAO_FinancialType();
198 $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
207 public function customGroups(&$xml, &$idMap) {
208 foreach ($xml->CustomGroups
as $customGroupsXML) {
209 foreach ($customGroupsXML->CustomGroup
as $customGroupXML) {
210 $customGroup = new CRM_Core_DAO_CustomGroup();
211 if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) {
212 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
217 if (!isset($customGroup->table_name
) ||
218 empty($customGroup->table_name
)
221 $customGroup->table_name
= "civicrm_value_" . strtolower(CRM_Utils_String
::munge($customGroup->title
, '_', 32)) . "_{$customGroup->id}";
226 // fix extends stuff if it exists
227 if (isset($customGroupXML->extends_entity_column_value_option_group
) &&
228 isset($customGroupXML->extends_entity_column_value
)
231 $optionValues = explode(",", $customGroupXML->extends_entity_column_value
);
232 $optValues = implode("','", $optionValues);
233 if (trim($customGroup->extends) != 'Participant') {
234 if ($customGroup->extends == 'Relationship') {
235 foreach ($optionValues as $key => $value) {
236 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b');
237 $valueIDs[] = $relTypeId;
240 elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) {
241 $valueIDs = $optionValues;
246 FROM civicrm_option_value v
247 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
249 AND v.name IN ('$optValues')
253 (string ) $customGroupXML->extends_entity_column_value_option_group
,
257 $dao = &CRM_Core_DAO
::executeQuery($sql, $params);
259 while ($dao->fetch()) {
260 $valueIDs[] = $dao->value
;
263 if (!empty($valueIDs)) {
264 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
266 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
270 // Note: No need to set extends_entity_column_id here.
276 // when custom group extends 'Participant'
279 FROM civicrm_option_value v
280 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
281 WHERE g.name = 'custom_data_type'
286 (string ) $customGroupXML->extends_entity_column_value_option_group
,
290 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
292 $customGroup->extends_entity_column_id
= $valueID;
295 $optionIDs = array();
299 $condition = "AND v.name IN ( '{$optValues}' )";
300 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
304 // ParticipantEventName
305 $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
306 $optionIDs = CRM_Event_PseudoConstant
::event(NULL, FALSE, $condition);
310 // ParticipantEventType
311 $condition = "AND v.name IN ( '{$optValues}' )";
312 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
316 if (is_array($optionIDs) && !empty($optionIDs)) {
317 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
318 array_keys($optionIDs)
319 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
327 $customGroup->save();
330 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
331 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
340 public function customFields(&$xml, &$idMap) {
341 // Re-index by group id so we can build out the custom fields one table
342 // at a time, and then rebuild the table triggers at the end, rather than
343 // rebuilding the table triggers after each field is added (which is
345 $fields_indexed_by_group_id = array();
346 foreach ($xml->CustomFields
as $customFieldsXML) {
347 $total = count($customFieldsXML->CustomField
);
348 foreach ($customFieldsXML->CustomField
as $customFieldXML) {
349 $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name
];
350 $fields_indexed_by_group_id[$id][] = $customFieldXML;
353 while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
354 $total = count($fields);
356 while (list(, $customFieldXML) = each($fields)) {
358 $customField = new CRM_Core_DAO_CustomField();
359 $customField->custom_group_id
= $group_id;
361 if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
365 if (empty($customField->option_group_id
) &&
366 isset($customFieldXML->option_group_name
)
368 $customField->option_group_id
= $idMap['option_group'][(string ) $customFieldXML->option_group_name
];
373 $customField->save();
375 // Only rebuild the table's trigger on the last field added to avoid un-necessary
376 // and slow rebuilds when adding many fields at the same time.
377 $triggerRebuild = FALSE;
378 if ($count == $total) {
379 $triggerRebuild = TRUE;
382 CRM_Core_BAO_CustomField
::createField($customField, 'add', $indexExist, $triggerRebuild);
391 public function dbTemplateString(&$xml, &$idMap) {
392 foreach ($xml->Persistent
as $persistentXML) {
393 foreach ($persistentXML->Persistent
as $persistent) {
394 $persistentObj = new CRM_Core_DAO_Persistent();
396 if ($persistent->is_config
== 1) {
397 $persistent->data
= serialize(explode(',', $persistent->data
));
399 $this->copyData($persistentObj, $persistent, TRUE, 'context');
408 public function profileGroups(&$xml, &$idMap) {
409 foreach ($xml->ProfileGroups
as $profileGroupsXML) {
410 foreach ($profileGroupsXML->ProfileGroup
as $profileGroupXML) {
411 $profileGroup = new CRM_Core_DAO_UFGroup();
412 $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
413 $idMap['profile_group'][$profileGroup->name
] = $profileGroup->id
;
414 $idMap['profile_group'][$profileGroup->title
] = $profileGroup->id
;
425 public function profileFields(&$xml, &$idMap) {
426 foreach ($xml->ProfileFields
as $profileFieldsXML) {
427 foreach ($profileFieldsXML->ProfileField
as $profileFieldXML) {
428 $profileField = new CRM_Core_DAO_UFField();
429 $profileField->uf_group_id
= $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name
];
430 $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
433 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
434 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
437 FROM civicrm_custom_field f
438 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
439 WHERE g.table_name = %1
440 AND f.column_name = %2
443 1 => array($tableName, 'String'),
444 2 => array($columnName, 'String'),
446 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
448 CRM_Core_Error
::fatal(ts("Could not find custom field for %1, %2, %3",
450 1 => $profileField->field_name
,
456 $profileField->field_name
= "custom_{$cfID}";
458 $profileField->save();
467 public function profileJoins(&$xml, &$idMap) {
468 foreach ($xml->ProfileJoins
as $profileJoinsXML) {
469 foreach ($profileJoinsXML->ProfileJoin
as $profileJoinXML) {
470 $profileJoin = new CRM_Core_DAO_UFJoin();
471 $profileJoin->uf_group_id
= $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name
];
472 $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
473 $profileJoin->save();