3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Utils_Migrate_Export
{
37 const XML_VALUE_SEPARATOR
= ":;:;:;";
40 * @var array description of export field mapping
43 * 'exampleEntityMappingName' => array(
44 * 'data' => array(), // placeholder; this will get filled-in during execution
45 * 'name' => 'CustomGroup', // per-item XML tag name
46 * 'scope' => 'CustomGroups', // container XML tag name
47 * 'required' => FALSE, // whether we *must* find records of this type
48 * 'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
49 * 'idNameMap' => array(), // placeholder; this will get filled-in during execution
58 function __construct() {
60 'customGroup' => array(
62 'name' => 'CustomGroup',
63 'scope' => 'CustomGroups',
65 'idNameFields' => array('id', 'name'),
66 'idNameMap' => array(),
68 'customField' => array(
70 'name' => 'CustomField',
71 'scope' => 'CustomFields',
73 'idNameFields' => array('id', 'column_name'),
74 'idNameMap' => array(),
75 'mappedFields' => array(
76 array('optionGroup', 'option_group_id', 'option_group_name'),
77 array('customGroup', 'custom_group_id', 'custom_group_name'),
80 'optionGroup' => array(
82 'name' => 'OptionGroup',
83 'scope' => 'OptionGroups',
85 'idNameMap' => array(),
86 'idNameFields' => array('id', 'name'),
88 'relationshipType' => array(
90 'name' => 'RelationshipType',
91 'scope' => 'RelationshipTypes',
93 'idNameFields' => array('id', 'name_a_b'),
94 'idNameMap' => array(),
96 'locationType' => array(
98 'name' => 'LocationType',
99 'scope' => 'LocationTypes',
101 'idNameFields' => array('id', 'name'),
102 'idNameMap' => array(),
104 'optionValue' => array(
106 'name' => 'OptionValue',
107 'scope' => 'OptionValues',
109 'idNameMap' => array(),
110 'idNameFields' => array('value', 'name', 'prefix'),
111 'mappedFields' => array(
112 array('optionGroup', 'option_group_id', 'option_group_name'),
115 'profileGroup' => array(
117 'name' => 'ProfileGroup',
118 'scope' => 'ProfileGroups',
120 'idNameFields' => array('id', 'title'),
121 'idNameMap' => array(),
123 'profileField' => array(
125 'name' => 'ProfileField',
126 'scope' => 'ProfileFields',
128 'idNameMap' => array(),
129 'mappedFields' => array(
130 array('profileGroup', 'uf_group_id', 'profile_group_name')
133 'profileJoin' => array(
135 'name' => 'ProfileJoin',
136 'scope' => 'ProfileJoins',
138 'idNameMap' => array(),
139 'mappedFields' => array(
140 array('profileGroup', 'uf_group_id', 'profile_group_name')
143 'mappingGroup' => array(
145 'name' => 'MappingGroup',
146 'scope' => 'MappingGroups',
148 'idNameFields' => array('id', 'name'),
149 'idNameMap' => array(),
150 'mappedFields' => array(
151 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
154 'mappingField' => array(
156 'name' => 'MappingField',
157 'scope' => 'MappingFields',
159 'idNameMap' => array(),
160 'mappedFields' => array(
161 array('mappingGroup', 'mapping_id', 'mapping_group_name'),
162 array('locationType', 'location_type_id', 'location_type_name'),
163 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
170 * Scan local customizations and build an in-memory representation
175 // fetch the option group / values for
176 // activity type and event_type
178 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
181 SELECT distinct(g.id), g.*
182 FROM civicrm_option_group g
183 WHERE g.name IN $optionGroups
185 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
188 SELECT distinct(g.id), g.*
189 FROM civicrm_option_group g,
190 civicrm_custom_field f,
191 civicrm_custom_group cg
192 WHERE f.option_group_id = g.id
193 AND f.custom_group_id = cg.id
196 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
199 SELECT v.*, g.name as prefix
200 FROM civicrm_option_value v,
201 civicrm_option_group g
202 WHERE v.option_group_id = g.id
203 AND g.name IN $optionGroups
206 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
209 SELECT distinct(v.id), v.*, g.name as prefix
210 FROM civicrm_option_value v,
211 civicrm_option_group g,
212 civicrm_custom_field f,
213 civicrm_custom_group cg
214 WHERE v.option_group_id = g.id
215 AND f.option_group_id = g.id
216 AND f.custom_group_id = cg.id
220 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
224 FROM civicrm_relationship_type rt
225 WHERE rt.is_active = 1
227 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
231 FROM civicrm_location_type lt
232 WHERE lt.is_active = 1
234 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
238 FROM civicrm_custom_group cg
239 WHERE cg.is_active = 1
241 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
245 FROM civicrm_custom_field f,
246 civicrm_custom_group cg
247 WHERE f.custom_group_id = cg.id
250 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
252 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
254 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
259 WHERE entity_table IS NULL
260 AND entity_id IS NULL
262 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
264 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
266 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
270 * @param array $customGroupIds list of custom groups to export
273 function buildCustomGroups($customGroupIds) {
274 $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
275 if (empty($customGroupIdsSql)) {
280 SELECT distinct(g.id), g.*
281 FROM civicrm_option_group g,
282 civicrm_custom_field f,
283 civicrm_custom_group cg
284 WHERE f.option_group_id = g.id
285 AND f.custom_group_id = cg.id
286 AND cg.id in ($customGroupIdsSql)
288 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
291 SELECT distinct(v.id), v.*, g.name as prefix
292 FROM civicrm_option_value v,
293 civicrm_option_group g,
294 civicrm_custom_field f,
295 civicrm_custom_group cg
296 WHERE v.option_group_id = g.id
297 AND f.option_group_id = g.id
298 AND f.custom_group_id = cg.id
299 AND cg.id in ($customGroupIdsSql)
302 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
306 FROM civicrm_custom_group cg
307 WHERE cg.id in ($customGroupIdsSql)
310 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
314 FROM civicrm_custom_field f,
315 civicrm_custom_group cg
316 WHERE f.custom_group_id = cg.id
317 AND cg.id in ($customGroupIdsSql)
319 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
323 * @param array $ufGroupIds list of custom groups to export
326 function buildUFGroups($ufGroupIds) {
327 $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
328 if (empty($ufGroupIdsSql)) {
334 FROM civicrm_uf_group cg
335 WHERE cg.id IN ($ufGroupIdsSql)
338 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
342 FROM civicrm_uf_field f,
344 WHERE f.uf_group_id = cg.id
345 AND cg.id IN ($ufGroupIdsSql)
347 $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
352 WHERE entity_table IS NULL
353 AND entity_id IS NULL
354 AND uf_group_id IN ($ufGroupIdsSql)
356 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
360 * Render the in-memory representation as XML
365 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
366 $buffer .= "\n\n<CustomData>\n";
367 foreach (array_keys($this->_xml
) as $key) {
368 if (!empty($this->_xml
[$key]['data'])) {
369 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
370 foreach ($this->_xml
[$key]['data'] as $item) {
371 $buffer .= $this->renderKeyValueXML($this->_xml
[$key]['name'], $item);
373 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
375 elseif ($this->_xml
[$key]['required']) {
376 CRM_Core_Error
::fatal("No records in DB for $key");
379 $buffer .= "</CustomData>\n";
384 * Generate an array-tree representation of the exported elements.
390 foreach (array_keys($this->_xml
) as $key) {
391 if (!empty($this->_xml
[$key]['data'])) {
392 $result[ $this->_xml
[$key]['name'] ] = array_values($this->_xml
[$key]['data']);
403 function fetch($groupName, $daoName, $sql = NULL) {
404 $idNameFields = isset($this->_xml
[$groupName]['idNameFields']) ?
$this->_xml
[$groupName]['idNameFields'] : NULL;
405 $mappedFields = isset($this->_xml
[$groupName]['mappedFields']) ?
$this->_xml
[$groupName]['mappedFields'] : NULL;
407 $dao = new $daoName();
415 while ($dao->fetch()) {
416 $this->_xml
[$groupName]['data'][$dao->id
] = $this->exportDAO($this->_xml
[$groupName]['name'], $dao, $mappedFields);
418 // index the id/name fields so that we can translate from FK ids to FK names
419 if (isset($idNameFields[2])) {
420 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
423 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
430 * Compute any fields of the entity defined by the $mappedFields specification
432 * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
433 * @param CRM_Core_DAO $dao the entity for which we want to prepare mapped fields
434 * @return array new fields
436 public function computeMappedFields($mappedFields, $dao) {
437 $keyValues = array();
439 foreach ($mappedFields as $mappedField) {
440 if (isset($dao->{$mappedField[1]})) {
441 if (isset($mappedField[3])) {
442 $label = $this->_xml
[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
445 $label = $this->_xml
[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
447 $keyValues[$mappedField[2]] = $label;
455 * @param string $objectName business-entity/xml-tag name
456 * @param CRM_Core_DAO $object
457 * @param $mappedFields
461 function exportDAO($objectName, $object, $mappedFields) {
462 $dbFields = & $object->fields();
464 // Filter the list of keys and values so that we only export interesting stuff
465 $keyValues = array();
466 foreach ($dbFields as $name => $dontCare) {
468 if ($name == 'id' ||
substr($name, -3, 3) == '_id') {
471 if (isset($object->$name) && $object->$name !== NULL) {
472 // hack for extends_entity_column_value
473 if ($name == 'extends_entity_column_value') {
474 if (in_array($object->extends, array('Event', 'Activity', 'Relationship', 'Individual', 'Organization', 'Household', 'Case'))) {
475 if ($object->extends == 'Event') {
478 elseif ($object->extends == 'Activity') {
479 $key = 'activity_type';
481 elseif ($object->extends == 'Relationship') {
482 $key = 'relationship_type';
484 elseif($object->extends == 'Case') {
487 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($object->$name, 1, -1));
489 if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) {
490 $key = 'contact_type';
494 foreach ($types as $type) {
495 if (in_array($key, array('activity_type', 'event_type', 'case_type'))) {
496 $ogID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
497 $ovParams = array('option_group_id' => $ogID, 'value' => $type);
498 CRM_Core_BAO_OptionValue
::retrieve($ovParams, $oValue);
499 $values[] = $oValue['name'];
502 $relTypeName = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
503 $values[] = $relTypeName;
507 $keyValues['extends_entity_column_value_option_group'] = $key;
508 $value = implode(',', $values);
509 $object->extends_entity_column_value
= $value;
512 echo "This extension: {$object->extends} is not yet handled";
517 $value = $object->$name;
518 if ($name == 'field_name') {
519 // hack for profile field_name
520 if (substr($value, 0, 7) == 'custom_') {
521 $cfID = substr($value, 7);
522 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
523 $value = "custom.{$tableName}.{$columnName}";
526 $keyValues[$name] = $value;
530 $keyValues +
= $this->computeMappedFields($mappedFields, $object);
536 * @param string $tagName
537 * @param array $keyValues
539 * @internal param string $additional XML
542 public function renderKeyValueXML($tagName, $keyValues) {
543 $xml = " <$tagName>";
544 foreach ($keyValues as $k => $v) {
545 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, self
::XML_VALUE_SEPARATOR
, $v));
547 $xml .= "\n </$tagName>\n";
552 * @param string $name tag name
553 * @param string $value text
554 * @param string $prefix
559 function renderTextTag($name, $value, $prefix = '') {
560 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
561 throw new Exception("Malformed tag name: $name");
563 return $prefix . "<$name>" . htmlentities($value) . "</$name>";