<?php

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = "$res_field = get_field('{$cur_field1}'); // поле получатель
$source_field = get_field('{$cur_field2}'); // поле источник
if ($res_field['type_field'] == 6 || $res_field['type_field'] == 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_field2}']);
    foreach ($files as $fname) {
        $orig_fpath = get_file_path({$cur_field2|replace:'f':''}, $line['id'], $fname);
        save_data_file({$cur_field1|replace:'f':''}, $line['id'], $fname, file_get_contents($orig_fpath));
    }
    $line['{$cur_field1}'] = $line['{$cur_field2}'];
} elseif ($res_field['type_field'] == 5) {
    if (empty($line['{$cur_field2}'])) return;

    $type_value = explode('|', $res_field['type_value']);
    if ($source_field['type_field'] == 7) {
        $user_source_field = sql_select_array(USERS_TABLE, 'id='. $line['{$cur_field2}'])['fio'];
        $stmt = data_select_array($type_value['0'], 'status=0 AND f'. $type_value['1'] .' LIKE \"%'. $user_source_field .'%\"');
    } else {
        $stmt = data_select_array($type_value['0'], 'status=0 AND (id=\"'. addslashes($line['{$cur_field2}']) .'\" OR f'. $type_value['1'] .' LIKE \"%'. addslashes($line['{$cur_field2}']) .'%\")');
    }
    if ($stmt) {
        $line['{$cur_field1}'] = $stmt['id'];
    }
} else {
    $line['{$cur_field1}'] = $line['{$cur_field2}'];
}", params='{
    "title": "Копировать поле",
    "help": "tpl_field_to_field",
    "parameters": {
        "cur_field1": {
            "type": "Field",
            "name": "cur_field1",
            "help": "receiver_field",
            "description": "Выберите поле-получатель"
        },
        "cur_field2": {
            "type": "Field",
            "name": "cur_field2",
            "help": "source_field",
            "description": "Выберите поле-источник"
        }
    },
    "events": [
        {
            "type": 3,
            "field": "cur_field2"
        }
    ]
}' WHERE id = 2
EOT;
run_query($sql_query);
