<?php
$sql_prefix = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query_50 = <<<'EOT'
`params` = '{\n    \"title\": \"Копировать запись\",\n    \"help\": \"tpl_copy_line\",\n    \"parameters\": {\n        \"correlation\": {\n            \"type\": \"Correlation\",\n            \"name\": \"correlation\",\n            \"description\": \"Таблица-получатель\",\n            \"help\": \"tpl_table_copy_line\",\n            \"save_field\": true\n        },\n        \"addit\": {\n            \"type\": \"List\",\n            \"name\": \"addit\",\n            \"description\": \"Опция\",\n            \"help\": \"tpl_help\",\n            \"values\": {\n              \"go_edit\": \"Переходить на созданную запись в режиме редактирования\",\n              \"go\": \"Переходить на созданную запись\",\n              \"stop\": \"Оставаться в текущей записи\"\n            }\n        }\n    }\n}', `calculate_tpl` = '$sel_table = (int)\'{$correlation[\'table\']}\';\n$sel_fields = [\n    {foreach from = $correlation[\'to_field\'] item = field_id}(int)\'{$field_id}\', {/foreach}\n];\n$sel_def_vals = [\n    {foreach from = $correlation[\'def_val\'] item = field_id}\'{$field_id}\', {/foreach}\n];\n$cur_fields = [\n    {foreach from = $correlation[\'cur_field\'] item = field_id}\'{$field_id}\', {/foreach}\n];\nif ($sel_table > 0) {\n    $ins_arr = [];\n    foreach ($sel_fields as $num => $field_id) {\n        if (!$field_id) continue;\n        $def_val = $sel_def_vals[$num];\n        if ($def_val) {\n             $ins_arr[\'f\' . $field_id] = $def_val;\n        } else {\n             if (is_array($line[\'f\' . $cur_field[$num]])) {\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\n             } else {\n                 if ($cur_fields[$num] !== \'id\') {\n                     $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\n                 } else {\n                     $ins_arr[\'f\' . $field_id] = $line[$cur_fields[$num]];\n                 }                \n             }\n        }\n    }\n    if (count($ins_arr) > 0) {\n        if ($new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr)) {\n{if $correlation[\'save_field\']}\n            $line[\'f{$correlation[\'save_field\']}\'] = $new_id;\n            data_update($table_id, EVENTS_ENABLE, [\'f{$correlation[\'save_field\']}\' => $new_id], \'id = \' . $ID);\n{/if}\n{if $addit neq \'stop\'}\n{if $addit eq \'go_edit\'}\n            if (isset($_REQUEST[\'parameters\'])) {\n                echo \'<script type=\"text/javascript\">\n                    window.opener.location.href = \"\' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'&edit_mode=on\";\n                    window.close();\n                    </script>\';\n            } else {\n                header(\'Location: \' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'&edit_mode=on\'); exit;\n            }\n{else}\n            if (isset($_REQUEST[\'parameters\'])) {\n                echo \'<script type=\"text/javascript\">\n                    window.opener.location.href = \"\' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'\";\n                    window.close();\n                    </script>\';\n            } else {\n                header(\'Location: \' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id); exit;\n            }\n{/if}\n{/if}\n        } else {\n            $error_trace[] = true;\n        }\n    } else {\n        $error_trace[] = true;\n    }\n} else {\n    $error_trace[] = true;\n}\n'
WHERE id = 50
EOT;
$sql_query_78 = <<<'EOT'
`params` = '{\n    \"title\": \"Копировать запись\",\n    \"help\": \"tpl_copy_line\",\n    \"parameters\": {\n        \"correlation\": {\n            \"type\": \"Correlation\",\n            \"name\": \"correlation\",\n            \"description\": \"Таблица-получатель\",\n            \"help\": \"tpl_table_copy_line\",\n            \"save_field\": true\n        },\n        \"addit\": {\n            \"type\": \"List\",\n            \"name\": \"addit\",\n            \"description\": \"Опция\",\n            \"help\": \"tpl_help\",\n            \"values\": {\n              \"go_edit\": \"Переходить на созданную запись в режиме редактирования\",\n              \"go\": \"Переходить на созданную запись\",\n              \"stop\": \"Оставаться в текущей записи\"\n            }\n        }\n    }\n}', `calculate_tpl` = '$sel_table = (int)\'{$correlation[\'table\']}\';\n$sel_fields = [\n    {foreach from = $correlation[\'to_field\'] item = field_id}(int)\'{$field_id}\', {/foreach}\n];\n$sel_def_vals = [\n    {foreach from = $correlation[\'def_val\'] item = field_id}\'{$field_id}\', {/foreach}\n];\n$cur_fields = [\n    {foreach from = $correlation[\'cur_field\'] item = field_id}\'{$field_id}\', {/foreach}\n];\nif ($sel_table > 0) {\n    $ins_arr = [];\n    foreach ($sel_fields as $num => $field_id) {\n        if (!$field_id) continue;\n        $def_val = $sel_def_vals[$num];\n        if ($def_val) {\n             $ins_arr[\'f\' . $field_id] = $def_val;\n        } else {\n             if (is_array($line[\'f\' . $cur_field[$num]])) {\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\n             } else {\n                 if ($cur_fields[$num] !== \'id\') {\n                     $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\n                 } else {\n                     $ins_arr[\'f\' . $field_id] = $line[$cur_fields[$num]];\n                 }                \n             }\n        }\n    }\n    if (count($ins_arr) > 0) {\n        $new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr);\n{if $correlation[\'save_field\']}\n        $line[\'f{$correlation[\'save_field\']}\'] = $new_id;\n{/if}\n    }\n}\n'
WHERE id = 78
EOT;
run_query($sql_prefix . $sql_query_50);
run_query($sql_prefix . $sql_query_78);