Пример #1
0
 public function Delete($ID, $arOptions = array())
 {
     global $DB, $APPLICATION;
     $ID = intval($ID);
     $iUserId = CCrmSecurityHelper::GetCurrentUserID();
     if (!is_array($arOptions)) {
         $arOptions = array();
     }
     $sWherePerm = '';
     if ($this->bCheckPermission) {
         $arEntityAttr = $this->cPerms->GetEntityAttr('CONTACT', $ID);
         $sEntityPerm = $this->cPerms->GetPermType('CONTACT', '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', 'OnBeforeCrmContactDelete');
     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;
         }
     }
     //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('CONTACT');
         $bizproc->ProcessDeletion($ID);
     }
     $obRes = $DB->Query("DELETE FROM b_crm_contact WHERE ID = {$ID}{$sWherePerm}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
     if (is_object($obRes) && $obRes->AffectedRowsCount() > 0) {
         CCrmSearch::DeleteSearch('CONTACT', $ID);
         $DB->Query("DELETE FROM b_crm_entity_perms WHERE ENTITY='CONTACT' AND ENTITY_ID = {$ID}", false, 'FILE: ' . __FILE__ . '<br /> LINE: ' . __LINE__);
         $GLOBALS['USER_FIELD_MANAGER']->Delete(self::$sUFEntityID, $ID);
         $CCrmFieldMulti = new CCrmFieldMulti();
         $CCrmFieldMulti->DeleteByElement('CONTACT', $ID);
         $CCrmEvent = new CCrmEvent();
         $CCrmEvent->DeleteByElement('CONTACT', $ID);
         \Bitrix\Crm\EntityAddress::unregister(CCrmOwnerType::Contact, $ID, \Bitrix\Crm\EntityAddress::Primary);
         \Bitrix\Crm\Integrity\DuplicateEntityRanking::unregisterEntityStatistics(CCrmOwnerType::Contact, $ID);
         \Bitrix\Crm\Integrity\DuplicateCommunicationCriterion::unregister(CCrmOwnerType::Contact, $ID);
         \Bitrix\Crm\Integrity\DuplicatePersonCriterion::unregister(CCrmOwnerType::Contact, $ID);
         \Bitrix\Crm\Integrity\DuplicateIndexMismatch::unregisterEntity(CCrmOwnerType::Contact, $ID);
         $enableDupIndexInvalidation = isset($arOptions['ENABLE_DUP_INDEX_INVALIDATION']) ? (bool) $arOptions['ENABLE_DUP_INDEX_INVALIDATION'] : true;
         if ($enableDupIndexInvalidation) {
             \Bitrix\Crm\Integrity\DuplicateIndexBuilder::markAsJunk(CCrmOwnerType::Contact, $ID);
         }
         CCrmActivity::DeleteByOwner(CCrmOwnerType::Contact, $ID);
         CCrmSonetSubscription::UnRegisterSubscriptionByEntity(CCrmOwnerType::Contact, $ID);
         CCrmLiveFeed::DeleteLogEvents(array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $ID));
         CCrmLead::ProcessContactDeletion($ID);
         if (defined("BX_COMP_MANAGED_CACHE")) {
             $GLOBALS["CACHE_MANAGER"]->ClearByTag("crm_entity_name_" . CCrmOwnerType::Contact . "_" . $ID);
         }
         $afterEvents = GetModuleEvents('crm', 'OnAfterCrmContactDelete');
         while ($arEvent = $afterEvents->Fetch()) {
             ExecuteModuleEventEx($arEvent, array($ID));
         }
     }
     return true;
 }