示例#1
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;
 }
示例#2
0
 public function merge($seedID, $targID, Integrity\DuplicateCriterion $targCriterion)
 {
     if (!is_int($seedID)) {
         $seedID = (int) $seedID;
     }
     if (!is_int($targID)) {
         $targID = (int) $targID;
     }
     $entityTypeID = $this->entityTypeID;
     if ($this->enablePermissionCheck && !$this->userIsAdmin) {
         $userPermissions = $this->getUserPermissions();
         if (!$this->checkEntityReadPermission($seedID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $seedID, self::ROLE_SEED, EntityMergerException::READ_DENIED);
         }
         if (!$this->checkEntityDeletePermission($seedID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $seedID, self::ROLE_SEED, EntityMergerException::DELETE_DENIED);
         }
         if (!$this->checkEntityReadPermission($targID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $targID, self::ROLE_TARG, EntityMergerException::READ_DENIED);
         }
         if (!$this->checkEntityUpdatePermission($targID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $targID, self::ROLE_TARG, EntityMergerException::UPDATE_DENIED);
         }
     }
     $collisions = self::getMergeCollisions($seedID, $targID);
     $seed = $this->getEntityFields($seedID, self::ROLE_SEED);
     $targ = $this->getEntityFields($targID, self::ROLE_TARG);
     $entityFieldInfos = $this->getEntityFieldsInfo();
     $userFieldInfos = $this->getEntityUserFieldsInfo();
     EntityMerger::mergeEntityFields($seed, $targ, $entityFieldInfos);
     EntityMerger::mergeUserFields($seed, $targ, $userFieldInfos);
     $seedMultiFields = $this->getEntityMultiFields($seedID, self::ROLE_SEED);
     $targMultiFields = $this->getEntityMultiFields($targID, self::ROLE_TARG);
     EntityMerger::mergeMultiFields($seedMultiFields, $targMultiFields);
     if (!empty($targMultiFields)) {
         $targ['FM'] = $targMultiFields;
     }
     //$recoveryData = self::prepareRecoveryData($seed, $entityFieldInfos, $userFieldInfos);
     //$recoveryData->setEntityTypeID($entityTypeID);
     //$recoveryData->setEntityID($seedID);
     //$this->setupRecoveryData($recoveryData, $seed);
     //if(!empty($seedMultiFields))
     //{
     //	$recoveryData->setDataItem('MULTI_FIELDS', $seedMultiFields);
     //}
     //$activityIDs = \CCrmActivity::GetBoundIDs($entityTypeID, $seedID);
     //if(!empty($activityIDs))
     //{
     //	$recoveryData->setDataItem('ACTIVITY_IDS', $activityIDs);
     //}
     //$eventIDs = array();
     //$result = \CCrmEvent::GetListEx(
     //	array('EVENT_REL_ID' => 'ASC'),
     //	array(
     //		'ENTITY_TYPE' => $entityTypeName,
     //		'ENTITY_ID' => $seedID,
     //		'EVENT_TYPE' => 0,
     //		'CHECK_PERMISSIONS' => 'N'
     //	),
     //	false,
     //	false,
     //	array('EVENT_REL_ID')
     //);
     //if(is_object($result))
     //{
     //	while($eventFields = $result->Fetch())
     //	{
     //		$eventIDs[] = (int)$eventFields['EVENT_REL_ID'];
     //	}
     //}
     //if(!empty($eventIDs))
     //{
     //	$recoveryData->setDataItem('EVENT_IDS', $eventIDs);
     //}
     //$recoveryData->setUserID($this->userID);
     //$recoveryData->save();
     $this->updateEntity($targID, $targ, self::ROLE_TARG);
     $this->rebind($seedID, $targID);
     $matches = $this->getRegisteredEntityMatches($entityTypeID, $seedID);
     $targIndexTypeID = $targCriterion->getIndexTypeID();
     if (!isset($matches[$targIndexTypeID])) {
         $matches[$targIndexTypeID] = array();
     }
     $targetMatchHash = $targCriterion->getMatchHash();
     if (!isset($matches[$targIndexTypeID][$targetMatchHash])) {
         $matches[$targIndexTypeID][$targetMatchHash] = $targCriterion->getMatches();
     }
     $this->deleteEntity($seedID, self::ROLE_SEED, array('ENABLE_DUP_INDEX_INVALIDATION' => false));
     if (!empty($matches)) {
         $this->processEntityDeletion($entityTypeID, $seedID, $matches);
     }
     Integrity\DuplicateIndexBuilder::markAsJunk($entityTypeID, $seedID);
     if (!empty($collisions)) {
         $messageFields = $this->prepareCollisionMessageFields($collisions, $seed, $targ);
         if (is_array($messageFields) && !empty($messageFields) && Main\Loader::includeModule('im')) {
             $messageFields['FROM_USER_ID'] = $this->userID;
             $messageFields['MESSAGE_TYPE'] = IM_MESSAGE_SYSTEM;
             $messageFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM;
             $messageFields['NOTIFY_MODULE'] = 'crm';
             $messageFields['NOTIFY_EVENT'] = 'merge';
             $messageFields['NOTIFY_TAG'] = 'CRM|MERGE|COLLISION';
             \CIMNotify::Add($messageFields);
         }
     }
 }
示例#3
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;
 }