<?php

$result = sql_select_array($config["table_prefix"] . 'calc_template', 'id=112');
if (!empty($result['calculate_tpl'])) {
    return;
}

$sql_query = 'INSERT INTO `' . $config["table_prefix"] . 'calc_template`';
$sql_query .= <<<'EOT'
 (`id`, `name`, `calculate_tpl`, `params`, `type`, `tpl_name`, `javascript_tpl`, `tpl_category`, `calc_types`, `calc_num`) VALUES
(112, 'Текст: Копировать как текст', '$type_value = sql_fetch_assoc(sql_select_field(FIELDS_TABLE, \'type_field, type_value\', \'id=\'. ( (int)mb_substr(\'{$cur_field2}\', 1) > 0 ? (int)mb_substr(\'{$cur_field2}\', 1) : -1 ) ));\r\n\r\n$cur_field2_type = \'{$cur_field2}\' == \'user_id\' ? 11 : $type_value[\'type_field\'];\r\nswitch ($cur_field2_type) {\r\n    case 1: // число\r\n    case 3: // текст\r\n    case 4: // список\r\n        $line[\'{$cur_field1}\'] = $line[\'{$cur_field2}\'];\r\n        break;\r\n\r\n    case 2: // дата\r\n        $line[\'{$cur_field1}\'] = date(\'d.m.Y H:i:s\', strtotime($line[\'{$cur_field2}\']));\r\n        break;\r\n\r\n    case 5: // связь\r\n        if ($type_value[\'type_value\']) {\r\n            $arr = explode(\"|\", $type_value[\'type_value\']);\r\n            $result = sql_fetch_assoc(data_select_field($arr[\'0\'], \'f\'.$arr[\'1\'], \'id=\'. ( $line[\'{$cur_field2}\'][\'id\'] ? $line[\'{$cur_field2}\'][\'id\'] : -1 )));\r\n            $line[\'{$cur_field1}\'] = $result[\'f\'.$arr[\'1\']];\r\n        }\r\n        break;\r\n\r\n    case 7: // пользователь\r\n    case 11: // кто добавил\r\n        $result = sql_fetch_assoc(sql_select_field(USERS_TABLE, \'fio\', \'id=\'. ( $line[\'{$cur_field2}\'] ? $line[\'{$cur_field2}\'] : -1 )));\r\n        $line[\'{$cur_field1}\'] = $result[\'fio\'];    // возвращает id пользователя\r\n        break;\r\n    case 14:\r\n    	$line[\'{$cur_field1}\'] = sql_fetch_assoc(sql_select_field(GROUPS_TABLE, \'name\', \'id=\' . ($line[\'{$cur_field2}\'] ? $line[\'{$cur_field2}\'] : -1) ))[\'name\'];\r\n        break;\r\n}', '{\r\n    \"title\": \"Текст: Копировать как текст\",\r\n    \"help\": \"tpl_field_to_field\",\r\n    \"parameters\": {\r\n        \"cur_field1\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field1\",\r\n            \"help\": \"receiver_field\",\r\n             \"type_field\": [3],\r\n            \"description\": \"Выберите поле-получатель\"\r\n        },\r\n        \"cur_field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field2\",\r\n            \"help\": \"source_field\",\r\n             \"type_field\": [1,2,3,4,5,7,11,14],\r\n            \"description\": \"Выберите поле-источник\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cur_field2\"\r\n        }\r\n    ]\r\n}', '', 'change_field_2.tpl', '', '1', '[1]', '7');
EOT;
run_query($sql_query);
