Beispiel #1
0
function GetCrmEntityCommunications($entityType, $entityID, $communicationType)
{
    $fullNameFormat = \Bitrix\Crm\Format\PersonNameFormatter::getFormat();
    if ($entityType === 'LEAD') {
        $data = array('ownerEntityType' => 'LEAD', 'ownerEntityId' => $entityID, 'entityType' => 'LEAD', 'entityId' => $entityID, 'entityTitle' => "{$entityType}_{$entityID}", 'entityDescription' => '', 'tabId' => 'main', 'communications' => array());
        $entity = CCrmLead::GetByID($entityID);
        if (!$entity) {
            return array('ERROR' => 'Invalid data');
        }
        // Prepare title
        $name = isset($entity['NAME']) ? $entity['NAME'] : '';
        $secondName = isset($entity['SECOND_NAME']) ? $entity['SECOND_NAME'] : '';
        $lastName = isset($entity['LAST_NAME']) ? $entity['LAST_NAME'] : '';
        if ($name !== '' || $secondName !== '' || $lastName !== '') {
            $data['entityTitle'] = CUser::FormatName($fullNameFormat, array('LOGIN' => '', 'NAME' => $name, 'SECOND_NAME' => $secondName, 'LAST_NAME' => $lastName), false, false);
            $data['entityDescription'] = isset($entity['TITLE']) ? $entity['TITLE'] : '';
        } else {
            $data['entityTitle'] = isset($entity['TITLE']) ? $entity['TITLE'] : '';
            $data['entityDescription'] = '';
        }
        // Try to load entity communications
        if (!CCrmActivity::CheckReadPermission(CCrmOwnerType::ResolveID($entityType), $entityID)) {
            return array('ERROR' => GetMessage('CRM_PERMISSION_DENIED'));
        }
        if ($communicationType !== '') {
            $dbResFields = CCrmFieldMulti::GetList(array('ID' => 'asc'), array('ENTITY_ID' => $entityType, 'ELEMENT_ID' => $entityID, 'TYPE_ID' => $communicationType));
            while ($arField = $dbResFields->Fetch()) {
                if (empty($arField['VALUE'])) {
                    continue;
                }
                $comm = array('type' => $communicationType, 'value' => $arField['VALUE']);
                $data['communications'][] = $comm;
            }
        }
        return array('DATA' => array('TABS' => array(array('id' => 'lead', 'title' => GetMessage('CRM_COMMUNICATION_TAB_LEAD'), 'active' => true, 'items' => array($data)))));
    } elseif ($entityType === 'DEAL') {
        $entity = CCrmDeal::GetByID($entityID);
        if (!$entity) {
            return array('ERROR' => 'Invalid data');
        }
        $dealData = array();
        // Prepare company data
        $entityCompanyData = null;
        $entityCompanyID = isset($entity['COMPANY_ID']) ? intval($entity['COMPANY_ID']) : 0;
        $entityCompany = $entityCompanyID > 0 ? CCrmCompany::GetByID($entityCompanyID) : null;
        if (is_array($entityCompany)) {
            $entityCompanyData = array('ownerEntityType' => 'DEAL', 'ownerEntityId' => $entityID, 'entityType' => 'COMPANY', 'entityId' => $entityCompanyID, 'entityTitle' => isset($entityCompany['TITLE']) ? $entityCompany['TITLE'] : '', 'entityDescription' => '', 'communications' => array());
            if ($communicationType !== '') {
                $entityCompanyComms = CCrmActivity::PrepareCommunications('COMPANY', $entityCompanyID, $communicationType);
                foreach ($entityCompanyComms as &$entityCompanyComm) {
                    $comm = array('type' => $entityCompanyComm['TYPE'], 'value' => $entityCompanyComm['VALUE']);
                    $entityCompanyData['communications'][] = $comm;
                }
                unset($entityCompanyComm);
            }
        }
        // Try to get contact of deal
        $entityContactID = isset($entity['CONTACT_ID']) ? intval($entity['CONTACT_ID']) : 0;
        if ($entityContactID > 0) {
            $entityContact = CCrmContact::GetByID($entityContactID);
            if (is_array($entityContact)) {
                $item = array('ownerEntityType' => 'DEAL', 'ownerEntityId' => $entityID, 'entityType' => 'CONTACT', 'entityId' => $entityContactID, 'entityTitle' => CUser::FormatName($fullNameFormat, array('LOGIN' => '', 'NAME' => $entityContact['NAME'], 'LAST_NAME' => $entityContact['LAST_NAME'], 'SECOND_NAME' => $entityContact['SECOND_NAME']), false, false), 'tabId' => 'deal', 'communications' => array());
                $entityCompany = isset($entityContact['COMPANY_ID']) ? CCrmCompany::GetByID($entityContact['COMPANY_ID']) : null;
                if ($entityCompany && isset($entityCompany['TITLE'])) {
                    $item['entityDescription'] = $entityCompany['TITLE'];
                }
                if ($communicationType !== '') {
                    $entityContactComms = CCrmActivity::PrepareCommunications('CONTACT', $entityContactID, $communicationType);
                    foreach ($entityContactComms as &$entityContactComm) {
                        $comm = array('type' => $entityContactComm['TYPE'], 'value' => $entityContactComm['VALUE']);
                        $item['communications'][] = $comm;
                    }
                    unset($entityContactComm);
                }
                if ($communicationType === '' || !empty($item['communications'])) {
                    $dealData["CONTACT_{$entityContactID}"] = $item;
                }
            }
        }
        if ($entityCompanyData && !empty($entityCompanyData['communications'])) {
            $dealData['COMPANY_' . $entityCompanyID] = $entityCompanyData;
            $dealData['COMPANY_' . $entityCompanyID]['tabId'] = 'deal';
        }
        // Try to get previous communications
        $entityComms = CCrmActivity::GetCommunicationsByOwner('DEAL', $entityID, $communicationType);
        foreach ($entityComms as &$entityComm) {
            CCrmActivity::PrepareCommunicationInfo($entityComm);
            $key = "{$entityComm['ENTITY_TYPE']}_{$entityComm['ENTITY_ID']}";
            if (!isset($dealData[$key])) {
                $dealData[$key] = array('ownerEntityType' => 'DEAL', 'ownerEntityId' => $entityID, 'entityType' => CCrmOwnerType::ResolveName($entityComm['ENTITY_TYPE_ID']), 'entityId' => $entityComm['ENTITY_ID'], 'entityTitle' => isset($entityComm['TITLE']) ? $entityComm['TITLE'] : '', 'entityDescription' => isset($entityComm['DESCRIPTION']) ? $entityComm['DESCRIPTION'] : '', 'tabId' => 'deal', 'communications' => array());
            }
            if ($communicationType !== '') {
                $commFound = false;
                foreach ($dealData[$key]['communications'] as &$comm) {
                    if ($comm['value'] === $entityComm['VALUE']) {
                        $commFound = true;
                        break;
                    }
                }
                unset($comm);
                if ($commFound) {
                    continue;
                }
                $comm = array('type' => $entityComm['TYPE'], 'value' => $entityComm['VALUE']);
                $dealData[$key]['communications'][] = $comm;
            }
        }
        unset($entityComm);
        $companyData = array();
        // Try to get contacts of company
        if ($entityCompany > 0) {
            $entityComms = CCrmActivity::GetCompanyCommunications($entityCompanyID, $communicationType);
            foreach ($entityComms as &$entityComm) {
                CCrmActivity::PrepareCommunicationInfo($entityComm);
                $key = "{$entityComm['ENTITY_TYPE']}_{$entityComm['ENTITY_ID']}";
                if (!isset($companyData[$key])) {
                    $companyData[$key] = array('ownerEntityType' => 'DEAL', 'ownerEntityId' => $entityID, 'entityType' => CCrmOwnerType::ResolveName($entityComm['ENTITY_TYPE_ID']), 'entityId' => $entityComm['ENTITY_ID'], 'entityTitle' => isset($entityComm['TITLE']) ? $entityComm['TITLE'] : '', 'entityDescription' => isset($entityComm['DESCRIPTION']) ? $entityComm['DESCRIPTION'] : '', 'tabId' => 'company', 'communications' => array());
                }
                if ($communicationType !== '') {
                    $comm = array('type' => $entityComm['TYPE'], 'value' => $entityComm['VALUE']);
                    $companyData[$key]['communications'][] = $comm;
                }
            }
            unset($entityComm);
        }
        if ($entityCompanyData && !empty($entityCompanyData['communications'])) {
            $companyData['COMPANY_' . $entityCompanyID] = $entityCompanyData;
            $companyData['COMPANY_' . $entityCompanyID]['tabId'] = 'company';
        }
        return array('DATA' => array('TABS' => array(array('id' => 'deal', 'title' => GetMessage('CRM_COMMUNICATION_TAB_DEAL'), 'active' => true, 'items' => array_values($dealData)), array('id' => 'company', 'title' => GetMessage('CRM_COMMUNICATION_TAB_COMPANY'), 'items' => array_values($companyData)))));
    } elseif ($entityType === 'COMPANY') {
        $companyData = array();
        $entity = CCrmCompany::GetByID($entityID);
        if (!$entity) {
            return array('ERROR' => 'Invalid data');
        }
        $companyItem = array('ownerEntityType' => 'COMPANY', 'ownerEntityId' => $entityID, 'entityType' => 'COMPANY', 'entityId' => $entityID, 'entityTitle' => isset($entity['TITLE']) ? $entity['TITLE'] : "{$entityType}_{$entityID}", 'entityDescription' => '', 'tabId' => 'company', 'communications' => array());
        // Try to load entity communications
        if (!CCrmActivity::CheckReadPermission(CCrmOwnerType::ResolveID($entityType), $entityID)) {
            return array('ERROR' => GetMessage('CRM_PERMISSION_DENIED'));
        }
        if ($communicationType !== '') {
            $dbResFields = CCrmFieldMulti::GetList(array('ID' => 'asc'), array('ENTITY_ID' => $entityType, 'ELEMENT_ID' => $entityID, 'TYPE_ID' => $communicationType));
            while ($arField = $dbResFields->Fetch()) {
                if (empty($arField['VALUE'])) {
                    continue;
                }
                $comm = array('type' => $communicationType, 'value' => $arField['VALUE']);
                $companyItem['communications'][] = $comm;
            }
        }
        $companyData["{$entityType}_{$entityID}"] = $companyItem;
        if ($communicationType !== '') {
            $entityComms = CCrmActivity::GetCompanyCommunications($entityID, $communicationType, 50);
            foreach ($entityComms as &$entityComm) {
                CCrmActivity::PrepareCommunicationInfo($entityComm);
                $key = "{$entityComm['ENTITY_TYPE']}_{$entityComm['ENTITY_ID']}";
                if (!isset($companyData[$key])) {
                    $companyData[$key] = array('ownerEntityType' => 'COMPANY', 'ownerEntityId' => $entityID, 'entityType' => $entityComm['ENTITY_TYPE'], 'entityId' => $entityComm['ENTITY_ID'], 'entityTitle' => isset($entityComm['TITLE']) ? $entityComm['TITLE'] : '', 'entityDescription' => isset($entityComm['DESCRIPTION']) ? $entityComm['DESCRIPTION'] : '', 'tabId' => 'company', 'communications' => array());
                }
                $comm = array('type' => $entityComm['TYPE'], 'value' => $entityComm['VALUE']);
                $companyData[$key]['communications'][] = $comm;
            }
            unset($entityComm);
        }
        return array('DATA' => array('TABS' => array(array('id' => 'company', 'title' => GetMessage('CRM_COMMUNICATION_TAB_COMPANY'), 'active' => true, 'items' => array_values($companyData)))));
    } elseif ($entityType === 'CONTACT') {
        $contactData = array();
        $entity = CCrmContact::GetByID($entityID);
        if (!$entity) {
            return array('ERROR' => 'Invalid data');
        }
        $entityCompany = isset($entity['COMPANY_ID']) ? CCrmCompany::GetByID($entity['COMPANY_ID']) : null;
        $contactItem = array('ownerEntityType' => 'CONTACT', 'ownerEntityId' => $entityID, 'entityType' => 'CONTACT', 'entityId' => $entityID, 'entityTitle' => CUser::FormatName($fullNameFormat, array('LOGIN' => '', 'NAME' => $entity['NAME'], 'LAST_NAME' => $entity['LAST_NAME'], 'SECOND_NAME' => $entity['SECOND_NAME']), false, false), 'entityDescription' => $entityCompany && isset($entityCompany['TITLE']) ? $entityCompany['TITLE'] : '', 'tabId' => 'contact', 'communications' => array());
        // Try to load entity communications
        if (!CCrmActivity::CheckReadPermission(CCrmOwnerType::ResolveID($entityType), $entityID)) {
            return array('ERROR' => GetMessage('CRM_PERMISSION_DENIED'));
        }
        if ($communicationType !== '') {
            $dbResFields = CCrmFieldMulti::GetList(array('ID' => 'asc'), array('ENTITY_ID' => $entityType, 'ELEMENT_ID' => $entityID, 'TYPE_ID' => $communicationType));
            while ($arField = $dbResFields->Fetch()) {
                if (empty($arField['VALUE'])) {
                    continue;
                }
                $comm = array('type' => $communicationType, 'value' => $arField['VALUE']);
                $contactItem['communications'][] = $comm;
            }
        }
        $contactData["{$entityType}_{$entityID}"] = $contactItem;
        return array('DATA' => array('TABS' => array(array('id' => 'contact', 'title' => GetMessage('CRM_COMMUNICATION_TAB_CONTACT'), 'active' => true, 'items' => array_values($contactData)))));
    }
    return array('ERROR' => 'Invalid data');
}
Beispiel #2
0
             unset($item);
         }
     }
 }
 $contactID = isset($entity['UF_CONTACT_ID']) ? intval($entity['UF_CONTACT_ID']) : 0;
 $contact = $contactID > 0 ? CCrmContact::GetByID($contactID, true) : null;
 if (is_array($contact)) {
     $info = array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $contactID);
     if (CCrmActivity::PrepareCommunicationInfo($info, $contact)) {
         $contactKey = "CONTACT_{$contactID}";
         $item = array('OWNER_ID' => $contactID, 'OWNER_TYPE_ID' => CCrmOwnerType::Contact, 'TITLE' => $info['TITLE'], 'DESCRIPTION' => $info['DESCRIPTION'], 'IMAGE_URL' => CCrmMobileHelper::PrepareContactImageUrl($contact, array('WIDTH' => 40, 'HEIGHT' => 40)), 'COMMUNICATIONS' => array());
         if ($commType === 'PERSON') {
             $items[$contactKey] =& $item;
             unset($item);
         } else {
             $contactComms = CCrmActivity::PrepareCommunications('CONTACT', $contactID, $commType);
             foreach ($contactComms as &$comm) {
                 $item['COMMUNICATIONS'][] = array('TYPE' => $comm['TYPE'], 'VALUE' => $comm['VALUE']);
             }
             unset($comm);
             if (!empty($item['COMMUNICATIONS'])) {
                 $items[$contactKey] =& $item;
             }
             unset($item);
         }
     }
 }
 // Try to get previous communications
 $invoiceComms = CCrmActivity::GetCommunicationsByOwner('INVOICE', $ownerID, $commType);
 foreach ($invoiceComms as &$comm) {
     if (!CCrmActivity::PrepareCommunicationInfo($comm)) {