- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/lib/integrity/duplicateentityranking.php
- Класс: Bitrix\Crm\Integrity\DuplicateEntityRanking
- Вызов: DuplicateEntityRanking::loadBulk
static function loadBulk($entityTypeID, array &$entityIDs, array &$itemMap, array $options = null)
{
/** @var DuplicateEntityRanking[] $itemMap */
if($entityTypeID !== \CCrmOwnerType::Contact
&& $entityTypeID !== \CCrmOwnerType::Company
&& $entityTypeID !== \CCrmOwnerType::Lead)
{
return;
}
if(!is_array($options))
{
$options = array();
}
$checkPermissions = isset($options['CHECK_PERMISSIONS']) ? (bool)$options['CHECK_PERMISSIONS'] : false;
$userID = isset($options['USER_ID']) ? (int)$options['USER_ID'] : 0;
$permissions = $checkPermissions ? \CCrmPerms::GetUserPermissions($userID) : null;
$limit = isset($options['LIMIT']) ? (int)$options['LIMIT'] : 3000;
if($limit <= 0)
{
$limit = 3000;
}
$length = count($entityIDs);
if($length === 0)
{
return;
}
while($length > 0)
{
if($length <= $limit)
{
$ids = $entityIDs;
unset($entityIDs);
$entityIDs = array();
}
else
{
$ids = array_splice($entityIDs, 0, $limit);
}
$length = count($entityIDs);
if(empty($ids))
{
continue;
}
if($entityTypeID === \CCrmOwnerType::Lead)
{
$dbResult = Entity\DuplicateEntityStatisticsTable::getList(
array(
'select' => array('ENTITY_ID', 'RANKING_DATA'),
'filter' => array(
'ENTITY_TYPE_ID' => \CCrmOwnerType::Lead,
'ENTITY_ID' => $ids
)
)
);
while($fields = $dbResult->fetch())
{
$entityID = intval($fields['ENTITY_ID']);
$key = "{$entityTypeID}_{$entityID}";
if(!isset($itemMap[$key]))
{
continue;
}
if(isset($fields['RANKING_DATA']) && $fields['RANKING_DATA'] !== '')
{
$data = unserialize($fields['RANKING_DATA'], ['allowed_classes' => false]);
/** @var DuplicateEntityRanking $ranking */
$ranking = $itemMap[$key];
$ranking->lastChanged = isset($data['LAST_CHANGED']) ? $data['LAST_CHANGED'] : 0;
$ranking->completeness = isset($data['COMPLETENESS']) ? $data['COMPLETENESS'] : 0;
if($checkPermissions)
{
$ranking->editable = \CCrmLead::CheckUpdatePermission($entityID, $permissions);
$ranking->deleteable = \CCrmLead::CheckDeletePermission($entityID, $permissions);
}
}
}
}
else
{
$query = new Main\Entity\Query(Entity\DuplicateEntityStatisticsTable::getEntity());
$query->addSelect('ENTITY_ID');
$query->addSelect('RANKING_DATA');
$query->addFilter('ENTITY_ID', $ids);
$query->addFilter('ENTITY_TYPE_ID', $entityTypeID);
if($entityTypeID === \CCrmOwnerType::Contact)
{
$subQuery = new Main\Entity\Query(DealTable::getEntity());
$subQuery->addSelect('CONTACT_ID');
$subQuery->addFilter('CONTACT_ID', $ids);
$subQuery->addSelect('QTY');
$subQuery->registerRuntimeField('', new Main\Entity\ExpressionField('QTY', 'COUNT(*)'));
$referenceField = new Main\Entity\ReferenceField('D',
Main\Entity\Base::getInstanceByQuery($subQuery),
array('=this.ENTITY_ID' => 'ref.CONTACT_ID'),
array('join_type' => 'LEFT')
);
}
else//($entityTypeID === \CCrmOwnerType::Company)
{
$subQuery = new Main\Entity\Query(DealTable::getEntity());
$subQuery->addSelect('COMPANY_ID');
$subQuery->addFilter('COMPANY_ID', $ids);
$subQuery->addSelect('QTY');
$subQuery->registerRuntimeField('', new Main\Entity\ExpressionField('QTY', 'COUNT(*)'));
$referenceField = new Main\Entity\ReferenceField('D',
Main\Entity\Base::getInstanceByQuery($subQuery),
array('=this.ENTITY_ID' => 'ref.COMPANY_ID'),
array('join_type' => 'LEFT')
);
}
$query->registerRuntimeField('', $referenceField);
$query->addSelect('D.QTY', 'QTY');
$dbResult = $query->exec();
while($fields = $dbResult->fetch())
{
$entityID = intval($fields['ENTITY_ID']);
$key = "{$entityTypeID}_{$entityID}";
if(!isset($itemMap[$key]))
{
continue;
}
$itemMap[$key]->referenceCount = isset($fields['QTY']) ? intval($fields['QTY']) : 0;
if(isset($fields['RANKING_DATA']) && $fields['RANKING_DATA'] !== '')
{
$data = unserialize($fields['RANKING_DATA'], ['allowed_classes' => false]);
/** @var DuplicateEntityRanking $ranking */
$ranking = $itemMap[$key];
$ranking->lastChanged = isset($data['LAST_CHANGED']) ? $data['LAST_CHANGED'] : 0;
$ranking->completeness = isset($data['COMPLETENESS']) ? $data['COMPLETENESS'] : 0;
if($checkPermissions)
{
if($entityTypeID === \CCrmOwnerType::Contact)
{
$ranking->editable = \CCrmContact::CheckUpdatePermission($entityID, $permissions);
$ranking->deleteable = \CCrmContact::CheckDeletePermission($entityID, $permissions);
}
else
{
$ranking->editable = \CCrmCompany::CheckUpdatePermission($entityID, $permissions);
$ranking->deleteable = \CCrmCompany::CheckDeletePermission($entityID, $permissions);
}
}
}
}
}
}
}