CCrmRestProxyBase::tryInternalizeFileField

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmRestProxyBase
  4. tryInternalizeFileField
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice.php
  • Класс: \CCrmRestProxyBase
  • Вызов: CCrmRestProxyBase::tryInternalizeFileField
protected function tryInternalizeFileField(&$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;
		}

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

		if(!self::isIndexedArray($fileData))
		{
			$fileName = '';
			$fileContent = $fileData;
		}
		else
		{
			$fileDataLength = count($fileData);

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

		if(is_string($fileContent) && $fileContent !== '')
		{
			// Add/replace file
			$fileInfo = CRestUtil::saveFile($fileContent, $fileName);
			if(is_array($fileInfo))
			{
				if($fileID > 0)
				{
					$fileInfo['old_id'] = $fileID;
				}

				//In this case 'del' flag does not make sense - old file will be replaced by new one.
				/*if($removeFile)
				{
					$fileInfo['del'] = true;
				}*/

				$result[] = &$fileInfo;
				unset($fileInfo);
			}
		}
		elseif($fileID > 0 && $removeFile)
		{
			// Remove file
			$result[] = array(
				'old_id' => $fileID,
				'del' => true
			);
		}
	}
	unset($v);

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

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

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