Merge pull request #695 from eileenmcnaughton/eval
[civicrm-core.git] / CRM / Utils / Migrate / Export.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Utils_Migrate_Export {
36
37 protected $_xml; function __construct() {
38 $this->_xml = array(
39 'customGroup' => array('data' => NULL,
40 'name' => 'CustomGroup',
41 'scope' => 'CustomGroups',
42 'required' => FALSE,
43 'map' => array(),
44 ),
45 'customField' => array(
46 'data' => NULL,
47 'name' => 'CustomField',
48 'scope' => 'CustomFields',
49 'required' => FALSE,
50 'map' => array(),
51 ),
52 'optionGroup' => array(
53 'data' => NULL,
54 'name' => 'OptionGroup',
55 'scope' => 'OptionGroups',
56 'required' => FALSE,
57 'map' => array(),
58 ),
59 'relationshipType' => array(
60 'data' => NULL,
61 'name' => 'RelationshipType',
62 'scope' => 'RelationshipTypes',
63 'required' => FALSE,
64 'map' => array(),
65 ),
66 'locationType' => array(
67 'data' => NULL,
68 'name' => 'LocationType',
69 'scope' => 'LocationTypes',
70 'required' => FALSE,
71 'map' => array(),
72 ),
73 'optionValue' => array(
74 'data' => NULL,
75 'name' => 'OptionValue',
76 'scope' => 'OptionValues',
77 'required' => FALSE,
78 'map' => array(),
79 ),
80 'profileGroup' => array(
81 'data' => NULL,
82 'name' => 'ProfileGroup',
83 'scope' => 'ProfileGroups',
84 'required' => FALSE,
85 'map' => array(),
86 ),
87 'profileField' => array(
88 'data' => NULL,
89 'name' => 'ProfileField',
90 'scope' => 'ProfileFields',
91 'required' => FALSE,
92 'map' => array(),
93 ),
94 'profileJoin' => array(
95 'data' => NULL,
96 'name' => 'ProfileJoin',
97 'scope' => 'ProfileJoins',
98 'required' => FALSE,
99 'map' => array(),
100 ),
101 'mappingGroup' => array(
102 'data' => NULL,
103 'name' => 'MappingGroup',
104 'scope' => 'MappingGroups',
105 'required' => FALSE,
106 'map' => array(),
107 ),
108 'mappingField' => array(
109 'data' => NULL,
110 'name' => 'MappingField',
111 'scope' => 'MappingFields',
112 'required' => FALSE,
113 'map' => array(),
114 ),
115 );
116 }
117
118 function run() {
119 // fetch the option group / values for
120 // activity type and event_type
121
122 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
123
124 $sql = "
125 SELECT distinct(g.id), g.*
126 FROM civicrm_option_group g
127 WHERE g.name IN $optionGroups
128 ";
129 $this->fetch('optionGroup',
130 'CRM_Core_DAO_OptionGroup',
131 $sql,
132 array('id', 'name')
133 );
134
135 $sql = "
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
142 AND cg.is_active = 1
143 ";
144 $this->fetch('optionGroup',
145 'CRM_Core_DAO_OptionGroup',
146 $sql,
147 array('id', 'name')
148 );
149
150 $sql = "
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
156 ";
157
158 $this->fetch('optionValue',
159 'CRM_Core_DAO_OptionValue',
160 $sql,
161 array('value', 'name', 'prefix'),
162 array(array('optionGroup', 'option_group_id', 'option_group_name'))
163 );
164
165 $sql = "
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
174 AND cg.is_active = 1
175 ";
176
177 $this->fetch('optionValue',
178 'CRM_Core_DAO_OptionValue',
179 $sql,
180 array('id', 'name', 'prefix'),
181 array(array('optionGroup', 'option_group_id', 'option_group_name'))
182 );
183
184 $sql = "
185 SELECT rt.*
186 FROM civicrm_relationship_type rt
187 WHERE rt.is_active = 1
188 ";
189 $this->fetch('relationshipType',
190 'CRM_Contact_DAO_RelationshipType',
191 $sql,
192 array('id', 'name_a_b')
193 );
194
195
196 $sql = "
197 SELECT lt.*
198 FROM civicrm_location_type lt
199 WHERE lt.is_active = 1
200 ";
201 $this->fetch('locationType',
202 'CRM_Core_DAO_LocationType',
203 $sql,
204 array('id', 'name')
205 );
206
207
208 $sql = "
209 SELECT cg.*
210 FROM civicrm_custom_group cg
211 WHERE cg.is_active = 1
212 ";
213 $this->fetch('customGroup',
214 'CRM_Core_DAO_CustomGroup',
215 $sql,
216 array('id', 'name')
217 );
218
219 $sql = "
220 SELECT f.*
221 FROM civicrm_custom_field f,
222 civicrm_custom_group cg
223 WHERE f.custom_group_id = cg.id
224 AND cg.is_active = 1
225 ";
226 $this->fetch('customField',
227 'CRM_Core_DAO_CustomField',
228 $sql,
229 array('id', 'column_name'),
230 array(array('optionGroup', 'option_group_id', 'option_group_name'),
231 array('customGroup', 'custom_group_id', 'custom_group_name'),
232 )
233 );
234
235 $this->fetch('profileGroup',
236 'CRM_Core_DAO_UFGroup',
237 NULL,
238 array('id', 'title'),
239 NULL
240 );
241
242 $this->fetch('profileField',
243 'CRM_Core_DAO_UFField',
244 NULL,
245 NULL,
246 array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
247 );
248
249 $sql = "
250 SELECT *
251 FROM civicrm_uf_join
252 WHERE entity_table IS NULL
253 AND entity_id IS NULL
254 ";
255 $this->fetch('profileJoin',
256 'CRM_Core_DAO_UFJoin',
257 $sql,
258 NULL,
259 array(array('profileGroup', 'uf_group_id', 'profile_group_name'))
260 );
261
262 $this->fetch('mappingGroup',
263 'CRM_Core_DAO_Mapping',
264 NULL,
265 array('id', 'name'),
266 array(array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'))
267 );
268
269 $this->fetch('mappingField',
270 'CRM_Core_DAO_MappingField',
271 NULL,
272 NULL,
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'),
276 )
277 );
278
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";
284 }
285 elseif ($this->_xml[$key]['required']) {
286 CRM_Core_Error::fatal("No records in DB for $key");
287 }
288 }
289 $buffer .= "</CustomData>\n";
290
291 CRM_Utils_System::download('CustomGroupData.xml', 'text/plain', $buffer);
292 }
293
294 function fetch($groupName, $daoName, $sql = NULL, $map = NULL, $add = NULL) {
295 $dao = new $daoName();
296 if ($sql) {
297 $dao->query($sql);
298 }
299 else {
300 $dao->find();
301 }
302
303 while ($dao->fetch()) {
304 $additional = NULL;
305 if ($add) {
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]}];
310 }
311 else {
312 $label = $this->_xml[$filter[0]]['map'][$dao->{$filter[1]}];
313 }
314 $additional .= "\n <{$filter[2]}>{$label}</{$filter[2]}>";
315 }
316 }
317 }
318 $this->_xml[$groupName]['data'] .= $this->exportDAO($dao,
319 $this->_xml[$groupName]['name'],
320 $additional
321 );
322 if ($map) {
323 if (isset($map[2])) {
324 $this->_xml[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]};
325 }
326 else {
327 $this->_xml[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]};
328 }
329 }
330 }
331 }
332
333 function exportDAO($object, $objectName, $additional = NULL) {
334 $dbFields = &$object->fields();
335
336 $xml = " <$objectName>";
337 foreach ($dbFields as $name => $dontCare) {
338 // ignore all ids
339 if ($name == 'id' ||
340 substr($name, -3, 3) == '_id'
341 ) {
342 continue;
343 }
344 if (isset($object->$name) &&
345 $object->$name !== NULL
346 ) {
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'
352 ) {
353 if ($object->extends == 'Event') {
354 $key = 'event_type';
355 }
356 elseif ($object->extends == 'Activity') {
357 $key = 'activity_type';
358 }
359 elseif ($object->extends == 'Relationship') {
360 $key = 'relationship_type';
361 }
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)
365 );
366 $value = array();
367 foreach ($types as $type) {
368 $values[] = $this->_xml['optionValue']['map']["$key.{$type}"];
369 }
370 $value = implode(',', $values);
371 $xml .= "\n <extends_entity_column_value_option_value>$value</extends_entity_column_value_option_value>";
372 }
373 else {
374 echo "This extension: {$object->extends} is not yet handled";
375 exit();
376 }
377 }
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}";
385 }
386 $xml .= "\n <$name>$value</$name>";
387 }
388 else {
389 $value = str_replace(CRM_Core_DAO::VALUE_SEPARATOR,
390 ":;:;:;",
391 $object->$name
392 );
393 $xml .= "\n <$name>$value</$name>";
394 }
395 }
396 }
397 if ($additional) {
398 $xml .= $additional;
399 }
400 $xml .= "\n </$objectName>\n";
401 return $xml;
402 }
403 }
404