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',
50 'customField' => array(
52 'name' => 'CustomField',
53 'scope' => 'CustomFields',
57 'optionGroup' => array(
59 'name' => 'OptionGroup',
60 'scope' => 'OptionGroups',
64 'relationshipType' => array(
66 'name' => 'RelationshipType',
67 'scope' => 'RelationshipTypes',
71 'locationType' => array(
73 'name' => 'LocationType',
74 'scope' => 'LocationTypes',
78 'optionValue' => array(
80 'name' => 'OptionValue',
81 'scope' => 'OptionValues',
85 'profileGroup' => array(
87 'name' => 'ProfileGroup',
88 'scope' => 'ProfileGroups',
92 'profileField' => array(
94 'name' => 'ProfileField',
95 'scope' => 'ProfileFields',
99 'profileJoin' => array(
101 'name' => 'ProfileJoin',
102 'scope' => 'ProfileJoins',
106 'mappingGroup' => array(
108 'name' => 'MappingGroup',
109 'scope' => 'MappingGroups',
113 'mappingField' => array(
115 'name' => 'MappingField',
116 'scope' => 'MappingFields',
124 * Scan local customizations and build an in-memory representation
129 // fetch the option group / values for
130 // activity type and event_type
132 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
135 SELECT distinct(g.id), g.*
136 FROM civicrm_option_group g
137 WHERE g.name IN $optionGroups
139 $this->fetch('optionGroup',
140 'CRM_Core_DAO_OptionGroup',
146 SELECT distinct(g.id), g.*
147 FROM civicrm_option_group g,
148 civicrm_custom_field f,
149 civicrm_custom_group cg
150 WHERE f.option_group_id = g.id
151 AND f.custom_group_id = cg.id
154 $this->fetch('optionGroup',
155 'CRM_Core_DAO_OptionGroup',
161 SELECT v.*, g.name as prefix
162 FROM civicrm_option_value v,
163 civicrm_option_group g
164 WHERE v.option_group_id = g.id
165 AND g.name IN $optionGroups
168 $this->fetch('optionValue',
169 'CRM_Core_DAO_OptionValue',
171 array('value', 'name', 'prefix'),
172 array(array('optionGroup', 'option_group_id', 'option_group_name'))
176 SELECT distinct(v.id), v.*, g.name as prefix
177 FROM civicrm_option_value v,
178 civicrm_option_group g,
179 civicrm_custom_field f,
180 civicrm_custom_group cg
181 WHERE v.option_group_id = g.id
182 AND f.option_group_id = g.id
183 AND f.custom_group_id = cg.id
187 $this->fetch('optionValue',
188 'CRM_Core_DAO_OptionValue',
190 array('id', 'name', 'prefix'),
191 array(array('optionGroup', 'option_group_id', 'option_group_name'))
196 FROM civicrm_relationship_type rt
197 WHERE rt.is_active = 1
199 $this->fetch('relationshipType',
200 'CRM_Contact_DAO_RelationshipType',
202 array('id', 'name_a_b')
208 FROM civicrm_location_type lt
209 WHERE lt.is_active = 1
211 $this->fetch('locationType',
212 'CRM_Core_DAO_LocationType',
220 FROM civicrm_custom_group cg
221 WHERE cg.is_active = 1
223 $this->fetch('customGroup',
224 'CRM_Core_DAO_CustomGroup',
231 FROM civicrm_custom_field f,
232 civicrm_custom_group cg
233 WHERE f.custom_group_id = cg.id
236 $this->fetch('customField',
237 'CRM_Core_DAO_CustomField',
239 array('id', 'column_name'),
241 array('optionGroup', 'option_group_id', 'option_group_name'),
242 array('customGroup', 'custom_group_id', 'custom_group_name'),
246 $this->fetch('profileGroup',
247 'CRM_Core_DAO_UFGroup',
249 array('id', 'title'),
253 $this->fetch('profileField',
254 'CRM_Core_DAO_UFField',
257 array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
263 WHERE entity_table IS NULL
264 AND entity_id IS NULL
266 $this->fetch('profileJoin',
267 'CRM_Core_DAO_UFJoin',
270 array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
273 $this->fetch('mappingGroup',
274 'CRM_Core_DAO_Mapping',
277 array(array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'))
280 $this->fetch('mappingField',
281 'CRM_Core_DAO_MappingField',
285 array('mappingGroup', 'mapping_id', 'mapping_group_name'),
286 array('locationType', 'location_type_id', 'location_type_name'),
287 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
293 * Render the in-memory representation as XML
298 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
299 $buffer .= "\n\n<CustomData>\n";
300 foreach (array_keys($this->_xml
) as $key) {
301 if (!empty($this->_xml
[$key]['data'])) {
302 $buffer .= " <{$this->_xml[$key]['scope']}>\n{$this->_xml[$key]['data']} </{$this->_xml[$key]['scope']}>\n";
304 elseif ($this->_xml
[$key]['required']) {
305 CRM_Core_Error
::fatal("No records in DB for $key");
308 $buffer .= "</CustomData>\n";
312 function fetch($groupName, $daoName, $sql = NULL, $map = NULL, $add = NULL) {
313 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $daoName) . '.php');
315 eval("\$dao = new $daoName( );");
323 while ($dao->fetch()) {
324 $this->_xml
[$groupName]['data'] .= $this->exportDAO($dao,
325 $this->_xml
[$groupName]['name'],
326 $this->addMappedXMLFields($add, $dao)
329 if (isset($map[2])) {
330 $this->_xml
[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
333 $this->_xml
[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
340 * Given a set of field mappings, generate XML for the mapped fields
342 * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
343 * @param CRM_Core_DAO $dao
344 * @return null|string XML
346 public function addMappedXMLFields($mappedFields, $dao) {
349 foreach ($mappedFields as $mappedField) {
350 if (isset($dao->{$mappedField[1]})) {
351 if (isset($mappedField[3])) {
352 $label = $this->_xml
[$mappedField[0]]['map']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
355 $label = $this->_xml
[$mappedField[0]]['map'][$dao->{$mappedField[1]}];
357 $additional .= "\n " . $this->renderTextTag($mappedField[2], $label);
366 * @param CRM_Core_DAO $object
367 * @param string $objectName business-entity/xml-tag name
368 * @param string $additional XML
371 function exportDAO($object, $objectName, $additional = NULL) {
372 $dbFields = & $object->fields();
374 $xml = " <$objectName>";
375 foreach ($dbFields as $name => $dontCare) {
378 substr($name, -3, 3) == '_id'
382 if (isset($object->$name) &&
383 $object->$name !== NULL
385 // hack for extends_entity_column_value
386 if ($name == 'extends_entity_column_value') {
387 if ($object->extends == 'Event' ||
388 $object->extends == 'Activity' ||
389 $object->extends == 'Relationship'
391 if ($object->extends == 'Event') {
394 elseif ($object->extends == 'Activity') {
395 $key = 'activity_type';
397 elseif ($object->extends == 'Relationship') {
398 $key = 'relationship_type';
400 $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_group', $key);
401 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
402 substr($object->$name, 1, -1)
405 foreach ($types as $type) {
406 $values[] = $this->_xml
['optionValue']['map']["$key.{$type}"];
408 $value = implode(',', $values);
409 $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_value', $value);
412 echo "This extension: {$object->extends} is not yet handled";
416 if ($name == 'field_name') {
417 $value = $object->$name;
418 // hack for profile field_name
419 if (substr($value, 0, 7) == 'custom_') {
420 $cfID = substr($value, 7);
421 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
422 $value = "custom.{$tableName}.{$columnName}";
424 $xml .= "\n " . $this->renderTextTag($name, $value);
427 $value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
,
428 self
::XML_VALUE_SEPARATOR
,
431 $xml .= "\n " . $this->renderTextTag($name, $value);
438 $xml .= "\n </$objectName>\n";
443 * @param string $name tag name
444 * @param string $value text
445 * @param string $prefix
448 function renderTextTag($name, $value, $prefix ='') {
449 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
450 throw new Exception("Malformed tag name: $name");
452 return $prefix . "<$name>" . htmlentities($value) . "</$name>";