<?php
$sql_query0 = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query1 = <<<'EOT'
calculate_tpl = 'if ($event[\'type\'] === \'delete\') {\r\n    $deleteCond = \" AND id <> $ID\";\r\n}\r\n$filter = sql_select_array(FILTERS_TABLE, \'id={$filter}\');\r\n$result = data_select_field($table_id, \'{$cond_field}\',\r\n    \'status = 0 AND {$cond_table.link_field_id} = \' . intval($line[\'{$cond_table.link_field_id}\'][\'raw\']) . $deleteCond .\r\n    ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\') . \' ORDER BY id DESC LIMIT 1\');\r\n$row = sql_fetch_assoc($result);\r\n$line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'{$cond_field}\'];' WHERE id = 32
EOT;
$sql_query2 = <<<'EOT'
calculate_tpl = 'if ($event[\'type\'] === \'delete\') {\r\n    $deleteCond = \" AND id <> $ID\";\r\n}\r\n$filter = sql_select_array(FILTERS_TABLE, \'id={$filter}\');\r\n$result = data_select_field($table_id, \'{$cond_field}\',\r\n    \'status = 0 AND {$cond_table.link_field_id} = \' . intval($line[\'{$cond_table.link_field_id}\'][\'raw\']) . $deleteCond .\r\n    ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\') . \' ORDER BY id {if $record == \"last\"}DESC{/if} LIMIT 1\');\r\n$row = sql_fetch_assoc($result);\r\n{if $add_value == \'yes\'}\r\nif ($line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] != \'\') {\r\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] .= \"{$add_delim}\" . $row[\'{$cond_field}\'];\r\n} else {\r\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'{$cond_field}\'];\r\n}\r\n{else}\r\n$line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'{$cond_field}\'];\r\n{/if}' WHERE id = 42
EOT;
$sql_query3 = <<<'EOT'
calculate_tpl = '$result = data_select_field({$sub_table.subTable}, \'{$sub_table.subField}\',\r\n    \'status = 0 AND {$sub_table.link_field_id} = \' . $ID . \' ORDER BY id {if $sub_record == \"last\"}DESC{/if} LIMIT 1\');\r\n$row = sql_fetch_assoc($result);\r\n{if $add_value == \'yes\'}\r\nif ($line[\'{$res_field}\'] != \'\') {\r\n    $line[\'{$res_field}\'] .= \"{$add_delim}\" . $row[\'{$sub_table.subField}\'];\r\n} else {\r\n    $line[\'{$res_field}\'] = $row[\'{$sub_table.subField}\'];\r\n}\r\n{else}\r\n$line[\'{$res_field}\'] = $row[\'{$sub_table.subField}\'];\r\n{/if}' WHERE id = 43
EOT;
run_query($sql_query0 . $sql_query1);
run_query($sql_query0 . $sql_query2);
run_query($sql_query0 . $sql_query3);