3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Utils_Migrate_Export
{
37 const XML_VALUE_SEPARATOR
= ":;:;:;";
40 * @var array description of export field mapping
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
57 public function __construct() {
59 'customGroup' => array(
61 'name' => 'CustomGroup',
62 'scope' => 'CustomGroups',
64 'idNameFields' => array('id', 'name'),
65 'idNameMap' => array(),
67 'customField' => array(
69 'name' => 'CustomField',
70 'scope' => 'CustomFields',
72 'idNameFields' => array('id', 'column_name'),
73 'idNameMap' => array(),
74 'mappedFields' => array(
75 array('optionGroup', 'option_group_id', 'option_group_name'),
76 array('customGroup', 'custom_group_id', 'custom_group_name'),
79 'optionGroup' => array(
81 'name' => 'OptionGroup',
82 'scope' => 'OptionGroups',
84 'idNameMap' => array(),
85 'idNameFields' => array('id', 'name'),
87 'relationshipType' => array(
89 'name' => 'RelationshipType',
90 'scope' => 'RelationshipTypes',
92 'idNameFields' => array('id', 'name_a_b'),
93 'idNameMap' => array(),
95 'locationType' => array(
97 'name' => 'LocationType',
98 'scope' => 'LocationTypes',
100 'idNameFields' => array('id', 'name'),
101 'idNameMap' => array(),
103 'optionValue' => array(
105 'name' => 'OptionValue',
106 'scope' => 'OptionValues',
108 'idNameMap' => array(),
109 'idNameFields' => array('value', 'name', 'prefix'),
110 'mappedFields' => array(
111 array('optionGroup', 'option_group_id', 'option_group_name'),
114 'profileGroup' => array(
116 'name' => 'ProfileGroup',
117 'scope' => 'ProfileGroups',
119 'idNameFields' => array('id', 'title'),
120 'idNameMap' => array(),
122 'profileField' => array(
124 'name' => 'ProfileField',
125 'scope' => 'ProfileFields',
127 'idNameMap' => array(),
128 'mappedFields' => array(
129 array('profileGroup', 'uf_group_id', 'profile_group_name'),
132 'profileJoin' => array(
134 'name' => 'ProfileJoin',
135 'scope' => 'ProfileJoins',
137 'idNameMap' => array(),
138 'mappedFields' => array(
139 array('profileGroup', 'uf_group_id', 'profile_group_name'),
142 'mappingGroup' => array(
144 'name' => 'MappingGroup',
145 'scope' => 'MappingGroups',
147 'idNameFields' => array('id', 'name'),
148 'idNameMap' => array(),
149 'mappedFields' => array(
150 array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
153 'mappingField' => array(
155 'name' => 'MappingField',
156 'scope' => 'MappingFields',
158 'idNameMap' => array(),
159 'mappedFields' => array(
160 array('mappingGroup', 'mapping_id', 'mapping_group_name'),
161 array('locationType', 'location_type_id', 'location_type_name'),
162 array('relationshipType', 'relationship_type_id', 'relationship_type_name'),
169 * Scan local customizations and build an in-memory representation
173 public function build() {
174 // fetch the option group / values for
175 // activity type and event_type
177 $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )";
180 SELECT distinct(g.id), g.*
181 FROM civicrm_option_group g
182 WHERE g.name IN $optionGroups
184 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
187 SELECT distinct(g.id), g.*
188 FROM civicrm_option_group g,
189 civicrm_custom_field f,
190 civicrm_custom_group cg
191 WHERE f.option_group_id = g.id
192 AND f.custom_group_id = cg.id
195 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
198 SELECT v.*, g.name as prefix
199 FROM civicrm_option_value v,
200 civicrm_option_group g
201 WHERE v.option_group_id = g.id
202 AND g.name IN $optionGroups
205 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
208 SELECT distinct(v.id), v.*, g.name as prefix
209 FROM civicrm_option_value v,
210 civicrm_option_group g,
211 civicrm_custom_field f,
212 civicrm_custom_group cg
213 WHERE v.option_group_id = g.id
214 AND f.option_group_id = g.id
215 AND f.custom_group_id = cg.id
219 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
223 FROM civicrm_relationship_type rt
224 WHERE rt.is_active = 1
226 $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql);
230 FROM civicrm_location_type lt
231 WHERE lt.is_active = 1
233 $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql);
237 FROM civicrm_custom_group cg
238 WHERE cg.is_active = 1
240 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
244 FROM civicrm_custom_field f,
245 civicrm_custom_group cg
246 WHERE f.custom_group_id = cg.id
249 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
251 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup');
253 $this->fetch('profileField', 'CRM_Core_DAO_UFField');
258 WHERE entity_table IS NULL
259 AND entity_id IS NULL
261 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
263 $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping');
265 $this->fetch('mappingField', 'CRM_Core_DAO_MappingField');
269 * @param array $customGroupIds
270 * List of custom groups to export.
273 public function buildCustomGroups($customGroupIds) {
274 $customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
275 if (empty($customGroupIdsSql)) {
280 SELECT distinct(g.id), g.*
281 FROM civicrm_option_group g,
282 civicrm_custom_field f,
283 civicrm_custom_group cg
284 WHERE f.option_group_id = g.id
285 AND f.custom_group_id = cg.id
286 AND cg.id in ($customGroupIdsSql)
288 $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql);
291 SELECT distinct(v.id), v.*, g.name as prefix
292 FROM civicrm_option_value v,
293 civicrm_option_group g,
294 civicrm_custom_field f,
295 civicrm_custom_group cg
296 WHERE v.option_group_id = g.id
297 AND f.option_group_id = g.id
298 AND f.custom_group_id = cg.id
299 AND cg.id in ($customGroupIdsSql)
302 $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
306 FROM civicrm_custom_group cg
307 WHERE cg.id in ($customGroupIdsSql)
310 $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql);
314 FROM civicrm_custom_field f,
315 civicrm_custom_group cg
316 WHERE f.custom_group_id = cg.id
317 AND cg.id in ($customGroupIdsSql)
319 $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
323 * @param array $ufGroupIds
324 * List of custom groups to export.
327 public function buildUFGroups($ufGroupIds) {
328 $ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
329 if (empty($ufGroupIdsSql)) {
335 FROM civicrm_uf_group cg
336 WHERE cg.id IN ($ufGroupIdsSql)
339 $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
343 FROM civicrm_uf_field f,
345 WHERE f.uf_group_id = cg.id
346 AND cg.id IN ($ufGroupIdsSql)
348 $this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
353 WHERE entity_table IS NULL
354 AND entity_id IS NULL
355 AND uf_group_id IN ($ufGroupIdsSql)
357 $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
361 * Render the in-memory representation as XML
366 public function toXML() {
367 $buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
368 $buffer .= "\n\n<CustomData>\n";
369 foreach (array_keys($this->_xml
) as $key) {
370 if (!empty($this->_xml
[$key]['data'])) {
371 $buffer .= " <{$this->_xml[$key]['scope']}>\n";
372 foreach ($this->_xml
[$key]['data'] as $item) {
373 $buffer .= $this->renderKeyValueXML($this->_xml
[$key]['name'], $item);
375 $buffer .= " </{$this->_xml[$key]['scope']}>\n";
377 elseif ($this->_xml
[$key]['required']) {
378 CRM_Core_Error
::fatal("No records in DB for $key");
381 $buffer .= "</CustomData>\n";
386 * Generate an array-tree representation of the exported elements.
390 public function toArray() {
392 foreach (array_keys($this->_xml
) as $key) {
393 if (!empty($this->_xml
[$key]['data'])) {
394 $result[$this->_xml
[$key]['name']] = array_values($this->_xml
[$key]['data']);
401 * @param string $groupName
402 * @param string $daoName
405 public function fetch($groupName, $daoName, $sql = NULL) {
406 $idNameFields = isset($this->_xml
[$groupName]['idNameFields']) ?
$this->_xml
[$groupName]['idNameFields'] : NULL;
407 $mappedFields = isset($this->_xml
[$groupName]['mappedFields']) ?
$this->_xml
[$groupName]['mappedFields'] : NULL;
409 $dao = new $daoName();
417 while ($dao->fetch()) {
418 $this->_xml
[$groupName]['data'][$dao->id
] = $this->exportDAO($this->_xml
[$groupName]['name'], $dao, $mappedFields);
420 // index the id/name fields so that we can translate from FK ids to FK names
421 if (isset($idNameFields[2])) {
422 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
425 $this->_xml
[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
432 * Compute any fields of the entity defined by the $mappedFields specification
434 * @param array $mappedFields
435 * Each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix).
436 * @param CRM_Core_DAO $dao
437 * The entity for which we want to prepare mapped fields.
441 public function computeMappedFields($mappedFields, $dao) {
442 $keyValues = array();
444 foreach ($mappedFields as $mappedField) {
445 if (isset($dao->{$mappedField[1]})) {
446 if (isset($mappedField[3])) {
447 $label = $this->_xml
[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
450 $label = $this->_xml
[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
452 $keyValues[$mappedField[2]] = $label;
460 * @param string $objectName
461 * Business-entity/xml-tag name.
462 * @param CRM_Core_DAO $object
463 * @param $mappedFields
467 public function exportDAO($objectName, $object, $mappedFields) {
468 $dbFields = &$object->fields();
470 // Filter the list of keys and values so that we only export interesting stuff
471 $keyValues = array();
472 foreach ($dbFields as $name => $dontCare) {
474 if ($name == 'id' ||
substr($name, -3, 3) == '_id') {
477 if (isset($object->$name) && $object->$name !== NULL) {
478 // hack for extends_entity_column_value
479 if ($name == 'extends_entity_column_value') {
480 if (in_array($object->extends, array(
489 if ($object->extends == 'Event') {
492 elseif ($object->extends == 'Activity') {
493 $key = 'activity_type';
495 elseif ($object->extends == 'Relationship') {
496 $key = 'relationship_type';
498 elseif ($object->extends == 'Case') {
501 $types = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, substr($object->$name, 1, -1));
503 if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) {
504 $key = 'contact_type';
508 foreach ($types as $type) {
509 if (in_array($key, array('activity_type', 'event_type', 'case_type'))) {
510 $ogID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
511 $ovParams = array('option_group_id' => $ogID, 'value' => $type);
512 CRM_Core_BAO_OptionValue
::retrieve($ovParams, $oValue);
513 $values[] = $oValue['name'];
516 $relTypeName = CRM_Core_DAO
::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
517 $values[] = $relTypeName;
521 $keyValues['extends_entity_column_value_option_group'] = $key;
522 $value = implode(',', $values);
523 $object->extends_entity_column_value
= $value;
526 echo "This extension: {$object->extends} is not yet handled";
531 $value = $object->$name;
532 if ($name == 'field_name') {
533 // hack for profile field_name
534 if (substr($value, 0, 7) == 'custom_') {
535 $cfID = substr($value, 7);
536 list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField
::getTableColumnGroup($cfID);
537 $value = "custom.{$tableName}.{$columnName}";
540 $keyValues[$name] = $value;
544 $keyValues +
= $this->computeMappedFields($mappedFields, $object);
550 * @param string $tagName
551 * @param array $keyValues
556 public function renderKeyValueXML($tagName, $keyValues) {
557 $xml = " <$tagName>";
558 foreach ($keyValues as $k => $v) {
559 $xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO
::VALUE_SEPARATOR
, self
::XML_VALUE_SEPARATOR
, $v));
561 $xml .= "\n </$tagName>\n";
566 * @param string $name
568 * @param string $value
570 * @param string $prefix
576 public function renderTextTag($name, $value, $prefix = '') {
577 if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) {
578 throw new Exception("Malformed tag name: $name");
580 return $prefix . "<$name>" . htmlentities($value) . "</$name>";