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
= ":;:;:;";
41 function __construct() {
43 'customGroup' => array(
45 'name' => 'CustomGroup',
46 'scope' => 'CustomGroups',
48 'idNameFields' => array('id', 'name'),
51 'customField' => array(
53 'name' => 'CustomField',
54 'scope' => 'CustomFields',
56 'idNameFields' => array('id', 'column_name'),
58 'mappedFields' => array(
59 array('optionGroup', 'option_group_id', 'option_group_name'),
60 array('customGroup', 'custom_group_id', 'custom_group_name'),
63 'optionGroup' => array(
65 'name' => 'OptionGroup',
66 'scope' => 'OptionGroups',
69 'idNameFields' => array('id', 'name'),
71 'relationshipType' => array(
73 'name' => 'RelationshipType',
74 'scope' => 'RelationshipTypes',
76 'idNameFields' => array('id', 'name_a_b'),
79 'locationType' => array(
81 'name' => 'LocationType',
82 'scope' => 'LocationTypes',
84 'idNameFields' => array('id', 'name'),
87 'optionValue' => array(
89 'name' => 'OptionValue',
90 'scope' => 'OptionValues',
93 'idNameFields' => array('value', 'name', 'prefix'),
94 'mappedFields' => array(
95 array('optionGroup', 'option_group_id', 'option_group_name'),
98 'profileGroup' => array(
100 'name' => 'ProfileGroup',
101 'scope' => 'ProfileGroups',
103 'idNameFields' => array('id', 'title'),
106 'profileField' => array(
108 'name' => 'ProfileField',
109 'scope' => 'ProfileFields',
112 'mappedFields' => array(
113 array('profileGroup', 'uf_group_id', 'profile_group_name')
116 'profileJoin' => array(
118 'name' => 'ProfileJoin',
119 'scope' => 'ProfileJoins',
122 'mappedFields' => array(
123 array('profileGroup', 'uf_group_id', 'profile_group_name')
126 'mappingGroup' => array(
128 'name' => 'MappingGroup',
129 'scope' => 'MappingGroups',
131 'idNameFields' => array('id', 'name'),
133 'mappedFields' => array(
134 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
137 'mappingField' => array(
139 'name' => 'MappingField',
140 'scope' => 'MappingFields',
143 'mappedFields' => array(
144 array('mappingGroup', 'mapping_id', 'mapping_group_name'),
145 array('locationType', 'location_type_id', 'location_type_name'),
146 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
153 * Scan local customizations and build an in-memory representation
158 // fetch the option group / values for
159 // activity type and event_type
161 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
164 SELECT distinct(g.id), g.*
165 FROM civicrm_option_group g
166 WHERE g.name IN $optionGroups
168 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
171 SELECT distinct(g.id), g.*
172 FROM civicrm_option_group g,
173 civicrm_custom_field f,
174 civicrm_custom_group cg
175 WHERE f.option_group_id = g.id
176 AND f.custom_group_id = cg.id
179 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
182 SELECT v.*, g.name as prefix
183 FROM civicrm_option_value v,
184 civicrm_option_group g
185 WHERE v.option_group_id = g.id
186 AND g.name IN $optionGroups
189 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
192 SELECT distinct(v.id), v.*, g.name as prefix
193 FROM civicrm_option_value v,
194 civicrm_option_group g,
195 civicrm_custom_field f,
196 civicrm_custom_group cg
197 WHERE v.option_group_id = g.id
198 AND f.option_group_id = g.id
199 AND f.custom_group_id = cg.id
203 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
207 FROM civicrm_relationship_type rt
208 WHERE rt.is_active = 1
210 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
214 FROM civicrm_location_type lt
215 WHERE lt.is_active = 1
217 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
221 FROM civicrm_custom_group cg
222 WHERE cg.is_active = 1
224 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
228 FROM civicrm_custom_field f,
229 civicrm_custom_group cg
230 WHERE f.custom_group_id = cg.id
233 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
235 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
237 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
242 WHERE entity_table IS NULL
243 AND entity_id IS NULL
245 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
247 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
249 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
253 * Render the in-memory representation as XML
258 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
259 $buffer .= "\n\n<CustomData>\n";
260 foreach (array_keys($this->_xml
) as $key) {
261 if (!empty($this->_xml
[$key]['data'])) {
262 $buffer .= " <{$this->_xml[$key]['scope']}>\n{$this->_xml[$key]['data']} </{$this->_xml[$key]['scope']}>\n";
264 elseif ($this->_xml
[$key]['required']) {
265 CRM_Core_Error
::fatal("No records in DB for $key");
268 $buffer .= "</CustomData>\n";
272 function fetch($groupName, $daoName, $sql = NULL) {
273 $map = isset($this->_xml
[$groupName]['idNameFields']) ?
$this->_xml
[$groupName]['idNameFields'] : NULL;
274 $add = isset($this->_xml
[$groupName]['mappedFields']) ?
$this->_xml
[$groupName]['mappedFields'] : NULL;
276 $dao = new $daoName();
284 while ($dao->fetch()) {
285 $this->_xml
[$groupName]['data'] .= $this->exportDAO($dao,
286 $this->_xml
[$groupName]['name'],
287 $this->addMappedXMLFields($add, $dao)
290 if (isset($map[2])) {
291 $this->_xml
[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
294 $this->_xml
[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
301 * Given a set of field mappings, generate XML for the mapped fields
303 * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
304 * @param CRM_Core_DAO $dao
305 * @return null|string XML
307 public function addMappedXMLFields($mappedFields, $dao) {
310 foreach ($mappedFields as $mappedField) {
311 if (isset($dao->{$mappedField[1]})) {
312 if (isset($mappedField[3])) {
313 $label = $this->_xml
[$mappedField[0]]['map']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
316 $label = $this->_xml
[$mappedField[0]]['map'][$dao->{$mappedField[1]}];
318 $additional .= "\n " . $this->renderTextTag($mappedField[2], $label);
327 * @param CRM_Core_DAO $object
328 * @param string $objectName business-entity/xml-tag name
329 * @param string $additional XML
332 function exportDAO($object, $objectName, $additional = NULL) {
333 $dbFields = & $object->fields();
335 $xml = " <$objectName>";
336 foreach ($dbFields as $name => $dontCare) {
339 substr($name, -3, 3) == '_id'
343 if (isset($object->$name) &&
344 $object->$name !== NULL
346 // hack for extends_entity_column_value
347 if ($name == 'extends_entity_column_value') {
348 if ($object->extends == 'Event' ||
349 $object->extends == 'Activity' ||
350 $object->extends == 'Relationship'
352 if ($object->extends == 'Event') {
355 elseif ($object->extends == 'Activity') {
356 $key = 'activity_type';
358 elseif ($object->extends == 'Relationship') {
359 $key = 'relationship_type';
361 $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_group', $key);
362 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
363 substr($object->$name, 1, -1)
366 foreach ($types as $type) {
367 $values[] = $this->_xml
['optionValue']['map']["$key.{$type}"];
369 $value = implode(',', $values);
370 $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_value', $value);
373 echo "This extension: {$object->extends} is not yet handled";
377 if ($name == 'field_name') {
378 $value = $object->$name;
379 // hack for profile field_name
380 if (substr($value, 0, 7) == 'custom_') {
381 $cfID = substr($value, 7);
382 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
383 $value = "custom.{$tableName}.{$columnName}";
385 $xml .= "\n " . $this->renderTextTag($name, $value);
388 $value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
,
389 self
::XML_VALUE_SEPARATOR
,
392 $xml .= "\n " . $this->renderTextTag($name, $value);
399 $xml .= "\n </$objectName>\n";
404 * @param string $name tag name
405 * @param string $value text
406 * @param string $prefix
409 function renderTextTag($name, $value, $prefix = '') {
410 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
411 throw new Exception("Malformed tag name: $name");
413 return $prefix . "<$name>" . htmlentities($value) . "</$name>";