CCrmRestProxyBase::tryInternalizeWebDavElementField

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmRestProxyBase
  4. tryInternalizeWebDavElementField
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
  • Класс: \CCrmRestProxyBase
  • Вызов: CCrmRestProxyBase::tryInternalizeWebDavElementField
protected function tryInternalizeWebDavElementField(&$fields, $fieldName, $multiple = false)
{
	if(!isset($fields[$fieldName]))
	{
		return false;
	}

	$result = array();

	$values = $multiple && self::isIndexedArray($fields[$fieldName]) ? $fields[$fieldName] : array($fields[$fieldName]);
	foreach($values as &$v)
	{
		if(!self::isAssociativeArray($v))
		{
			continue;
		}

		$elementID = isset($v['id']) ? intval($v['id']) : 0;
		$removeElement = isset($v['remove']) && is_string($v['remove']) && mb_strtoupper($v['remove']) === 'Y';
		$fileData = $v['fileData'] ?? '';

		if(!self::isIndexedArray($fileData))
		{
			continue;
		}

		$fileDataLength = count($fileData);
		if($fileDataLength === 0)
		{
			continue;
		}

		if($fileDataLength === 1)
		{
			$fileName = '';
			$fileContent = $fileData[0];
		}
		else
		{
			$fileName = $fileData[0];
			$fileContent = $fileData[1];
		}

		if(is_string($fileContent) && $fileContent !== '')
		{
			$fileInfo = CRestUtil::saveFile($fileContent, $fileName);

			$settings = $this->getWebDavSettings();
			$iblock = $this->prepareWebDavIBlock($settings);
			$fileName = $iblock->CorrectName($fileName);

			$filePath = $fileInfo['tmp_name'];
			$options = array(
				'new' => true,
				'dropped' => false,
				'arDocumentStates' => array(),
				'arUserGroups' => $iblock->USER['GROUPS'],
				'TMP_FILE' => $filePath,
				'FILE_NAME' => $fileName,
				'IBLOCK_ID' => $settings['IBLOCK_ID'],
				'IBLOCK_SECTION_ID' => $settings['IBLOCK_SECTION_ID'],
				'WF_STATUS_ID' => 1
			);
			$options['arUserGroups'][] = 'Author';

			global $DB;
			$DB->StartTransaction();
			if (!$iblock->put_commit($options))
			{
				$DB->Rollback();
				unlink($filePath);
				throw new RestException($iblock->LAST_ERROR);
			}
			$DB->Commit();
			unlink($filePath);

			if(!isset($options['ELEMENT_ID']))
			{
				throw new RestException('Could not save webdav element.');
			}

			$elementData = array(
				'ELEMENT_ID' => $options['ELEMENT_ID']
			);

			if($elementID > 0)
			{
				$elementData['OLD_ELEMENT_ID'] = $elementID;
			}

			$result[] = &$elementData;
			unset($elementData);
		}
		elseif($elementID > 0 && $removeElement)
		{
			$result[] = array(
				'OLD_ELEMENT_ID' => $elementID,
				'DELETE' => true
			);
		}
	}
	unset($v);

	if($multiple)
	{
		$fields[$fieldName] = $result;
		return true;
	}
	elseif(!empty($result))
	{
		$fields[$fieldName] = $result[0];
		return true;
	}

	unset($fields[$fieldName]);
	return false;
}

Добавить комментарий