public function merge($seedID, $targID, Integrity\DuplicateCriterion $targCriterion)
{
if(!is_int($seedID))
{
$seedID = (int)$seedID;
}
if(!is_int($targID))
{
$targID = (int)$targID;
}
$entityTypeID = $this->entityTypeID;
if($this->enablePermissionCheck && !$this->userIsAdmin)
{
$userPermissions = $this->getUserPermissions();
if(!$this->checkEntityReadPermission($seedID, $userPermissions))
{
throw new EntityMergerException($entityTypeID, $seedID, self::ROLE_SEED, EntityMergerException::READ_DENIED);
}
if(!$this->checkEntityDeletePermission($seedID, $userPermissions))
{
throw new EntityMergerException($entityTypeID, $seedID, self::ROLE_SEED, EntityMergerException::DELETE_DENIED);
}
if(!$this->checkEntityReadPermission($targID, $userPermissions))
{
throw new EntityMergerException($entityTypeID, $targID, self::ROLE_TARG, EntityMergerException::READ_DENIED);
}
if(!$this->checkEntityUpdatePermission($targID, $userPermissions))
{
throw new EntityMergerException($entityTypeID, $targID, self::ROLE_TARG, EntityMergerException::UPDATE_DENIED);
}
}
$collisions = self::getMergeCollisions($seedID, $targID);
$seed = $this->getEntityFields($seedID, self::ROLE_SEED);
$targ = $this->getEntityFields($targID, self::ROLE_TARG);
$entityFieldInfos = $this->getEntityFieldsInfo();
$userFieldInfos = $this->getEntityUserFieldsInfo();
$options = array('conflictResolutionMode' => $this->conflictResolutionMode);
$this->innerMergeEntityFields($seed, $targ, $entityFieldInfos, false, $options);
EntityMerger::mergeUserFields($seed, $targ, $userFieldInfos, $options);
$matches = $this->getRegisteredEntityMatches($entityTypeID, $seedID);
//region Merge requisites
if ($entityTypeID === \CCrmOwnerType::Company || $entityTypeID === \CCrmOwnerType::Contact)
{
$requisiteMergingHelper = new RequisiteMergingHelper($entityTypeID, $seedID, $targID);
$requisiteMergingHelper->merge();
}
//endregion Merge requisites
$historyItems = null;
if (isset($targ['HISTORY_ITEMS']))
{
$historyItems = $targ['HISTORY_ITEMS'];
unset($targ['HISTORY_ITEMS']);
}
$this->updateEntity($targID, $targ, self::ROLE_TARG, array('DISABLE_USER_FIELD_CHECK' => true));
if (is_array($historyItems))
{
$this->saveHistoryItems($targID, $historyItems);
}
$this->rebind($seedID, $targID);
$targIndexTypeID = $targCriterion->getIndexTypeID();
$targScope = $targCriterion->getScope();
if(!isset($matches[$targIndexTypeID][$targScope]))
{
$matches[$targIndexTypeID][$targScope] = array();
}
$targetMatchHash = $targCriterion->getMatchHash();
if(!isset($matches[$targIndexTypeID][$targScope][$targetMatchHash]))
{
$matches[$targIndexTypeID][$targScope][$targetMatchHash] = $targCriterion->getMatches();
}
$this->deleteEntity($seedID, self::ROLE_SEED, array('ENABLE_DUP_INDEX_INVALIDATION' => false));
if(!empty($matches))
{
$this->processEntityDeletion($entityTypeID, $seedID, $matches);
}
Integrity\DuplicateManager::markDuplicateIndexAsJunk($entityTypeID, $seedID);
//region Send event
$event = new Main\Event(
'crm',
'OnAfterEntityMerge',
array(
'entityTypeID' => $this->entityTypeID,
'entityTypeName' => \CCrmOwnerType::ResolveName($this->entityTypeID),
'seedEntityID' => $seedID,
'targetEntityID' => $targID,
'userID' => $this->getUserID()
)
);
$event->send();
//endregion
if(!empty($collisions))
{
$messageFields = $this->prepareCollisionMessageFields($collisions, $seed, $targ);
if(is_array($messageFields) && !empty($messageFields) && Main\Loader::includeModule('im'))
{
$messageFields['FROM_USER_ID'] = $this->userID;
$messageFields['MESSAGE_TYPE'] = IM_MESSAGE_SYSTEM;
$messageFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM;
$messageFields['NOTIFY_MODULE'] = 'crm';
$messageFields['NOTIFY_EVENT'] = 'merge';
$messageFields['NOTIFY_TAG'] = 'CRM|MERGE|COLLISION';
\CIMNotify::Add($messageFields);
}
}
}