• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/classes/general/taskfilterctrl.php
  • Класс: CTaskFilterCtrl
  • Вызов: CTaskFilterCtrl::convertItemForImport
static function convertItemForImport($arItem)
{
	static $arManifest = false;
	static $arAllowedFields = false;
	$arResult = array();

	if ($arManifest === false)
	{
		$arManifest      = self::getManifest();
		$arAllowedFields = array_keys($arManifest['Fields']);
	}

	foreach ($arItem as $itemName => $itemData)
	{
		CTaskAssert::assert(
			(mb_strlen($itemName) > 2)
			|| CTaskAssert::isLaxIntegers($itemName)
		);

		if ($itemName === '::LOGIC')
		{
			$arResult[$itemName] = $itemData;
			continue;
		}

		if (mb_substr($itemName, 0, 12) === '::SUBFILTER-')
		{
			$arResult[$itemName] = self::convertItemForImport($itemData);
			continue;
		}

		CTaskAssert::assert(
			isset($itemData['operation'], $itemData['field'])
			&& array_key_exists('value', $itemData)
			&& CTaskAssert::isLaxIntegers($itemData['operation'])
			&& is_string($itemData['field'])
			&& ($itemData['field'] !== '')
		);

		if ( ! in_array($itemData['field'], $arAllowedFields, true) )
			throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);

		$arSupportedOperations = $arManifest['Fields'][$itemData['field']]['Supported operations'];
		$itemType = $arManifest['Fields'][$itemData['field']]['Type'];
		$operation = (int) $itemData['operation'];

		if ( ! in_array($operation, $arSupportedOperations, true) )
			throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);

		// Resolve operation prefix
		CTaskAssert::assert(isset(self::$arOperationsMap[$operation]));
		$operationPrefix = self::$arOperationsMap[$operation];

		$value = false;

		switch ($itemType)
		{
			case self::TYPE_DATE:
				if ( ! (
					is_string($itemData['value'])
					|| CTaskAssert::isLaxIntegers($itemData['value'])
				))
				{
					throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);
				}

				$value = self::convertDateStringToTimestamp($operation, $itemData['value']);
			break;

			case self::TYPE_TEXT:
				if ( ! is_string($itemData['value']) )
					throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);

				$value = (string) $itemData['value'];
			break;

			case self::TYPE_PRIORITY:
				if ( ! (
					is_string($itemData['value'])
					|| CTaskAssert::isLaxIntegers($itemData['value'])
				))
				{
					throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);
				}

				$value = (int) $itemData['value'];

				if ( ! in_array(
					$value,
					array_values(Priority::getAll()),
					true
				))
				{
					throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);
				}
			break;

			case self::TYPE_STATUS:
				if (is_array($itemData['value']))
				{
					foreach ($itemData['value'] as $statusId)
					{
						if ( ! CTaskAssert::isLaxIntegers($statusId) )
							throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);
					}

					$value = array_map('intval', $itemData['value']);
				}
				else
				{
					$statusId = $itemData['value'];
					if ( ! CTaskAssert::isLaxIntegers($statusId) )
						throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);

					$value = (int) $statusId;
				}
			break;

			case self::TYPE_GROUP_ID:
			case self::TYPE_USER_ID:
				$entityId = $itemData['value'];
				if ( ! CTaskAssert::isLaxIntegers($entityId) )
					throw new TasksException('', TasksException::TE_FILTER_MANIFEST_MISMATCH);

				$value = (int) $entityId;
			break;

			default:
				CTaskAssert::assert(false);
			break;
		}

		$arKey = $operationPrefix . $itemData['field'];

		while (isset($arResult[$arKey]))
			$arKey = ' ' . $arKey;

		$arResult[$arKey] = $value;
	}

	return ($arResult);

	/*
		data in:
		array(
			'::LOGIC' => 'AND',
			array(
				'field'     => 'RESPONSIBLE_ID',
				'operation' => CTaskFilterCtrlInterface::OP_EQUAL,
				'value'     => 1
			),
			'::SUBFILTER-1' => array(
				'::LOGIC' => 'OR',
				array(
					'field'     => 'CREATED_BY',
					'operation' => CTaskFilterCtrlInterface::OP_EQUAL,
					'value'     => 1
				),
				array(
					'field'     => 'CREATED_BY',
					'operation' => CTaskFilterCtrlInterface::OP_NOT_EQUAL,
					'value'     => 2
				)
			),
			'::SUBFILTER-2' => array(
				'::LOGIC' => 'AND',
				array(
					'field'     => 'TITLE',
					'operation' => CTaskFilterCtrlInterface::OP_SUBSTRING,
					'value'     => 1
				),
				array(
					'field'     => 'TITLE',
					'operation' => CTaskFilterCtrlInterface::OP_NOT_SUBSTRING,
					'value'     => 2
				)
			)
		)

		data out:
		$arFilter = array(
			'::LOGIC' => 'AND',
			'RESPONSIBLE_ID' => 1,
			'::SUBFILTER-1' => array(
				'::LOGIC' => 'OR',
				'CREATED_BY' => 1,
				'!=CREATED_BY' => 2
			),
			'::SUBFILTER-2' => array(
				'::LOGIC' => 'AND',
				'%TITLE' => 'some interesting substring',
				'!%TITLE' => 'some not interesting substring'
			)
		);
	*/
}