<?php
$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = '$field_info = sql_fetch_row(sql_select_field(FIELDS_TABLE, \'type_value\', \'id = {$link_field|substr:1}\'))[0];\r\nlist($link_table_id, $link_field_id) = explode(\'|\', $field_info);\r\n$line[\'{$link_field}\'] = sql_fetch_row(data_select_field($link_table_id, \'id\', \"f$link_field_id = \'\", $line[\'{$source_field}\'], \"\'\"))[0];\r\n{if $addit_field}\r\nif (!$line[\'{$link_field}\']) {\r\n    $result = sql_select_field(FIELDS_LINKS_SHOW_TABLE, \'show_field\', \'field_id = {$link_field|substr:1}\');\r\n    while ($addit_field_id = sql_fetch_row($result)[0]) {\r\n        if ($line[\'{$link_field}\'] = sql_fetch_row(data_select_field($link_table_id, \'id\', \"f$addit_field_id = \'\", $line[\'{$source_field}\'], \"\'\"))[0]) {\r\n            break;\r\n        }\r\n    }\r\n}\r\n{/if}',
params = '{\r\n    \"title\": \"Заполнить поле связи по текстовому полю\",\r\n    \"help\": \"tpl_txt_field_to_link_field\",\r\n    \"parameters\": {\r\n        \"source_field\": {\r\n            \"type\": \"Field\",\r\n            \"type_field\": 3,\r\n            \"name\": \"source_field\",\r\n            \"help\": \"source_field\",\r\n            \"description\": \"Поле-источник\"\r\n        },\r\n        \"link_field\": {\r\n            \"type\": \"Field\",\r\n            \"type_field\": 5,\r\n            \"name\": \"link_field\",\r\n            \"help\": \"receiver_field\",\r\n            \"description\": \"Поле-получатель\"\r\n        },\r\n        \"addit_field\": {\r\n            \"type\": \"List\",\r\n            \"values\":{\"0\":\"Нет\",\"1\":\"Да\"},\r\n            \"name\": \"addit_field\",\r\n            \"help\": \"addit_field\",\r\n            \"description\": \"Искать по доп.полям\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"source_field\"\r\n        }\r\n    ]\r\n}'
WHERE id = 124
EOT;
run_query($sql_query);