<?php

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = "data_update({$link_table.table}, {if !$operand}EVENTS_ENABLE, {/if}
    ['{$link_table.field}' => $line['{$cur_field}']{if $cur_field_type == 5}['raw']{/if}],
    'status = 0 AND {$link_table.link_field_id} = ' . $line['id']
);
{if $link_table.field_type == 6 || $link_table.field_type == 9}
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent, 'MSIE') !== false) {
    $exp = \"\\n\\r\";
} else {
    $exp = \"\\r\\n\";
}
$files = explode($exp, $line['{$cur_field}']);
$res = data_select_field({$link_table.table}, 'id','status=0 and {$link_table.link_field_id} =' . $line['id']);
while ($row = sql_fetch_assoc($res)) {
    foreach ($files as $fname) {
        $orig_fpath = get_file_path({$cur_field|replace:'f':''}, $line['id'], $fname);
        save_data_file({$link_table.field|replace:'f':''}, $row['id'], $fname, file_get_contents($orig_fpath));
    }
}
{/if}" WHERE name = "Обновлять поле при изменении в связанной таблице"
EOT;
run_query($sql_query);
