- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
- Класс: \CCrmActivityRestProxy
- Вызов: CCrmActivityRestProxy::innerUpdate
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;
}
if ($currentFields['PROVIDER_ID'] === \Bitrix\Crm\Activity\Provider\RestApp::getId())
{
$clientId = $this->getServer()->getClientId();
$application = $clientId ? \Bitrix\Rest\AppTable::getByClientId($clientId) : null;
if (!$application)
{
$errors[] = 'Application context required.';
return false;
}
if ((int)$currentFields['ASSOCIATED_ENTITY_ID'] !== $application['ID'])
{
$errors[] = 'Access denied.';
return false;
}
}
if (($currentFields['PROVIDER_ID'] ?? null) === \Bitrix\Crm\Activity\Provider\ConfigurableRestApp::getId())
{
$errors[] = 'Use crm.activity.configurable.update for this activity provider';
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->internalizeCommunications($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();
unset($fields['STORAGE_ELEMENT_IDS']);
if($storageTypeID === StorageType::WebDav)
{
$webdavElements = isset($fields['WEBDAV_ELEMENTS']) && is_array($fields['WEBDAV_ELEMENTS'])
? $fields['WEBDAV_ELEMENTS'] : array();
$prevStorageElementIDs = $currentFields['STORAGE_ELEMENT_IDS'] ?? array();
$oldStorageElementIDs = array();
foreach($webdavElements as &$element)
{
$elementID = isset($element['ELEMENT_ID']) ? intval($element['ELEMENT_ID']) : 0;
if($elementID > 0)
{
if(!isset($fields['STORAGE_ELEMENT_IDS']))
{
$fields['STORAGE_ELEMENT_IDS'] = array();
}
$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)
{
if(!isset($fields['STORAGE_ELEMENT_IDS']))
{
$fields['STORAGE_ELEMENT_IDS'] = array();
}
$fields['STORAGE_ELEMENT_IDS'][] = $fileID;
}
}
unset($fileInfo);
}
$regEvent = true;
if(is_array($params) && isset($params['REGISTER_HISTORY_EVENT']))
{
$regEvent = mb_strtoupper($params['REGISTER_HISTORY_EVENT']) === 'Y';
}
$result = CCrmActivity::Update($ID, $fields, false, $regEvent, 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;
}