Пример #1
0
 /**
  * @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;
 }
Пример #2
0
 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);
     }
 }
Пример #3
0
 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);
 }
Пример #4
0
 public static function compareByRankingReversed(DuplicateEntity $a, DuplicateEntity $b)
 {
     return DuplicateEntityRanking::compareReversed($a->getRanking(), $b->getRanking());
 }
Пример #5
0
 public function Delete($ID, $arOptions = array())
 {
     global $DB, $APPLICATION;
     $ID = intval($ID);
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     $sWherePerm = '';
     if ($this->bCheckPermission) {
         $arEntityAttr = $this->cPerms->GetEntityAttr('COMPANY', $ID);
         $sEntityPerm = $this->cPerms->GetPermType('COMPANY', 'DELETE', $arEntityAttr[$ID]);
         if ($sEntityPerm == BX_CRM_PERM_NONE) {
             return false;
         } else {
             if ($sEntityPerm == BX_CRM_PERM_SELF) {
                 $sWherePerm = " AND ASSIGNED_BY_ID = {$iUserId}";
             } else {
                 if ($sEntityPerm == BX_CRM_PERM_OPEN) {
                     $sWherePerm = " AND (OPENED = 'Y' OR ASSIGNED_BY_ID = {$iUserId})";
                 }
             }
         }
     }
     $APPLICATION->ResetException();
     $events = GetModuleEvents('crm', 'OnBeforeCrmCompanyDelete');
     while ($arEvent = $events->Fetch()) {
         if (ExecuteModuleEventEx($arEvent, array($ID)) === false) {
             $err = GetMessage("MAIN_BEFORE_DEL_ERR") . ' ' . $arEvent['TO_NAME'];
             if ($ex = $APPLICATION->GetException()) {
                 $err .= ': ' . $ex->GetString();
             }
             $APPLICATION->throwException($err);
             return false;
         }
     }
     $obRes = $DB->Query("DELETE FROM b_crm_company WHERE ID = {$ID}{$sWherePerm}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
     if (is_object($obRes) && $obRes->AffectedRowsCount() > 0) {
         $DB->Query("DELETE FROM b_crm_entity_perms WHERE ENTITY='COMPANY' AND ENTITY_ID = {$ID}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
         $CCrmFieldMulti = new CCrmFieldMulti();
         $CCrmFieldMulti->DeleteByElement('COMPANY', $ID);
         $CCrmEvent = new CCrmEvent();
         $CCrmEvent->DeleteByElement('COMPANY', $ID);
         \Bitrix\Crm\Integrity\DuplicateEntityRanking::unregisterEntityStatistics(CCrmOwnerType::Company, $ID);
         \Bitrix\Crm\Integrity\DuplicateOrganizationCriterion::unregister(CCrmOwnerType::Company, $ID);
         \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::unregister(CCrmOwnerType::Company, $ID);
         \Bitrix\Crm\Integrity\DuplicateIndexMismatch::unregisterEntity(CCrmOwnerType::Company, $ID);
         $enableDupIndexInvalidation = is_array($arOptions) && isset($arOptions['ENABLE_DUP_INDEX_INVALIDATION']) ? (bool) $arOptions['ENABLE_DUP_INDEX_INVALIDATION'] : true;
         if ($enableDupIndexInvalidation) {
             \Bitrix\Crm\Integrity\DuplicateIndexBuilder::markAsJunk(CCrmOwnerType::Company, $ID);
         }
         CCrmActivity::DeleteByOwner(CCrmOwnerType::Company, $ID);
         CCrmSearch::DeleteSearch('COMPANY', $ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Company, $ID);
         CCrmLiveFeed::DeleteLogEvents(array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $ID));
         if (defined("BX_COMP_MANAGED_CACHE")) {
             $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_" . CCrmOwnerType::Company . "_" . $ID);
         }
     }
     return true;
 }