<?php

/* 
 * #68731
 */

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

$sql_query_2 = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query_2 .= <<<'EOT'
calculate_tpl = '$sel_table = get_table_id({$correlation[\'table\']|default:0});\r\n$sel_fields = [\r\n    {foreach from = $correlation[\'to_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\n$cur_fields = [\r\n    {foreach from = $correlation[\'cur_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\n$def_vals = [\r\n    {foreach from = $correlation[\'def_val\'] item = value}\'{$value}\', {/foreach}\r\n];\r\nif ($sel_table > 0) {\r\n    $ins_arr = [];\r\n    foreach ($sel_fields as $num => $field_id) {\r\n        $field_name = form_int_name($field_id);\r\n        if (!$field_name) continue;\r\n        if ($def_vals[$num] && $def_vals[$num] != \'null\') {\r\n            $ins_arr[$field_name] = $def_vals[$num];\r\n        } else {\r\n            if (is_int($cur_fields[$num])) $cur_field_name = form_int_name($cur_fields[$num]); else $cur_field_name = $cur_fields[$num];\r\n            if (is_array($line[$cur_field_name])) {\r\n                $ins_arr[$field_name] = $line[$cur_field_name][\'raw\'];\r\n            } else {\r\n                $ins_arr[$field_name] = $line[$cur_field_name];\r\n            }\r\n        }\r\n    }\r\n    if (count($ins_arr) > 0) {\r\n        if ($new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr)) {\r\n{if $correlation[\'save_field\']}\r\n            $line[\'f{$correlation[\'save_field\']}\'] = $new_id;\r\n            data_update($table_id, EVENTS_ENABLE, [\'f{$correlation[\'save_field\']}\' => $new_id], \'id = \' . $ID);\r\n{/if}\r\n            // сохраняем файлы на диске\r\n            foreach ($sel_fields as $num => $field_id) {\r\n                if (!$def_vals[$num]) {\r\n                    $cur_field_name = form_int_name($cur_fields[$num]);\r\n                    $field_row = sql_select_array(FIELDS_TABLE, \'id = \' . $field_id);\r\n                    if ($field_row[\'type_field\'] == 6 || $field_row[\'type_field\'] == 9) { \r\n                        $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n                        if (strpos($user_agent, \"MSIE\") !== false) {      \r\n                            $exp = \"\\n\\r\";      \r\n                        } else {         \r\n                            $exp = \"\\r\\n\";    \r\n                        }   \r\n                        $files = explode($exp, $line[$cur_field_name]);      \r\n                        foreach ($files as $fname) {        \r\n                            $orig_fpath = get_file_path($cur_fields[$num], $line[\'id\'], $fname); \r\n                            save_data_file($field_id, $new_id, $fname, file_get_contents($orig_fpath));  \r\n                        }\r\n                    }      \r\n                }\r\n            }\r\n        } \r\n    }\r\n}'
WHERE id = 78
EOT;
run_query($sql_query_2);
