First commit
This commit is contained in:
commit
c6e2478c40
13918 changed files with 2303184 additions and 0 deletions
580
sites/all/modules/civicrm/CRM/Utils/Migrate/Export.php
Normal file
580
sites/all/modules/civicrm/CRM/Utils/Migrate/Export.php
Normal file
|
@ -0,0 +1,580 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
class CRM_Utils_Migrate_Export {
|
||||
|
||||
const XML_VALUE_SEPARATOR = ":;:;:;";
|
||||
|
||||
/**
|
||||
* @var array description of export field mapping
|
||||
*
|
||||
* @code
|
||||
* 'exampleEntityMappingName' => array(
|
||||
* 'data' => array(), // placeholder; this will get filled-in during execution
|
||||
* 'name' => 'CustomGroup', // per-item XML tag name
|
||||
* 'scope' => 'CustomGroups', // container XML tag name
|
||||
* 'required' => FALSE, // whether we *must* find records of this type
|
||||
* 'idNameFields' => array('id', 'name'), // name of the (local/autogenerated) "id" and (portable) "name" columns
|
||||
* 'idNameMap' => array(), // placeholder; this will get filled-in during execution
|
||||
* ),
|
||||
* @endcode
|
||||
*/
|
||||
protected $_xml;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_xml = array(
|
||||
'customGroup' => array(
|
||||
'data' => array(),
|
||||
'name' => 'CustomGroup',
|
||||
'scope' => 'CustomGroups',
|
||||
'required' => FALSE,
|
||||
'idNameFields' => array('id', 'name'),
|
||||
'idNameMap' => array(),
|
||||
),
|
||||
'customField' => array(
|
||||
'data' => array(),
|
||||
'name' => 'CustomField',
|
||||
'scope' => 'CustomFields',
|
||||
'required' => FALSE,
|
||||
'idNameFields' => array('id', 'column_name'),
|
||||
'idNameMap' => array(),
|
||||
'mappedFields' => array(
|
||||
array('optionGroup', 'option_group_id', 'option_group_name'),
|
||||
array('customGroup', 'custom_group_id', 'custom_group_name'),
|
||||
),
|
||||
),
|
||||
'optionGroup' => array(
|
||||
'data' => array(),
|
||||
'name' => 'OptionGroup',
|
||||
'scope' => 'OptionGroups',
|
||||
'required' => FALSE,
|
||||
'idNameMap' => array(),
|
||||
'idNameFields' => array('id', 'name'),
|
||||
),
|
||||
'relationshipType' => array(
|
||||
'data' => array(),
|
||||
'name' => 'RelationshipType',
|
||||
'scope' => 'RelationshipTypes',
|
||||
'required' => FALSE,
|
||||
'idNameFields' => array('id', 'name_a_b'),
|
||||
'idNameMap' => array(),
|
||||
),
|
||||
'locationType' => array(
|
||||
'data' => array(),
|
||||
'name' => 'LocationType',
|
||||
'scope' => 'LocationTypes',
|
||||
'required' => FALSE,
|
||||
'idNameFields' => array('id', 'name'),
|
||||
'idNameMap' => array(),
|
||||
),
|
||||
'optionValue' => array(
|
||||
'data' => array(),
|
||||
'name' => 'OptionValue',
|
||||
'scope' => 'OptionValues',
|
||||
'required' => FALSE,
|
||||
'idNameMap' => array(),
|
||||
'idNameFields' => array('value', 'name', 'prefix'),
|
||||
'mappedFields' => array(
|
||||
array('optionGroup', 'option_group_id', 'option_group_name'),
|
||||
),
|
||||
),
|
||||
'profileGroup' => array(
|
||||
'data' => array(),
|
||||
'name' => 'ProfileGroup',
|
||||
'scope' => 'ProfileGroups',
|
||||
'required' => FALSE,
|
||||
'idNameFields' => array('id', 'title'),
|
||||
'idNameMap' => array(),
|
||||
),
|
||||
'profileField' => array(
|
||||
'data' => array(),
|
||||
'name' => 'ProfileField',
|
||||
'scope' => 'ProfileFields',
|
||||
'required' => FALSE,
|
||||
'idNameMap' => array(),
|
||||
'mappedFields' => array(
|
||||
array('profileGroup', 'uf_group_id', 'profile_group_name'),
|
||||
),
|
||||
),
|
||||
'profileJoin' => array(
|
||||
'data' => array(),
|
||||
'name' => 'ProfileJoin',
|
||||
'scope' => 'ProfileJoins',
|
||||
'required' => FALSE,
|
||||
'idNameMap' => array(),
|
||||
'mappedFields' => array(
|
||||
array('profileGroup', 'uf_group_id', 'profile_group_name'),
|
||||
),
|
||||
),
|
||||
'mappingGroup' => array(
|
||||
'data' => array(),
|
||||
'name' => 'MappingGroup',
|
||||
'scope' => 'MappingGroups',
|
||||
'required' => FALSE,
|
||||
'idNameFields' => array('id', 'name'),
|
||||
'idNameMap' => array(),
|
||||
'mappedFields' => array(
|
||||
array('optionValue', 'mapping_type_id', 'mapping_type_name', 'mapping_type'),
|
||||
),
|
||||
),
|
||||
'mappingField' => array(
|
||||
'data' => array(),
|
||||
'name' => 'MappingField',
|
||||
'scope' => 'MappingFields',
|
||||
'required' => FALSE,
|
||||
'idNameMap' => 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.
|
||||
*/
|
||||
public 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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Build custom groups.
|
||||
*
|
||||
* @param array $customGroupIds
|
||||
* List of custom groups to export.
|
||||
*/
|
||||
public function buildCustomGroups($customGroupIds) {
|
||||
$customGroupIdsSql = implode(',', array_filter($customGroupIds, 'is_numeric'));
|
||||
if (empty($customGroupIdsSql)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$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.id in ($customGroupIdsSql)
|
||||
";
|
||||
$this->fetch('optionGroup', 'CRM_Core_DAO_OptionGroup', $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.id in ($customGroupIdsSql)
|
||||
";
|
||||
|
||||
$this->fetch('optionValue', 'CRM_Core_DAO_OptionValue', $sql);
|
||||
|
||||
$sql = "
|
||||
SELECT cg.*
|
||||
FROM civicrm_custom_group cg
|
||||
WHERE cg.id in ($customGroupIdsSql)
|
||||
|
||||
";
|
||||
$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.id in ($customGroupIdsSql)
|
||||
";
|
||||
$this->fetch('customField', 'CRM_Core_DAO_CustomField', $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $ufGroupIds
|
||||
* List of custom groups to export.
|
||||
*/
|
||||
public function buildUFGroups($ufGroupIds) {
|
||||
$ufGroupIdsSql = implode(',', array_filter($ufGroupIds, 'is_numeric'));
|
||||
if (empty($ufGroupIdsSql)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sql = "
|
||||
SELECT cg.*
|
||||
FROM civicrm_uf_group cg
|
||||
WHERE cg.id IN ($ufGroupIdsSql)
|
||||
|
||||
";
|
||||
$this->fetch('profileGroup', 'CRM_Core_DAO_UFGroup', $sql);
|
||||
|
||||
$sql = "
|
||||
SELECT f.*
|
||||
FROM civicrm_uf_field f,
|
||||
civicrm_uf_group cg
|
||||
WHERE f.uf_group_id = cg.id
|
||||
AND cg.id IN ($ufGroupIdsSql)
|
||||
";
|
||||
$this->fetch('profileField', 'CRM_Core_DAO_UFField', $sql);
|
||||
|
||||
$sql = "
|
||||
SELECT *
|
||||
FROM civicrm_uf_join
|
||||
WHERE entity_table IS NULL
|
||||
AND entity_id IS NULL
|
||||
AND uf_group_id IN ($ufGroupIdsSql)
|
||||
";
|
||||
$this->fetch('profileJoin', 'CRM_Core_DAO_UFJoin', $sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the in-memory representation as XML
|
||||
*
|
||||
* @return string
|
||||
* XML
|
||||
*/
|
||||
public function toXML() {
|
||||
$buffer = '<?xml version="1.0" encoding="iso-8859-1" ?>';
|
||||
$buffer .= "\n\n<CustomData>\n";
|
||||
foreach (array_keys($this->_xml) as $key) {
|
||||
if (!empty($this->_xml[$key]['data'])) {
|
||||
$buffer .= " <{$this->_xml[$key]['scope']}>\n";
|
||||
foreach ($this->_xml[$key]['data'] as $item) {
|
||||
$buffer .= $this->renderKeyValueXML($this->_xml[$key]['name'], $item);
|
||||
}
|
||||
$buffer .= " </{$this->_xml[$key]['scope']}>\n";
|
||||
}
|
||||
elseif ($this->_xml[$key]['required']) {
|
||||
CRM_Core_Error::fatal("No records in DB for $key");
|
||||
}
|
||||
}
|
||||
$buffer .= "</CustomData>\n";
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an array-tree representation of the exported elements.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray() {
|
||||
$result = array();
|
||||
foreach (array_keys($this->_xml) as $key) {
|
||||
if (!empty($this->_xml[$key]['data'])) {
|
||||
$result[$this->_xml[$key]['name']] = array_values($this->_xml[$key]['data']);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $groupName
|
||||
* @param string $daoName
|
||||
* @param null $sql
|
||||
*/
|
||||
public function fetch($groupName, $daoName, $sql = NULL) {
|
||||
$idNameFields = isset($this->_xml[$groupName]['idNameFields']) ? $this->_xml[$groupName]['idNameFields'] : NULL;
|
||||
$mappedFields = 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'][$dao->id] = $this->exportDAO($this->_xml[$groupName]['name'], $dao, $mappedFields);
|
||||
if ($idNameFields) {
|
||||
// index the id/name fields so that we can translate from FK ids to FK names
|
||||
if (isset($idNameFields[2])) {
|
||||
$this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[2]} . '.' . $dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
|
||||
}
|
||||
else {
|
||||
$this->_xml[$groupName]['idNameMap'][$dao->{$idNameFields[0]}] = $dao->{$idNameFields[1]};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute any fields of the entity defined by the $mappedFields specification
|
||||
*
|
||||
* @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
|
||||
* The entity for which we want to prepare mapped fields.
|
||||
* @return array
|
||||
* new fields
|
||||
*/
|
||||
public function computeMappedFields($mappedFields, $dao) {
|
||||
$keyValues = array();
|
||||
if ($mappedFields) {
|
||||
foreach ($mappedFields as $mappedField) {
|
||||
if (isset($dao->{$mappedField[1]})) {
|
||||
if (isset($mappedField[3])) {
|
||||
$label = $this->_xml[$mappedField[0]]['idNameMap']["{$mappedField[3]}." . $dao->{$mappedField[1]}];
|
||||
}
|
||||
else {
|
||||
$label = $this->_xml[$mappedField[0]]['idNameMap'][$dao->{$mappedField[1]}];
|
||||
}
|
||||
$keyValues[$mappedField[2]] = $label;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $keyValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $objectName
|
||||
* Business-entity/xml-tag name.
|
||||
* @param CRM_Core_DAO $object
|
||||
* @param $mappedFields
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function exportDAO($objectName, $object, $mappedFields) {
|
||||
$dbFields = &$object->fields();
|
||||
|
||||
// Filter the list of keys and values so that we only export interesting stuff
|
||||
$keyValues = array();
|
||||
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 (in_array($object->extends, array(
|
||||
'Event',
|
||||
'Activity',
|
||||
'Relationship',
|
||||
'Individual',
|
||||
'Organization',
|
||||
'Household',
|
||||
'Case',
|
||||
))) {
|
||||
if ($object->extends == 'Event') {
|
||||
$key = 'event_type';
|
||||
}
|
||||
elseif ($object->extends == 'Activity') {
|
||||
$key = 'activity_type';
|
||||
}
|
||||
elseif ($object->extends == 'Relationship') {
|
||||
$key = 'relationship_type';
|
||||
}
|
||||
elseif ($object->extends == 'Case') {
|
||||
$key = 'case_type';
|
||||
}
|
||||
$types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($object->$name, 1, -1));
|
||||
$values = array();
|
||||
if (in_array($object->extends, array('Individual', 'Organization', 'Household'))) {
|
||||
$key = 'contact_type';
|
||||
$values = $types;
|
||||
}
|
||||
else {
|
||||
foreach ($types as $type) {
|
||||
if (in_array($key, array('activity_type', 'event_type', 'case_type'))) {
|
||||
$ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $key, 'id', 'name');
|
||||
$ovParams = array('option_group_id' => $ogID, 'value' => $type);
|
||||
CRM_Core_BAO_OptionValue::retrieve($ovParams, $oValue);
|
||||
$values[] = $oValue['name'];
|
||||
}
|
||||
else {
|
||||
$relTypeName = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $type, 'name_a_b', 'id');
|
||||
$values[] = $relTypeName;
|
||||
}
|
||||
}
|
||||
}
|
||||
$keyValues['extends_entity_column_value_option_group'] = $key;
|
||||
$value = implode(',', $values);
|
||||
$object->extends_entity_column_value = $value;
|
||||
}
|
||||
else {
|
||||
echo "This extension: {$object->extends} is not yet handled";
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
$value = $object->$name;
|
||||
if ($name == 'field_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}";
|
||||
}
|
||||
}
|
||||
$keyValues[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$keyValues += $this->computeMappedFields($mappedFields, $object);
|
||||
|
||||
return $keyValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tagName
|
||||
* @param array $keyValues
|
||||
* @throws Exception
|
||||
* @return string
|
||||
* XML
|
||||
*/
|
||||
public function renderKeyValueXML($tagName, $keyValues) {
|
||||
$xml = " <$tagName>";
|
||||
foreach ($keyValues as $k => $v) {
|
||||
$xml .= "\n " . $this->renderTextTag($k, str_replace(CRM_Core_DAO::VALUE_SEPARATOR, self::XML_VALUE_SEPARATOR, $v));
|
||||
}
|
||||
$xml .= "\n </$tagName>\n";
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* Tag name.
|
||||
* @param string $value
|
||||
* Text.
|
||||
* @param string $prefix
|
||||
*
|
||||
* @throws Exception
|
||||
* @return string
|
||||
* XML
|
||||
*/
|
||||
public 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) . "</$name>";
|
||||
}
|
||||
|
||||
}
|
649
sites/all/modules/civicrm/CRM/Utils/Migrate/ExportJSON.php
Normal file
649
sites/all/modules/civicrm/CRM/Utils/Migrate/ExportJSON.php
Normal file
|
@ -0,0 +1,649 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
class CRM_Utils_Migrate_ExportJSON {
|
||||
const CHUNK_SIZE = 128;
|
||||
|
||||
protected $_contactIDs;
|
||||
|
||||
protected $_allContactIDs;
|
||||
|
||||
protected $_values;
|
||||
|
||||
protected $_discoverContacts = FALSE;
|
||||
|
||||
protected $_renameGroups = 1;
|
||||
|
||||
protected $_renameTags = 1;
|
||||
|
||||
protected $_sitePrefix = 'Site 1';
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*/
|
||||
public function __construct(&$params) {
|
||||
foreach ($params as $name => $value) {
|
||||
$varName = '_' . $name;
|
||||
$this->$varName = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a large array of contactIDs into more manageable smaller chunks.
|
||||
*
|
||||
* @param array $contactIDs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function &splitContactIDs(&$contactIDs) {
|
||||
// contactIDs could be a real large array, so we split it up into
|
||||
// smaller chunks and then general xml for each chunk
|
||||
$chunks = array();
|
||||
$current = 0;
|
||||
$chunks[$current] = array();
|
||||
$count = 0;
|
||||
|
||||
foreach ($contactIDs as $k => $v) {
|
||||
$chunks[$current][$k] = $v;
|
||||
$count++;
|
||||
|
||||
if ($count == self::CHUNK_SIZE) {
|
||||
$current++;
|
||||
$chunks[$current] = array();
|
||||
$count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($chunks[$current])) {
|
||||
unset($chunks[$current]);
|
||||
}
|
||||
|
||||
return $chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a set of contact IDs get the values.
|
||||
*
|
||||
* @param array $contactIDs
|
||||
* @param array $additionalContactIDs
|
||||
*/
|
||||
public function getValues(&$contactIDs, &$additionalContactIDs) {
|
||||
|
||||
$this->contact($contactIDs);
|
||||
$this->address($contactIDs);
|
||||
$this->phone($contactIDs);
|
||||
$this->email($contactIDs);
|
||||
$this->im($contactIDs);
|
||||
$this->website($contactIDs);
|
||||
$this->note($contactIDs);
|
||||
|
||||
$this->group($contactIDs);
|
||||
$this->groupContact($contactIDs);
|
||||
$this->savedSearch($contactIDs);
|
||||
|
||||
$this->tag($contactIDs);
|
||||
$this->entityTag($contactIDs);
|
||||
|
||||
$this->relationship($contactIDs, $additionalContactIDs);
|
||||
$this->activity($contactIDs, $additionalContactIDs);
|
||||
}
|
||||
|
||||
public function metaData() {
|
||||
$optionGroupVars = array(
|
||||
'prefix_id' => 'individual_prefix',
|
||||
'suffix_id' => 'individual_suffix',
|
||||
'gender_id' => 'gender',
|
||||
'mobile_provider' => 'mobile_provider',
|
||||
'phone_type' => 'phone_type',
|
||||
'activity_type' => 'activity_type',
|
||||
'status_id' => 'activity_status_id',
|
||||
'priority_id' => 'activity_priority_id',
|
||||
'medium_id' => 'encounter_medium',
|
||||
'communication_style_id' => 'communication_style',
|
||||
'email_greeting' => 'email_greeting',
|
||||
'postal_greeting' => 'postal_greeting',
|
||||
'addressee_id' => 'addressee',
|
||||
);
|
||||
$this->optionGroup($optionGroupVars);
|
||||
|
||||
$auxilaryTables = array(
|
||||
'civicrm_location_type' => 'CRM_Core_DAO_LocationType',
|
||||
'civicrm_relationship_type' => 'CRM_Contact_DAO_RelationshipType',
|
||||
);
|
||||
$this->auxTable($auxilaryTables);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $tables
|
||||
*/
|
||||
public function auxTable($tables) {
|
||||
foreach ($tables as $tableName => $daoName) {
|
||||
$fields = &$this->dbFields($daoName, TRUE);
|
||||
|
||||
$sql = "SELECT * from $tableName";
|
||||
$this->sql($sql, $tableName, $fields);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $optionGroupVars
|
||||
*/
|
||||
public function optionGroup($optionGroupVars) {
|
||||
$names = array_values($optionGroupVars);
|
||||
$str = array();
|
||||
foreach ($names as $name) {
|
||||
$str[] = "'$name'";
|
||||
}
|
||||
$nameString = implode(",", $str);
|
||||
|
||||
$sql = "
|
||||
SELECT *
|
||||
FROM civicrm_option_group
|
||||
WHERE name IN ( $nameString )
|
||||
";
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_OptionGroup', TRUE);
|
||||
$this->sql($sql, 'civicrm_option_group', $fields);
|
||||
|
||||
$sql = "
|
||||
SELECT v.*
|
||||
FROM civicrm_option_value v
|
||||
INNER JOIN civicrm_option_group g ON v.option_group_id = g.id
|
||||
WHERE g.name IN ( $nameString )
|
||||
";
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_OptionValue', TRUE);
|
||||
$this->sql($sql, 'civicrm_option_value', $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $ids
|
||||
* @param string $tableName
|
||||
* @param $fields
|
||||
* @param $whereField
|
||||
* @param null $additionalWhereCond
|
||||
*/
|
||||
public function table(
|
||||
&$ids,
|
||||
$tableName,
|
||||
&$fields,
|
||||
$whereField,
|
||||
$additionalWhereCond = NULL
|
||||
) {
|
||||
if (empty($ids)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$idString = implode(',', $ids);
|
||||
|
||||
$sql = "
|
||||
SELECT *
|
||||
FROM $tableName
|
||||
WHERE $whereField IN ( $idString )
|
||||
";
|
||||
|
||||
if ($additionalWhereCond) {
|
||||
$sql .= " AND $additionalWhereCond";
|
||||
}
|
||||
|
||||
$this->sql($sql, $tableName, $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @param string $tableName
|
||||
* @param $fields
|
||||
*/
|
||||
public function sql($sql, $tableName, &$fields) {
|
||||
$dao = &CRM_Core_DAO::executeQuery($sql);
|
||||
|
||||
while ($dao->fetch()) {
|
||||
$value = array();
|
||||
foreach ($fields as $name) {
|
||||
if (empty($dao->$name)) {
|
||||
$value[$name] = NULL;
|
||||
}
|
||||
else {
|
||||
$value[$name] = $dao->$name;
|
||||
}
|
||||
}
|
||||
$this->appendValue($dao->id, $tableName, $value);
|
||||
}
|
||||
$dao->free();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function contact(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Contact_DAO_Contact', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_contact', $fields, 'id', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function note(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_Note', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function phone(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_Phone', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_phone', $fields, 'contact_id', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function email(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_email', $fields, 'contact_id', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function im(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_IM', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_im', $fields, 'contact_id', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function website(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_Website', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_website', $fields, 'contact_id', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function address(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_Email', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_address', $fields, 'contact_id', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function groupContact(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Contact_DAO_GroupContact', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_group_contact', $fields, 'contact_id', NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo support group inheritance
|
||||
*
|
||||
* Parent child group ids are encoded in a text string
|
||||
*
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function group(&$contactIDs) {
|
||||
// handle groups only once
|
||||
static $_groupsHandled = array();
|
||||
|
||||
$ids = implode(',', $contactIDs);
|
||||
|
||||
$sql = "
|
||||
SELECT DISTINCT group_id
|
||||
FROM civicrm_group_contact
|
||||
WHERE contact_id IN ( $ids )
|
||||
";
|
||||
$dao = CRM_Core_DAO::executeQuery($sql);
|
||||
$groupIDs = array();
|
||||
while ($dao->fetch()) {
|
||||
if (!isset($_groupsHandled[$dao->group_id])) {
|
||||
$groupIDs[] = $dao->group_id;
|
||||
$_groupsHandled[$dao->group_id] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$fields = &$this->dbFields('CRM_Contact_DAO_Group', TRUE);
|
||||
$this->table($groupIDs, 'civicrm_group', $fields, 'id');
|
||||
|
||||
$this->savedSearch($groupIDs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo support search builder and custom saved searches
|
||||
* @param $groupIDs
|
||||
*/
|
||||
public function savedSearch(&$groupIDs) {
|
||||
if (empty($groupIDs)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$idString = implode(",", $groupIDs);
|
||||
$sql = "
|
||||
SELECT s.*
|
||||
FROM civicrm_saved_search s
|
||||
INNER JOIN civicrm_group g on g.saved_search_id = s.id
|
||||
WHERE g.id IN ( $idString )
|
||||
";
|
||||
|
||||
$fields = &$this->dbFields('CRM_Contact_DAO_SavedSearch', TRUE);
|
||||
$this->sql($sql, 'civicrm_saved_search', $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function entityTag(&$contactIDs) {
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_EntityTag', TRUE);
|
||||
$this->table($contactIDs, 'civicrm_entity_tag', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function tag(&$contactIDs) {
|
||||
// handle tags only once
|
||||
static $_tagsHandled = array();
|
||||
|
||||
$ids = implode(',', $contactIDs);
|
||||
|
||||
$sql = "
|
||||
SELECT DISTINCT tag_id
|
||||
FROM civicrm_entity_tag
|
||||
WHERE entity_id IN ( $ids )
|
||||
AND entity_table = 'civicrm_contact'
|
||||
";
|
||||
$dao = CRM_Core_DAO::executeQuery($sql);
|
||||
$tagIDs = array();
|
||||
while ($dao->fetch()) {
|
||||
if (!isset($_tagsHandled[$dao->tag_id])) {
|
||||
$tagIDs[] = $dao->tag_id;
|
||||
$_tagsHandled[$dao->tag_id] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
$fields = &$this->dbFields('CRM_Core_DAO_Tag', TRUE);
|
||||
$this->table($tagIDs, 'civicrm_tag', $fields, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
* @param $additionalContacts
|
||||
*/
|
||||
public function relationship(&$contactIDs, &$additionalContacts) {
|
||||
// handle relationships only once
|
||||
static $_relationshipsHandled = array();
|
||||
|
||||
$ids = implode(',', $contactIDs);
|
||||
|
||||
$sql = "(
|
||||
SELECT r.*
|
||||
FROM civicrm_relationship r
|
||||
WHERE r.contact_id_a IN ( $ids )
|
||||
) UNION (
|
||||
SELECT r.*
|
||||
FROM civicrm_relationship r
|
||||
WHERE r.contact_id_b IN ( $ids )
|
||||
)
|
||||
";
|
||||
|
||||
$fields = $this->dbFields('CRM_Contact_DAO_Relationship', TRUE);
|
||||
$dao = &CRM_Core_DAO::executeQuery($sql);
|
||||
while ($dao->fetch()) {
|
||||
if (isset($_relationshipsHandled[$dao->id])) {
|
||||
continue;
|
||||
}
|
||||
$_relationshipsHandled[$dao->id] = $dao->id;
|
||||
|
||||
$relationship = array();
|
||||
foreach ($fields as $fld) {
|
||||
if (empty($dao->$fld)) {
|
||||
$relationship[$fld] = NULL;
|
||||
}
|
||||
else {
|
||||
$relationship[$fld] = $dao->$fld;
|
||||
}
|
||||
}
|
||||
$this->appendValue($dao->id, 'civicrm_relationship', $relationship);
|
||||
|
||||
$this->addAdditionalContacts(array(
|
||||
$dao->contact_id_a,
|
||||
$dao->contact_id_b,
|
||||
),
|
||||
$additionalContacts
|
||||
);
|
||||
}
|
||||
$dao->free();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
* @param $additionalContacts
|
||||
*/
|
||||
public function activity(&$contactIDs, &$additionalContacts) {
|
||||
static $_activitiesHandled = array();
|
||||
$activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
|
||||
$assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
|
||||
$targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
|
||||
$ids = implode(',', $contactIDs);
|
||||
|
||||
// query framing returning all contacts in valid activity
|
||||
$sql = "
|
||||
SELECT a.*, ac.id as acID, ac.activity_id, ac.contact_id, ac.record_type_id
|
||||
FROM civicrm_activity a
|
||||
INNER JOIN civicrm_activity_contact ac ON ac.activity_id = a.id
|
||||
WHERE ac.contact_id IN ( $ids )
|
||||
AND (a.activity_type_id != 3 AND a.activity_type_id != 20)
|
||||
";
|
||||
|
||||
$fields = &$this->dbFields('CRM_Activity_DAO_Activity', TRUE);
|
||||
|
||||
$dao = &CRM_Core_DAO::executeQuery($sql);
|
||||
while ($dao->fetch()) {
|
||||
// adding source, target and assignee contacts in additional contacts array
|
||||
$this->addAdditionalContacts(array($dao->contact_id),
|
||||
$additionalContacts
|
||||
);
|
||||
|
||||
// append values of activity contacts
|
||||
$activityContacts = array(
|
||||
'id' => $dao->acID,
|
||||
'contact_id' => $dao->contact_id,
|
||||
'activity_id' => $dao->activity_id,
|
||||
'record_type_id' => $dao->record_type_id,
|
||||
);
|
||||
$this->appendValue($dao->acID, 'civicrm_activity_contact', $activityContacts);
|
||||
|
||||
if (isset($_activitiesHandled[$dao->id])) {
|
||||
continue;
|
||||
}
|
||||
$_activitiesHandled[$dao->id] = $dao->id;
|
||||
|
||||
$activity = array();
|
||||
foreach ($fields as $fld) {
|
||||
if (empty($dao->$fld)) {
|
||||
$activity[$fld] = NULL;
|
||||
}
|
||||
else {
|
||||
$activity[$fld] = $dao->$fld;
|
||||
}
|
||||
}
|
||||
|
||||
// append activity value
|
||||
$this->appendValue($dao->id, 'civicrm_activity', $activity);
|
||||
}
|
||||
$dao->free();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param string $name
|
||||
* @param $value
|
||||
*/
|
||||
public function appendValue($id, $name, $value) {
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($this->_values[$name])) {
|
||||
$this->_values[$name] = array();
|
||||
$this->_values[$name][] = array_keys($value);
|
||||
}
|
||||
$this->_values[$name][] = array_values($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $daoName
|
||||
* @param bool $onlyKeys
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function dbFields($daoName, $onlyKeys = FALSE) {
|
||||
static $_fieldsRetrieved = array();
|
||||
|
||||
if (!isset($_fieldsRetrieved[$daoName])) {
|
||||
$_fieldsRetrieved[$daoName] = array();
|
||||
$daoFile = str_replace('_',
|
||||
DIRECTORY_SEPARATOR,
|
||||
$daoName
|
||||
) . '.php';
|
||||
include_once $daoFile;
|
||||
|
||||
$daoFields = &$daoName::fields();
|
||||
|
||||
foreach ($daoFields as $key => & $value) {
|
||||
$_fieldsRetrieved[$daoName][$value['name']] = array(
|
||||
'uniqueName' => $key,
|
||||
'type' => $value['type'],
|
||||
'title' => CRM_Utils_Array::value('title', $value, NULL),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($onlyKeys) {
|
||||
return array_keys($_fieldsRetrieved[$daoName]);
|
||||
}
|
||||
else {
|
||||
return $_fieldsRetrieved[$daoName];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
* @param $additionalContacts
|
||||
*/
|
||||
public function addAdditionalContacts($contactIDs, &$additionalContacts) {
|
||||
if (!$this->_discoverContacts) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($contactIDs as $cid) {
|
||||
if ($cid &&
|
||||
!isset($this->_allContactIDs[$cid]) &&
|
||||
!isset($additionalContacts[$cid])
|
||||
) {
|
||||
$additionalContacts[$cid] = $cid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contactIDs
|
||||
*/
|
||||
public function export(&$contactIDs) {
|
||||
$chunks = &$this->splitContactIDs($contactIDs);
|
||||
|
||||
$additionalContactIDs = array();
|
||||
|
||||
foreach ($chunks as $chunk) {
|
||||
$this->getValues($chunk, $additionalContactIDs);
|
||||
}
|
||||
|
||||
if (!empty($additionalContactIDs)) {
|
||||
$this->_allContactIDs = $this->_allContactIDs + $additionalContactIDs;
|
||||
$this->export($additionalContactIDs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @param null $lastExportTime
|
||||
* @param bool $discoverContacts
|
||||
*/
|
||||
public function run(
|
||||
$fileName,
|
||||
$lastExportTime = NULL,
|
||||
$discoverContacts = FALSE
|
||||
) {
|
||||
$this->_discoverContacts = $discoverContacts;
|
||||
|
||||
if (!$lastExportTime) {
|
||||
$sql = "
|
||||
SELECT id
|
||||
FROM civicrm_contact
|
||||
";
|
||||
}
|
||||
else {
|
||||
$sql = "(
|
||||
SELECT DISTINCT entity_id
|
||||
FROM civicrm_log
|
||||
WHERE entity_table = 'civicrm_contact'
|
||||
AND modified_date >= $lastExportTime
|
||||
) UNION (
|
||||
SELECT DISTINCT contact_id
|
||||
FROM civicrm_subscription_history
|
||||
WHERE date >= $lastExportTime
|
||||
)
|
||||
";
|
||||
}
|
||||
|
||||
$dao = &CRM_Core_DAO::executeQuery($sql);
|
||||
|
||||
$contactIDs = array();
|
||||
while ($dao->fetch()) {
|
||||
$contactIDs[$dao->id] = $dao->id;
|
||||
}
|
||||
|
||||
$this->_allContactIDs = $contactIDs;
|
||||
$this->_values = array();
|
||||
|
||||
$this->metaData();
|
||||
|
||||
$this->export($contactIDs);
|
||||
|
||||
$json = json_encode($this->_values, JSON_NUMERIC_CHECK);
|
||||
file_put_contents($fileName,
|
||||
$json
|
||||
);
|
||||
|
||||
// print_r( json_decode( $json ) );
|
||||
}
|
||||
|
||||
}
|
480
sites/all/modules/civicrm/CRM/Utils/Migrate/Import.php
Normal file
480
sites/all/modules/civicrm/CRM/Utils/Migrate/Import.php
Normal file
|
@ -0,0 +1,480 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
class CRM_Utils_Migrate_Import {
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Import custom-data from an XML file.
|
||||
*
|
||||
* @param string $file
|
||||
* Path to an XML file.
|
||||
*
|
||||
* @throws CRM_Core_Exception
|
||||
*/
|
||||
public function run($file) {
|
||||
// read xml file
|
||||
$dom = new DomDocument();
|
||||
$xmlString = file_get_contents($file);
|
||||
$load = $dom->loadXML($xmlString);
|
||||
if (!$load) {
|
||||
throw new CRM_Core_Exception("Failed to parse XML file \"$file\"");
|
||||
}
|
||||
$dom->xinclude();
|
||||
$xml = simplexml_import_dom($dom);
|
||||
return $this->runXmlElement($xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import custom-data from an XML element.
|
||||
*
|
||||
* @param SimpleXMLElement $xml
|
||||
*/
|
||||
public function runXmlElement($xml) {
|
||||
$idMap = array(
|
||||
'custom_group' => array(),
|
||||
'option_group' => array(),
|
||||
);
|
||||
|
||||
// first create option groups and values if any
|
||||
$this->optionGroups($xml, $idMap);
|
||||
$this->optionValues($xml, $idMap);
|
||||
|
||||
$this->relationshipTypes($xml);
|
||||
$this->contributionTypes($xml);
|
||||
|
||||
// now create custom groups
|
||||
$this->customGroups($xml, $idMap);
|
||||
$this->customFields($xml, $idMap);
|
||||
|
||||
// now create profile groups
|
||||
$this->profileGroups($xml, $idMap);
|
||||
$this->profileFields($xml, $idMap);
|
||||
$this->profileJoins($xml, $idMap);
|
||||
|
||||
// create DB Template String sample data
|
||||
$this->dbTemplateString($xml, $idMap);
|
||||
|
||||
// clean up all caches etc
|
||||
CRM_Core_Config::clearDBCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CRM_Core_DAO $dao
|
||||
* @param $xml
|
||||
* @param bool $save
|
||||
* @param null $keyName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function copyData(&$dao, &$xml, $save = FALSE, $keyName = NULL) {
|
||||
if ($keyName) {
|
||||
if (isset($xml->$keyName)) {
|
||||
$dao->$keyName = (string ) $xml->$keyName;
|
||||
if ($dao->find(TRUE)) {
|
||||
CRM_Core_Session::setStatus(ts("Found %1, %2, %3",
|
||||
array(
|
||||
1 => $keyName,
|
||||
2 => $dao->$keyName,
|
||||
3 => $dao->__table,
|
||||
)
|
||||
), '', 'info');
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$fields = &$dao->fields();
|
||||
foreach ($fields as $name => $dontCare) {
|
||||
if (isset($xml->$name)) {
|
||||
$value = (string ) $xml->$name;
|
||||
$value = str_replace(CRM_Utils_Migrate_Export::XML_VALUE_SEPARATOR,
|
||||
CRM_Core_DAO::VALUE_SEPARATOR,
|
||||
$value
|
||||
);
|
||||
$dao->$name = $value;
|
||||
}
|
||||
}
|
||||
if ($save) {
|
||||
$dao->save();
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*/
|
||||
public function optionGroups(&$xml, &$idMap) {
|
||||
foreach ($xml->OptionGroups as $optionGroupsXML) {
|
||||
foreach ($optionGroupsXML->OptionGroup as $optionGroupXML) {
|
||||
$optionGroup = new CRM_Core_DAO_OptionGroup();
|
||||
$this->copyData($optionGroup, $optionGroupXML, TRUE, 'name');
|
||||
$idMap['option_group'][$optionGroup->name] = $optionGroup->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*/
|
||||
public function optionValues(&$xml, &$idMap) {
|
||||
foreach ($xml->OptionValues as $optionValuesXML) {
|
||||
foreach ($optionValuesXML->OptionValue as $optionValueXML) {
|
||||
$optionValue = new CRM_Core_DAO_OptionValue();
|
||||
$optionValue->option_group_id = $idMap['option_group'][(string ) $optionValueXML->option_group_name];
|
||||
if (empty($optionValue->option_group_id)) {
|
||||
//CRM-17410 check if option group already exist.
|
||||
$optionValue->option_group_id = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $optionValueXML->option_group_name, 'id', 'name');
|
||||
}
|
||||
$this->copyData($optionValue, $optionValueXML, FALSE, 'label');
|
||||
if (!isset($optionValue->value)) {
|
||||
$sql = "
|
||||
SELECT MAX(ROUND(v.value)) + 1
|
||||
FROM civicrm_option_value v
|
||||
WHERE v.option_group_id = %1
|
||||
";
|
||||
$params = array(1 => array($optionValue->option_group_id, 'Integer'));
|
||||
$optionValue->value = CRM_Core_DAO::singleValueQuery($sql, $params);
|
||||
}
|
||||
$optionValue->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
*/
|
||||
public function relationshipTypes(&$xml) {
|
||||
|
||||
foreach ($xml->RelationshipTypes as $relationshipTypesXML) {
|
||||
foreach ($relationshipTypesXML->RelationshipType as $relationshipTypeXML) {
|
||||
$relationshipType = new CRM_Contact_DAO_RelationshipType();
|
||||
$this->copyData($relationshipType, $relationshipTypeXML, TRUE, 'name_a_b');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
*/
|
||||
public function contributionTypes(&$xml) {
|
||||
|
||||
foreach ($xml->ContributionTypes as $contributionTypesXML) {
|
||||
foreach ($contributionTypesXML->ContributionType as $contributionTypeXML) {
|
||||
$contributionType = new CRM_Financial_DAO_FinancialType();
|
||||
$this->copyData($contributionType, $contributionTypeXML, TRUE, 'name');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*/
|
||||
public function customGroups(&$xml, &$idMap) {
|
||||
foreach ($xml->CustomGroups as $customGroupsXML) {
|
||||
foreach ($customGroupsXML->CustomGroup as $customGroupXML) {
|
||||
$customGroup = new CRM_Core_DAO_CustomGroup();
|
||||
if (!$this->copyData($customGroup, $customGroupXML, TRUE, 'name')) {
|
||||
$idMap['custom_group'][$customGroup->name] = $customGroup->id;
|
||||
continue;
|
||||
}
|
||||
|
||||
$saveAgain = FALSE;
|
||||
if (!isset($customGroup->table_name) ||
|
||||
empty($customGroup->table_name)
|
||||
) {
|
||||
// fix table name
|
||||
$customGroup->table_name = "civicrm_value_" . strtolower(CRM_Utils_String::munge($customGroup->title, '_', 32)) . "_{$customGroup->id}";
|
||||
|
||||
$saveAgain = TRUE;
|
||||
}
|
||||
|
||||
// fix extends stuff if it exists
|
||||
if (isset($customGroupXML->extends_entity_column_value_option_group) &&
|
||||
isset($customGroupXML->extends_entity_column_value)
|
||||
) {
|
||||
$valueIDs = array();
|
||||
$optionValues = explode(",", $customGroupXML->extends_entity_column_value);
|
||||
$optValues = implode("','", $optionValues);
|
||||
if (trim($customGroup->extends) != 'Participant') {
|
||||
if ($customGroup->extends == 'Relationship') {
|
||||
foreach ($optionValues as $key => $value) {
|
||||
$relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_RelationshipType', $value, 'id', 'name_a_b');
|
||||
$valueIDs[] = $relTypeId;
|
||||
}
|
||||
}
|
||||
elseif (in_array($customGroup->extends, array('Individual', 'Organization', 'Household'))) {
|
||||
$valueIDs = $optionValues;
|
||||
}
|
||||
else {
|
||||
$sql = "
|
||||
SELECT v.value
|
||||
FROM civicrm_option_value v
|
||||
INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
|
||||
WHERE g.name = %1
|
||||
AND v.name IN ('$optValues')
|
||||
";
|
||||
$params = array(
|
||||
1 => array(
|
||||
(string ) $customGroupXML->extends_entity_column_value_option_group,
|
||||
'String',
|
||||
),
|
||||
);
|
||||
$dao = &CRM_Core_DAO::executeQuery($sql, $params);
|
||||
|
||||
while ($dao->fetch()) {
|
||||
$valueIDs[] = $dao->value;
|
||||
}
|
||||
}
|
||||
if (!empty($valueIDs)) {
|
||||
$customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
|
||||
$valueIDs
|
||||
) . CRM_Core_DAO::VALUE_SEPARATOR;
|
||||
|
||||
unset($valueIDs);
|
||||
|
||||
// Note: No need to set extends_entity_column_id here.
|
||||
|
||||
$saveAgain = TRUE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// when custom group extends 'Participant'
|
||||
$sql = "
|
||||
SELECT v.value
|
||||
FROM civicrm_option_value v
|
||||
INNER JOIN civicrm_option_group g ON g.id = v.option_group_id
|
||||
WHERE g.name = 'custom_data_type'
|
||||
AND v.name = %1
|
||||
";
|
||||
$params = array(
|
||||
1 => array(
|
||||
(string ) $customGroupXML->extends_entity_column_value_option_group,
|
||||
'String',
|
||||
),
|
||||
);
|
||||
$valueID = (int ) CRM_Core_DAO::singleValueQuery($sql, $params);
|
||||
if ($valueID) {
|
||||
$customGroup->extends_entity_column_id = $valueID;
|
||||
}
|
||||
|
||||
$optionIDs = array();
|
||||
switch ($valueID) {
|
||||
case 1:
|
||||
// ParticipantRole
|
||||
$condition = "AND v.name IN ( '{$optValues}' )";
|
||||
$optionIDs = CRM_Core_OptionGroup::values('participant_role', FALSE, FALSE, FALSE, $condition, 'name');
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// ParticipantEventName
|
||||
$condition = "( is_template IS NULL OR is_template != 1 ) AND title IN( '{$optValues}' )";
|
||||
$optionIDs = CRM_Event_PseudoConstant::event(NULL, FALSE, $condition);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// ParticipantEventType
|
||||
$condition = "AND v.name IN ( '{$optValues}' )";
|
||||
$optionIDs = CRM_Core_OptionGroup::values('event_type', FALSE, FALSE, FALSE, $condition, 'name');
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_array($optionIDs) && !empty($optionIDs)) {
|
||||
$customGroup->extends_entity_column_value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
|
||||
array_keys($optionIDs)
|
||||
) . CRM_Core_DAO::VALUE_SEPARATOR;
|
||||
|
||||
$saveAgain = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($saveAgain) {
|
||||
$customGroup->save();
|
||||
}
|
||||
|
||||
CRM_Core_BAO_CustomGroup::createTable($customGroup);
|
||||
$idMap['custom_group'][$customGroup->name] = $customGroup->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*/
|
||||
public function customFields(&$xml, &$idMap) {
|
||||
// Re-index by group id so we can build out the custom fields one table
|
||||
// at a time, and then rebuild the table triggers at the end, rather than
|
||||
// rebuilding the table triggers after each field is added (which is
|
||||
// painfully slow).
|
||||
$fields_indexed_by_group_id = array();
|
||||
foreach ($xml->CustomFields as $customFieldsXML) {
|
||||
$total = count($customFieldsXML->CustomField);
|
||||
foreach ($customFieldsXML->CustomField as $customFieldXML) {
|
||||
$id = $idMap['custom_group'][(string ) $customFieldXML->custom_group_name];
|
||||
$fields_indexed_by_group_id[$id][] = $customFieldXML;
|
||||
}
|
||||
}
|
||||
while (list($group_id, $fields) = each($fields_indexed_by_group_id)) {
|
||||
$total = count($fields);
|
||||
$count = 0;
|
||||
while (list(, $customFieldXML) = each($fields)) {
|
||||
$count++;
|
||||
$customField = new CRM_Core_DAO_CustomField();
|
||||
$customField->custom_group_id = $group_id;
|
||||
$skipStore = FALSE;
|
||||
if (!$this->copyData($customField, $customFieldXML, FALSE, 'label')) {
|
||||
$skipStore = TRUE;
|
||||
}
|
||||
|
||||
if (empty($customField->option_group_id) &&
|
||||
isset($customFieldXML->option_group_name)
|
||||
) {
|
||||
$customField->option_group_id = $idMap['option_group'][(string ) $customFieldXML->option_group_name];
|
||||
}
|
||||
if ($skipStore) {
|
||||
continue;
|
||||
}
|
||||
$customField->save();
|
||||
|
||||
// Only rebuild the table's trigger on the last field added to avoid un-necessary
|
||||
// and slow rebuilds when adding many fields at the same time.
|
||||
$triggerRebuild = FALSE;
|
||||
if ($count == $total) {
|
||||
$triggerRebuild = TRUE;
|
||||
}
|
||||
$indexExist = FALSE;
|
||||
CRM_Core_BAO_CustomField::createField($customField, 'add', $indexExist, $triggerRebuild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*/
|
||||
public function dbTemplateString(&$xml, &$idMap) {
|
||||
foreach ($xml->Persistent as $persistentXML) {
|
||||
foreach ($persistentXML->Persistent as $persistent) {
|
||||
$persistentObj = new CRM_Core_DAO_Persistent();
|
||||
|
||||
if ($persistent->is_config == 1) {
|
||||
$persistent->data = serialize(explode(',', $persistent->data));
|
||||
}
|
||||
$this->copyData($persistentObj, $persistent, TRUE, 'context');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*/
|
||||
public function profileGroups(&$xml, &$idMap) {
|
||||
foreach ($xml->ProfileGroups as $profileGroupsXML) {
|
||||
foreach ($profileGroupsXML->ProfileGroup as $profileGroupXML) {
|
||||
$profileGroup = new CRM_Core_DAO_UFGroup();
|
||||
$this->copyData($profileGroup, $profileGroupXML, TRUE, 'title');
|
||||
$idMap['profile_group'][$profileGroup->name] = $profileGroup->id;
|
||||
$idMap['profile_group'][$profileGroup->title] = $profileGroup->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function profileFields(&$xml, &$idMap) {
|
||||
foreach ($xml->ProfileFields as $profileFieldsXML) {
|
||||
foreach ($profileFieldsXML->ProfileField as $profileFieldXML) {
|
||||
$profileField = new CRM_Core_DAO_UFField();
|
||||
$profileField->uf_group_id = $idMap['profile_group'][(string ) $profileFieldXML->profile_group_name];
|
||||
$this->copyData($profileField, $profileFieldXML, FALSE, 'field_name');
|
||||
|
||||
// fix field name
|
||||
if (substr($profileField->field_name, 0, 7) == 'custom.') {
|
||||
list($dontCare, $tableName, $columnName) = explode('.', $profileField->field_name);
|
||||
$sql = "
|
||||
SELECT f.id
|
||||
FROM civicrm_custom_field f
|
||||
INNER JOIN civicrm_custom_group g ON f.custom_group_id = g.id
|
||||
WHERE g.table_name = %1
|
||||
AND f.column_name = %2
|
||||
";
|
||||
$params = array(
|
||||
1 => array($tableName, 'String'),
|
||||
2 => array($columnName, 'String'),
|
||||
);
|
||||
$cfID = CRM_Core_DAO::singleValueQuery($sql, $params);
|
||||
if (!$cfID) {
|
||||
CRM_Core_Error::fatal(ts("Could not find custom field for %1, %2, %3",
|
||||
array(
|
||||
1 => $profileField->field_name,
|
||||
2 => $tableName,
|
||||
3 => $columnName,
|
||||
)
|
||||
) . "<br />");
|
||||
}
|
||||
$profileField->field_name = "custom_{$cfID}";
|
||||
}
|
||||
$profileField->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $xml
|
||||
* @param $idMap
|
||||
*/
|
||||
public function profileJoins(&$xml, &$idMap) {
|
||||
foreach ($xml->ProfileJoins as $profileJoinsXML) {
|
||||
foreach ($profileJoinsXML->ProfileJoin as $profileJoinXML) {
|
||||
$profileJoin = new CRM_Core_DAO_UFJoin();
|
||||
$profileJoin->uf_group_id = $idMap['profile_group'][(string ) $profileJoinXML->profile_group_name];
|
||||
$this->copyData($profileJoin, $profileJoinXML, FALSE, 'module');
|
||||
$profileJoin->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
304
sites/all/modules/civicrm/CRM/Utils/Migrate/ImportJSON.php
Normal file
304
sites/all/modules/civicrm/CRM/Utils/Migrate/ImportJSON.php
Normal file
|
@ -0,0 +1,304 @@
|
|||
<?php
|
||||
/*
|
||||
+--------------------------------------------------------------------+
|
||||
| CiviCRM version 4.7 |
|
||||
+--------------------------------------------------------------------+
|
||||
| Copyright CiviCRM LLC (c) 2004-2017 |
|
||||
+--------------------------------------------------------------------+
|
||||
| This file is a part of CiviCRM. |
|
||||
| |
|
||||
| CiviCRM is free software; you can copy, modify, and distribute it |
|
||||
| under the terms of the GNU Affero General Public License |
|
||||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
|
||||
| |
|
||||
| CiviCRM is distributed in the hope that it will be useful, but |
|
||||
| WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
||||
| See the GNU Affero General Public License for more details. |
|
||||
| |
|
||||
| You should have received a copy of the GNU Affero General Public |
|
||||
| License and the CiviCRM Licensing Exception along |
|
||||
| with this program; if not, contact CiviCRM LLC |
|
||||
| at info[AT]civicrm[DOT]org. If you have questions about the |
|
||||
| GNU Affero General Public License or the licensing of CiviCRM, |
|
||||
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
|
||||
+--------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package CRM
|
||||
* @copyright CiviCRM LLC (c) 2004-2017
|
||||
*/
|
||||
class CRM_Utils_Migrate_ImportJSON {
|
||||
|
||||
protected $_lookupCache;
|
||||
|
||||
protected $_saveMapping;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_lookupCache = array();
|
||||
$this->_saveMapping = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run import.
|
||||
*
|
||||
* @param string $file
|
||||
*/
|
||||
public function run($file) {
|
||||
$json = file_get_contents($file);
|
||||
|
||||
$decodedContacts = json_decode($json);
|
||||
|
||||
// migrate contact data
|
||||
$this->contact($decodedContacts->civicrm_contact);
|
||||
$this->email($decodedContacts->civicrm_email);
|
||||
$this->phone($decodedContacts->civicrm_phone);
|
||||
$this->address($decodedContacts->civicrm_address);
|
||||
$this->note($decodedContacts->civicrm_note);
|
||||
$this->relationship($decodedContacts->civicrm_relationship);
|
||||
$this->activity($decodedContacts->civicrm_activity,
|
||||
$decodedContacts->civicrm_activity_contact
|
||||
);
|
||||
$this->group($decodedContacts->civicrm_group,
|
||||
$decodedContacts->civicrm_group_contact
|
||||
);
|
||||
$this->tag($decodedContacts->civicrm_tag,
|
||||
$decodedContacts->civicrm_entity_tag
|
||||
);
|
||||
|
||||
// clean up all caches etc
|
||||
CRM_Core_Config::clearDBCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $contact
|
||||
*/
|
||||
public function contact(&$contact) {
|
||||
$this->restore($contact,
|
||||
'CRM_Contact_DAO_Contact',
|
||||
array('id' => 'civicrm_contact'),
|
||||
array('birth_date', 'deceased_date', 'created_date', 'modified_date')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $email
|
||||
*/
|
||||
public function email(&$email) {
|
||||
$this->restore($email,
|
||||
'CRM_Core_DAO_Email',
|
||||
array('contact_id' => 'civicrm_contact')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $phone
|
||||
*/
|
||||
public function phone(&$phone) {
|
||||
$this->restore($phone,
|
||||
'CRM_Core_DAO_Phone',
|
||||
array('contact_id' => 'civicrm_contact')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $address
|
||||
*/
|
||||
public function address(&$address) {
|
||||
$this->restore($address,
|
||||
'CRM_Core_DAO_Address',
|
||||
array('contact_id' => 'civicrm_contact')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $note
|
||||
*/
|
||||
public function note(&$note) {
|
||||
$this->restore($note,
|
||||
'CRM_Core_DAO_Note',
|
||||
array('contact_id' => 'civicrm_contact'),
|
||||
array('modified_date')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $relationship
|
||||
*/
|
||||
public function relationship(&$relationship) {
|
||||
$this->restore($relationship,
|
||||
'CRM_Contact_DAO_Relationship',
|
||||
array(
|
||||
'contact_id_a' => 'civicrm_contact',
|
||||
'contact_id_b' => 'civicrm_contact',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $activity
|
||||
* @param $activityContacts
|
||||
*/
|
||||
public function activity($activity, $activityContacts) {
|
||||
$this->restore($activity,
|
||||
'CRM_Activity_DAO_Activity',
|
||||
NULL,
|
||||
array('activity_date_time')
|
||||
);
|
||||
|
||||
$this->restore($activityContacts,
|
||||
'CRM_Activity_DAO_ActivityContact',
|
||||
array(
|
||||
'contact_id' => 'civicrm_contact',
|
||||
'activity_id' => 'civicrm_activity',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $group
|
||||
* @param $groupContact
|
||||
*/
|
||||
public function group($group, $groupContact) {
|
||||
$this->restore($group,
|
||||
'CRM_Contact_DAO_Group',
|
||||
NULL,
|
||||
array('cache_date', 'refresh_date')
|
||||
);
|
||||
|
||||
$this->restore($groupContact,
|
||||
'CRM_Contact_DAO_GroupContact',
|
||||
array(
|
||||
'group_id' => 'civicrm_group',
|
||||
'contact_id' => 'civicrm_contact',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $tag
|
||||
* @param $entityTag
|
||||
*/
|
||||
public function tag($tag, $entityTag) {
|
||||
$this->restore($tag,
|
||||
'CRM_Core_DAO_Tag',
|
||||
array(
|
||||
'created_id' => 'civicrm_contact',
|
||||
'parent_id' => 'civicrm_tag',
|
||||
)
|
||||
);
|
||||
|
||||
$this->restore($entityTag,
|
||||
'CRM_Core_DAO_EntityTag',
|
||||
array(
|
||||
'entity_id' => 'civicrm_contact',
|
||||
'tag_id' => 'civicrm_tag',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $chunk
|
||||
* @param string $daoName
|
||||
* @param null $lookUpMapping
|
||||
* @param null $dateFields
|
||||
*/
|
||||
public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
|
||||
$object = new $daoName();
|
||||
$tableName = $object->__table;
|
||||
|
||||
if (is_array($lookUpMapping)) {
|
||||
$lookUpMapping['id'] = $tableName;
|
||||
}
|
||||
else {
|
||||
$lookUpMapping = array('id' => $tableName);
|
||||
}
|
||||
|
||||
foreach ($lookUpMapping as $columnName => $tableName) {
|
||||
$this->populateCache($tableName);
|
||||
}
|
||||
|
||||
$saveMapping = FALSE;
|
||||
$columns = $chunk[0];
|
||||
foreach ($chunk as $key => $value) {
|
||||
if ($key) {
|
||||
$object = new $daoName();
|
||||
foreach ($columns as $k => $column) {
|
||||
if ($column == 'id') {
|
||||
$childID = $value[$k];
|
||||
$masterID = CRM_Utils_Array::value($value[$k],
|
||||
$this->_lookupCache[$tableName],
|
||||
NULL
|
||||
);
|
||||
if ($masterID) {
|
||||
$object->id = $masterID;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (array_key_exists($column, $lookUpMapping)) {
|
||||
$object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]];
|
||||
}
|
||||
elseif (!empty($dateFields) && in_array($column, $dateFields)) {
|
||||
$object->$column = CRM_Utils_Date::isoToMysql($value[$k]);
|
||||
}
|
||||
else {
|
||||
$object->$column = $value[$k];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$object->save();
|
||||
if (!$masterID) {
|
||||
$this->_lookupCache[$tableName][$childID] = $object->id;
|
||||
$this->_saveMapping[$tableName] = TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function saveCache() {
|
||||
$sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
|
||||
|
||||
foreach ($this->_lookupCache as $tableName => & $values) {
|
||||
if (!$this->_saveMapping[$tableName]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mapValues = array();
|
||||
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'");
|
||||
foreach ($values as $childID => $masterID) {
|
||||
$mapValues[] = "($masterID,$childID,'$tableName' )";
|
||||
}
|
||||
$insertSQL = $sql . implode(",\n", $mapValues);
|
||||
CRM_Core_DAO::executeQuery($insertSQL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tableName
|
||||
*/
|
||||
public function populateCache($tableName) {
|
||||
if (isset($this->_lookupCache[$tableName])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->_lookupCache[$tableName] = array();
|
||||
$this->_saveMapping[$tableName] = FALSE;
|
||||
|
||||
$query = "SELECT master_id, slave_id
|
||||
FROM civicrm_migration_mapping
|
||||
WHERE entity_table = '{$tableName}'
|
||||
";
|
||||
|
||||
$dao = CRM_Core_DAO::executeQuery($query);
|
||||
while ($dao->fetch()) {
|
||||
$this->_lookupCache[$dao->slave_id] = $dao->master_id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue