Exemplo n.º 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;
 }
 public function calculateEntityCount(DuplicateCriterion $criterion, array $options = null)
 {
     $entityTypeID = $this->getEntityTypeID();
     $enablePermissionCheck = $this->isPermissionCheckEnabled();
     $userID = $this->getUserID();
     $query = new Main\Entity\Query(DuplicateOrganizationMatchCodeTable::getEntity());
     $query->addSelect('QTY');
     $query->registerRuntimeField('', new Main\Entity\ExpressionField('QTY', 'COUNT(*)'));
     $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
     if ($enablePermissionCheck) {
         $permissionSql = $this->preparePermissionSql();
         if ($permissionSql === false) {
             //Access denied;
             return 0;
         }
         if (is_string($permissionSql) && $permissionSql !== '') {
             $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql));
         }
     }
     $matches = $criterion->getMatches();
     $title = isset($matches['TITLE']) ? $matches['TITLE'] : '';
     if ($title === '') {
         throw new Main\ArgumentException("Parameter 'TITLE' is required.", 'matches');
     }
     $query->addFilter('=TITLE', $title);
     $rootEntityID = 0;
     if (is_array($options) && isset($options['ROOT_ENTITY_ID'])) {
         $rootEntityID = (int) $options['ROOT_ENTITY_ID'];
     }
     if ($rootEntityID > 0) {
         $query->addFilter('!ENTITY_ID', $rootEntityID);
         $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField($criterion, $entityTypeID, $rootEntityID, $userID));
     }
     $limit = 0;
     if (is_array($options) && isset($options['LIMIT'])) {
         $limit = (int) $options['LIMIT'];
     }
     if ($limit > 0) {
         $query->setLimit($limit);
     }
     $dbResult = $query->exec();
     $fields = $dbResult->fetch();
     return is_array($fields) && isset($fields['QTY']) ? intval($fields['QTY']) : 0;
 }
Exemplo n.º 3
0
 public function registerCriterionMismatch(Integrity\DuplicateCriterion $criterion, $leftEntityID, $rightEntityID)
 {
     $entityTypeID = $this->entityTypeID;
     $userID = $this->userID;
     $typeID = $criterion->getIndexTypeID();
     $matchHash = $criterion->getMatchHash();
     if ($matchHash === '') {
         throw new Main\ArgumentException('Match hash is empty', 'criterion');
     }
     Integrity\DuplicateIndexMismatch::register($entityTypeID, $leftEntityID, $rightEntityID, $typeID, $matchHash, $userID);
 }
Exemplo n.º 4
0
 public function Delete($ID, $arOptions = array())
 {
     global $DB, $APPLICATION;
     $ID = intval($ID);
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     $this->LAST_ERROR = '';
     $APPLICATION->ResetException();
     if (!is_array($arOptions)) {
         $arOptions = array();
     }
     if (isset($arOptions['CHECK_DEPENDENCIES']) && (bool) $arOptions['CHECK_DEPENDENCIES']) {
         $dbRes = self::GetListEx(array(), array('=ID' => $ID), false, false, array('TITLE', 'STATUS_ID', 'COMPANY_ID', 'CONTACT_ID'));
         $arFields = $dbRes ? $dbRes->Fetch() : null;
         if (is_array($arFields) && isset($arFields['STATUS_ID']) && $arFields['STATUS_ID'] === 'CONVERTED' && (CCrmCompany::Exists(isset($arFields['COMPANY_ID']) ? intval($arFields['COMPANY_ID']) : 0) || CCrmContact::Exists(isset($arFields['CONTACT_ID']) ? intval($arFields['CONTACT_ID']) : 0))) {
             $title = isset($arFields['TITLE']) && $arFields['TITLE'] !== '' ? $arFields['TITLE'] : $ID;
             $err = GetMessage('CRM_LEAD_DELETION_DEPENDENCIES_FOUND', array('#TITLE#' => $title));
             $this->LAST_ERROR = $err;
             $APPLICATION->throwException($err);
             return false;
         }
     }
     $sWherePerm = '';
     if ($this->bCheckPermission) {
         $arEntityAttr = $this->cPerms->GetEntityAttr('LEAD', $ID);
         $sEntityPerm = $this->cPerms->GetPermType('LEAD', '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})";
                 }
             }
         }
     }
     $events = GetModuleEvents('crm', 'OnBeforeCrmLeadDelete');
     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);
             $this->LAST_ERROR = $err;
             return false;
         }
     }
     //By defaut we need to clean up related bizproc entities
     $processBizproc = isset($arOptions['PROCESS_BIZPROC']) ? (bool) $arOptions['PROCESS_BIZPROC'] : true;
     if ($processBizproc) {
         $bizproc = new CCrmBizProc('LEAD');
         $bizproc->ProcessDeletion($ID);
     }
     $tableName = CCrmLead::TABLE_NAME;
     $sSql = "DELETE FROM {$tableName} WHERE ID = {$ID}{$sWherePerm}";
     $obRes = $DB->Query($sSql, false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
     if (is_object($obRes) && $obRes->AffectedRowsCount() > 0) {
         CCrmSearch::DeleteSearch('LEAD', $ID);
         $DB->Query("DELETE FROM b_crm_entity_perms WHERE ENTITY='LEAD' AND ENTITY_ID = {$ID}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
         $CCrmFieldMulti = new CCrmFieldMulti();
         $CCrmFieldMulti->DeleteByElement('LEAD', $ID);
         $CCrmEvent = new CCrmEvent();
         $CCrmEvent->DeleteByElement('LEAD', $ID);
         \Bitrix\Crm\EntityAddress::unregister(CCrmOwnerType::Lead, $ID, \Bitrix\Crm\EntityAddress::Primary);
         \Bitrix\Crm\Integrity\DuplicateEntityRanking::unregisterEntityStatistics(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicatePersonCriterion::unregister(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicateOrganizationCriterion::unregister(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::unregister(CCrmOwnerType::Lead, $ID);
         \Bitrix\Crm\Integrity\DuplicateIndexMismatch::unregisterEntity(CCrmOwnerType::Lead, $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::Lead, $ID);
         }
         // Deletion of lead details
         CCrmProductRow::DeleteByOwner('L', $ID);
         CCrmProductRow::DeleteSettings('L', $ID);
         CCrmActivity::DeleteByOwner(CCrmOwnerType::Lead, $ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Lead, $ID);
         CCrmLiveFeed::DeleteLogEvents(array('ENTITY_TYPE_ID' => CCrmOwnerType::Lead, 'ENTITY_ID' => $ID));
         if (defined("BX_COMP_MANAGED_CACHE")) {
             $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_" . CCrmOwnerType::Lead . "_" . $ID);
         }
         $afterEvents = GetModuleEvents('crm', 'OnAfterCrmLeadDelete');
         while ($arEvent = $afterEvents->Fetch()) {
             ExecuteModuleEventEx($arEvent, array($ID));
         }
     }
     return true;
 }
Exemplo n.º 5
0
 protected function checkRootEntityMismatches($rootEntityID, $matchHash, array $entities)
 {
     $map = array();
     /** @var DuplicateEntity $entity */
     foreach ($entities as $entity) {
         $entityID = $entity->getEntityID();
         if ($entityID === $rootEntityID) {
             continue;
         }
         $entityCriterion = $entity->getCriterion();
         $entityMatchHash = $entityCriterion ? $entityCriterion->getMatchHash() : $matchHash;
         if (!isset($map[$entityMatchHash])) {
             $map[$entityMatchHash] = array();
         }
         $map[$entityMatchHash][] = $entityID;
     }
     foreach ($map as $entityMatchHash => $entityIDs) {
         $mismatches = array_intersect($entityIDs, DuplicateIndexMismatch::getMismatches($this->getEntityTypeID(), $rootEntityID, $this->typeID, $entityMatchHash, $this->getUserID(), 100));
         if (count($entityIDs) > count($mismatches)) {
             return true;
         }
     }
     return false;
 }
 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);
         }
     }
 }
Exemplo n.º 7
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;
 }