- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
- Класс: \CCrmActivityRestProxy
- Вызов: CCrmActivityRestProxy::innerAdd
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
);
}
if(!isset($fields['SUBJECT']) || trim($fields['SUBJECT']) === '')
{
$errors[] = 'The field SUBJECT is not defined or empty.';
return false;
}
$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;
}
if (isset($fields['PROVIDER_ID']) && empty($fields['TYPE_ID']))
$fields['TYPE_ID'] = CCrmActivityType::Provider;
$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 ($typeID === CCrmActivityType::Provider && ($provider = CCrmActivity::GetActivityProvider($fields)) === null)
{
$errors[] = 'The custom activity without provider is not supported in current context.';
return false;
}
if(!in_array($typeID, array(CCrmActivityType::Call, CCrmActivityType::Meeting, CCrmActivityType::Email, CCrmActivityType::Provider), true))
{
$errors[] = 'The activity type "'.CCrmActivityType::ResolveDescription($typeID).' is not supported in current context".';
return false;
}
$isRestActivity = isset($fields['PROVIDER_ID']) && $fields['PROVIDER_ID'] === \Bitrix\Crm\Activity\Provider\RestApp::getId();
if (($fields['PROVIDER_ID'] ?? null) === \Bitrix\Crm\Activity\Provider\ConfigurableRestApp::getId())
{
$errors[] = 'Use crm.activity.configurable.add for this activity provider';
return false;
}
if ($isRestActivity)
{
$clientId = $this->getServer()->getClientId();
$application = $clientId ? \Bitrix\Rest\AppTable::getByClientId($clientId) : null;
if (!$application)
{
$errors[] = 'Application context required.';
return false;
}
$fields['ASSOCIATED_ENTITY_ID'] = $application['ID'];
}
$description = $fields['DESCRIPTION'] ?? '';
$descriptionType = isset($fields['DESCRIPTION_TYPE']) ? intval($fields['DESCRIPTION_TYPE']) : CCrmContentType::PlainText;
if($description !== '' && !$isRestActivity && CCrmActivity::AddEmailSignature($description, $descriptionType))
{
$fields['DESCRIPTION'] = $description;
}
$direction = isset($fields['DIRECTION']) ? intval($fields['DIRECTION']) : CCrmActivityDirection::Undefined;
$completed = isset($fields['COMPLETED']) && mb_strtoupper($fields['COMPLETED']) === 'Y';
$communications = isset($fields['COMMUNICATIONS']) && is_array($fields['COMMUNICATIONS'])
? $fields['COMMUNICATIONS'] : array();
$this->internalizeCommunications($ownerTypeID, $ownerID, $typeID, $communications, $bindings);
if(empty($communications) && $typeID !== CCrmActivityType::Provider)
{
$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)
{
$bindingOwnerTypeName = CCrmOwnerType::ResolveName($binding['OWNER_TYPE_ID']);
if($bindingOwnerTypeName === '')
{
$bindingOwnerTypeName = "[{$binding['OWNER_TYPE_ID']}]";
}
$factory = \Bitrix\Crm\Service\Container::getInstance()->getFactory($binding['OWNER_TYPE_ID']);
if($factory === null)
{
$errors[] = "Entity type '{$bindingOwnerTypeName}' is not supported in current context";
return false;
}
if(!$factory->getItem($binding['OWNER_ID']))
{
$errors[] = "Could not find '{$bindingOwnerTypeName}' with ID: {$binding['OWNER_ID']}";
return false;
}
if(!CCrmActivity::CheckUpdatePermission($binding['OWNER_TYPE_ID'], $binding['OWNER_ID']))
{
$errors[] = 'Access denied.';
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();
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;
}
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 = $error['MESSAGE'] ?? '';
if($msg !== '')
{
$errors[] = $msg;
}
}
unset($error);
return false;
}
addEventToStatFile(
'crm',
'send_email_message',
sprintf('rest_%s', $this->getServer()->getClientId() ?: 'undefined'),
trim(trim($fields['SETTINGS']['MESSAGE_HEADERS']['Message-Id']), '<>')
);
}
return $ID;
}