Beispiel #1
0
$bindings = isset($componentParams['BINDINGS']) && is_array($componentParams['BINDINGS']) ? $componentParams['BINDINGS'] : array();
if (empty($bindings)) {
    die;
}
$binding = $bindings[0];
if (!is_array($binding)) {
    die;
}
$ownerTypeName = isset($binding['TYPE_NAME']) ? $binding['TYPE_NAME'] : '';
$ownerTypeID = CCrmOwnerType::ResolveID($ownerTypeName);
$ownerID = isset($binding['ID']) ? (int) $binding['ID'] : 0;
if ($ownerTypeID === CCrmOwnerType::Undefined || $ownerID <= 0) {
    die;
}
$userPermissions = CCrmPerms::GetCurrentUserPermissions();
if (!CCrmActivity::CheckReadPermission($ownerTypeID, $ownerID, $userPermissions)) {
    die;
}
if (count($bindings) > 1) {
    array_splice($bindings, 1);
}
$componentParams['BINDINGS'] = $componentData['params']['BINDINGS'] = $bindings;
//Sanitaizing of Permission Type
$permissionType = isset($componentParams['PERMISSION_TYPE']) ? strtoupper($componentParams['PERMISSION_TYPE']) : 'READ';
if ($permissionType !== 'READ' && !CCrmActivity::CheckUpdatePermission($ownerTypeID, $ownerID, $userPermissions)) {
    $componentParams['PERMISSION_TYPE'] = $componentData['params']['PERMISSION_TYPE'] = 'READ';
}
//For custom reload with params
$ajaxLoaderParams = array('url' => '', 'method' => 'POST', 'dataType' => 'ajax', 'data' => array('PARAMS' => $componentData));
global $APPLICATION;
Header('Content-Type: text/html; charset=' . LANG_CHARSET);
Beispiel #2
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 #3
0
 private static function SynchronizeLiveFeedEvent($activityID, $params)
 {
     if (!is_array($params)) {
         $params = array();
     }
     $processBindings = isset($params['PROCESS_BINDINGS']) ? (bool) $params['PROCESS_BINDINGS'] : false;
     $bindings = isset($params['BINDINGS']) && is_array($params['BINDINGS']) ? $params['BINDINGS'] : array();
     $hasBindings = !empty($bindings);
     if ($processBindings) {
         CCrmSonetRelation::UnRegisterRelationsByEntity(CCrmOwnerType::Activity, $activityID, array('QUICK' => $hasBindings));
     }
     $arOwners = $hasBindings ? $bindings : self::GetBindings($activityID);
     $slEntities = CCrmLiveFeed::GetLogEvents(array(), array('ENTITY_TYPE_ID' => CCrmOwnerType::Activity, 'ENTITY_ID' => $activityID), array('ID', 'EVENT_ID'));
     if (empty($slEntities)) {
         return false;
     }
     global $DB;
     foreach ($slEntities as &$slEntity) {
         $slID = intval($slEntity['ID']);
         $slEventType = $slEntity['EVENT_ID'];
         if (isset($params['REFRESH_DATE']) ? (bool) $params['REFRESH_DATE'] : false) {
             //Update LOG_UPDATE for force event to rise in global feed
             //Update LOG_DATE for force event to rise in entity feed
             CCrmLiveFeed::UpdateLogEvent($slID, array('=LOG_UPDATE' => $DB->CurrentTimeFunction(), '=LOG_DATE' => $DB->CurrentTimeFunction()));
         } else {
             //HACK: FAKE UPDATE FOR INVALIDATE CACHE
             CCrmLiveFeed::UpdateLogEvent($slID, array('ENTITY_TYPE_ID' => CCrmOwnerType::Activity, 'ENTITY_ID' => $activityID));
         }
         /*
         					'START_RESPONSIBLE_ID' => $arPrevEntity['RESPONSIBLE_ID'],
         					'FINAL_RESPONSIBLE_ID' => $responsibleID
         */
         $userID = intval($params['EDITOR_ID']) > 0 ? $params['EDITOR_ID'] : CCrmSecurityHelper::GetCurrentUserID();
         if (intval($params['START_RESPONSIBLE_ID']) != intval($params['FINAL_RESPONSIBLE_ID']) && CModule::IncludeModule("im")) {
             switch ($params['TYPE_ID']) {
                 case CCrmActivityType::Call:
                     $type = 'CALL';
                     break;
                 case CCrmActivityType::Meeting:
                     $type = 'MEETING';
                     break;
                 default:
                     $type = false;
             }
             if ($type) {
                 $url = "/crm/stream/?log_id=#log_id#";
                 $url = str_replace(array("#log_id#"), array($slID), $url);
                 $serverName = (CMain::IsHTTPS() ? "https" : "http") . "://" . (defined("SITE_SERVER_NAME") && strlen(SITE_SERVER_NAME) > 0 ? SITE_SERVER_NAME : COption::GetOptionString("main", "server_name", ""));
                 $arMessageFields = array("MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "FROM_USER_ID" => $userID, "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "crm", "LOG_ID" => $slID, "NOTIFY_EVENT" => "activity_add", "NOTIFY_TAG" => "CRM|ACTIVITY|" . $activityID);
                 if (intval($params['START_RESPONSIBLE_ID']) != $userID) {
                     $bHasPermissions = false;
                     $perms = CCrmPerms::GetUserPermissions($params['START_RESPONSIBLE_ID']);
                     foreach ($arOwners as $arOwner) {
                         if (CCrmActivity::CheckReadPermission($arOwner["OWNER_TYPE_ID"], $arOwner["OWNER_ID"], $perms)) {
                             $bHasPermissions = true;
                             break;
                         }
                     }
                     if ($bHasPermissions) {
                         $arMessageFields["TO_USER_ID"] = $params['START_RESPONSIBLE_ID'];
                         $arMessageFields["NOTIFY_MESSAGE"] = GetMessage("CRM_ACTIVITY_" . $type . "_NOT_RESPONSIBLE_IM_NOTIFY", array("#title#" => '<a href="' . $url . '">' . htmlspecialcharsbx($params['SUBJECT']) . '</a>'));
                         $arMessageFields["NOTIFY_MESSAGE_OUT"] = GetMessage("CRM_ACTIVITY_" . $type . "_NOT_RESPONSIBLE_IM_NOTIFY", array("#title#" => htmlspecialcharsbx($params['SUBJECT']))) . " (" . $serverName . $url . ")";
                         CIMNotify::Add($arMessageFields);
                     }
                 }
                 if (intval($params['FINAL_RESPONSIBLE_ID']) != $userID) {
                     $bHasPermissions = false;
                     $perms = CCrmPerms::GetUserPermissions($params['FINAL_RESPONSIBLE_ID']);
                     foreach ($arOwners as $arOwner) {
                         if (CCrmActivity::CheckReadPermission($arOwner["OWNER_TYPE_ID"], $arOwner["OWNER_ID"], $perms)) {
                             $bHasPermissions = true;
                             break;
                         }
                     }
                     if ($bHasPermissions) {
                         $arMessageFields["TO_USER_ID"] = $params['FINAL_RESPONSIBLE_ID'];
                         $arMessageFields["NOTIFY_MESSAGE"] = GetMessage("CRM_ACTIVITY_" . $type . "_RESPONSIBLE_IM_NOTIFY", array("#title#" => '<a href="' . $url . '">' . htmlspecialcharsbx($params['SUBJECT']) . '</a>'));
                         $arMessageFields["NOTIFY_MESSAGE_OUT"] = GetMessage("CRM_ACTIVITY_" . $type . "_RESPONSIBLE_IM_NOTIFY", array("#title#" => htmlspecialcharsbx($params['SUBJECT']))) . " (" . $serverName . $url . ")";
                         CIMNotify::Add($arMessageFields);
                     }
                 }
             }
         }
         if ($processBindings && $hasBindings) {
             CCrmSonetRelation::RegisterRelationBundle($slID, $slEventType, CCrmOwnerType::Activity, $activityID, $bindings, array('ENTITY_TYPE_ID_KEY' => 'OWNER_TYPE_ID', 'ENTITY_ID_KEY' => 'OWNER_ID', 'TYPE_ID' => CCrmSonetRelationType::Ownership));
         }
     }
     unset($slEntity);
     return true;
 }
Beispiel #4
0
                $dbMultiFields = CCrmFieldMulti::GetList(array('ID' => 'asc'), array('ENTITY_ID' => CCrmOwnerType::ContactName, 'ELEMENT_ID' => $ownerID, 'TYPE_ID' => $commType));
                while ($multiField = $dbMultiFields->Fetch()) {
                    if (empty($multiField['VALUE'])) {
                        continue;
                    }
                    $item['COMMUNICATIONS'][] = array('TYPE' => $commType, 'VALUE' => $multiField['VALUE']);
                }
                if (!empty($item['COMMUNICATIONS'])) {
                    $items["CONTACT_{$ownerID}"] =& $item;
                }
                unset($item);
            }
        }
    }
} elseif ($ownerTypeID === CCrmOwnerType::Invoice) {
    if (!CCrmActivity::CheckReadPermission(CCrmOwnerType::Invoice, $ownerID)) {
        ShowError(GetMessage('CRM_PERMISSION_DENIED'));
        return;
    }
    if ($enableSearch && $needle !== '') {
        $results = CCrmActivity::FindContactCommunications($needle, $commType !== 'PERSON' ? $commType : '', 10);
        if ($commType !== '') {
            //If communication type defined add companies communications
            $results = array_merge($results, CCrmActivity::FindCompanyCommunications($needle, $commType !== 'PERSON' ? $commType : '', 10));
        }
        $results = array_merge($results, CCrmActivity::FindLeadCommunications($needle, $commType !== 'PERSON' ? $commType : '', 10));
        foreach ($results as &$result) {
            $entityID = intval($result['ENTITY_ID']);
            $entityTypeID = intval($result['ENTITY_TYPE_ID']);
            $entityTypeName = CCrmOwnerType::ResolveName($entityTypeID);
            if (!CCrmAuthorizationHelper::CheckReadPermission($entityTypeName, $entityID, $userPerms)) {