<?php
run_query("UPDATE " . $config['table_prefix'] . "calc SET calculate = REPLACE(calculate, 'window.opener.location.reload();window.close();', 'if(window.opener.location.href.indexOf(\'report.php\') === -1){window.opener.location.reload();}else{window.opener.updateReport();}window.close();')");

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';

$sql_query .= <<<'EOT'
calculate_tpl = "$sel_table = get_table_id({$correlation['table']|default:0});
$sel_fields = [
    {foreach from = $correlation['to_field'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}
];
$cur_fields = [
    {foreach from = $correlation['cur_field'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}
];
$def_vals = [
    {foreach from = $correlation['def_val'] item = value}'{$value}', {/foreach}
];
if ($sel_table > 0) {
    $ins_arr = [];
    foreach ($sel_fields as $num => $field_id) {
        $field_name = form_int_name($field_id);
        if (!$field_name) continue;
        if ($def_vals[$num] && $def_vals[$num] != 'null') {
            $ins_arr[$field_name] = $def_vals[$num];
        } else {
            if (is_int($cur_fields[$num])) $cur_field_name = form_int_name($cur_fields[$num]); else $cur_field_name = $cur_fields[$num];
            if (is_array($line[$cur_field_name])) {
                $ins_arr[$field_name] = $line[$cur_field_name]['raw'] ?? $def_vals[$num];
            } else {
                $ins_arr[$field_name] = $line[$cur_field_name] ?? $def_vals[$num];
            }
        }
    }
    if (count($ins_arr) > 0) {
        if ($new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr)) {
{if $correlation['save_field']}
            $line['f{$correlation['save_field']}'] = $new_id;
            data_update($table_id, EVENTS_ENABLE, ['f{$correlation['save_field']}' => $new_id], 'id = ' . $ID);
{/if}
            // сохраняем файлы на диске
            foreach ($sel_fields as $num => $field_id) {
                if (!$def_vals[$num]) {
                    $cur_field_name = form_int_name($cur_fields[$num]);
                    $field_row = sql_select_array(FIELDS_TABLE, 'id = ' . $field_id);
                    if ($field_row['type_field'] == 6 || $field_row['type_field'] == 9) {
                        $user_agent = $_SERVER['HTTP_USER_AGENT'];
                        if (strpos($user_agent, 'MSIE') !== false) {
                            $exp = \"\\n\\r\";
                        } else {
                            $exp = \"\\r\\n\";
                        }
                        $files = explode($exp, $line[$cur_field_name]);
                        foreach ($files as $fname) {
                            $orig_fpath = get_file_path($cur_fields[$num], $line['id'], $fname);
                            save_data_file($field_id, $new_id, $fname, file_get_contents($orig_fpath));
                        }
                    }
                }
            }
{if $addit neq 'stop'}
{if $addit eq 'go_edit'}
            if (isset($_REQUEST['parameters'])) {
                echo '<script type=\"text/javascript\">
                    window.opener.location.href = \"' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id . '&edit_mode=on\";
                    window.close();
                    </script>';
            } else {
                if(strpos($_SERVER['SCRIPT_NAME'], 'report') !== false){
                    echo 'link|||' . ($config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id) . '&edit_mode=on'; exit;
                }
                else {
                    header('Location: ' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id . '&edit_mode=on'); exit;
                }
            }
{else}
            if (isset($_REQUEST['parameters'])) {
                echo '<script type=\"text/javascript\">
                    window.opener.location.href = \"' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id . '\";
                    window.close();
                    </script>';
            } else {
                if(strpos($_SERVER['SCRIPT_NAME'], 'report') !== false){
                    echo 'link|||' . ($config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id); exit;
                }
                else {
                    header('Location: ' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id); exit;
                }
            }
{/if}
{/if}
        } else {
            $error_trace[] = true;
        }
    } else {
        $error_trace[] = true;
    }
} else {
    $error_trace[] = true;
}"
WHERE id = 50
EOT;

run_query($sql_query);


$sql_query = "UPDATE " . $config['table_prefix'] . "calc SET calculate = REPLACE(calculate,";
$sql_query .=<<<'EOT'
"header('Location: ' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id); exit;", "if(strpos($_SERVER['SCRIPT_NAME'], 'report') !== false){
    echo 'link|||' . ($config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id); exit;
}
else {
    header('Location: ' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id); exit;
}")
EOT;
run_query($sql_query);

$sql_query = "UPDATE " . $config['table_prefix'] . "calc SET calculate = REPLACE(calculate,";
$sql_query .=<<<'EOT'
"header('Location: ' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id . '&edit_mode=on'); exit;", "if(strpos($_SERVER['SCRIPT_NAME'], 'report') !== false){
    echo 'link|||' . ($config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id . '&edit_mode=on');
}
else {
    header('Location: ' . $config['site_root'] . '/view_line2.php?table=' . $sel_table . '&line=' . $new_id . '&edit_mode=on'); exit;
}")
EOT;
run_query($sql_query);
