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_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
55 function __construct() {
57 'customGroup' => array(
59 'name' => 'CustomGroup',
60 'scope' => 'CustomGroups',
62 'idNameFields' => array('id', 'name'),
63 'idNameMap' => array(),
65 'customField' => array(
67 'name' => 'CustomField',
68 'scope' => 'CustomFields',
70 'idNameFields' => array('id', 'column_name'),
71 'idNameMap' => array(),
72 'mappedFields' => array(
73 array('optionGroup', 'option_group_id', 'option_group_name'),
74 array('customGroup', 'custom_group_id', 'custom_group_name'),
77 'optionGroup' => array(
79 'name' => 'OptionGroup',
80 'scope' => 'OptionGroups',
82 'idNameMap' => array(),
83 'idNameFields' => array('id', 'name'),
85 'relationshipType' => array(
87 'name' => 'RelationshipType',
88 'scope' => 'RelationshipTypes',
90 'idNameFields' => array('id', 'name_a_b'),
91 'idNameMap' => array(),
93 'locationType' => array(
95 'name' => 'LocationType',
96 'scope' => 'LocationTypes',
98 'idNameFields' => array('id', 'name'),
99 'idNameMap' => array(),
101 'optionValue' => array(
103 'name' => 'OptionValue',
104 'scope' => 'OptionValues',
106 'idNameMap' => array(),
107 'idNameFields' => array('value', 'name', 'prefix'),
108 'mappedFields' => array(
109 array('optionGroup', 'option_group_id', 'option_group_name'),
112 'profileGroup' => array(
114 'name' => 'ProfileGroup',
115 'scope' => 'ProfileGroups',
117 'idNameFields' => array('id', 'title'),
118 'idNameMap' => array(),
120 'profileField' => array(
122 'name' => 'ProfileField',
123 'scope' => 'ProfileFields',
125 'idNameMap' => array(),
126 'mappedFields' => array(
127 array('profileGroup', 'uf_group_id', 'profile_group_name')
130 'profileJoin' => array(
132 'name' => 'ProfileJoin',
133 'scope' => 'ProfileJoins',
135 'idNameMap' => array(),
136 'mappedFields' => array(
137 array('profileGroup', 'uf_group_id', 'profile_group_name')
140 'mappingGroup' => array(
142 'name' => 'MappingGroup',
143 'scope' => 'MappingGroups',
145 'idNameFields' => array('id', 'name'),
146 'idNameMap' => array(),
147 'mappedFields' => array(
148 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
151 'mappingField' => array(
153 'name' => 'MappingField',
154 'scope' => 'MappingFields',
156 'idNameMap' => array(),
157 'mappedFields' => array(
158 array('mappingGroup', 'mapping_id', 'mapping_group_name'),
159 array('locationType', 'location_type_id', 'location_type_name'),
160 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
167 * Scan local customizations and build an in-memory representation
172 // fetch the option group / values for
173 // activity type and event_type
175 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
178 SELECT distinct(g.id), g.*
179 FROM civicrm_option_group g
180 WHERE g.name IN $optionGroups
182 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
185 SELECT distinct(g.id), g.*
186 FROM civicrm_option_group g,
187 civicrm_custom_field f,
188 civicrm_custom_group cg
189 WHERE f.option_group_id = g.id
190 AND f.custom_group_id = cg.id
193 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
196 SELECT v.*, g.name as prefix
197 FROM civicrm_option_value v,
198 civicrm_option_group g
199 WHERE v.option_group_id = g.id
200 AND g.name IN $optionGroups
203 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
206 SELECT distinct(v.id), v.*, g.name as prefix
207 FROM civicrm_option_value v,
208 civicrm_option_group g,
209 civicrm_custom_field f,
210 civicrm_custom_group cg
211 WHERE v.option_group_id = g.id
212 AND f.option_group_id = g.id
213 AND f.custom_group_id = cg.id
217 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
221 FROM civicrm_relationship_type rt
222 WHERE rt.is_active = 1
224 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
228 FROM civicrm_location_type lt
229 WHERE lt.is_active = 1
231 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
235 FROM civicrm_custom_group cg
236 WHERE cg.is_active = 1
238 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
242 FROM civicrm_custom_field f,
243 civicrm_custom_group cg
244 WHERE f.custom_group_id = cg.id
247 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
249 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
251 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
256 WHERE entity_table IS NULL
257 AND entity_id IS NULL
259 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
261 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
263 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
267 * Render the in-memory representation as XML
272 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
273 $buffer .= "\n\n<CustomData>\n";
274 foreach (array_keys($this->_xml
) as $key) {
275 if (!empty($this->_xml
[$key]['data'])) {
276 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
277 foreach ($this->_xml
[$key]['data'] as $item) {
278 $buffer .= $this->renderKeyValueXML($this->_xml
[$key]['name'], $item);
280 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
282 elseif ($this->_xml
[$key]['required']) {
283 CRM_Core_Error
::fatal("No records in DB for $key");
286 $buffer .= "</CustomData>\n";
291 * Generate an array-tree representation of the exported elements.
297 foreach (array_keys($this->_xml
) as $key) {
298 if (!empty($this->_xml
[$key]['data'])) {
299 $result[ $this->_xml
[$key]['name'] ] = $this->_xml
[$key]['data'];
305 function fetch($groupName, $daoName, $sql = NULL) {
306 $idNameFields = isset($this->_xml
[$groupName]['idNameFields']) ?
$this->_xml
[$groupName]['idNameFields'] : NULL;
307 $mappedFields = isset($this->_xml
[$groupName]['mappedFields']) ?
$this->_xml
[$groupName]['mappedFields'] : NULL;
309 $dao = new $daoName();
317 while ($dao->fetch()) {
318 $this->_xml
[$groupName]['data'][] = $this->exportDAO($this->_xml
[$groupName]['name'], $dao, $mappedFields);
320 // index the id/name fields so that we can translate from FK ids to FK names
321 if (isset($idNameFields[2])) {
322 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
325 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
332 * Compute any fields of the entity defined by the $mappedFields specification
334 * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
335 * @param CRM_Core_DAO $dao the entity for which we want to prepare mapped fields
336 * @return array new fields
338 public function computeMappedFields($mappedFields, $dao) {
339 $keyValues = array();
341 foreach ($mappedFields as $mappedField) {
342 if (isset($dao->{$mappedField[1]})) {
343 if (isset($mappedField[3])) {
344 $label = $this->_xml
[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
347 $label = $this->_xml
[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
349 $keyValues[$mappedField[2]] = $label;
357 * @param CRM_Core_DAO $object
358 * @param string $objectName business-entity/xml-tag name
361 function exportDAO($objectName, $object, $mappedFields) {
362 $dbFields = & $object->fields();
364 // Filter the list of keys and values so that we only export interesting stuff
365 $keyValues = array();
366 foreach ($dbFields as $name => $dontCare) {
368 if ($name == 'id' ||
substr($name, -3, 3) == '_id') {
371 if (isset($object->$name) && $object->$name !== NULL) {
372 // hack for extends_entity_column_value
373 if ($name == 'extends_entity_column_value') {
374 if ($object->extends == 'Event' ||
375 $object->extends == 'Activity' ||
376 $object->extends == 'Relationship'
378 if ($object->extends == 'Event') {
381 elseif ($object->extends == 'Activity') {
382 $key = 'activity_type';
384 elseif ($object->extends == 'Relationship') {
385 $key = 'relationship_type';
387 $keyValues['extends_entity_column_value_option_group'] = $key;
388 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($object->$name, 1, -1));
390 foreach ($types as $type) {
391 $values[] = $this->_xml
['optionValue']['idNameMap']["$key.{$type}"];
393 $value = implode(',', $values);
394 $keyValues['extends_entity_column_value_option_value'] = $value;
397 echo "This extension: {$object->extends} is not yet handled";
402 $value = $object->$name;
403 if ($name == 'field_name') {
404 // hack for profile field_name
405 if (substr($value, 0, 7) == 'custom_') {
406 $cfID = substr($value, 7);
407 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
408 $value = "custom.{$tableName}.{$columnName}";
411 $keyValues[$name] = $value;
415 $keyValues +
= $this->computeMappedFields($mappedFields, $object);
421 * @param string $tagName
422 * @param array $keyValues
423 * @param string $additional XML
426 public function renderKeyValueXML($tagName, $keyValues) {
427 $xml = " <$tagName>";
428 foreach ($keyValues as $k => $v) {
429 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, self
::XML_VALUE_SEPARATOR
, $v));
431 $xml .= "\n </$tagName>\n";
436 * @param string $name tag name
437 * @param string $value text
438 * @param string $prefix
441 function renderTextTag($name, $value, $prefix = '') {
442 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
443 throw new Exception("Malformed tag name: $name");
445 return $prefix . "<$name>" . htmlentities($value) . "</$name>";