Esempio n. 1
0
 public static function TrySendEmail($ID, &$arFields, &$arErrors)
 {
     global $APPLICATION;
     if (!CModule::IncludeModule('subscribe')) {
         $arErrors[] = array('CODE' => self::ERR_CANT_LOAD_SUBSCRIBE);
         return false;
     }
     $ID = intval($ID);
     if ($ID <= 0 && isset($arFields['ID'])) {
         $ID = intval($arFields['ID']);
     }
     if ($ID <= 0 || !is_array($arFields)) {
         $arErrors[] = array('CODE' => self::ERR_INVALID_DATA);
         return false;
     }
     $typeID = isset($arFields['TYPE_ID']) ? intval($arFields['TYPE_ID']) : CCrmActivityType::Undefined;
     if ($typeID !== CCrmActivityType::Email) {
         $arErrors[] = array('CODE' => self::ERR_INVALID_DATA);
         return false;
     }
     $urn = CCrmActivity::PrepareUrn($arFields);
     if (!($urn !== '' && CCrmActivity::Update($ID, array('URN' => $urn), false, false))) {
         $arErrors[] = array('CODE' => self::ERR_CANT_UPDATE_ACTIVITY);
         return false;
     }
     $settings = isset($arFields['SETTINGS']) && is_array($arFields['SETTINGS']) ? $arFields['SETTINGS'] : array();
     // Creating Email -->
     $crmEmail = CCrmMailHelper::ExtractEmail(COption::GetOptionString('crm', 'mail', ''));
     $from = isset($settings['MESSAGE_FROM']) ? trim(strval($settings['MESSAGE_FROM'])) : '';
     if ($from === '') {
         if ($crmEmail !== '') {
             $from = $crmEmail;
         } else {
             $arErrors[] = array('CODE' => self::ERR_CANT_FIND_EMAIL_FROM);
         }
     } elseif (!check_email($from)) {
         $arErrors[] = array('CODE' => self::ERR_INVALID_EMAIL, 'DATA' => array('EMAIL' => $from));
     }
     //Save user email in settings -->
     if ($from !== CUserOptions::GetOption('crm', 'activity_email_addresser', '')) {
         CUserOptions::SetOption('crm', 'activity_email_addresser', $from);
     }
     //<-- Save user email in settings
     $to = array();
     $commData = isset($arFields['COMMUNICATIONS']) ? $arFields['COMMUNICATIONS'] : array();
     foreach ($commData as &$commDatum) {
         $commType = isset($commDatum['TYPE']) ? strtoupper(strval($commDatum['TYPE'])) : '';
         $commValue = isset($commDatum['VALUE']) ? strval($commDatum['VALUE']) : '';
         if ($commType !== 'EMAIL' || $commValue === '') {
             continue;
         }
         if (!check_email($commValue)) {
             $arErrors[] = array('CODE' => self::ERR_INVALID_EMAIL, 'DATA' => array('EMAIL' => $commValue));
             continue;
         }
         $to[] = strtolower(trim($commValue));
     }
     unset($commDatum);
     if (count($to) == 0) {
         $arErrors[] = array('CODE' => self::ERR_CANT_FIND_EMAIL_TO);
     }
     if (!empty($arErrors)) {
         return false;
     }
     // Try to resolve posting charset -->
     $postingCharset = '';
     $siteCharset = defined('LANG_CHARSET') ? LANG_CHARSET : (defined('SITE_CHARSET') ? SITE_CHARSET : 'windows-1251');
     $arSupportedCharset = explode(',', COption::GetOptionString('subscribe', 'posting_charset'));
     if (count($arSupportedCharset) === 0) {
         $postingCharset = $siteCharset;
     } else {
         foreach ($arSupportedCharset as $curCharset) {
             if (strcasecmp($curCharset, $siteCharset) === 0) {
                 $postingCharset = $curCharset;
                 break;
             }
         }
         if ($postingCharset === '') {
             $postingCharset = $arSupportedCharset[0];
         }
     }
     //<-- Try to resolve posting charset
     $subject = isset($arFields['SUBJECT']) ? $arFields['SUBJECT'] : '';
     $description = isset($arFields['DESCRIPTION']) ? $arFields['DESCRIPTION'] : '';
     $descriptionType = isset($arFields['DESCRIPTION_TYPE']) ? intval($arFields['DESCRIPTION_TYPE']) : CCrmContentType::PlainText;
     $descriptionHtml = '';
     if ($descriptionType === CCrmContentType::Html) {
         $descriptionHtml = $description;
     } elseif ($descriptionType === CCrmContentType::BBCode) {
         $parser = new CTextParser();
         $descriptionHtml = $parser->convertText($description);
     } elseif ($descriptionType === CCrmContentType::PlainText) {
         $descriptionHtml = htmlspecialcharsbx($description);
     }
     $postingData = array('STATUS' => 'D', 'FROM_FIELD' => $from, 'TO_FIELD' => $from, 'BCC_FIELD' => implode(',', $to), 'SUBJECT' => $subject, 'BODY_TYPE' => 'html', 'BODY' => $descriptionHtml, 'DIRECT_SEND' => 'Y', 'SUBSCR_FORMAT' => 'html', 'CHARSET' => $postingCharset);
     CCrmActivity::InjectUrnInMessage($postingData, $urn, CCrmEMailCodeAllocation::GetCurrent());
     $posting = new CPosting();
     $postingID = $posting->Add($postingData);
     if ($postingID === false) {
         $arErrors[] = array('CODE' => self::ERR_CANT_ADD_POSTING, 'MESSAGE' => $posting->LAST_ERROR);
         return false;
     }
     $arUpdateFields = array('COMPLETED' => 'Y', 'ASSOCIATED_ENTITY_ID' => $postingID, 'SETTINGS' => $settings);
     $fromEmail = strtolower(trim(CCrmMailHelper::ExtractEmail($from)));
     if ($crmEmail !== '' && $fromEmail !== $crmEmail) {
         $arUpdateFields['SETTINGS']['MESSAGE_HEADERS'] = array('Reply-To' => "<{$fromEmail}>, <{$crmEmail}>");
     }
     $arUpdateFields['SETTINGS']['IS_MESSAGE_SENT'] = true;
     if (!CCrmActivity::Update($ID, $arUpdateFields, false, false)) {
         $arErrors[] = array('CODE' => self::ERR_CANT_UPDATE_ACTIVITY);
         return false;
     }
     // <-- Creating Email
     // Attaching files -->
     $storageTypeID = isset($arFields['STORAGE_TYPE_ID']) ? intval($arFields['STORAGE_TYPE_ID']) : StorageType::Undefined;
     $storageElementsID = isset($arFields['STORAGE_ELEMENT_IDS']) && is_array($arFields['STORAGE_ELEMENT_IDS']) ? $arFields['STORAGE_ELEMENT_IDS'] : array();
     $arRawFiles = StorageManager::makeFileArray($storageElementsID, $storageTypeID);
     foreach ($arRawFiles as &$arRawFile) {
         if (!$posting->SaveFile($postingID, $arRawFile)) {
             $arErrors[] = array('CODE' => self::ERR_CANT_SAVE_POSTING_FILE, 'MESSAGE' => $posting->LAST_ERROR);
             return false;
         }
     }
     unset($arRawFile);
     // <-- Attaching files
     // Sending Email -->
     if ($posting->ChangeStatus($postingID, 'P')) {
         $rsAgents = CAgent::GetList(array('ID' => 'DESC'), array('MODULE_ID' => 'subscribe', 'NAME' => 'CPosting::AutoSend(' . $postingID . ',%'));
         if (!$rsAgents->Fetch()) {
             CAgent::AddAgent('CPosting::AutoSend(' . $postingID . ',true);', 'subscribe', 'N', 0);
         }
     }
     // Try add event to entity
     $CCrmEvent = new CCrmEvent();
     $ownerID = isset($arFields['OWNER_ID']) ? intval($arFields['OWNER_ID']) : 0;
     $ownerTypeID = isset($arFields['OWNER_TYPE_ID']) ? intval($arFields['OWNER_TYPE_ID']) : 0;
     if ($ownerID > 0 && $ownerTypeID > 0) {
         $eventText = '';
         $eventText .= GetMessage('CRM_ACTIVITY_EMAIL_SUBJECT') . ': ' . $subject . "\n\r";
         $eventText .= GetMessage('CRM_ACTIVITY_EMAIL_FROM') . ': ' . $from . "\n\r";
         $eventText .= GetMessage('CRM_ACTIVITY_EMAIL_TO') . ': ' . implode(',', $to) . "\n\r\n\r";
         $eventText .= $description;
         // Register event only for owner
         $CCrmEvent->Add(array('ENTITY' => array($ownerID => array('ENTITY_TYPE' => CCrmOwnerType::ResolveName($ownerTypeID), 'ENTITY_ID' => $ownerID)), 'EVENT_ID' => 'MESSAGE', 'EVENT_TEXT_1' => $eventText, 'FILES' => $arRawFiles));
     }
     // <-- Sending Email
     return true;
 }
Esempio n. 2
0
 public static function AttachRecordToCall($params)
 {
     if (!CModule::IncludeModule('crm')) {
         return false;
     }
     CVoxImplantHistory::WriteToLog($params, 'CRM ATTACH RECORD TO CALL');
     if ($params['CALL_WEBDAV_ID'] > 0) {
         $activityId = CCrmActivity::GetIDByOrigin('VI_' . $params['CALL_ID']);
         if ($activityId) {
             $arFields['STORAGE_TYPE_ID'] = CCrmActivity::GetDefaultStorageTypeID();
             $arFields['STORAGE_ELEMENT_IDS'] = array($params['CALL_WEBDAV_ID']);
             CCrmActivity::Update($activityId, $arFields, false);
         }
     }
     return true;
 }
Esempio n. 3
0
 protected function innerUpdate($ID, &$fields, &$errors, array $params = null)
 {
     $currentFields = CCrmActivity::GetByID($ID);
     CCrmActivity::PrepareStorageElementIDs($currentFields);
     if (!is_array($currentFields)) {
         $errors[] = 'Activity is not found.';
         return false;
     }
     $typeID = intval($currentFields['TYPE_ID']);
     $currentOwnerID = intval($currentFields['OWNER_ID']);
     $currentOwnerTypeID = intval($currentFields['OWNER_TYPE_ID']);
     if (!CCrmActivity::CheckUpdatePermission($currentOwnerTypeID, $currentOwnerID)) {
         $errors[] = 'Access denied.';
         return false;
     }
     $ownerID = isset($fields['OWNER_ID']) ? intval($fields['OWNER_ID']) : 0;
     if ($ownerID <= 0) {
         $ownerID = $currentOwnerID;
     }
     $ownerTypeID = isset($fields['OWNER_TYPE_ID']) ? intval($fields['OWNER_TYPE_ID']) : 0;
     if ($ownerTypeID <= 0) {
         $ownerTypeID = $currentOwnerTypeID;
     }
     if (($ownerTypeID !== $currentOwnerTypeID || $ownerID !== $currentOwnerID) && !CCrmActivity::CheckUpdatePermission($ownerTypeID, $ownerID)) {
         $errors[] = 'Access denied.';
         return false;
     }
     $communications = isset($fields['COMMUNICATIONS']) && is_array($fields['COMMUNICATIONS']) ? $fields['COMMUNICATIONS'] : null;
     if (is_array($communications)) {
         $bindings = array();
         if ($ownerTypeID > 0 && $ownerID > 0) {
             $bindings["{$ownerTypeID}_{$ownerID}"] = array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID);
         }
         $this->prepareCommunications($ownerTypeID, $ownerID, $typeID, $communications, $bindings);
         if (empty($communications)) {
             $errors[] = 'The field COMMUNICATIONS is not defined or invalid.';
             return false;
         }
         $fields['BINDINGS'] = array_values($bindings);
         $fields['COMMUNICATIONS'] = $communications;
     }
     $storageTypeID = $fields['STORAGE_TYPE_ID'] = CCrmActivity::GetDefaultStorageTypeID();
     $fields['STORAGE_ELEMENT_IDS'] = array();
     if ($storageTypeID === StorageType::WebDav) {
         $webdavElements = isset($fields['WEBDAV_ELEMENTS']) && is_array($fields['WEBDAV_ELEMENTS']) ? $fields['WEBDAV_ELEMENTS'] : array();
         $prevStorageElementIDs = isset($currentFields['STORAGE_ELEMENT_IDS']) ? $currentFields['STORAGE_ELEMENT_IDS'] : array();
         $oldStorageElementIDs = array();
         foreach ($webdavElements as &$element) {
             $elementID = isset($element['ELEMENT_ID']) ? intval($element['ELEMENT_ID']) : 0;
             if ($elementID > 0) {
                 $fields['STORAGE_ELEMENT_IDS'][] = $elementID;
             }
             $oldElementID = isset($element['OLD_ELEMENT_ID']) ? intval($element['OLD_ELEMENT_ID']) : 0;
             if ($oldElementID > 0 && ($elementID > 0 || isset($element['DELETE']) && $element['DELETE'] === true)) {
                 if (in_array($oldElementID, $prevStorageElementIDs)) {
                     $oldStorageElementIDs[] = $oldElementID;
                 }
             }
         }
         unset($element);
     } else {
         if ($storageTypeID === StorageType::Disk) {
             $diskFiles = isset($fields['FILES']) && is_array($fields['FILES']) ? $fields['FILES'] : array();
             if (empty($diskFiles)) {
                 //For backward compatibility only
                 $diskFiles = isset($fields['WEBDAV_ELEMENTS']) && is_array($fields['WEBDAV_ELEMENTS']) ? $fields['WEBDAV_ELEMENTS'] : array();
             }
             foreach ($diskFiles as &$fileInfo) {
                 $fileID = isset($fileInfo['FILE_ID']) ? (int) $fileInfo['FILE_ID'] : 0;
                 if ($fileID > 0) {
                     $fields['STORAGE_ELEMENT_IDS'][] = $fileID;
                 }
             }
             unset($fileInfo);
         }
     }
     $result = CCrmActivity::Update($ID, $fields, false, true, array());
     if ($result === false) {
         $errors[] = CCrmActivity::GetLastErrorMessage();
     } else {
         if (is_array($communications)) {
             CCrmActivity::SaveCommunications($ID, $communications, $fields, false, false);
         }
         if (!empty($oldStorageElementIDs)) {
             $webdavIBlock = $this->prepareWebDavIBlock();
             foreach ($oldStorageElementIDs as $elementID) {
                 $webdavIBlock->Delete(array('element_id' => $elementID));
             }
         }
     }
     return $result;
 }
Esempio n. 4
0
         }
         if (!$posting->SaveFile($postingID, $arRawFile)) {
             $arErrors[] = GetMessage('CRM_ACTIVITY_COULD_NOT_SAVE_POSTING_FILE', array('#FILE_NAME#' => $arRawFile['ORIGINAL_NAME']));
             $arErrors[] = $posting->LAST_ERROR;
             break;
         }
     }
     unset($arRawFile);
     // <-- Attaching files
     if (empty($arErrors)) {
         $arUpdateFields = array('ASSOCIATED_ENTITY_ID' => $postingID);
         $fromEmail = strtolower(trim(CCrmMailHelper::ExtractEmail($from)));
         if ($crmEmail !== '' && $fromEmail !== $crmEmail) {
             $arUpdateFields['SETTINGS'] = array('MESSAGE_HEADERS' => array('Reply-To' => "<{$fromEmail}>, <{$crmEmail}>"));
         }
         CCrmActivity::Update($ID, $arUpdateFields, false, false);
     }
 }
 // <-- Creating Email
 if (!empty($arErrors)) {
     if ($isNew) {
         $arErrors[] = GetMessage('CRM_ACTIVITY_EMAIL_CREATION_CANCELED');
         CCrmActivity::Delete($ID);
     }
     echo CUtil::PhpToJSObject(array('ERROR' => $arErrors));
     die;
 }
 // Sending Email -->
 if ($posting->ChangeStatus($postingID, 'P')) {
     $rsAgents = CAgent::GetList(array('ID' => 'DESC'), array('MODULE_ID' => 'subscribe', 'NAME' => 'CPosting::AutoSend(' . $postingID . ',%'));
     if (!$rsAgents->Fetch()) {
Esempio n. 5
0
                $arActivity['COMPLETED'] = $completed;
                CCrmActivity::Update($arActivity['ID'], $arActivity);
            }
        } elseif (!empty($arTargetItemID)) {
            $arActionFilter = $arFilter;
            $arActionFilter['@ID'] = $arTargetItemID;
            $dbResult = CCrmActivity::GetList(array(), $arActionFilter, false, false, array('ID', 'OWNER_TYPE_ID', 'OWNER_ID', 'TYPE_ID', 'ASSOCIATED_ENTITY_ID', 'COMPLETED'));
            while ($arActivity = $dbResult->Fetch()) {
                if ($arActivity['COMPLETED'] === $completed) {
                    continue;
                }
                if (!CCrmActivity::CheckCompletePermission($arActivity['OWNER_TYPE_ID'], $arActivity['OWNER_ID'], $currentUserPermissions, array('FIELDS' => $arActivity))) {
                    continue;
                }
                $arActivity['COMPLETED'] = $completed;
                CCrmActivity::Update($arActivity['ID'], $arActivity);
            }
        }
    }
    if (!isset($_POST['AJAX_CALL'])) {
        LocalRedirect($APPLICATION->GetCurPageParam(urlencode($tabParamName) . '=' . urlencode($arResult['TAB_ID']), array($tabParamName)));
    }
    //	else
    //	{
    //		$arResult['AJAX_RELOAD_ITEMS'] = true;
    //	}
}
foreach ($arFilter as $fieldID => $values) {
    if ($fieldID !== 'TYPE_ID') {
        continue;
    }