public function findDuplicates(Crm\EntityAdapter $adapter, DuplicateSearchParams $params) { $result = array(); $fieldNames = $params->getFieldNames(); $processAllFields = empty($fieldNames); $lastName = $processAllFields || in_array('LAST_NAME', $fieldNames, true) ? $adapter->getFieldValue('LAST_NAME', '') : ''; if ($lastName !== '') { $name = $processAllFields || in_array('NAME', $fieldNames, true) ? $adapter->getFieldValue('NAME', '') : ''; $secondName = $processAllFields || in_array('SECOND_NAME', $fieldNames, true) ? $adapter->getFieldValue('SECOND_NAME', '') : ''; $criterion = new DuplicatePersonCriterion($lastName, $name, $secondName); $duplicate = $criterion->find(\CCrmOwnerType::Undefined, 20); if ($duplicate !== null) { $result[] = $duplicate; } } if ($processAllFields || in_array('FM.PHONE', $fieldNames, true)) { $phones = $this->findMultifieldDuplicates('PHONE', $adapter, $params); if (!empty($phones)) { $result = array_merge($result, $phones); } } if ($processAllFields || in_array('FM.EMAIL', $fieldNames, true)) { $email = $this->findMultifieldDuplicates('EMAIL', $adapter, $params); if (!empty($email)) { $result = array_merge($result, $email); } } return $result; }
public static function getMatchHash($typeID, array $matches) { if ($typeID === DuplicateIndexType::PERSON) { return DuplicatePersonCriterion::prepareMatchHash($matches); } elseif ($typeID === DuplicateIndexType::ORGANIZATION) { return DuplicateOrganizationCriterion::prepareMatchHash($matches); } elseif ($typeID === DuplicateIndexType::COMMUNICATION_EMAIL || $typeID === DuplicateIndexType::COMMUNICATION_PHONE) { return DuplicateCommunicationCriterion::prepareMatchHash($matches); } throw new Main\NotSupportedException("Criterion type(s): '" . DuplicateIndexType::resolveName($typeID) . "' is not supported in current context"); }
protected function getRegisteredEntityMatches($entityTypeID, $entityID) { $results = array(); $types = Integrity\DuplicateIndexBuilder::getExistedTypes($entityTypeID, $this->userID); foreach ($types as $typeID) { if ($typeID === Integrity\DuplicateIndexType::PERSON) { $results[$typeID] = Integrity\DuplicatePersonCriterion::getRegisteredEntityMatches($entityTypeID, $entityID); } elseif ($typeID === Integrity\DuplicateIndexType::ORGANIZATION) { $results[$typeID] = Integrity\DuplicateOrganizationCriterion::getRegisteredEntityMatches($entityTypeID, $entityID); } elseif ($typeID === Integrity\DuplicateIndexType::COMMUNICATION_EMAIL || $typeID === Integrity\DuplicateIndexType::COMMUNICATION_PHONE) { $results[$typeID] = Integrity\DuplicateCommunicationCriterion::getRegisteredEntityMatches($entityTypeID, $entityID, Integrity\DuplicateCommunicationCriterion::resolveTypeByIndexTypeID($typeID)); } } return $results; }
public static function RebuildDuplicateIndex($IDs) { if (!is_array($IDs)) { $IDs = array($IDs); } $dbResult = self::GetListEx(array(), array('@ID' => $IDs, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'TITLE', 'COMPANY_TITLE', 'NAME', 'SECOND_NAME', 'LAST_NAME', 'ADDRESS', 'DATE_MODIFY')); if (!is_object($dbResult)) { return; } $emails = array(); $phones = array(); $dbResultMultiFields = CCrmFieldMulti::GetList(array('ID' => 'asc'), array('=ENTITY_ID' => CCrmOwnerType::LeadName, '@TYPE_ID' => array('EMAIL', 'PHONE'), '@ELEMENT_ID' => $IDs)); if (is_object($dbResultMultiFields)) { while ($multiFields = $dbResultMultiFields->Fetch()) { $elementID = isset($multiFields['ELEMENT_ID']) ? $multiFields['ELEMENT_ID'] : ''; $typeID = isset($multiFields['TYPE_ID']) ? $multiFields['TYPE_ID'] : ''; $value = isset($multiFields['VALUE']) ? $multiFields['VALUE'] : ''; if ($elementID === '' || $typeID === '' || $value === '') { continue; } if ($typeID === 'EMAIL') { if (!isset($emails[$elementID])) { $emails[$elementID] = array(); } $emails[$elementID][] = $value; } elseif ($typeID === 'PHONE') { if (!isset($phones[$elementID])) { $phones[$elementID] = array(); } $phones[$elementID][] = $value; } } } while ($fields = $dbResult->Fetch()) { $ID = intval($fields['ID']); $companyTitle = isset($fields['COMPANY_TITLE']) ? $fields['COMPANY_TITLE'] : ''; if ($companyTitle !== '') { \Bitrix\Crm\Integrity\DuplicateOrganizationCriterion::register(CCrmOwnerType::Lead, $ID, $companyTitle); } $lastName = isset($fields['LAST_NAME']) ? $fields['LAST_NAME'] : ''; if ($lastName !== '') { \Bitrix\Crm\Integrity\DuplicatePersonCriterion::register(CCrmOwnerType::Lead, $ID, $lastName, isset($fields['NAME']) ? $fields['NAME'] : '', isset($fields['SECOND_NAME']) ? $fields['SECOND_NAME'] : ''); } $key = strval($ID); if (isset($emails[$key])) { \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::register(CCrmOwnerType::Lead, $ID, 'EMAIL', $emails[$key]); } if (isset($phones[$key])) { \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::register(CCrmOwnerType::Lead, $ID, 'PHONE', $phones[$key]); } \Bitrix\Crm\Integrity\DuplicateEntityRanking::registerEntityStatistics(CCrmOwnerType::Lead, $ID, $fields); } }
protected function prepareSortParams(array $entityIDs) { $resut = array('PERS' => array(), 'ORG' => array(), 'COMM' => array()); if (!empty($entityIDs)) { $entityTypeID = $this->getEntityTypeID(); if ($entityTypeID === \CCrmOwnerType::Lead) { $resut['PERS'] = DuplicatePersonCriterion::prepareSortParams($entityTypeID, $entityIDs); $resut['ORG'] = DuplicateOrganizationCriterion::prepareSortParams($entityTypeID, $entityIDs); } elseif ($entityTypeID === \CCrmOwnerType::Contact) { $resut['PERS'] = DuplicatePersonCriterion::prepareSortParams($entityTypeID, $entityIDs); } elseif ($entityTypeID === \CCrmOwnerType::Company) { $resut['ORG'] = DuplicateOrganizationCriterion::prepareSortParams($entityTypeID, $entityIDs); } $resut['COMM'] = DuplicateCommunicationCriterion::prepareSortParams($entityTypeID, $entityIDs); } return $resut; }
public function calculateEntityCount(DuplicateCriterion $criterion, array $options = null) { $count = $this->innerCalculateEntityCount($criterion, $options); $matches = $criterion->getMatches(); $name = isset($matches['NAME']) ? $matches['NAME'] : ''; $secondName = isset($matches['SECOND_NAME']) ? $matches['SECOND_NAME'] : ''; $lastName = isset($matches['LAST_NAME']) ? $matches['LAST_NAME'] : ''; if ($secondName !== '' && $name !== '') { $count += $this->innerCalculateEntityCount(DuplicatePersonCriterion::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name)), $options); } if ($name !== '') { $count += $this->innerCalculateEntityCount(DuplicatePersonCriterion::createFromMatches(array('LAST_NAME' => $lastName)), $options); } return $count; }