<?php
$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = '$filter = sql_select_array(FILTERS_TABLE, \'id = \', get_filter_id({$filter|default:0}));\r\n$result = data_select_field($table_id, \'id, {$cur_field}\',\r\n    \'status = 0\' . ($event[\'type\'] === \'delete\' ? \' AND id != \' . $ID : \'\') .\r\n    \' AND {$gen_field.link_field_id} = \', $line[\'{$gen_field.link_field_id}\'][\'raw\'],\r\n    ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\') .\r\n    \' ORDER BY id {if $record == \"last\"}DESC{/if} {if $record != \"all\"}LIMIT 1{/if}\');\r\n{if $record != \"all\"}\r\n$row = sql_fetch_assoc($result);\r\n$record = $row[\'{$cur_field}\'];\r\n\r\n{if $cur_field_type == 6 || $cur_field_type == 9}\r\n$user_agent = $_SERVER[\'HTTP_USER_AGENT\'];\r\nif (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, $row[\'{$cur_field}\']);\r\nforeach ($files as $fname) {\r\n    $orig_fpath = get_file_path({$cur_field|substr:1}, $row[\'id\'], $fname);\r\n    save_data_file({$cond_table.field|substr:1}, $line[\'{$cond_table.link_field_id}\'][\'id\'], $fname, file_get_contents($orig_fpath));\r\n}\r\n{/if}\r\n\r\n{else}\r\n\r\n$record = \'\';\r\n$res_field = sql_select_array(FIELDS_TABLE, \'id = {$gen_field.field|substr:1}\');\r\nif ($res_field[\'type_field\'] == 4 && $res_field[\'mult_value\']) {\r\n    $list_arr = [];\r\n    foreach(explode(\"\\r\\n\", $res_field[\'type_value\']) as $key => $value) {\r\n        $list_arr[trim($value)] = 0;\r\n    }\r\n}\r\nwhile ($row = sql_fetch_assoc($result)) {\r\n    if ($res_field[\'mult_value\']) {\r\n        switch ( (int)$res_field[\'type_field\'] ) {\r\n            case 4:\r\n                if (isset($list_arr[$row[\'{$cur_field}\']])) {\r\n                    $list_arr[$row[\'{$cur_field}\']] = 1;\r\n                }\r\n                break;\r\n            case 7:\r\n                $record .= (!empty($row[\'{$cur_field}\'])) ? ($row[\'{$cur_field}\'] . \'-\') : \'\' ;\r\n                break;\r\n            case 11:\r\n                $record .= (!empty($row[\'{$cur_field}\'])) ? ($row[\'{$cur_field}\'] . \'-\') : \'\' ;\r\n                break;\r\n            default:\r\n                $record .= ($record == \'\') ?\r\n                    ( $row[\'{$cur_field}\'] ?: \'\' ) :\r\n                    ( !empty($row[\'{$cur_field}\']) ? (\"{$add_delim}\" . $row[\'{$cur_field}\']) : \'\' );\r\n                break;\r\n        }\r\n        {if $cur_field_type == 6 || $cur_field_type == 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, $row[\'{$cur_field}\']);\r\n        foreach ($files as $fname) {\r\n            $orig_fpath = get_file_path({$cur_field|replace:\'f\':\'\'}, $row[\'id\'], $fname);\r\n            save_data_file({$cond_table.field|replace:\'f\':\'\'}, $line[\'{$cond_table.link_field_id}\'][\'id\'], $fname, file_get_contents($orig_fpath));\r\n        }\r\n        {/if}\r\n    } else {\r\n        $record = $row[\'{$cur_field}\'];\r\n    }\r\n}\r\nif ($res_field[\'type_field\'] == 4 && $res_field[\'mult_value\']) {\r\n    $record = implode(\"\\r\\n\", array_keys($list_arr, 1));\r\n}\r\n{/if}\r\n\r\n{if $add_value == \'yes\'}\r\n$field_type = {$cur_field_type};\r\n$res_field = $res_field ?? sql_select_array(FIELDS_TABLE, \'id = {$gen_field.field|substr:1}\');\r\nif ($field_type == 4 && $res_field[\'mult_value\']) {\r\n    if ($line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] != \'\') {\r\n        $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] .= \"\\r\\n\" . $record;\r\n    } else {\r\n        $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] = $record;\r\n    }\r\n} elseif (($field_type == 7 || $field_type == 11) && $res_field[\'mult_value\']) {\r\n    if ($line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] != \'\') {\r\n        $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] .= $record . \'-\';\r\n    } else {\r\n        $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] = \'-\' . $record . \'-\';\r\n    }\r\n} else {\r\n    if ($line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] != \'\') {\r\n        $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] .= \"{$add_delim}\" . $record;\r\n    } else {\r\n        $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] = $record;\r\n    }\r\n}\r\n{else}\r\n$line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] = $record;\r\n{/if}'
WHERE id = 42
EOT;
run_query($sql_query);