• Модуль: conversion
  • Путь к файлу: ~/bitrix/modules/conversion/lib/internals/basecontext.php
  • Класс: BitrixConversionInternalsBaseContext
  • Вызов: BaseContext::save
protected function save()
{
	if (($id =& $this->id) !== null)
		throw new SystemException('Cannot save existent context!');

	$id = self::EMPTY_CONTEXT_ID;

	if ($attributes = $this->attributes)
	{
		// leave only one attribute in group

		static $groupedTypes;

		if (! $groupedTypes)
		{
			$groupedTypes = AttributeManager::getGroupedTypes();
			unset($groupedTypes[null]);
		}

		foreach ($groupedTypes as $types)
		{
			$intersection = array_intersect_key($types, $attributes);

			if (count($intersection) > 1)
			{
				array_shift($intersection);

				foreach ($intersection as $name => $type)
				{
					unset($attributes[$name]);
				}
			}
		}

		// save to database

		$snapshot = self::getSnapshot($attributes);

		$query = array(
			'limit'  => 1,
			'select' => array('ID'),
			'filter' => array('=SNAPSHOT' => $snapshot),
		);

		if ($row = ContextTable::getList($query)->fetch())
		{
			$id = (int) $row['ID'];
		}
		elseif (Option::get('conversion', 'CONTEXT_TABLE') != 'locked') // TODO remove if
		{
			try
			{
				$result = ContextTable::add(array('SNAPSHOT' => $snapshot));

				if ($result->isSuccess())
				{
					$id = $result->getId();

					foreach ($attributes as $name => $value)
					{
						// TODO resetContext if not success and return null!!!
						$result = ContextAttributeTable::add(array(
							'CONTEXT_ID' => $id,
							'NAME'       => $name,
							'VALUE'      => (string) $value, // can be null!
						));
					}
				}
				else
				{
					throw new DBSqlQueryException();
				}
			}
			catch (DBSqlQueryException $e)
			{
				if ($row = ContextTable::getList($query)->fetch())
				{
					$id = (int) $row['ID'];
				}
			}
		}
	}
}