<?php

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = 'foreach ($_SESSION["back_urls"] as $back_url => $url) {
    if (strpos($url, "view_line2.php?table=$table_id") && strpos($url, "&line=$ID")) break;
}

$table = get_table({$sub_table.subTable});
// Сохранение при добавлении включено
if (($table["auto_save"] ?? 0) == 1) {
    $new_line_id = data_insert({$sub_table.subTable}, EVENTS_ENABLE, [
        "{$sub_table.link_field_id}" => $ID
    ]);
{if $open_form == 1}
    header("Location: view_line2.php?table={$sub_table.subTable}&line=$new_line_id&back_url=$back_url");
    exit;
{else}
    echo "<script>window.open(\'view_line2.php?table={$sub_table.subTable}&line=$new_line_id&back_url=$back_url\')</script>";
{/if}
} else {
{if $open_form == 1}
    header("Location: view_line2.php?table={$sub_table.subTable}&line=new&{$sub_table.link_field_id}=$ID&back_url=$back_url&save_close");
    exit;
{else}
    echo "<script>window.open(\'view_line2.php?table={$sub_table.subTable}&line=new&{$sub_table.link_field_id}=$ID\')</script>";
{/if}
}' WHERE id = 126
EOT;
run_query($sql_query);
