/** * @return Duplicate */ public function createDuplicate($entityTypeID, $rootEntityID, $userID, $enablePermissionCheck, $enableRanking, $limit = 0) { if ($entityTypeID !== \CCrmOwnerType::Lead && $entityTypeID !== \CCrmOwnerType::Contact && $entityTypeID !== \CCrmOwnerType::Company) { throw new Main\NotSupportedException("Entity type: '" . \CCrmOwnerType::ResolveName($entityTypeID) . "' is not supported in current context"); } /** @var Duplicate $dup **/ $dup = new Duplicate($this, array()); $query = static::createQuery(); $query->addSelect('ENTITY_ID'); $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID); static::setQueryFilter($query, $this->getMatches()); if ($enablePermissionCheck) { $permissions = isset($params['PERMISSIONS']) ? $params['PERMISSIONS'] : null; if ($permissions === null) { $permissions = \CCrmPerms::GetUserPermissions($userID); } $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions)); if ($permissionSql === false) { //Access denied; return null; } if ($permissionSql !== '') { $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } } if ($limit > 0) { $query->setLimit($limit); } if ($rootEntityID > 0) { $dup->setRootEntityID($rootEntityID); $query->addFilter('!ENTITY_ID', $rootEntityID); $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField($this, $entityTypeID, $rootEntityID, $userID)); } $dbResult = $query->exec(); $rankings = array(); while ($fields = $dbResult->fetch()) { $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0; if ($entityID <= 0) { continue; } $entity = new DuplicateEntity($entityTypeID, $entityID); if ($enableRanking) { $rankings[] = $entity->getRanking(); } $dup->addEntity($entity); } $this->onAfterDuplicateCreated($dup, $entityTypeID, $userID, $enablePermissionCheck, $enableRanking, $rankings); if ($enableRanking) { DuplicateEntityRanking::initializeBulk($rankings, array('CHECK_PERMISSIONS' => $enablePermissionCheck, 'USER_ID' => $userID)); } return $dup; }
protected function prepareResult(array &$map, DedupeDataSourceResult $result) { $entityTypeID = $this->getEntityTypeID(); foreach ($map as $matchHash => &$entry) { $primaryQty = isset($entry['PRIMARY']) ? count($entry['PRIMARY']) : 0; $secondaryQty = isset($entry['SECONDARY']) ? count($entry['SECONDARY']) : 0; if ($primaryQty > 1) { $matches = $this->getEntityMatchesByHash($entityTypeID, $entry['PRIMARY'][0], $matchHash); if (is_array($matches)) { $criterion = $this->createCriterionFromMatches($matches); $dup = new Duplicate($criterion, array()); foreach ($entry['PRIMARY'] as $entityID) { $dup->addEntity(new DuplicateEntity($entityTypeID, $entityID)); } $result->addItem($matchHash, $dup); } } if ($primaryQty > 0 && $secondaryQty > 0) { $matches = $this->loadEntitesMatches($entityTypeID, $entry['SECONDARY']); foreach ($matches as $entityID => $entityMatches) { $criterion = $this->createCriterionFromMatches($entityMatches); $entityMatchHash = $criterion->getMatchHash(); if ($entityMatchHash === '') { continue; } $dup = $result->getItem($entityMatchHash); if (!$dup) { $dup = new Duplicate($criterion, array(new DuplicateEntity($entityTypeID, $entityID))); $dup->setOption('enableOverwrite', false); $dup->setRootEntityID($entityID); } $result->addItem($entityMatchHash, $dup); foreach ($entry['PRIMARY'] as $primaryEntityID) { $matches = $this->getEntityMatchesByHash($entityTypeID, $primaryEntityID, $matchHash); if (is_array($matches)) { $entity = new DuplicateEntity($entityTypeID, $primaryEntityID); $entity->setCriterion($this->createCriterionFromMatches($matches)); $dup->addEntity($entity); } } } } } unset($entry); }
protected function prepareResult(array &$map, DedupeDataSourceResult $result) { $entityTypeID = $this->getEntityTypeID(); foreach ($map as $matchHash => &$entry) { $primaryQty = isset($entry['PRIMARY']) ? count($entry['PRIMARY']) : 0; if ($primaryQty > 1) { $matches = $this->getEntityMatchesByHash($entityTypeID, $entry['PRIMARY'][0], $matchHash); if (is_array($matches)) { $criterion = $this->createCriterionFromMatches($matches); $dup = new Duplicate($criterion, array()); foreach ($entry['PRIMARY'] as $entityID) { $dup->addEntity(new DuplicateEntity($entityTypeID, $entityID)); } $result->addItem($matchHash, $dup); } } } unset($entry); }
$info['EMAIL'] = $multiFields['EMAIL']; } } $dupInfo['ENTITIES'][] =& $info; unset($info); } unset($entity); $criterion = $dup->getCriterion(); if ($criterion instanceof \Bitrix\Crm\Integrity\DuplicateCriterion) { $dupInfo['CRITERION'] = array('TYPE_NAME' => $criterion->getTypeName(), 'MATCHES' => $criterion->getMatches()); } $dupInfos[] =& $dupInfo; unset($dupInfo); } unset($dup); $groupResults[] = array('DUPLICATES' => &$dupInfos, 'GROUP_ID' => isset($group['GROUP_ID']) ? $group['GROUP_ID'] : '', 'FIELD_ID' => isset($group['FIELD_ID']) ? $group['FIELD_ID'] : '', 'HASH_CODE' => isset($group['HASH_CODE']) ? intval($group['HASH_CODE']) : 0, 'ENTITY_TOTAL_TEXT' => \Bitrix\Crm\Integrity\Duplicate::entityCountToText($totalEntities)); unset($dupInfos); } unset($group); __CrmCompanyEditEndResonse(array('GROUP_RESULTS' => $groupResults)); } elseif ($action === 'FIND_LOCALITIES') { $localityType = isset($_POST['LOCALITY_TYPE']) ? $_POST['LOCALITY_TYPE'] : 'COUNTRY'; $needle = isset($_POST['NEEDLE']) ? $_POST['NEEDLE'] : ''; if ($localityType === 'COUNTRY') { $result = \Bitrix\Crm\EntityAddress::getCountries(array('CAPTION' => $needle)); __CrmCompanyEditEndResonse(array('DATA' => array('ITEMS' => $result))); } else { __CrmCompanyEditEndResonse(array('ERROR' => "Locality '{$localityType}' is not supported in current context.")); } } else { __CrmCompanyEditEndResonse(array('ERROR' => "Action '{$action}' is not supported in current context."));
protected function prepareTableData($matchHash, Duplicate $item, array &$sortParams, $enablePrimaryKey = true) { $data = array('ROOT_ENTITY_ID' => 0, 'ROOT_ENTITY_NAME' => '', 'ROOT_ENTITY_TITLE' => '', 'ROOT_ENTITY_PHONE' => '', 'ROOT_ENTITY_EMAIL' => '', 'QUANTITY' => 0); if ($enablePrimaryKey) { $data['USER_ID'] = $this->getUserID(); $data['ENTITY_TYPE_ID'] = $this->getEntityTypeID(); $data['TYPE_ID'] = $this->typeID; $data['MATCH_HASH'] = $matchHash; $criterion = $item->getCriterion(); $data['MATCHES'] = serialize($criterion->getMatches()); } $entityID = $item->getRootEntityID(); if ($entityID > 0) { $data['ROOT_ENTITY_ID'] = $entityID; $pers = isset($sortParams['PERS']) ? $sortParams['PERS'] : null; if (is_array($pers) && isset($pers[$entityID]) && isset($pers[$entityID]['FULL_NAME'])) { $data['ROOT_ENTITY_NAME'] = $pers[$entityID]['FULL_NAME']; } $org = isset($sortParams['ORG']) ? $sortParams['ORG'] : null; if (is_array($org) && isset($org[$entityID]) && isset($org[$entityID]['TITLE'])) { $data['ROOT_ENTITY_TITLE'] = $org[$entityID]['TITLE']; } $comm = isset($sortParams['COMM']) ? $sortParams['COMM'] : null; if (is_array($comm) && isset($comm[$entityID])) { if (isset($comm[$entityID]['PHONE'])) { $data['ROOT_ENTITY_PHONE'] = $comm[$entityID]['PHONE']; } if (isset($comm[$entityID]['EMAIL'])) { $data['ROOT_ENTITY_EMAIL'] = $comm[$entityID]['EMAIL']; } } } return $data; }
protected function onAfterDuplicateCreated(Duplicate $dup, $entityTypeID, $userID, $enablePermissionCheck, $enableRanking, array &$rankings) { $name = $this->name; $lastName = $this->lastName; $secondName = $this->secondName; $rootEntityID = $dup->getRootEntityID(); if ($secondName === '' && $name === '') { return; } $permissionSql = ''; if ($enablePermissionCheck) { $permissions = isset($params['PERMISSIONS']) ? $params['PERMISSIONS'] : null; if ($permissions === null) { $permissions = \CCrmPerms::GetUserPermissions($userID); } $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions)); if ($permissionSql === false) { //Access denied; return; } } if ($secondName !== '') { $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity()); $query->addSelect('ENTITY_ID'); $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID); $query->addFilter('=LAST_NAME', $lastName); $query->addFilter('=NAME', $name); $query->addFilter('=SECOND_NAME', ''); if ($rootEntityID) { $query->addFilter('!ENTITY_ID', $rootEntityID); $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name)), $entityTypeID, $rootEntityID, $userID)); } if ($enablePermissionCheck && $permissionSql !== '') { $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } $dbResult = $query->exec(); while ($fields = $dbResult->fetch()) { $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0; if ($entityID <= 0) { continue; } $entity = new DuplicateEntity($entityTypeID, $entityID); $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name))); if ($enableRanking) { $rankings[] = $entity->getRanking(); } $dup->addEntity($entity); } } if ($name !== '') { $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity()); $query->addSelect('ENTITY_ID'); $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID); $query->addFilter('=LAST_NAME', $lastName); $query->addFilter('=NAME', ''); $query->addFilter('=SECOND_NAME', ''); if ($rootEntityID) { $query->addFilter('!ENTITY_ID', $rootEntityID); $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName)), $entityTypeID, $rootEntityID, $userID)); } if ($enablePermissionCheck && $permissionSql !== '') { $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql)); } $dbResult = $query->exec(); while ($fields = $dbResult->fetch()) { $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0; if ($entityID <= 0) { continue; } $entity = new DuplicateEntity($entityTypeID, $entityID); $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName))); if ($enableRanking) { $rankings[] = $entity->getRanking(); } $dup->addEntity($entity); } } }
public function getTextTotals($count, $limit = 0) { self::includeLangFile(); if (!is_int($count)) { $count = (int) $count; } if (!is_int($limit)) { $limit = (int) $limit; } $exceeded = $limit > 0 && $count > $limit; if ($exceeded) { $count = $limit; } return GetMessage($exceeded ? 'CRM_DUP_CRITERION_ORG_ENTITY_TOTAL_EXCEEDED' : 'CRM_DUP_CRITERION_ORG_ENTITY_TOTAL', array('#DESCR#' => $this->getMatchDescription(), '#QTY#' => Duplicate::entityCountToText($count))); }
private function createDuplicate(array &$fields) { $rootEntityID = isset($fields['ROOT_ENTITY_ID']) ? (int) $fields['ROOT_ENTITY_ID'] : 0; $typeID = isset($fields['TYPE_ID']) ? (int) $fields['TYPE_ID'] : 0; $matches = isset($fields['MATCHES']) ? $fields['MATCHES'] : ''; $matches = $matches !== '' ? unserialize($matches) : array(); $quantity = isset($fields['QUANTITY']) ? (int) $fields['QUANTITY'] : 0; $result = new Duplicate(DuplicateManager::createCriterion($typeID, $matches), array()); $result->setRootEntityID($rootEntityID); $isJunk = isset($fields['IS_JUNK']) && strtoupper($fields['IS_JUNK']) === 'Y'; if ($isJunk) { $result->markAsJunk(true); //Try to supply more information for junked item (if root entity is already deleted) $rootPersName = isset($fields['ROOT_ENTITY_NAME']) ? $fields['ROOT_ENTITY_NAME'] : ''; if ($rootPersName !== '') { $names = explode(' ', $rootPersName); $qty = count($names); for ($i = 0; $i < $qty; $i++) { $names[$i] = ucfirst($names[$i]); } $result->setRootPersonName(implode(' ', $names)); } $rootOrgTitle = isset($fields['ROOT_ENTITY_TITLE']) ? $fields['ROOT_ENTITY_TITLE'] : ''; if ($rootOrgTitle !== '') { $result->setRootOrganizationTitle(ucfirst($rootOrgTitle)); } } $result->setTotalEntityCount($quantity); return $result; }
public function getTextTotals($count, $limit = 0) { self::includeLangFile(); if (!is_int($count)) { $count = (int) $count; } if (!is_int($limit)) { $limit = (int) $limit; } $exceeded = $limit > 0 && $count > $limit; if ($exceeded) { $count = $limit; } /* * CRM_DUP_CRITERION_COMM_PHONE_ENTITY_TOTAL * CRM_DUP_CRITERION_COMM_PHONE_ENTITY_TOTAL_EXCEEDED * CRM_DUP_CRITERION_COMM_EMAIL_ENTITY_TOTAL * CRM_DUP_CRITERION_COMM_EMAIL_ENTITY_TOTAL_EXCEEDED */ return GetMessage($exceeded ? "CRM_DUP_CRITERION_COMM_{$this->communicationType}_ENTITY_TOTAL_EXCEEDED" : "CRM_DUP_CRITERION_COMM_{$this->communicationType}_ENTITY_TOTAL", array('#DESCR#' => $this->getMatchDescription(), '#QTY#' => Duplicate::entityCountToText($count))); }