- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_activity.php
- Класс: \CAllCrmActivity
- Вызов: CAllCrmActivity::ChangeOwner
static function ChangeOwner($oldOwnerTypeID, $oldOwnerID, $newOwnerTypeID, $newOwnerID)
{
global $DB;
$oldOwnerTypeID = intval($oldOwnerTypeID);
$oldOwnerID = intval($oldOwnerID);
$newOwnerTypeID = intval($newOwnerTypeID);
$newOwnerID = intval($newOwnerID);
$tableName = CCrmActivity::TABLE_NAME;
$bindingTableName = CCrmActivity::BINDING_TABLE_NAME;
$communicationTableName = CCrmActivity::COMMUNICATION_TABLE_NAME;
$sql= "SELECT ID FROM ".CCrmActivity::BINDING_TABLE_NAME." WHERE OWNER_TYPE_ID = {$oldOwnerTypeID} AND OWNER_ID = {$oldOwnerID}";
CSqlUtil::PrepareSelectTop($sql, 1, CCrmActivity::DB_TYPE);
$dbResult = $DB->Query($sql, false, 'File: '.__FILE__.'
Line: '.__LINE__);
if(!(is_object($dbResult) && is_array($dbResult->Fetch())))
{
return;
}
$responsibleIDs = [];
$activityIDs = [];
$sql = "SELECT B.ACTIVITY_ID, A.RESPONSIBLE_ID FROM {$bindingTableName} B INNER JOIN {$tableName} A ON A.ID = B.ACTIVITY_ID AND B.OWNER_TYPE_ID = {$oldOwnerTypeID} AND B.OWNER_ID = {$oldOwnerID}";
$dbResult = $DB->Query($sql, false, 'File: '.__FILE__.'
Line: '.__LINE__);
if(is_object($dbResult))
{
while($fields = $dbResult->Fetch())
{
$singleResponsibleId = (int)$fields['RESPONSIBLE_ID'];
if (!isset($responsibleIDs[$singleResponsibleId]))
{
$responsibleIDs[$singleResponsibleId] = $singleResponsibleId;
}
$singleActivityId = (int)$fields['ACTIVITY_ID'];
if (!isset($activityIDs[$singleActivityId]))
{
$activityIDs[$singleActivityId] = $singleActivityId;
}
}
}
$comm = array('ENTITY_ID'=> $newOwnerID, 'ENTITY_TYPE_ID' => $newOwnerTypeID);
self::PrepareCommunicationSettings($comm);
$entityCommSettings = isset($comm['ENTITY_SETTINGS']) ? $DB->ForSql(serialize($comm['ENTITY_SETTINGS'])) : '';
$DB->Query(
"UPDATE {$communicationTableName} SET ENTITY_TYPE_ID = {$newOwnerTypeID}, ENTITY_ID = {$newOwnerID}, ENTITY_SETTINGS = '{$entityCommSettings}' WHERE ENTITY_TYPE_ID = {$oldOwnerTypeID} AND ENTITY_ID = {$oldOwnerID}",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$DB->Query(
"UPDATE {$communicationTableName} SET OWNER_TYPE_ID = {$newOwnerTypeID}, OWNER_ID = {$newOwnerID} WHERE OWNER_TYPE_ID = {$oldOwnerTypeID} AND OWNER_ID = {$oldOwnerID}",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$DB->Query(
"UPDATE {$bindingTableName} SET OWNER_TYPE_ID = {$newOwnerTypeID}, OWNER_ID = {$newOwnerID} WHERE OWNER_TYPE_ID = {$oldOwnerTypeID} AND OWNER_ID = {$oldOwnerID}",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
$DB->Query(
"UPDATE {$tableName} SET OWNER_TYPE_ID = {$newOwnerTypeID}, OWNER_ID = {$newOwnerID} WHERE OWNER_TYPE_ID = {$oldOwnerTypeID} AND OWNER_ID = {$oldOwnerID}",
false,
'File: '.__FILE__.'
Line: '.__LINE__
);
if(!empty($responsibleIDs))
{
foreach($responsibleIDs as $responsibleID)
{
self::SynchronizeUserActivity($oldOwnerTypeID, $oldOwnerID, $responsibleID);
self::SynchronizeUserActivity($newOwnerTypeID, $newOwnerID, $responsibleID);
}
}
self::SynchronizeUserActivity($oldOwnerTypeID, $oldOwnerID, 0);
self::SynchronizeUserActivity($newOwnerTypeID, $newOwnerID, 0);
\Bitrix\Crm\Activity\CommunicationStatistics::rebuild($newOwnerTypeID, array($newOwnerID));
foreach ($activityIDs as $singleActivityId)
{
Crm\Activity\Provider\ProviderManager::syncBadgesOnBindingsChange(
$singleActivityId,
[
['OWNER_TYPE_ID' => $newOwnerTypeID, 'OWNER_ID' => $newOwnerID],
],
[
['OWNER_TYPE_ID' => $oldOwnerTypeID, 'OWNER_ID' => $oldOwnerID],
],
);
}
$monitor = Crm\Service\Timeline\Monitor::getInstance();
if (\CCrmOwnerType::IsDefined($oldOwnerTypeID) && $oldOwnerID > 0)
{
$monitor->onActivityRemove(new Crm\ItemIdentifier($oldOwnerTypeID, $oldOwnerID));
}
if (\CCrmOwnerType::IsDefined($newOwnerTypeID) && $newOwnerID > 0)
{
$monitor->onActivityAdd(new Crm\ItemIdentifier($newOwnerTypeID, $newOwnerID));
}
}