-- CRM-12845 CRM_Utils_Migrate - Handle CustomGroup subtypes
[civicrm-core.git] / CRM / Utils / Migrate / Export.php
CommitLineData
6a488035
TO
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 */
35class CRM_Utils_Migrate_Export {
36
4f652853
TO
37 const XML_VALUE_SEPARATOR = ":;:;:;";
38
18315de9
TO
39 /**
40 * @var array description of export field mapping
41 *
42 * @code
43 * 'exampleEntityMappingName' => array(
44 * 'data' => array(), // placeholder; this will get filled-in during execution
45 * 'name' => 'CustomGroup', // per-item XML tag name
46 * 'scope' => 'CustomGroups', // container XML tag name
47 * 'required' => FALSE, // whether we *must* find records of this type
48 * 'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
49 * 'idNameMap' => array(), // placeholder; this will get filled-in during execution
50 * ),
51 * @endcode
52 */
3302658e
TO
53 protected $_xml;
54
55 function __construct() {
6a488035 56 $this->_xml = array(
3302658e 57 'customGroup' => array(
9593cf03 58 'data' => array(),
6a488035
TO
59 'name' => 'CustomGroup',
60 'scope' => 'CustomGroups',
61 'required' => FALSE,
8632d2d7 62 'idNameFields' => array('id', 'name'),
9910627b 63 'idNameMap' => array(),
6a488035
TO
64 ),
65 'customField' => array(
9593cf03 66 'data' => array(),
6a488035
TO
67 'name' => 'CustomField',
68 'scope' => 'CustomFields',
69 'required' => FALSE,
8632d2d7 70 'idNameFields' => array('id', 'column_name'),
9910627b 71 'idNameMap' => array(),
8632d2d7
TO
72 'mappedFields' => array(
73 array('optionGroup', 'option_group_id', 'option_group_name'),
74 array('customGroup', 'custom_group_id', 'custom_group_name'),
75 )
6a488035
TO
76 ),
77 'optionGroup' => array(
9593cf03 78 'data' => array(),
6a488035
TO
79 'name' => 'OptionGroup',
80 'scope' => 'OptionGroups',
81 'required' => FALSE,
9910627b 82 'idNameMap' => array(),
8632d2d7 83 'idNameFields' => array('id', 'name'),
6a488035
TO
84 ),
85 'relationshipType' => array(
9593cf03 86 'data' => array(),
6a488035
TO
87 'name' => 'RelationshipType',
88 'scope' => 'RelationshipTypes',
89 'required' => FALSE,
8632d2d7 90 'idNameFields' => array('id', 'name_a_b'),
9910627b 91 'idNameMap' => array(),
6a488035
TO
92 ),
93 'locationType' => array(
9593cf03 94 'data' => array(),
6a488035
TO
95 'name' => 'LocationType',
96 'scope' => 'LocationTypes',
97 'required' => FALSE,
8632d2d7 98 'idNameFields' => array('id', 'name'),
9910627b 99 'idNameMap' => array(),
6a488035
TO
100 ),
101 'optionValue' => array(
9593cf03 102 'data' => array(),
6a488035
TO
103 'name' => 'OptionValue',
104 'scope' => 'OptionValues',
105 'required' => FALSE,
9910627b 106 'idNameMap' => array(),
8632d2d7
TO
107 'idNameFields' => array('value', 'name', 'prefix'),
108 'mappedFields' => array(
109 array('optionGroup', 'option_group_id', 'option_group_name'),
110 ),
6a488035
TO
111 ),
112 'profileGroup' => array(
9593cf03 113 'data' => array(),
6a488035
TO
114 'name' => 'ProfileGroup',
115 'scope' => 'ProfileGroups',
116 'required' => FALSE,
8632d2d7 117 'idNameFields' => array('id', 'title'),
9910627b 118 'idNameMap' => array(),
6a488035
TO
119 ),
120 'profileField' => array(
9593cf03 121 'data' => array(),
6a488035
TO
122 'name' => 'ProfileField',
123 'scope' => 'ProfileFields',
124 'required' => FALSE,
9910627b 125 'idNameMap' => array(),
8632d2d7
TO
126 'mappedFields' => array(
127 array('profileGroup', 'uf_group_id', 'profile_group_name')
128 ),
6a488035
TO
129 ),
130 'profileJoin' => array(
9593cf03 131 'data' => array(),
6a488035
TO
132 'name' => 'ProfileJoin',
133 'scope' => 'ProfileJoins',
134 'required' => FALSE,
9910627b 135 'idNameMap' => array(),
8632d2d7
TO
136 'mappedFields' => array(
137 array('profileGroup', 'uf_group_id', 'profile_group_name')
138 ),
6a488035
TO
139 ),
140 'mappingGroup' => array(
9593cf03 141 'data' => array(),
6a488035
TO
142 'name' => 'MappingGroup',
143 'scope' => 'MappingGroups',
144 'required' => FALSE,
8632d2d7 145 'idNameFields' => array('id', 'name'),
9910627b 146 'idNameMap' => array(),
8632d2d7
TO
147 'mappedFields' => array(
148 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
149 )
6a488035
TO
150 ),
151 'mappingField' => array(
9593cf03 152 'data' => array(),
6a488035
TO
153 'name' => 'MappingField',
154 'scope' => 'MappingFields',
155 'required' => FALSE,
9910627b 156 'idNameMap' => array(),
8632d2d7
TO
157 'mappedFields' => array(
158 array('mappingGroup', 'mapping_id', 'mapping_group_name'),
159 array('locationType', 'location_type_id', 'location_type_name'),
160 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
161 ),
6a488035
TO
162 ),
163 );
164 }
165
8575f879
TO
166 /**
167 * Scan local customizations and build an in-memory representation
168 *
169 * @return void
170 */
171 function build() {
6a488035
TO
172 // fetch the option group / values for
173 // activity type and event_type
174
175 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
176
177 $sql = "
0c52a8f5
TO
178 SELECT distinct(g.id), g.*
179 FROM civicrm_option_group g
180 WHERE g.name IN $optionGroups
181 ";
8632d2d7 182 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
6a488035
TO
183
184 $sql = "
0c52a8f5
TO
185 SELECT distinct(g.id), g.*
186 FROM civicrm_option_group g,
187 civicrm_custom_field f,
188 civicrm_custom_group cg
189 WHERE f.option_group_id = g.id
190 AND f.custom_group_id = cg.id
191 AND cg.is_active = 1
192 ";
8632d2d7 193 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
6a488035
TO
194
195 $sql = "
0c52a8f5
TO
196 SELECT v.*, g.name as prefix
197 FROM civicrm_option_value v,
198 civicrm_option_group g
199 WHERE v.option_group_id = g.id
200 AND g.name IN $optionGroups
201 ";
6a488035 202
8632d2d7 203 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
6a488035
TO
204
205 $sql = "
0c52a8f5
TO
206 SELECT distinct(v.id), v.*, g.name as prefix
207 FROM civicrm_option_value v,
208 civicrm_option_group g,
209 civicrm_custom_field f,
210 civicrm_custom_group cg
211 WHERE v.option_group_id = g.id
212 AND f.option_group_id = g.id
213 AND f.custom_group_id = cg.id
214 AND cg.is_active = 1
215 ";
6a488035 216
8632d2d7 217 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
6a488035
TO
218
219 $sql = "
0c52a8f5
TO
220 SELECT rt.*
221 FROM civicrm_relationship_type rt
222 WHERE rt.is_active = 1
223 ";
8632d2d7 224 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
6a488035
TO
225
226 $sql = "
0c52a8f5
TO
227 SELECT lt.*
228 FROM civicrm_location_type lt
229 WHERE lt.is_active = 1
230 ";
8632d2d7 231 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
6a488035
TO
232
233 $sql = "
0c52a8f5
TO
234 SELECT cg.*
235 FROM civicrm_custom_group cg
236 WHERE cg.is_active = 1
237 ";
8632d2d7 238 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
6a488035
TO
239
240 $sql = "
0c52a8f5
TO
241 SELECT f.*
242 FROM civicrm_custom_field f,
243 civicrm_custom_group cg
244 WHERE f.custom_group_id = cg.id
245 AND cg.is_active = 1
246 ";
8632d2d7 247 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
6a488035 248
8632d2d7 249 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
6a488035 250
8632d2d7 251 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
6a488035
TO
252
253 $sql = "
0c52a8f5
TO
254 SELECT *
255 FROM civicrm_uf_join
256 WHERE entity_table IS NULL
257 AND entity_id IS NULL
258 ";
8632d2d7 259 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
6a488035 260
8632d2d7 261 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
6a488035 262
8632d2d7 263 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
8575f879 264 }
6a488035 265
d18d8cd0
TO
266 /**
267 * @param array $customGroupIds list of custom groups to export
268 * @return void
269 */
270 function buildCustomGroups($customGroupIds) {
271 $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
272 if (empty($customGroupIdsSql)) {
273 return;
274 }
275
276 $sql = "
277 SELECT distinct(g.id), g.*
278 FROM civicrm_option_group g,
279 civicrm_custom_field f,
280 civicrm_custom_group cg
281 WHERE f.option_group_id = g.id
282 AND f.custom_group_id = cg.id
283 AND cg.id in ($customGroupIdsSql)
284 ";
285 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
286
287 $sql = "
288 SELECT distinct(v.id), v.*, g.name as prefix
289 FROM civicrm_option_value v,
290 civicrm_option_group g,
291 civicrm_custom_field f,
292 civicrm_custom_group cg
293 WHERE v.option_group_id = g.id
294 AND f.option_group_id = g.id
295 AND f.custom_group_id = cg.id
296 AND cg.id in ($customGroupIdsSql)
297 ";
298
299 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
300
301 $sql = "
302 SELECT cg.*
303 FROM civicrm_custom_group cg
304 WHERE cg.id in ($customGroupIdsSql)
305
306 ";
307 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
308
309 $sql = "
310 SELECT f.*
311 FROM civicrm_custom_field f,
312 civicrm_custom_group cg
313 WHERE f.custom_group_id = cg.id
314 AND cg.id in ($customGroupIdsSql)
315 ";
316 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
317 }
318
a304337b
TO
319 /**
320 * @param array $ufGroupIds list of custom groups to export
321 * @return void
322 */
323 function buildUFGroups($ufGroupIds) {
324 $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
325 if (empty($ufGroupIdsSql)) {
326 return;
327 }
328
329 $sql = "
330 SELECT cg.*
331 FROM civicrm_uf_group cg
332 WHERE cg.id IN ($ufGroupIdsSql)
333
334 ";
335 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
336
337 $sql = "
338 SELECT f.*
339 FROM civicrm_uf_field f,
340 civicrm_uf_group cg
341 WHERE f.uf_group_id = cg.id
342 AND cg.id IN ($ufGroupIdsSql)
343 ";
344 $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
345
346 $sql = "
347 SELECT *
348 FROM civicrm_uf_join
349 WHERE entity_table IS NULL
350 AND entity_id IS NULL
351 AND uf_group_id IN ($ufGroupIdsSql)
352 ";
353 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
354 }
355
8575f879
TO
356 /**
357 * Render the in-memory representation as XML
358 *
359 * @return string XML
360 */
361 function toXML() {
6a488035
TO
362 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
363 $buffer .= "\n\n<CustomData>\n";
364 foreach (array_keys($this->_xml) as $key) {
365 if (!empty($this->_xml[$key]['data'])) {
9593cf03
TO
366 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
367 foreach ($this->_xml[$key]['data'] as $item) {
368 $buffer .= $this->renderKeyValueXML($this->_xml[$key]['name'], $item);
369 }
370 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
6a488035
TO
371 }
372 elseif ($this->_xml[$key]['required']) {
373 CRM_Core_Error::fatal("No records in DB for $key");
374 }
375 }
376 $buffer .= "</CustomData>\n";
8575f879 377 return $buffer;
6a488035
TO
378 }
379
5ba9f99c
TO
380 /**
381 * Generate an array-tree representation of the exported elements.
382 *
383 * @return array
384 */
385 function toArray() {
386 $result = array();
387 foreach (array_keys($this->_xml) as $key) {
388 if (!empty($this->_xml[$key]['data'])) {
d18d8cd0 389 $result[ $this->_xml[$key]['name'] ] = array_values($this->_xml[$key]['data']);
5ba9f99c
TO
390 }
391 }
392 return $result;
393 }
394
8632d2d7 395 function fetch($groupName, $daoName, $sql = NULL) {
880a956b
TO
396 $idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
397 $mappedFields = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL;
8632d2d7 398
a0d9d279 399 $dao = new $daoName();
6a488035
TO
400 if ($sql) {
401 $dao->query($sql);
402 }
403 else {
404 $dao->find();
405 }
406
407 while ($dao->fetch()) {
d18d8cd0 408 $this->_xml[$groupName]['data'][$dao->id] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields);
880a956b
TO
409 if ($idNameFields) {
410 // index the id/name fields so that we can translate from FK ids to FK names
411 if (isset($idNameFields[2])) {
9910627b 412 $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
6a488035
TO
413 }
414 else {
9910627b 415 $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
6a488035
TO
416 }
417 }
418 }
419 }
420
a2bc8bf7 421 /**
2b2a2d10 422 * Compute any fields of the entity defined by the $mappedFields specification
a2bc8bf7
TO
423 *
424 * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix)
2b2a2d10
TO
425 * @param CRM_Core_DAO $dao the entity for which we want to prepare mapped fields
426 * @return array new fields
a2bc8bf7 427 */
2b2a2d10
TO
428 public function computeMappedFields($mappedFields, $dao) {
429 $keyValues = array();
a2bc8bf7
TO
430 if ($mappedFields) {
431 foreach ($mappedFields as $mappedField) {
432 if (isset($dao->{$mappedField[1]})) {
433 if (isset($mappedField[3])) {
9910627b 434 $label = $this->_xml[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
a2bc8bf7
TO
435 }
436 else {
9910627b 437 $label = $this->_xml[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
a2bc8bf7 438 }
2b2a2d10 439 $keyValues[$mappedField[2]] = $label;
a2bc8bf7
TO
440 }
441 }
a2bc8bf7 442 }
2b2a2d10 443 return $keyValues;
a2bc8bf7
TO
444 }
445
446 /**
447 * @param CRM_Core_DAO $object
448 * @param string $objectName business-entity/xml-tag name
2b2a2d10 449 * @return array
a2bc8bf7 450 */
2b2a2d10 451 function exportDAO($objectName, $object, $mappedFields) {
3302658e 452 $dbFields = & $object->fields();
6a488035 453
d6379d54
TO
454 // Filter the list of keys and values so that we only export interesting stuff
455 $keyValues = array();
6a488035
TO
456 foreach ($dbFields as $name => $dontCare) {
457 // ignore all ids
54fe18bf 458 if ($name == 'id' || substr($name, -3, 3) == '_id') {
6a488035
TO
459 continue;
460 }
54fe18bf 461 if (isset($object->$name) && $object->$name !== NULL) {
6a488035
TO
462 // hack for extends_entity_column_value
463 if ($name == 'extends_entity_column_value') {
464 if ($object->extends == 'Event' ||
465 $object->extends == 'Activity' ||
466 $object->extends == 'Relationship'
467 ) {
468 if ($object->extends == 'Event') {
469 $key = 'event_type';
470 }
471 elseif ($object->extends == 'Activity') {
472 $key = 'activity_type';
473 }
474 elseif ($object->extends == 'Relationship') {
475 $key = 'relationship_type';
476 }
d6379d54 477 $keyValues['extends_entity_column_value_option_group'] = $key;
18e61355 478 $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1));
464cb8fd 479 $values = array();
6a488035 480 foreach ($types as $type) {
0f2ea47d
RN
481 if (in_array($key, array('activity_type', 'event_type'))) {
482 $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
483 $ovParams = array('option_group_id' => $ogID, 'value' => $type);
484 CRM_Core_BAO_OptionValue::retrieve($ovParams, $oValue);
485 $values[] = $oValue['name'];
486 }
487 else {
488 $relTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
489 $values[] = $relTypeName;
490 }
6a488035
TO
491 }
492 $value = implode(',', $values);
0f2ea47d 493 $object->extends_entity_column_value = $value;
6a488035
TO
494 }
495 else {
496 echo "This extension: {$object->extends} is not yet handled";
497 exit();
498 }
499 }
23c41970
TO
500
501 $value = $object->$name;
6a488035 502 if ($name == 'field_name') {
6a488035
TO
503 // hack for profile field_name
504 if (substr($value, 0, 7) == 'custom_') {
505 $cfID = substr($value, 7);
506 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($cfID);
507 $value = "custom.{$tableName}.{$columnName}";
508 }
6a488035 509 }
d6379d54 510 $keyValues[$name] = $value;
6a488035
TO
511 }
512 }
d6379d54 513
2b2a2d10
TO
514 $keyValues += $this->computeMappedFields($mappedFields, $object);
515
516 return $keyValues;
82a166f0
TO
517 }
518
519 /**
520 * @param string $tagName
521 * @param array $keyValues
522 * @param string $additional XML
523 * @return string XML
524 */
2b2a2d10 525 public function renderKeyValueXML($tagName, $keyValues) {
82a166f0 526 $xml = " <$tagName>";
d6379d54 527 foreach ($keyValues as $k => $v) {
23c41970 528 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO::VALUE_SEPARATOR, self::XML_VALUE_SEPARATOR, $v));
d6379d54 529 }
82a166f0 530 $xml .= "\n </$tagName>\n";
6a488035
TO
531 return $xml;
532 }
fc44bb03
TO
533
534 /**
535 * @param string $name tag name
536 * @param string $value text
537 * @param string $prefix
538 * @return string XML
539 */
8632d2d7 540 function renderTextTag($name, $value, $prefix = '') {
fc44bb03
TO
541 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
542 throw new Exception("Malformed tag name: $name");
543 }
544 return $prefix . "<$name>" . htmlentities($value) . "</$name>";
545 }
6a488035
TO
546}
547