} } } unset($commDatum); // <-- Bindings & Communications if (empty($to)) { echo CUtil::PhpToJSObject(array('ERROR' => GetMessage('CRM_ACTIVITY_EMAIL_EMPTY_TO_FIELD'))); die; } elseif (!empty($arErrors)) { echo CUtil::PhpToJSObject(array('ERROR' => $arErrors)); die; } $subject = isset($data['subject']) ? strval($data['subject']) : ''; $message = isset($data['message']) ? strval($data['message']) : ''; if ($message !== '') { CCrmActivity::AddEmailSignature($message, CCrmContentType::BBCode); } if ($message === '') { $messageHtml = ''; } else { //Convert BBCODE to HTML $parser = new CTextParser(); $messageHtml = $parser->convertText($message); } $now = ConvertTimeStamp(time() + CTimeZone::GetOffset(), 'FULL', $siteID); if ($subject === '') { $subject = GetMessage('CRM_EMAIL_ACTION_DEFAULT_SUBJECT', array('#DATE#' => $now)); } $description = $message; $descriptionHtml = $messageHtml; //$description = preg_replace('/<br\s*[^>]*>/i', PHP_EOL, $message);
protected function innerAdd(&$fields, &$errors, array $params = null) { $ownerTypeID = isset($fields['OWNER_TYPE_ID']) ? intval($fields['OWNER_TYPE_ID']) : 0; $ownerID = isset($fields['OWNER_ID']) ? intval($fields['OWNER_ID']) : 0; $bindings = array(); if ($ownerTypeID > 0 && $ownerID > 0) { $bindings["{$ownerTypeID}_{$ownerID}"] = array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID); } $responsibleID = isset($fields['RESPONSIBLE_ID']) ? intval($fields['RESPONSIBLE_ID']) : 0; if ($responsibleID <= 0 && $ownerTypeID > 0 && $ownerID > 0) { $fields['RESPONSIBLE_ID'] = $responsibleID = CCrmOwnerType::GetResponsibleID($ownerTypeID, $ownerID); } if ($responsibleID <= 0) { $responsibleID = CCrmSecurityHelper::GetCurrentUserID(); } if ($responsibleID <= 0) { $errors[] = 'The field RESPONSIBLE_ID is not defined or invalid.'; return false; } $typeID = isset($fields['TYPE_ID']) ? intval($fields['TYPE_ID']) : CCrmActivityType::Undefined; if (!CCrmActivityType::IsDefined($typeID)) { $errors[] = 'The field TYPE_ID is not defined or invalid.'; return false; } if (!in_array($typeID, array(CCrmActivityType::Call, CCrmActivityType::Meeting, CCrmActivityType::Email), true)) { $errors[] = 'The activity type "' . CCrmActivityType::ResolveDescription($typeID) . ' is not supported in current context".'; return false; } $description = isset($fields['DESCRIPTION']) ? $fields['DESCRIPTION'] : ''; $descriptionType = isset($fields['DESCRIPTION_TYPE']) ? intval($fields['DESCRIPTION_TYPE']) : CCrmContentType::PlainText; if ($description !== '' && CCrmActivity::AddEmailSignature($description, $descriptionType)) { $fields['DESCRIPTION'] = $description; } $direction = isset($fields['DIRECTION']) ? intval($fields['DIRECTION']) : CCrmActivityDirection::Undefined; $completed = isset($fields['COMPLETED']) && strtoupper($fields['COMPLETED']) === 'Y'; $communications = isset($fields['COMMUNICATIONS']) && is_array($fields['COMMUNICATIONS']) ? $fields['COMMUNICATIONS'] : array(); $this->prepareCommunications($ownerTypeID, $ownerID, $typeID, $communications, $bindings); if (empty($communications)) { $errors[] = 'The field COMMUNICATIONS is not defined or invalid.'; return false; } if (($typeID === CCrmActivityType::Call || $typeID === CCrmActivityType::Meeting) && count($communications) > 1) { $errors[] = 'The only one communication is allowed for activity of specified type.'; return false; } if (empty($bindings)) { $errors[] = 'Could not build binding. Please ensure that owner info and communications are defined correctly.'; return false; } foreach ($bindings as &$binding) { if (!CCrmActivity::CheckUpdatePermission($binding['OWNER_TYPE_ID'], $binding['OWNER_ID'])) { $errors[] = 'Access denied.'; return false; } } unset($binding); $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(); foreach ($webdavElements as &$element) { $elementID = isset($element['ELEMENT_ID']) ? intval($element['ELEMENT_ID']) : 0; if ($elementID > 0) { $fields['STORAGE_ELEMENT_IDS'][] = $elementID; } } unset($element); } elseif ($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); } if (!($ID = CCrmActivity::Add($fields))) { $errors[] = CCrmActivity::GetLastErrorMessage(); return false; } CCrmActivity::SaveCommunications($ID, $communications, $fields, false, false); if ($completed && $typeID === CCrmActivityType::Email && $direction === CCrmActivityDirection::Outgoing) { $sendErrors = array(); if (!CCrmActivityEmailSender::TrySendEmail($ID, $fields, $sendErrors)) { foreach ($sendErrors as &$error) { $code = $error['CODE']; if ($code === CCrmActivityEmailSender::ERR_CANT_LOAD_SUBSCRIBE) { $errors[] = 'Email send error. Failed to load module "subscribe".'; } elseif ($code === CCrmActivityEmailSender::ERR_INVALID_DATA) { $errors[] = 'Email send error. Invalid data.'; } elseif ($code === CCrmActivityEmailSender::ERR_INVALID_EMAIL) { $errors[] = 'Email send error. Invalid email is specified.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_FIND_EMAIL_FROM) { $errors[] = 'Email send error. "From" is not found.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_FIND_EMAIL_TO) { $errors[] = 'Email send error. "To" is not found.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_ADD_POSTING) { $errors[] = 'Email send error. Failed to add posting. Please see details below.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_SAVE_POSTING_FILE) { $errors[] = 'Email send error. Failed to save posting file. Please see details below.'; } elseif ($code === CCrmActivityEmailSender::ERR_CANT_UPDATE_ACTIVITY) { $errors[] = 'Email send error. Failed to update activity.'; } else { $errors[] = 'Email send error. General error.'; } $msg = isset($error['MESSAGE']) ? $error['MESSAGE'] : ''; if ($msg !== '') { $errors[] = $msg; } } unset($error); return false; } } return $ID; }