{*
 Шаблон вычисления "Копировать таблицу"
*}

$orig_table_id = sql_fetch_row(sql_select_field(TABLES_TABLE, 'id', "name_table = '", $line['{$name_table1}'], "'"))[0];
if ($orig_table_id) {
    update_run_flags('head_tables');
    $result = copy_table($orig_table_id, $line['{$name_table2}']);
    session_start();
    unset($_SESSION[$ses_id]['head_tables']);
{if $link_field}
    $link_table_id = $result[0];
    $field_id = sql_fetch_row(sql_select_field(FIELDS_TABLE, 'id', 'table_id = ', $table_id, ' AND type_field = 10'))[0];
    $field_num = sql_fetch_row(sql_select_field(FIELDS_TABLE, 'field_num', 'table_id = ', $link_table_id, ' ORDER BY field_num DESC LIMIT 1'))[0] + 1;
    $link_field_id = sql_insert(FIELDS_TABLE, [
        'table_id' => $link_table_id,
        'field_num' => $field_num,
        'name_field' => $lang['Link_with'] . ' "' . $table['name_table'] . '"',
        'type_field' => 5,
        'type_value' => $table_id . '|' . $field_id . '|0|0|0',
        'default_value' => $ID,
    ]);
    set_access(ACC_FIELDS_TABLE, [
        'table_id' => $link_table_id,
        'field_id' => $link_field_id,
        'view_tb' => 1,
        'view_add' => 1,
        'view_edit' => 1,
        'view_sub' => 1,
        'view' => 1,
        'read_acc' => 1,
        'write_acc' => 1
    ]);
    sql_query('ALTER TABLE ' . DATA_TABLE . $link_table_id . ' ADD f' . $link_field_id . ' INTEGER DEFAULT 0');
{/if}
}