3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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
56 public function __construct() {
58 'customGroup' => array(
60 'name' => 'CustomGroup',
61 'scope' => 'CustomGroups',
63 'idNameFields' => array('id', 'name'),
64 'idNameMap' => array(),
66 'customField' => array(
68 'name' => 'CustomField',
69 'scope' => 'CustomFields',
71 'idNameFields' => array('id', 'column_name'),
72 'idNameMap' => array(),
73 'mappedFields' => array(
74 array('optionGroup', 'option_group_id', 'option_group_name'),
75 array('customGroup', 'custom_group_id', 'custom_group_name'),
78 'optionGroup' => array(
80 'name' => 'OptionGroup',
81 'scope' => 'OptionGroups',
83 'idNameMap' => array(),
84 'idNameFields' => array('id', 'name'),
86 'relationshipType' => array(
88 'name' => 'RelationshipType',
89 'scope' => 'RelationshipTypes',
91 'idNameFields' => array('id', 'name_a_b'),
92 'idNameMap' => array(),
94 'locationType' => array(
96 'name' => 'LocationType',
97 'scope' => 'LocationTypes',
99 'idNameFields' => array('id', 'name'),
100 'idNameMap' => array(),
102 'optionValue' => array(
104 'name' => 'OptionValue',
105 'scope' => 'OptionValues',
107 'idNameMap' => array(),
108 'idNameFields' => array('value', 'name', 'prefix'),
109 'mappedFields' => array(
110 array('optionGroup', 'option_group_id', 'option_group_name'),
113 'profileGroup' => array(
115 'name' => 'ProfileGroup',
116 'scope' => 'ProfileGroups',
118 'idNameFields' => array('id', 'title'),
119 'idNameMap' => array(),
121 'profileField' => array(
123 'name' => 'ProfileField',
124 'scope' => 'ProfileFields',
126 'idNameMap' => array(),
127 'mappedFields' => array(
128 array('profileGroup', 'uf_group_id', 'profile_group_name'),
131 'profileJoin' => array(
133 'name' => 'ProfileJoin',
134 'scope' => 'ProfileJoins',
136 'idNameMap' => array(),
137 'mappedFields' => array(
138 array('profileGroup', 'uf_group_id', 'profile_group_name'),
141 'mappingGroup' => array(
143 'name' => 'MappingGroup',
144 'scope' => 'MappingGroups',
146 'idNameFields' => array('id', 'name'),
147 'idNameMap' => array(),
148 'mappedFields' => array(
149 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
152 'mappingField' => array(
154 'name' => 'MappingField',
155 'scope' => 'MappingFields',
157 'idNameMap' => array(),
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'),
168 * Scan local customizations and build an in-memory representation.
170 public function build() {
171 // fetch the option group / values for
172 // activity type and event_type
174 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
177 SELECT distinct(g.id), g.*
178 FROM civicrm_option_group g
179 WHERE g.name IN $optionGroups
181 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
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
192 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
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
202 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
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
216 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
220 FROM civicrm_relationship_type rt
221 WHERE rt.is_active = 1
223 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
227 FROM civicrm_location_type lt
228 WHERE lt.is_active = 1
230 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
234 FROM civicrm_custom_group cg
235 WHERE cg.is_active = 1
237 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
241 FROM civicrm_custom_field f,
242 civicrm_custom_group cg
243 WHERE f.custom_group_id = cg.id
246 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
248 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
250 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
255 WHERE entity_table IS NULL
256 AND entity_id IS NULL
258 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
260 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
262 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
266 * Build custom groups.
268 * @param array $customGroupIds
269 * List of custom groups to export.
271 public function buildCustomGroups($customGroupIds) {
272 $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
273 if (empty($customGroupIdsSql)) {
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)
286 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $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)
300 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
304 FROM civicrm_custom_group cg
305 WHERE cg.id in ($customGroupIdsSql)
308 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
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)
317 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
321 * @param array $ufGroupIds
322 * List of custom groups to export.
324 public function buildUFGroups($ufGroupIds) {
325 $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
326 if (empty($ufGroupIdsSql)) {
332 FROM civicrm_uf_group cg
333 WHERE cg.id IN ($ufGroupIdsSql)
336 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
340 FROM civicrm_uf_field f,
342 WHERE f.uf_group_id = cg.id
343 AND cg.id IN ($ufGroupIdsSql)
345 $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
350 WHERE entity_table IS NULL
351 AND entity_id IS NULL
352 AND uf_group_id IN ($ufGroupIdsSql)
354 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
358 * Render the in-memory representation as XML
363 public function toXML() {
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'])) {
368 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
369 foreach ($this->_xml
[$key]['data'] as $item) {
370 $buffer .= $this->renderKeyValueXML($this->_xml
[$key]['name'], $item);
372 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
374 elseif ($this->_xml
[$key]['required']) {
375 CRM_Core_Error
::fatal("No records in DB for $key");
378 $buffer .= "</CustomData>\n";
383 * Generate an array-tree representation of the exported elements.
387 public function toArray() {
389 foreach (array_keys($this->_xml
) as $key) {
390 if (!empty($this->_xml
[$key]['data'])) {
391 $result[$this->_xml
[$key]['name']] = array_values($this->_xml
[$key]['data']);
398 * @param string $groupName
399 * @param string $daoName
402 public function fetch($groupName, $daoName, $sql = NULL) {
403 $idNameFields = isset($this->_xml
[$groupName]['idNameFields']) ?
$this->_xml
[$groupName]['idNameFields'] : NULL;
404 $mappedFields = isset($this->_xml
[$groupName]['mappedFields']) ?
$this->_xml
[$groupName]['mappedFields'] : NULL;
406 $dao = new $daoName();
414 while ($dao->fetch()) {
415 $this->_xml
[$groupName]['data'][$dao->id
] = $this->exportDAO($this->_xml
[$groupName]['name'], $dao, $mappedFields);
417 // index the id/name fields so that we can translate from FK ids to FK names
418 if (isset($idNameFields[2])) {
419 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
422 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
429 * Compute any fields of the entity defined by the $mappedFields specification
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.
438 public function computeMappedFields($mappedFields, $dao) {
439 $keyValues = array();
441 foreach ($mappedFields as $mappedField) {
442 if (isset($dao->{$mappedField[1]})) {
443 if (isset($mappedField[3])) {
444 $label = $this->_xml
[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
447 $label = $this->_xml
[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
449 $keyValues[$mappedField[2]] = $label;
457 * @param string $objectName
458 * Business-entity/xml-tag name.
459 * @param CRM_Core_DAO $object
460 * @param $mappedFields
464 public function exportDAO($objectName, $object, $mappedFields) {
465 $dbFields = &$object->fields();
467 // Filter the list of keys and values so that we only export interesting stuff
468 $keyValues = array();
469 foreach ($dbFields as $name => $dontCare) {
471 if ($name == 'id' ||
substr($name, -3, 3) == '_id') {
474 if (isset($object->$name) && $object->$name !== NULL) {
475 // hack for extends_entity_column_value
476 if ($name == 'extends_entity_column_value') {
477 if (in_array($object->extends, array(
486 if ($object->extends == 'Event') {
489 elseif ($object->extends == 'Activity') {
490 $key = 'activity_type';
492 elseif ($object->extends == 'Relationship') {
493 $key = 'relationship_type';
495 elseif ($object->extends == 'Case') {
498 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($object->$name, 1, -1));
500 if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) {
501 $key = 'contact_type';
505 foreach ($types as $type) {
506 if (in_array($key, array('activity_type', 'event_type', 'case_type'))) {
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'];
513 $relTypeName = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
514 $values[] = $relTypeName;
518 $keyValues['extends_entity_column_value_option_group'] = $key;
519 $value = implode(',', $values);
520 $object->extends_entity_column_value
= $value;
523 echo "This extension: {$object->extends} is not yet handled";
528 $value = $object->$name;
529 if ($name == 'field_name') {
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}";
537 $keyValues[$name] = $value;
541 $keyValues +
= $this->computeMappedFields($mappedFields, $object);
547 * @param string $tagName
548 * @param array $keyValues
553 public function renderKeyValueXML($tagName, $keyValues) {
554 $xml = " <$tagName>";
555 foreach ($keyValues as $k => $v) {
556 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, self
::XML_VALUE_SEPARATOR
, $v));
558 $xml .= "\n </$tagName>\n";
563 * @param string $name
565 * @param string $value
567 * @param string $prefix
573 public function renderTextTag($name, $value, $prefix = '') {
574 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
575 throw new Exception("Malformed tag name: $name");
577 return $prefix . "<$name>" . htmlentities($value) . "</$name>";