3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 $this->copyData($optionValue, $optionValueXML, FALSE, 'label');
159 if (!isset($optionValue->value
)) {
161 SELECT MAX(ROUND(v.value)) + 1
162 FROM civicrm_option_value v
163 WHERE v.option_group_id = %1
165 $params = array(1 => array($optionValue->option_group_id
, 'Integer'));
166 $optionValue->value
= CRM_Core_DAO
::singleValueQuery($sql, $params);
168 $optionValue->save();
176 public function relationshipTypes(&$xml) {
178 foreach ($xml->RelationshipTypes
as $relationshipTypesXML) {
179 foreach ($relationshipTypesXML->RelationshipType
as $relationshipTypeXML) {
180 $relationshipType = new CRM_Contact_DAO_RelationshipType();
181 $this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b');
189 public function contributionTypes(&$xml) {
191 foreach ($xml->ContributionTypes
as $contributionTypesXML) {
192 foreach ($contributionTypesXML->ContributionType
as $contributionTypeXML) {
193 $contributionType = new CRM_Financial_DAO_FinancialType();
194 $this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
203 public function customGroups(&$xml, &$idMap) {
204 foreach ($xml->CustomGroups
as $customGroupsXML) {
205 foreach ($customGroupsXML->CustomGroup
as $customGroupXML) {
206 $customGroup = new CRM_Core_DAO_CustomGroup();
207 if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) {
208 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
213 if (!isset($customGroup->table_name
) ||
214 empty($customGroup->table_name
)
217 $customGroup->table_name
= "civicrm_value_" . strtolower(CRM_Utils_String
::munge($customGroup->title
, '_', 32)) . "_{$customGroup->id}";
222 // fix extends stuff if it exists
223 if (isset($customGroupXML->extends_entity_column_value_option_group
) &&
224 isset($customGroupXML->extends_entity_column_value
)
227 $optionValues = explode(",", $customGroupXML->extends_entity_column_value
);
228 $optValues = implode("','", $optionValues);
229 if (trim($customGroup->extends) != 'Participant') {
230 if ($customGroup->extends == 'Relationship') {
231 foreach ($optionValues as $key => $value) {
232 $relTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b');
233 $valueIDs[] = $relTypeId;
236 elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) {
237 $valueIDs = $optionValues;
242 FROM civicrm_option_value v
243 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
245 AND v.name IN ('$optValues')
249 (string ) $customGroupXML->extends_entity_column_value_option_group
,
253 $dao = &CRM_Core_DAO
::executeQuery($sql, $params);
255 while ($dao->fetch()) {
256 $valueIDs[] = $dao->value
;
259 if (!empty($valueIDs)) {
260 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
262 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
266 // Note: No need to set extends_entity_column_id here.
272 // when custom group extends 'Participant'
275 FROM civicrm_option_value v
276 INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
277 WHERE g.name = 'custom_data_type'
282 (string ) $customGroupXML->extends_entity_column_value_option_group
,
286 $valueID = (int ) CRM_Core_DAO
::singleValueQuery($sql, $params);
288 $customGroup->extends_entity_column_id
= $valueID;
291 $optionIDs = array();
295 $condition = "AND v.name IN ( '{$optValues}' )";
296 $optionIDs = CRM_Core_OptionGroup
::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
300 // ParticipantEventName
301 $condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
302 $optionIDs = CRM_Event_PseudoConstant
::event(NULL, FALSE, $condition);
306 // ParticipantEventType
307 $condition = "AND v.name IN ( '{$optValues}' )";
308 $optionIDs = CRM_Core_OptionGroup
::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
312 if (is_array($optionIDs) && !empty($optionIDs)) {
313 $customGroup->extends_entity_column_value
= CRM_Core_DAO
::VALUE_SEPARATOR
. implode(CRM_Core_DAO
::VALUE_SEPARATOR
,
314 array_keys($optionIDs)
315 ) . CRM_Core_DAO
::VALUE_SEPARATOR
;
323 $customGroup->save();
326 CRM_Core_BAO_CustomGroup
::createTable($customGroup);
327 $idMap['custom_group'][$customGroup->name
] = $customGroup->id
;
336 public function customFields(&$xml, &$idMap) {
337 // Re-index by group id so we can build out the custom fields one table
338 // at a time, and then rebuild the table triggers at the end, rather than
339 // rebuilding the table triggers after each field is added (which is
341 $fields_indexed_by_group_id = array();
342 foreach ($xml->CustomFields
as $customFieldsXML) {
343 $total = count($customFieldsXML->CustomField
);
344 foreach ($customFieldsXML->CustomField
as $customFieldXML) {
345 $id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name
];
346 $fields_indexed_by_group_id[$id][] = $customFieldXML;
349 while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
350 $total = count($fields);
352 while (list(, $customFieldXML) = each($fields)) {
354 $customField = new CRM_Core_DAO_CustomField();
355 $customField->custom_group_id
= $group_id;
357 if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
361 if (empty($customField->option_group_id
) &&
362 isset($customFieldXML->option_group_name
)
364 $customField->option_group_id
= $idMap['option_group'][(string ) $customFieldXML->option_group_name
];
369 $customField->save();
371 // Only rebuild the table's trigger on the last field added to avoid un-necessary
372 // and slow rebuilds when adding many fields at the same time.
373 $triggerRebuild = FALSE;
374 if ($count == $total) {
375 $triggerRebuild = TRUE;
378 CRM_Core_BAO_CustomField
::createField($customField, 'add', $indexExist, $triggerRebuild);
387 public function dbTemplateString(&$xml, &$idMap) {
388 foreach ($xml->Persistent
as $persistentXML) {
389 foreach ($persistentXML->Persistent
as $persistent) {
390 $persistentObj = new CRM_Core_DAO_Persistent();
392 if ($persistent->is_config
== 1) {
393 $persistent->data
= serialize(explode(',', $persistent->data
));
395 $this->copyData($persistentObj, $persistent, TRUE, 'context');
404 public function profileGroups(&$xml, &$idMap) {
405 foreach ($xml->ProfileGroups
as $profileGroupsXML) {
406 foreach ($profileGroupsXML->ProfileGroup
as $profileGroupXML) {
407 $profileGroup = new CRM_Core_DAO_UFGroup();
408 $this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
409 $idMap['profile_group'][$profileGroup->name
] = $profileGroup->id
;
410 $idMap['profile_group'][$profileGroup->title
] = $profileGroup->id
;
421 public function profileFields(&$xml, &$idMap) {
422 foreach ($xml->ProfileFields
as $profileFieldsXML) {
423 foreach ($profileFieldsXML->ProfileField
as $profileFieldXML) {
424 $profileField = new CRM_Core_DAO_UFField();
425 $profileField->uf_group_id
= $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name
];
426 $this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
429 if (substr($profileField->field_name
, 0, 7) == 'custom.') {
430 list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name
);
433 FROM civicrm_custom_field f
434 INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
435 WHERE g.table_name = %1
436 AND f.column_name = %2
439 1 => array($tableName, 'String'),
440 2 => array($columnName, 'String'),
442 $cfID = CRM_Core_DAO
::singleValueQuery($sql, $params);
444 CRM_Core_Error
::fatal(ts("Could not find custom field for %1, %2, %3",
446 1 => $profileField->field_name
,
452 $profileField->field_name
= "custom_{$cfID}";
454 $profileField->save();
463 public function profileJoins(&$xml, &$idMap) {
464 foreach ($xml->ProfileJoins
as $profileJoinsXML) {
465 foreach ($profileJoinsXML->ProfileJoin
as $profileJoinXML) {
466 $profileJoin = new CRM_Core_DAO_UFJoin();
467 $profileJoin->uf_group_id
= $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name
];
468 $this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
469 $profileJoin->save();