/** * @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); }
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; }