_xml = array( 'customGroup' => array( 'data' => NULL, 'name' => 'CustomGroup', 'scope' => 'CustomGroups', 'required' => FALSE, 'idNameFields' => array('id', 'name'), 'map' => array(), ), 'customField' => array( 'data' => NULL, 'name' => 'CustomField', 'scope' => 'CustomFields', 'required' => FALSE, 'idNameFields' => array('id', 'column_name'), 'map' => array(), 'mappedFields' => array( array('optionGroup', 'option_group_id', 'option_group_name'), array('customGroup', 'custom_group_id', 'custom_group_name'), ) ), 'optionGroup' => array( 'data' => NULL, 'name' => 'OptionGroup', 'scope' => 'OptionGroups', 'required' => FALSE, 'map' => array(), 'idNameFields' => array('id', 'name'), ), 'relationshipType' => array( 'data' => NULL, 'name' => 'RelationshipType', 'scope' => 'RelationshipTypes', 'required' => FALSE, 'idNameFields' => array('id', 'name_a_b'), 'map' => array(), ), 'locationType' => array( 'data' => NULL, 'name' => 'LocationType', 'scope' => 'LocationTypes', 'required' => FALSE, 'idNameFields' => array('id', 'name'), 'map' => array(), ), 'optionValue' => array( 'data' => NULL, 'name' => 'OptionValue', 'scope' => 'OptionValues', 'required' => FALSE, 'map' => array(), 'idNameFields' => array('value', 'name', 'prefix'), 'mappedFields' => array( array('optionGroup', 'option_group_id', 'option_group_name'), ), ), 'profileGroup' => array( 'data' => NULL, 'name' => 'ProfileGroup', 'scope' => 'ProfileGroups', 'required' => FALSE, 'idNameFields' => array('id', 'title'), 'map' => array(), ), 'profileField' => array( 'data' => NULL, 'name' => 'ProfileField', 'scope' => 'ProfileFields', 'required' => FALSE, 'map' => array(), 'mappedFields' => array( array('profileGroup', 'uf_group_id', 'profile_group_name') ), ), 'profileJoin' => array( 'data' => NULL, 'name' => 'ProfileJoin', 'scope' => 'ProfileJoins', 'required' => FALSE, 'map' => array(), 'mappedFields' => array( array('profileGroup', 'uf_group_id', 'profile_group_name') ), ), 'mappingGroup' => array( 'data' => NULL, 'name' => 'MappingGroup', 'scope' => 'MappingGroups', 'required' => FALSE, 'idNameFields' => array('id', 'name'), 'map' => array(), 'mappedFields' => array( array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'), ) ), 'mappingField' => array( 'data' => NULL, 'name' => 'MappingField', 'scope' => 'MappingFields', 'required' => FALSE, 'map' => array(), 'mappedFields' => array( array('mappingGroup', 'mapping_id', 'mapping_group_name'), array('locationType', 'location_type_id', 'location_type_name'), array('relationshipType', 'relationship_type_id', 'relationship_type_name'), ), ), ); } /** * Scan local customizations and build an in-memory representation * * @return void */ function build() { // fetch the option group / values for // activity type and event_type $optionGroups = "( 'activity_type', 'event_type', 'mapping_type' )"; $sql = " SELECT distinct(g.id), g.* FROM civicrm_option_group g WHERE g.name IN $optionGroups "; $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql); $sql = " SELECT distinct(g.id), g.* FROM civicrm_option_group g, civicrm_custom_field f, civicrm_custom_group cg WHERE f.option_group_id = g.id AND f.custom_group_id = cg.id AND cg.is_active = 1 "; $this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $sql); $sql = " SELECT v.*, g.name as prefix FROM civicrm_option_value v, civicrm_option_group g WHERE v.option_group_id = g.id AND g.name IN $optionGroups "; $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql); $sql = " SELECT distinct(v.id), v.*, g.name as prefix FROM civicrm_option_value v, civicrm_option_group g, civicrm_custom_field f, civicrm_custom_group cg WHERE v.option_group_id = g.id AND f.option_group_id = g.id AND f.custom_group_id = cg.id AND cg.is_active = 1 "; $this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql); $sql = " SELECT rt.* FROM civicrm_relationship_type rt WHERE rt.is_active = 1 "; $this->fetch('relationshipType', 'CRM_Contact_DAO_RelationshipType', $sql); $sql = " SELECT lt.* FROM civicrm_location_type lt WHERE lt.is_active = 1 "; $this->fetch('locationType', 'CRM_Core_DAO_LocationType', $sql); $sql = " SELECT cg.* FROM civicrm_custom_group cg WHERE cg.is_active = 1 "; $this->fetch('customGroup', 'CRM_Core_DAO_CustomGroup', $sql); $sql = " SELECT f.* FROM civicrm_custom_field f, civicrm_custom_group cg WHERE f.custom_group_id = cg.id AND cg.is_active = 1 "; $this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql); $this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup'); $this->fetch('profileField', 'CRM_Core_DAO_UFField'); $sql = " SELECT * FROM civicrm_uf_join WHERE entity_table IS NULL AND entity_id IS NULL "; $this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql); $this->fetch('mappingGroup', 'CRM_Core_DAO_Mapping'); $this->fetch('mappingField', 'CRM_Core_DAO_MappingField'); } /** * Render the in-memory representation as XML * * @return string XML */ function toXML() { $buffer = ''; $buffer .= "\n\n\n"; foreach (array_keys($this->_xml) as $key) { if (!empty($this->_xml[$key]['data'])) { $buffer .= " <{$this->_xml[$key]['scope']}>\n{$this->_xml[$key]['data']} _xml[$key]['scope']}>\n"; } elseif ($this->_xml[$key]['required']) { CRM_Core_Error::fatal("No records in DB for $key"); } } $buffer .= "\n"; return $buffer; } function fetch($groupName, $daoName, $sql = NULL) { $map = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL; $add = isset($this->_xml[$groupName]['mappedFields']) ? $this->_xml[$groupName]['mappedFields'] : NULL; $dao = new $daoName(); if ($sql) { $dao->query($sql); } else { $dao->find(); } while ($dao->fetch()) { $this->_xml[$groupName]['data'] .= $this->exportDAO($dao, $this->_xml[$groupName]['name'], $this->addMappedXMLFields($add, $dao) ); if ($map) { if (isset($map[2])) { $this->_xml[$groupName]['map'][$dao->{$map[2]} . '.' . $dao->{$map[0]}] = $dao->{$map[1]}; } else { $this->_xml[$groupName]['map'][$dao->{$map[0]}] = $dao->{$map[1]}; } } } } /** * Given a set of field mappings, generate XML for the mapped fields * * @param array $mappedFields each item is an array(0 => MappedEntityname, 1 => InputFieldName (id-field), 2 => OutputFieldName (name-field), 3 => OptionalPrefix) * @param CRM_Core_DAO $dao * @return null|string XML */ public function addMappedXMLFields($mappedFields, $dao) { $additional = NULL; if ($mappedFields) { foreach ($mappedFields as $mappedField) { if (isset($dao->{$mappedField[1]})) { if (isset($mappedField[3])) { $label = $this->_xml[$mappedField[0]]['map']["{$mappedField[3]}." . $dao->{$mappedField[1]}]; } else { $label = $this->_xml[$mappedField[0]]['map'][$dao->{$mappedField[1]}]; } $additional .= "\n " . $this->renderTextTag($mappedField[2], $label); } } return $additional; } return $additional; } /** * @param CRM_Core_DAO $object * @param string $objectName business-entity/xml-tag name * @param string $additional XML * @return string XML */ function exportDAO($object, $objectName, $additional = NULL) { $dbFields = & $object->fields(); $xml = " <$objectName>"; foreach ($dbFields as $name => $dontCare) { // ignore all ids if ($name == 'id' || substr($name, -3, 3) == '_id' ) { continue; } if (isset($object->$name) && $object->$name !== NULL ) { // hack for extends_entity_column_value if ($name == 'extends_entity_column_value') { if ($object->extends == 'Event' || $object->extends == 'Activity' || $object->extends == 'Relationship' ) { if ($object->extends == 'Event') { $key = 'event_type'; } elseif ($object->extends == 'Activity') { $key = 'activity_type'; } elseif ($object->extends == 'Relationship') { $key = 'relationship_type'; } $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_group', $key); $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1) ); $value = array(); foreach ($types as $type) { $values[] = $this->_xml['optionValue']['map']["$key.{$type}"]; } $value = implode(',', $values); $xml .= "\n " . $this->renderTextTag('extends_entity_column_value_option_value', $value); } else { echo "This extension: {$object->extends} is not yet handled"; exit(); } } if ($name == 'field_name') { $value = $object->$name; // hack for profile field_name if (substr($value, 0, 7) == 'custom_') { $cfID = substr($value, 7); list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($cfID); $value = "custom.{$tableName}.{$columnName}"; } $xml .= "\n " . $this->renderTextTag($name, $value); } else { $value = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, self::XML_VALUE_SEPARATOR, $object->$name ); $xml .= "\n " . $this->renderTextTag($name, $value); } } } if ($additional) { $xml .= $additional; } $xml .= "\n \n"; return $xml; } /** * @param string $name tag name * @param string $value text * @param string $prefix * @return string XML */ function renderTextTag($name, $value, $prefix = '') { if (!preg_match('/^[a-zA-Z0-9\_]+$/', $name)) { throw new Exception("Malformed tag name: $name"); } return $prefix . "<$name>" . htmlentities($value) . ""; } }