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()) {
326 foreach ($add as $filter) {
327 if (isset($dao->{$filter[1]})) {
328 if (isset($filter[3])) {
329 $label = $this->_xml
[$filter[0]]['map']["{$filter[3]}." . $dao->{$filter[1]}];
332 $label = $this->_xml
[$filter[0]]['map'][$dao->{$filter[1]}];
334 $additional .= "\n <{$filter[2]}>{$label}</{$filter[2]}>";
338 $this->_xml
[$groupName]['data'] .= $this->exportDAO($dao,
339 $this->_xml
[$groupName]['name'],
343 if (isset($map[2])) {
344 $this->_xml
[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
347 $this->_xml
[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
353 function exportDAO($object, $objectName, $additional = NULL) {
354 $dbFields = & $object->fields();
356 $xml = " <$objectName>";
357 foreach ($dbFields as $name => $dontCare) {
360 substr($name, -3, 3) == '_id'
364 if (isset($object->$name) &&
365 $object->$name !== NULL
367 // hack for extends_entity_column_value
368 if ($name == 'extends_entity_column_value') {
369 if ($object->extends == 'Event' ||
370 $object->extends == 'Activity' ||
371 $object->extends == 'Relationship'
373 if ($object->extends == 'Event') {
376 elseif ($object->extends == 'Activity') {
377 $key = 'activity_type';
379 elseif ($object->extends == 'Relationship') {
380 $key = 'relationship_type';
382 $xml .= "\n <extends_entity_column_value_option_group>$key</extends_entity_column_value_option_group>";
383 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
384 substr($object->$name, 1, -1)
387 foreach ($types as $type) {
388 $values[] = $this->_xml
['optionValue']['map']["$key.{$type}"];
390 $value = implode(',', $values);
391 $xml .= "\n <extends_entity_column_value_option_value>$value</extends_entity_column_value_option_value>";
394 echo "This extension: {$object->extends} is not yet handled";
398 if ($name == 'field_name') {
399 $value = $object->$name;
400 // hack for profile field_name
401 if (substr($value, 0, 7) == 'custom_') {
402 $cfID = substr($value, 7);
403 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
404 $value = "custom.{$tableName}.{$columnName}";
406 $xml .= "\n <$name>$value</$name>";
409 $value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
,
410 self
::XML_VALUE_SEPARATOR
,
413 $xml .= "\n <$name>$value</$name>";
420 $xml .= "\n </$objectName>\n";