<?php
run_query("UPDATE " . $config['table_prefix'] . "calc_template SET calculate_tpl = '{\"file_name\":\"Average_value_by_subtable.tpl\"}' WHERE id = 12");
run_query("UPDATE " . $config['table_prefix'] . "calc_template SET calculate_tpl = '{\"file_name\":\"Copy_field_from_subtable_to_main_record.tpl\"}' WHERE id = 42");
run_query("UPDATE " . $config['table_prefix'] . "calc_template SET calculate_tpl = '{\"file_name\":\"Copy_field_from_subtable_to_main_record.tpl\"}' WHERE id = 43");

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = 'data_update({$subtable.table}, {if $operand}EVENTS_ENABLE, {/if}\r\n    [\'{$subtable.field}\' => $line[\'{$cur_field}\']{if $cur_field_type == 5}[\'raw\']{/if}], \r\n    \'status = 0 AND ({$subtable.link_field_id} = \' . $ID . \' OR {$subtable.link_field_id} LIKE \"%-\' . $ID . \'-%\")\'\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, $line[\'{$cur_field}\']);\r\n$res = data_select_field({$subtable.table}, \'id\', \'status = 0 AND ({$subtable.link_field_id} = \' . $ID . \' OR {$subtable.link_field_id} LIKE \"%-\' . $ID . \'-%\")\');\r\nwhile ($row = sql_fetch_assoc($res)) {\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$cur_field|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n        save_data_file({$subtable.field|replace:\'f\':\'\'}, $row[\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n}\r\n{/if}'
WHERE id = 31
EOT;
run_query($sql_query);

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = 'data_update({$link_table.table}, {if !$operand}EVENTS_ENABLE, {/if}\r\n    [\'{$link_table.field}\' => $line[\'{$cur_field}\']{if $cur_field_type == 5}[\'raw\']{/if}],\r\n    \'status = 0 AND ({$link_table.link_field_id} = \' . $ID . \' OR {$link_table.link_field_id} LIKE \"%-\' . $ID . \'-%\")\'\r\n);\r\n{if $link_table.field_type == 6 || $link_table.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, $line[\'{$cur_field}\']);\r\n$res = data_select_field({$link_table.table}, \'id\', \'status = 0 AND ({$link_table.link_field_id} = \' . $ID . \' OR {$link_table.link_field_id} LIKE \"%-\' . $ID . \'-%\")\');\r\nwhile ($row = sql_fetch_assoc($res)) {\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$cur_field|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n        save_data_file({$link_table.field|replace:\'f\':\'\'}, $row[\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n}\r\n{/if}'
WHERE id = 109
EOT;
run_query($sql_query);
