• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/automation/engine/entity/template.php
  • Класс: Bitrix\Crm\Automation\Engine\Entity\TemplateTable
  • Вызов: TemplateTable::upsert
static function upsert(array $template)
{
	$templateId = (int)$template['ID'];
	$iterator = static::getList(array(
		'select' => array('ID'),
		'filter' => array(
			'=ENTITY_TYPE_ID' => $template['ENTITY_TYPE_ID'],
			'=ENTITY_STATUS' => $template['ENTITY_STATUS']
		)
	));

	$ids = array();
	while ($row = $iterator->fetch())
	{
		$ids[] = (int)$row['ID'];
	}

	$toSave = array(
		'ENTITY_TYPE_ID' => $template['ENTITY_TYPE_ID'],
		'ENTITY_STATUS' => $template['ENTITY_STATUS'],
		'TEMPLATE_ID' => $template['TEMPLATE_ID'],
	);

	if (in_array($templateId, $ids, true))
	{
		static::update($templateId, $toSave);
	}
	else
	{
		static::add($toSave);
	}

	foreach ($ids as $id)
	{
		if ($id === $templateId)
			continue;

		static::delete($id);
	}
}