<?php
$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = 'if (!function_exists(str_replace_crm_function_for_this_expression)) {\r\n    function str_replace_crm_function_for_this_expression(& $str_line, & $table, & $line) {\r\n        if (strpos($str_line, {literal}\'{{\'{/literal}) !== false) {\r\n            $str = substr($str_line, strpos($str_line, {literal}\'{{\'{/literal}) + 2);\r\n            $str = substr($str, 0, strpos($str, {literal}\'}}\'{/literal}));\r\n\r\n            if ($str == \'id\') {\r\n                $field = sql_select_array(FIELDS_TABLE, \'table_id = \', $table[\'id\'], \' AND type_field = 10\');\r\n            } elseif ($str == \'user_id\') {\r\n                $field = sql_select_array(FIELDS_TABLE, \'table_id = \', $table[\'id\'], \' AND type_field = 11\');\r\n            } elseif ($str == \'add_time\') {\r\n                $field = sql_select_array(FIELDS_TABLE, \'table_id = \', $table[\'id\'], \' AND type_field = 12\');\r\n            } else {\r\n                $field = sql_select_array(FIELDS_TABLE, \'table_id = \', $table[\'id\'], \' AND id = \', substr($str, 1));\r\n            }\r\n\r\n            if ($field[\'id\']) {\r\n                if ($field[\'type_field\'] == 1) {\r\n                    $replace = form_local_number($line[$str], explode(\'/\', explode(\'|\', $field[\'type_value\'])[0])[1]);\r\n                } elseif ($field[\'type_field\'] == 2 || $field[\'type_field\'] == 12) {\r\n                    $replace = form_local_time($line[$str], $field[\'type_value\']);\r\n                } elseif ($field[\'type_field\'] == 7 || $field[\'type_field\'] == 11) {\r\n                    $replace = sql_fetch_row(sql_select_field(USERS_TABLE, \'fio\', \'id = \', $line[$str]))[0];\r\n                } elseif ($field[\'type_field\'] == 5) {\r\n                    $type_value = explode(\'|\', $field[\'type_value\']);\r\n                    $line2_id = is_array($line[\'f\'.$field[\'id\']]) ? ($line[\'f\'.$field[\'id\']][\'id\'] ? $line[\'f\'.$field[\'id\']][\'id\'] : -1) : ($line[\'f\'.$field[\'id\']] ? $line[\'f\'.$field[\'id\']] : -1);\r\n                    $line2 = sql_fetch_assoc(data_select_field($type_value[\'0\'], \'f\'.$type_value[\'1\'], \'id = \', $line2_id));\r\n                    $replace = $line2[\'f\'.$type_value[\'1\']];\r\n                } else {\r\n                    $replace = $line[$str];\r\n                }\r\n                $str_line = str_replace({literal}\'{{\'{/literal}.$str.{literal}\'}}\'{/literal}, $replace, $str_line);\r\n                str_replace_crm_function_for_this_expression($str_line, $table, $line);\r\n            }\r\n        }\r\n        return false;\r\n    }\r\n}\r\n\r\n$str_line = \"{$flex_data}\";\r\nstr_replace_crm_function_for_this_expression($str_line, $table, $line);\r\n$line[\'{$result}\'] = $str_line;',
params = '{\r\n    \"title\": \"Текст: Объединение текста\",\r\n    \"help\": \"text_concatenation\",\r\n    \"parameters\": {\r\n        \"field1\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field1\",\r\n            \"description\": \"Выберите поле для шаблона\",\r\n            \"type_field\": [1,2,3,4,5,7,10,11,12],\r\n            \"help\": \"tpl_sel_field\"\r\n        },\r\n        \"result\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"result\",\r\n            \"description\": \"Поле-получатель\",\r\n            \"type_field\": 3,\r\n            \"help\": \"tpl_to_field\"\r\n        },\r\n        \"func_row\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"func_row\",\r\n            \"description\": \"Шаблон\",\r\n            \"textarea\": true,\r\n            \"help\": \"text_concatenation_func\"\r\n        },\r\n        \"flex_data\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"flex_data\",\r\n            \"description\": \"\",\r\n            \"help\": \"tpl_flex_string\"\r\n        }\r\n    }\r\n}'
WHERE id = 108
EOT;
run_query($sql_query);