<?php
$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
(108, 'Текст: Объединение текста', '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 (mb_stripos($str_line, {literal}\"{{\"{/literal}) !== false) {\r\n            $str = mb_strcut($str_line, (strpos($str_line, {literal}\"{{\"{/literal})+2) );\r\n            $str = mb_strcut($str, 0, strpos($str, {literal}\"}}\"{/literal}) );\r\n\r\n            if ($str == \'user_id\') {\r\n                $result = sql_fetch_assoc(sql_select_field(FIELDS_TABLE, \'*\', \'table_id=\'.$table[\'id\'].\' AND type_field=11\'));\r\n            } else {\r\n                $str = mb_strcut($str, 1);\r\n                $result = sql_fetch_assoc(sql_select_field(FIELDS_TABLE, \'*\', \'table_id=\'.$table[\'id\'].\' AND id=\\\'\'.$str.\'\\\'\'));\r\n            }\r\n\r\n            if ($result[\'id\']) {\r\n                if ($result[\'type_field\'] == 1 || $result[\'type_field\'] == 2 || $result[\'type_field\'] == 3 || $result[\'type_field\'] == 4 ) {\r\n                    $replace = $line[\'f\'.$result[\'id\']];\r\n                } elseif ($result[\'type_field\'] == 5) {\r\n                    $type_value  = explode(\"|\", $result[\'type_value\']);\r\n                    $result_2 = sql_fetch_assoc(data_select_field($type_value[\'0\'], \'f\'.$type_value[\'1\'], \'id=\'.( $line[\'f\'.$result[\'id\']][\'id\'] ? $line[\'f\'.$result[\'id\']][\'id\'] : -1 )));\r\n                    $replace = $result_2[\'f\'.$type_value[\'1\']];\r\n                } elseif ($result[\'type_field\'] == 7 || $result[\'type_field\'] == 11) {\r\n                    $result_2 = sql_fetch_assoc(sql_select_field(USERS_TABLE, \'fio\', \'id=\'.( $str == \'user_id\' ? $line[\'user_id\'] : $line[\'f\'.$result[\'id\']] )));\r\n                    $replace = $result_2[\'fio\'];\r\n                }\r\n\r\n                if ($str == \'user_id\') {\r\n                    $str_line = str_replace({literal}\"{{\"{/literal}.$str.{literal}\"}}\"{/literal}, $replace, $str_line);\r\n                } else {\r\n                    $str_line = str_replace({literal}\"{{f\"{/literal}.$str.{literal}\"}}\"{/literal}, $replace, $str_line);\r\n                }\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;', '{\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,11],\r\n             \"help\": \"tpl_sel_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         \"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         \"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}', '', 'text_concatenation.tpl',	'',	1,	'[1]',	7);
EOT;
run_query($sql_query);