/** * @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 internalBuild(array &$progressData) { $offset = isset($progressData['OFFSET']) ? max((int) $progressData['OFFSET'], 0) : 0; $limit = isset($progressData['LIMIT']) ? max((int) $progressData['LIMIT'], 0) : 0; $dataSource = $this->getDataSource(); $result = $dataSource->getList($offset, $limit); $rankings = $result->getAllRankings(); DuplicateEntityRanking::initializeBulk($rankings, array('CHECK_PERMISSIONS' => $this->isPermissionCheckEnabled(), 'USER_ID' => $this->getUserID())); $rootEntityIDs = array(); $items = $result->getItems(); foreach ($items as $matchHash => $item) { $rootEntityInfo = array(); if ($this->tryResolveRootEntity($item, $matchHash, $rootEntityInfo)) { $entityID = $rootEntityInfo['ENTITY_ID']; $rootEntityIDs[] = $entityID; $item->setRootEntityID($entityID); } else { $result->removeItem($matchHash); } } $sortParams = $this->prepareSortParams($rootEntityIDs); $effectiveItemCount = 0; $items = $result->getItems(); foreach ($items as $matchHash => $item) { $enableOverwrite = $item->getOption('enableOverwrite', true); if (!$enableOverwrite && Entity\DuplicateIndexTable::exists($this->getPrimaryKey($matchHash))) { continue; } $data = $this->prepareTableData($matchHash, $item, $sortParams, true); Entity\DuplicateIndexTable::upsert($data); $effectiveItemCount++; } $processedItemCount = $result->getProcessedItemCount(); $progressData['EFFECTIVE_ITEM_COUNT'] = $effectiveItemCount; $progressData['PROCESSED_ITEM_COUNT'] = $processedItemCount; $progressData['OFFSET'] = $offset + $processedItemCount; return $this->isInProgress($progressData); }