<?php
run_query('DELETE FROM `' . $config['table_prefix'] . 'calc_template` WHERE id = 60');
$sql_query = 'INSERT INTO `' . $config["table_prefix"] . 'calc_template` ';
$sql_query .= <<<'EOT'
(`id`, `name`, `calculate_tpl`, `params`, `type`, `tpl_name`, `javascript_tpl`, `tpl_category`, `calc_types`) VALUES
(60,	'Копировать запись в подтаблицу',	'$sel_table = (int)\'{$correlation[\'table\']}\';\r\n$sel_fields = [\r\n    {foreach from = $correlation[\'to_field\'] item = field_id}(int)\'{$field_id}\', {/foreach}\r\n];\r\n$sel_def_vals = [\r\n    {foreach from = $correlation[\'def_val\'] item = field_id}\'{$field_id}\', {/foreach}\r\n];\r\n$cur_fields = [\r\n    {foreach from = $correlation[\'cur_field\'] item = field_id}(int)\'{$field_id}\', {/foreach}\r\n];\r\n$link_field = (int)\'{$correlation[\'link_field_id\']}\';\r\nif ($sel_table > 0) {\r\n    $ins_arr = [];\r\n    foreach ($sel_fields as $num => $field_id) {\r\n        if (!$field_id) continue;\r\n        $def_val = $sel_def_vals[$num];\r\n        if ($def_val) {\r\n             $ins_arr[\'f\' . $field_id] = $def_val;\r\n        } else {\r\n             if (is_array($line[$cur_field[$num]])) {\r\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\r\n             } else {\r\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\r\n             }\r\n        }\r\n    }\r\n    if ($link_field) {\r\n        $ins_arr[\'f\' . $link_field] = $ID;\r\n    }\r\n    if (count($ins_arr) > 0) {\r\n        data_insert($sel_table, EVENTS_ENABLE, $ins_arr);\r\n    } else {\r\n        $error_trace[] = true;\r\n    }\r\n} else {\r\n    $error_trace[] = true;\r\n}\r\n',	'{\r\n    \"title\": \"Копировать запись в подтаблицу\",\r\n    \"help\": \"tpl_copy_line\",\r\n    \"parameters\": {\r\n        \"correlation\": {\r\n            \"type\": \"Correlation\",\r\n            \"name\": \"correlation\",\r\n            \"description\": \"Выберите подтаблицу, куда копировать\",\r\n            \"help\": \"tpl_table_copy_line\",\r\n            \"only_subtables\": true\r\n        }\r\n    }\r\n}',	'button',	'copy_line_subtable.tpl',	'',	6,	'[]');
EOT;
run_query($sql_query);