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