Ian province abbreviation patch - issue 724
[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
5bc392e6 53 /**
5bc392e6 54 */
00be9182 55 public 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'),
21dfd5f5 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 126 'mappedFields' => array(
21dfd5f5 127 array('profileGroup', 'uf_group_id', 'profile_group_name'),
8632d2d7 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 136 'mappedFields' => array(
21dfd5f5 137 array('profileGroup', 'uf_group_id', 'profile_group_name'),
8632d2d7 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'),
21dfd5f5 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 166 /**
b8c71ffa 167 * Scan local customizations and build an in-memory representation.
8575f879 168 */
00be9182 169 public function build() {
6a488035
TO
170 // fetch the option group / values for
171 // activity type and event_type
172
173 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
174
175 $sql = "
0c52a8f5
TO
176 SELECT distinct(g.id), g.*
177 FROM civicrm_option_group g
178 WHERE g.name IN $optionGroups
179 ";
8632d2d7 180 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
6a488035
TO
181
182 $sql = "
0c52a8f5
TO
183 SELECT distinct(g.id), g.*
184 FROM civicrm_option_group g,
185 civicrm_custom_field f,
186 civicrm_custom_group cg
187 WHERE f.option_group_id = g.id
188 AND f.custom_group_id = cg.id
189 AND cg.is_active = 1
190 ";
8632d2d7 191 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
6a488035
TO
192
193 $sql = "
0c52a8f5
TO
194 SELECT v.*, g.name as prefix
195 FROM civicrm_option_value v,
196 civicrm_option_group g
197 WHERE v.option_group_id = g.id
198 AND g.name IN $optionGroups
199 ";
6a488035 200
8632d2d7 201 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
6a488035
TO
202
203 $sql = "
0c52a8f5
TO
204 SELECT distinct(v.id), v.*, g.name as prefix
205 FROM civicrm_option_value v,
206 civicrm_option_group g,
207 civicrm_custom_field f,
208 civicrm_custom_group cg
209 WHERE v.option_group_id = g.id
210 AND f.option_group_id = g.id
211 AND f.custom_group_id = cg.id
212 AND cg.is_active = 1
213 ";
6a488035 214
8632d2d7 215 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
6a488035
TO
216
217 $sql = "
0c52a8f5
TO
218 SELECT rt.*
219 FROM civicrm_relationship_type rt
220 WHERE rt.is_active = 1
221 ";
8632d2d7 222 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
6a488035
TO
223
224 $sql = "
0c52a8f5
TO
225 SELECT lt.*
226 FROM civicrm_location_type lt
227 WHERE lt.is_active = 1
228 ";
8632d2d7 229 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
6a488035
TO
230
231 $sql = "
0c52a8f5
TO
232 SELECT cg.*
233 FROM civicrm_custom_group cg
234 WHERE cg.is_active = 1
235 ";
8632d2d7 236 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
6a488035
TO
237
238 $sql = "
0c52a8f5
TO
239 SELECT f.*
240 FROM civicrm_custom_field f,
241 civicrm_custom_group cg
242 WHERE f.custom_group_id = cg.id
243 AND cg.is_active = 1
244 ";
8632d2d7 245 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
6a488035 246
8632d2d7 247 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
6a488035 248
8632d2d7 249 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
6a488035
TO
250
251 $sql = "
0c52a8f5
TO
252 SELECT *
253 FROM civicrm_uf_join
254 WHERE entity_table IS NULL
255 AND entity_id IS NULL
256 ";
8632d2d7 257 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
6a488035 258
8632d2d7 259 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
6a488035 260
8632d2d7 261 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
8575f879 262 }
6a488035 263
d18d8cd0 264 /**
b8c71ffa 265 * Build custom groups.
266 *
77855840
TO
267 * @param array $customGroupIds
268 * List of custom groups to export.
d18d8cd0 269 */
00be9182 270 public function buildCustomGroups($customGroupIds) {
d18d8cd0
TO
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 319 /**
77855840
TO
320 * @param array $ufGroupIds
321 * List of custom groups to export.
a304337b 322 */
00be9182 323 public function buildUFGroups($ufGroupIds) {
a304337b
TO
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 *
a6c01b45
CW
359 * @return string
360 * XML
8575f879 361 */
00be9182 362 public function toXML() {
6a488035
TO
363 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
364 $buffer .= "\n\n<CustomData>\n";
365 foreach (array_keys($this->_xml) as $key) {
366 if (!empty($this->_xml[$key]['data'])) {
9593cf03
TO
367 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
368 foreach ($this->_xml[$key]['data'] as $item) {
369 $buffer .= $this->renderKeyValueXML($this->_xml[$key]['name'], $item);
370 }
371 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
6a488035
TO
372 }
373 elseif ($this->_xml[$key]['required']) {
374 CRM_Core_Error::fatal("No records in DB for $key");
375 }
376 }
377 $buffer .= "</CustomData>\n";
8575f879 378 return $buffer;
6a488035
TO
379 }
380
5ba9f99c
TO
381 /**
382 * Generate an array-tree representation of the exported elements.
383 *
384 * @return array
385 */
00be9182 386 public function toArray() {
5ba9f99c
TO
387 $result = array();
388 foreach (array_keys($this->_xml) as $key) {
389 if (!empty($this->_xml[$key]['data'])) {
e7292422 390 $result[$this->_xml[$key]['name']] = array_values($this->_xml[$key]['data']);
5ba9f99c
TO
391 }
392 }
393 return $result;
394 }
395
5bc392e6 396 /**
100fef9d 397 * @param string $groupName
c490a46a 398 * @param string $daoName
5bc392e6
EM
399 * @param null $sql
400 */
00be9182 401 public function fetch($groupName, $daoName, $sql = NULL) {
880a956b
TO
402 $idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
403 $mappedFields = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL;
8632d2d7 404
a0d9d279 405 $dao = new $daoName();
6a488035
TO
406 if ($sql) {
407 $dao->query($sql);
408 }
409 else {
410 $dao->find();
411 }
412
413 while ($dao->fetch()) {
d18d8cd0 414 $this->_xml[$groupName]['data'][$dao->id] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields);
880a956b
TO
415 if ($idNameFields) {
416 // index the id/name fields so that we can translate from FK ids to FK names
417 if (isset($idNameFields[2])) {
9910627b 418 $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
6a488035
TO
419 }
420 else {
9910627b 421 $this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
6a488035
TO
422 }
423 }
424 }
425 }
426
a2bc8bf7 427 /**
2b2a2d10 428 * Compute any fields of the entity defined by the $mappedFields specification
a2bc8bf7 429 *
77855840
TO
430 * @param array $mappedFields
431 * Each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix).
432 * @param CRM_Core_DAO $dao
433 * The entity for which we want to prepare mapped fields.
a6c01b45
CW
434 * @return array
435 * new fields
a2bc8bf7 436 */
2b2a2d10
TO
437 public function computeMappedFields($mappedFields, $dao) {
438 $keyValues = array();
a2bc8bf7
TO
439 if ($mappedFields) {
440 foreach ($mappedFields as $mappedField) {
441 if (isset($dao->{$mappedField[1]})) {
442 if (isset($mappedField[3])) {
9910627b 443 $label = $this->_xml[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
a2bc8bf7
TO
444 }
445 else {
9910627b 446 $label = $this->_xml[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
a2bc8bf7 447 }
2b2a2d10 448 $keyValues[$mappedField[2]] = $label;
a2bc8bf7
TO
449 }
450 }
a2bc8bf7 451 }
2b2a2d10 452 return $keyValues;
a2bc8bf7
TO
453 }
454
455 /**
77855840
TO
456 * @param string $objectName
457 * Business-entity/xml-tag name.
2a6da8d7
EM
458 * @param CRM_Core_DAO $object
459 * @param $mappedFields
460 *
2b2a2d10 461 * @return array
a2bc8bf7 462 */
00be9182 463 public function exportDAO($objectName, $object, $mappedFields) {
353ffa53 464 $dbFields = &$object->fields();
6a488035 465
d6379d54
TO
466 // Filter the list of keys and values so that we only export interesting stuff
467 $keyValues = array();
6a488035
TO
468 foreach ($dbFields as $name => $dontCare) {
469 // ignore all ids
54fe18bf 470 if ($name == 'id' || substr($name, -3, 3) == '_id') {
6a488035
TO
471 continue;
472 }
54fe18bf 473 if (isset($object->$name) && $object->$name !== NULL) {
6a488035
TO
474 // hack for extends_entity_column_value
475 if ($name == 'extends_entity_column_value') {
353ffa53
TO
476 if (in_array($object->extends, array(
477 'Event',
478 'Activity',
479 'Relationship',
480 'Individual',
481 'Organization',
482 'Household',
d5cc0fc2 483 'Case',
353ffa53 484 ))) {
6a488035
TO
485 if ($object->extends == 'Event') {
486 $key = 'event_type';
487 }
488 elseif ($object->extends == 'Activity') {
489 $key = 'activity_type';
490 }
491 elseif ($object->extends == 'Relationship') {
492 $key = 'relationship_type';
493 }
353ffa53 494 elseif ($object->extends == 'Case') {
ccc78496 495 $key = 'case_type';
496 }
18e61355 497 $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1));
464cb8fd 498 $values = array();
9d1c4909
RN
499 if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) {
500 $key = 'contact_type';
501 $values = $types;
502 }
503 else {
504 foreach ($types as $type) {
0ebdbd37 505 if (in_array($key, array('activity_type', 'event_type', 'case_type'))) {
9d1c4909
RN
506 $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
507 $ovParams = array('option_group_id' => $ogID, 'value' => $type);
508 CRM_Core_BAO_OptionValue::retrieve($ovParams, $oValue);
509 $values[] = $oValue['name'];
510 }
511 else {
512 $relTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
513 $values[] = $relTypeName;
514 }
0f2ea47d 515 }
6a488035 516 }
9d1c4909 517 $keyValues['extends_entity_column_value_option_group'] = $key;
6a488035 518 $value = implode(',', $values);
0f2ea47d 519 $object->extends_entity_column_value = $value;
6a488035
TO
520 }
521 else {
522 echo "This extension: {$object->extends} is not yet handled";
523 exit();
524 }
525 }
23c41970
TO
526
527 $value = $object->$name;
6a488035 528 if ($name == 'field_name') {
6a488035
TO
529 // hack for profile field_name
530 if (substr($value, 0, 7) == 'custom_') {
531 $cfID = substr($value, 7);
532 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($cfID);
533 $value = "custom.{$tableName}.{$columnName}";
534 }
6a488035 535 }
d6379d54 536 $keyValues[$name] = $value;
6a488035
TO
537 }
538 }
d6379d54 539
2b2a2d10
TO
540 $keyValues += $this->computeMappedFields($mappedFields, $object);
541
542 return $keyValues;
82a166f0
TO
543 }
544
545 /**
546 * @param string $tagName
547 * @param array $keyValues
2a6da8d7 548 * @throws Exception
a6c01b45
CW
549 * @return string
550 * XML
82a166f0 551 */
2b2a2d10 552 public function renderKeyValueXML($tagName, $keyValues) {
82a166f0 553 $xml = " <$tagName>";
d6379d54 554 foreach ($keyValues as $k => $v) {
23c41970 555 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO::VALUE_SEPARATOR, self::XML_VALUE_SEPARATOR, $v));
d6379d54 556 }
82a166f0 557 $xml .= "\n </$tagName>\n";
6a488035
TO
558 return $xml;
559 }
fc44bb03
TO
560
561 /**
77855840
TO
562 * @param string $name
563 * Tag name.
564 * @param string $value
565 * Text.
fc44bb03 566 * @param string $prefix
77b97be7
EM
567 *
568 * @throws Exception
a6c01b45
CW
569 * @return string
570 * XML
fc44bb03 571 */
00be9182 572 public function renderTextTag($name, $value, $prefix = '') {
fc44bb03
TO
573 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
574 throw new Exception("Malformed tag name: $name");
575 }
576 return $prefix . "<$name>" . htmlentities($value) . "</$name>";
577 }
96025800 578
6a488035 579}