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 protected $_xml; function __construct() {
39 'customGroup' => array('data' => NULL,
40 'name' => 'CustomGroup',
41 'scope' => 'CustomGroups',
45 'customField' => array(
47 'name' => 'CustomField',
48 'scope' => 'CustomFields',
52 'optionGroup' => array(
54 'name' => 'OptionGroup',
55 'scope' => 'OptionGroups',
59 'relationshipType' => array(
61 'name' => 'RelationshipType',
62 'scope' => 'RelationshipTypes',
66 'locationType' => array(
68 'name' => 'LocationType',
69 'scope' => 'LocationTypes',
73 'optionValue' => array(
75 'name' => 'OptionValue',
76 'scope' => 'OptionValues',
80 'profileGroup' => array(
82 'name' => 'ProfileGroup',
83 'scope' => 'ProfileGroups',
87 'profileField' => array(
89 'name' => 'ProfileField',
90 'scope' => 'ProfileFields',
94 'profileJoin' => array(
96 'name' => 'ProfileJoin',
97 'scope' => 'ProfileJoins',
101 'mappingGroup' => array(
103 'name' => 'MappingGroup',
104 'scope' => 'MappingGroups',
108 'mappingField' => array(
110 'name' => 'MappingField',
111 'scope' => 'MappingFields',
119 // fetch the option group / values for
120 // activity type and event_type
122 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
125 SELECT distinct(g.id), g.*
126 FROM civicrm_option_group g
127 WHERE g.name IN $optionGroups
129 $this->fetch('optionGroup',
130 'CRM_Core_DAO_OptionGroup',
136 SELECT distinct(g.id), g.*
137 FROM civicrm_option_group g,
138 civicrm_custom_field f,
139 civicrm_custom_group cg
140 WHERE f.option_group_id = g.id
141 AND f.custom_group_id = cg.id
144 $this->fetch('optionGroup',
145 'CRM_Core_DAO_OptionGroup',
151 SELECT v.*, g.name as prefix
152 FROM civicrm_option_value v,
153 civicrm_option_group g
154 WHERE v.option_group_id = g.id
155 AND g.name IN $optionGroups
158 $this->fetch('optionValue',
159 'CRM_Core_DAO_OptionValue',
161 array('value', 'name', 'prefix'),
162 array(array('optionGroup', 'option_group_id', 'option_group_name'))
166 SELECT distinct(v.id), v.*, g.name as prefix
167 FROM civicrm_option_value v,
168 civicrm_option_group g,
169 civicrm_custom_field f,
170 civicrm_custom_group cg
171 WHERE v.option_group_id = g.id
172 AND f.option_group_id = g.id
173 AND f.custom_group_id = cg.id
177 $this->fetch('optionValue',
178 'CRM_Core_DAO_OptionValue',
180 array('id', 'name', 'prefix'),
181 array(array('optionGroup', 'option_group_id', 'option_group_name'))
186 FROM civicrm_relationship_type rt
187 WHERE rt.is_active = 1
189 $this->fetch('relationshipType',
190 'CRM_Contact_DAO_RelationshipType',
192 array('id', 'name_a_b')
198 FROM civicrm_location_type lt
199 WHERE lt.is_active = 1
201 $this->fetch('locationType',
202 'CRM_Core_DAO_LocationType',
210 FROM civicrm_custom_group cg
211 WHERE cg.is_active = 1
213 $this->fetch('customGroup',
214 'CRM_Core_DAO_CustomGroup',
221 FROM civicrm_custom_field f,
222 civicrm_custom_group cg
223 WHERE f.custom_group_id = cg.id
226 $this->fetch('customField',
227 'CRM_Core_DAO_CustomField',
229 array('id', 'column_name'),
230 array(array('optionGroup', 'option_group_id', 'option_group_name'),
231 array('customGroup', 'custom_group_id', 'custom_group_name'),
235 $this->fetch('profileGroup',
236 'CRM_Core_DAO_UFGroup',
238 array('id', 'title'),
242 $this->fetch('profileField',
243 'CRM_Core_DAO_UFField',
246 array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
252 WHERE entity_table IS NULL
253 AND entity_id IS NULL
255 $this->fetch('profileJoin',
256 'CRM_Core_DAO_UFJoin',
259 array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
262 $this->fetch('mappingGroup',
263 'CRM_Core_DAO_Mapping',
266 array(array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'))
269 $this->fetch('mappingField',
270 'CRM_Core_DAO_MappingField',
273 array(array('mappingGroup', 'mapping_id', 'mapping_group_name'),
274 array('locationType', 'location_type_id', 'location_type_name'),
275 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
279 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
280 $buffer .= "\n\n<CustomData>\n";
281 foreach (array_keys($this->_xml
) as $key) {
282 if (!empty($this->_xml
[$key]['data'])) {
283 $buffer .= " <{$this->_xml[$key]['scope']}>\n{$this->_xml[$key]['data']} </{$this->_xml[$key]['scope']}>\n";
285 elseif ($this->_xml
[$key]['required']) {
286 CRM_Core_Error
::fatal("No records in DB for $key");
289 $buffer .= "</CustomData>\n";
291 CRM_Utils_System
::download('CustomGroupData.xml', 'text/plain', $buffer);
294 function fetch($groupName, $daoName, $sql = NULL, $map = NULL, $add = NULL) {
295 $dao = new $daoName();
303 while ($dao->fetch()) {
306 foreach ($add as $filter) {
307 if (isset($dao->{$filter[1]})) {
308 if (isset($filter[3])) {
309 $label = $this->_xml
[$filter[0]]['map']["{$filter[3]}." . $dao->{$filter[1]}];
312 $label = $this->_xml
[$filter[0]]['map'][$dao->{$filter[1]}];
314 $additional .= "\n <{$filter[2]}>{$label}</{$filter[2]}>";
318 $this->_xml
[$groupName]['data'] .= $this->exportDAO($dao,
319 $this->_xml
[$groupName]['name'],
323 if (isset($map[2])) {
324 $this->_xml
[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
327 $this->_xml
[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
333 function exportDAO($object, $objectName, $additional = NULL) {
334 $dbFields = &$object->fields();
336 $xml = " <$objectName>";
337 foreach ($dbFields as $name => $dontCare) {
340 substr($name, -3, 3) == '_id'
344 if (isset($object->$name) &&
345 $object->$name !== NULL
347 // hack for extends_entity_column_value
348 if ($name == 'extends_entity_column_value') {
349 if ($object->extends == 'Event' ||
350 $object->extends == 'Activity' ||
351 $object->extends == 'Relationship'
353 if ($object->extends == 'Event') {
356 elseif ($object->extends == 'Activity') {
357 $key = 'activity_type';
359 elseif ($object->extends == 'Relationship') {
360 $key = 'relationship_type';
362 $xml .= "\n <extends_entity_column_value_option_group>$key</extends_entity_column_value_option_group>";
363 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
364 substr($object->$name, 1, -1)
367 foreach ($types as $type) {
368 $values[] = $this->_xml
['optionValue']['map']["$key.{$type}"];
370 $value = implode(',', $values);
371 $xml .= "\n <extends_entity_column_value_option_value>$value</extends_entity_column_value_option_value>";
374 echo "This extension: {$object->extends} is not yet handled";
378 if ($name == 'field_name') {
379 $value = $object->$name;
380 // hack for profile field_name
381 if (substr($value, 0, 7) == 'custom_') {
382 $cfID = substr($value, 7);
383 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
384 $value = "custom.{$tableName}.{$columnName}";
386 $xml .= "\n <$name>$value</$name>";
389 $value = str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
,
393 $xml .= "\n <$name>$value</$name>";
400 $xml .= "\n </$objectName>\n";