3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Utils_Migrate_Export
{
35 const XML_VALUE_SEPARATOR
= ":;:;:;";
38 * @var array description of export field mapping
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
55 public function __construct() {
57 'customGroup' => array(
59 'name' => 'CustomGroup',
60 'scope' => 'CustomGroups',
62 'idNameFields' => array('id', 'name'),
63 'idNameMap' => array(),
65 'customField' => array(
67 'name' => 'CustomField',
68 'scope' => 'CustomFields',
70 'idNameFields' => array('id', 'column_name'),
71 'idNameMap' => array(),
72 'mappedFields' => array(
73 array('optionGroup', 'option_group_id', 'option_group_name'),
74 array('customGroup', 'custom_group_id', 'custom_group_name'),
77 'optionGroup' => array(
79 'name' => 'OptionGroup',
80 'scope' => 'OptionGroups',
82 'idNameMap' => array(),
83 'idNameFields' => array('id', 'name'),
85 'relationshipType' => array(
87 'name' => 'RelationshipType',
88 'scope' => 'RelationshipTypes',
90 'idNameFields' => array('id', 'name_a_b'),
91 'idNameMap' => array(),
93 'locationType' => array(
95 'name' => 'LocationType',
96 'scope' => 'LocationTypes',
98 'idNameFields' => array('id', 'name'),
99 'idNameMap' => array(),
101 'optionValue' => array(
103 'name' => 'OptionValue',
104 'scope' => 'OptionValues',
106 'idNameMap' => array(),
107 'idNameFields' => array('value', 'name', 'prefix'),
108 'mappedFields' => array(
109 array('optionGroup', 'option_group_id', 'option_group_name'),
112 'profileGroup' => array(
114 'name' => 'ProfileGroup',
115 'scope' => 'ProfileGroups',
117 'idNameFields' => array('id', 'title'),
118 'idNameMap' => array(),
120 'profileField' => array(
122 'name' => 'ProfileField',
123 'scope' => 'ProfileFields',
125 'idNameMap' => array(),
126 'mappedFields' => array(
127 array('profileGroup', 'uf_group_id', 'profile_group_name'),
130 'profileJoin' => array(
132 'name' => 'ProfileJoin',
133 'scope' => 'ProfileJoins',
135 'idNameMap' => array(),
136 'mappedFields' => array(
137 array('profileGroup', 'uf_group_id', 'profile_group_name'),
140 'mappingGroup' => array(
142 'name' => 'MappingGroup',
143 'scope' => 'MappingGroups',
145 'idNameFields' => array('id', 'name'),
146 'idNameMap' => array(),
147 'mappedFields' => array(
148 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
151 'mappingField' => array(
153 'name' => 'MappingField',
154 'scope' => 'MappingFields',
156 'idNameMap' => array(),
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'),
167 * Scan local customizations and build an in-memory representation.
169 public function build() {
170 // fetch the option group / values for
171 // activity type and event_type
173 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
176 SELECT distinct(g.id), g.*
177 FROM civicrm_option_group g
178 WHERE g.name IN $optionGroups
180 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
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
191 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
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
201 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
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
215 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
219 FROM civicrm_relationship_type rt
220 WHERE rt.is_active = 1
222 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
226 FROM civicrm_location_type lt
227 WHERE lt.is_active = 1
229 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
233 FROM civicrm_custom_group cg
234 WHERE cg.is_active = 1
236 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
240 FROM civicrm_custom_field f,
241 civicrm_custom_group cg
242 WHERE f.custom_group_id = cg.id
245 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
247 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
249 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
254 WHERE entity_table IS NULL
255 AND entity_id IS NULL
257 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
259 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
261 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
265 * Build custom groups.
267 * @param array $customGroupIds
268 * List of custom groups to export.
270 public function buildCustomGroups($customGroupIds) {
271 $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
272 if (empty($customGroupIdsSql)) {
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)
285 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $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)
299 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
303 FROM civicrm_custom_group cg
304 WHERE cg.id in ($customGroupIdsSql)
307 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
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)
316 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
320 * @param array $ufGroupIds
321 * List of custom groups to export.
323 public function buildUFGroups($ufGroupIds) {
324 $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
325 if (empty($ufGroupIdsSql)) {
331 FROM civicrm_uf_group cg
332 WHERE cg.id IN ($ufGroupIdsSql)
335 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
339 FROM civicrm_uf_field f,
341 WHERE f.uf_group_id = cg.id
342 AND cg.id IN ($ufGroupIdsSql)
344 $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
349 WHERE entity_table IS NULL
350 AND entity_id IS NULL
351 AND uf_group_id IN ($ufGroupIdsSql)
353 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
357 * Render the in-memory representation as XML
362 public function toXML() {
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'])) {
367 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
368 foreach ($this->_xml
[$key]['data'] as $item) {
369 $buffer .= $this->renderKeyValueXML($this->_xml
[$key]['name'], $item);
371 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
373 elseif ($this->_xml
[$key]['required']) {
374 CRM_Core_Error
::fatal("No records in DB for $key");
377 $buffer .= "</CustomData>\n";
382 * Generate an array-tree representation of the exported elements.
386 public function toArray() {
388 foreach (array_keys($this->_xml
) as $key) {
389 if (!empty($this->_xml
[$key]['data'])) {
390 $result[$this->_xml
[$key]['name']] = array_values($this->_xml
[$key]['data']);
397 * @param string $groupName
398 * @param string $daoName
401 public function fetch($groupName, $daoName, $sql = NULL) {
402 $idNameFields = isset($this->_xml
[$groupName]['idNameFields']) ?
$this->_xml
[$groupName]['idNameFields'] : NULL;
403 $mappedFields = isset($this->_xml
[$groupName]['mappedFields']) ?
$this->_xml
[$groupName]['mappedFields'] : NULL;
405 $dao = new $daoName();
413 while ($dao->fetch()) {
414 $this->_xml
[$groupName]['data'][$dao->id
] = $this->exportDAO($this->_xml
[$groupName]['name'], $dao, $mappedFields);
416 // index the id/name fields so that we can translate from FK ids to FK names
417 if (isset($idNameFields[2])) {
418 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
421 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
428 * Compute any fields of the entity defined by the $mappedFields specification
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.
437 public function computeMappedFields($mappedFields, $dao) {
438 $keyValues = array();
440 foreach ($mappedFields as $mappedField) {
441 if (isset($dao->{$mappedField[1]})) {
442 if (isset($mappedField[3])) {
443 $label = $this->_xml
[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
446 $label = $this->_xml
[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
448 $keyValues[$mappedField[2]] = $label;
456 * @param string $objectName
457 * Business-entity/xml-tag name.
458 * @param CRM_Core_DAO $object
459 * @param $mappedFields
463 public function exportDAO($objectName, $object, $mappedFields) {
464 $dbFields = &$object->fields();
466 // Filter the list of keys and values so that we only export interesting stuff
467 $keyValues = array();
468 foreach ($dbFields as $name => $dontCare) {
470 if ($name == 'id' ||
substr($name, -3, 3) == '_id') {
473 if (isset($object->$name) && $object->$name !== NULL) {
474 // hack for extends_entity_column_value
475 if ($name == 'extends_entity_column_value') {
476 if (in_array($object->extends, array(
485 if ($object->extends == 'Event') {
488 elseif ($object->extends == 'Activity') {
489 $key = 'activity_type';
491 elseif ($object->extends == 'Relationship') {
492 $key = 'relationship_type';
494 elseif ($object->extends == 'Case') {
497 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($object->$name, 1, -1));
499 if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) {
500 $key = 'contact_type';
504 foreach ($types as $type) {
505 if (in_array($key, array('activity_type', 'event_type', 'case_type'))) {
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'];
512 $relTypeName = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
513 $values[] = $relTypeName;
517 $keyValues['extends_entity_column_value_option_group'] = $key;
518 $value = implode(',', $values);
519 $object->extends_entity_column_value
= $value;
522 echo "This extension: {$object->extends} is not yet handled";
527 $value = $object->$name;
528 if ($name == 'field_name') {
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}";
536 $keyValues[$name] = $value;
540 $keyValues +
= $this->computeMappedFields($mappedFields, $object);
546 * @param string $tagName
547 * @param array $keyValues
552 public function renderKeyValueXML($tagName, $keyValues) {
553 $xml = " <$tagName>";
554 foreach ($keyValues as $k => $v) {
555 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, self
::XML_VALUE_SEPARATOR
, $v));
557 $xml .= "\n </$tagName>\n";
562 * @param string $name
564 * @param string $value
566 * @param string $prefix
572 public function renderTextTag($name, $value, $prefix = '') {
573 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
574 throw new Exception("Malformed tag name: $name");
576 return $prefix . "<$name>" . htmlentities($value) . "</$name>";