<?php
run_query('DELETE FROM ' . $config['table_prefix'] . 'calc_template');
$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
(1,	'Сумма по подтаблице',	'if ($line[\'{$gen_field.link_field_id}\'][\'raw\'] > 0) {\r\n    $filter = sql_select_array(FILTERS_TABLE, \'id = \', get_filter_id({$filter|default:0}));\r\n    $result = data_select_field(\r\n        $table_id, \r\n        \'sum({$cur_field}) as f_sum\', \r\n        \'status = 0\' . ($event[\'type\'] === \'delete\' ? \' AND id != \' . $ID : \'\') . \r\n        \' AND {$gen_field.link_field_id} = \' . $line[\'{$gen_field.link_field_id}\'][\'raw\'] . \r\n        ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\')\r\n    );\r\n    $row = sql_fetch_assoc($result);\r\n    $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] = $row[\'f_sum\'];\r\n}',	'{\r\n    \"title\": \"Сумма по подтаблице\",\r\n    \"help\": \"tpl_sum_from_subtable\",\r\n    \"parameters\": {\r\n        \"cur_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field\",\r\n            \"description\": \"Выберите поле, по которому считать сумму\",\r\n            \"type_field\": 1,\r\n            \"help\": \"tpl_sum_field\"\r\n        },\r\n        \"filter\": {\r\n            \"type\": \"Filter\",\r\n            \"name\": \"filter\",\r\n            \"description\": \"Выберите фильтр\",\r\n            \"help\": \"tpl_sel_filter\"\r\n        },\r\n        \"gen_field\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"gen_field\",\r\n            \"description\": \"Выберите основную таблицу\",\r\n            \"field_description\": \"Выберите поле, в которое подтягивать сумму\",\r\n            \"only_parent\": true,\r\n            \"type_field\": 1,\r\n            \"help\": \"tpl_sel_table\",\r\n            \"field_help\": \"tpl_sel_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cur_field\"\r\n        },\r\n        {\r\n            \"type\": 4,\r\n            \"field\": \"\"\r\n        },\r\n        {\r\n            \"type\": 7,\r\n            \"field\": \"\"\r\n        }\r\n    ]\r\n}',	'calc',	'calc_1.tpl',	'',	2,	'[2]',	1),
(2,	'Копировать поле',	'{if $list eq 0}\r\n{if $cur_field1_type eq 6 || $cur_field1_type eq 9}\r\n$user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\nif (strpos($user_agent, \"MSIE\") !== false) {\r\n    $exp = \"\\n\\r\";\r\n} else {\r\n    $exp = \"\\r\\n\";    \r\n}\r\n$files = explode($exp, $line[\'{$cur_field2}\']);\r\nforeach ($files as $fname) {\r\n    $orig_fpath = get_file_path({$cur_field2|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n    save_data_file({$cur_field1|replace:\'f\':\'\'}, $line[\'id\'], $fname, file_get_contents($orig_fpath));\r\n}\r\n{/if}\r\n$line[\'{$cur_field1}\'] = $line[\'{$cur_field2}\'];\r\n{else}\r\nif (!$line[\'{$cur_field1}\']) {\r\n{if $cur_field1_type eq 6 || $cur_field1_type eq 9}\r\n    $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n    if (strpos($user_agent, \"MSIE\") !== false) {\r\n        $exp = \"\\n\\r\";\r\n    } else {\r\n        $exp = \"\\r\\n\";    \r\n    }\r\n    $files = explode($exp, $line[\'{$cur_field2}\']);\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$cur_field2|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n        save_data_file({$cur_field1|replace:\'f\':\'\'}, $line[\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n{/if}\r\n    $line[\'{$cur_field1}\'] = $line[\'{$cur_field2}\'];\r\n}\r\n{/if}',	'{\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            \"description\": \"Выберите поле-получатель\"\r\n        },\r\n        \"cur_field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field2\",\r\n            \"help\": \"source_field\",\r\n            \"description\": \"Выберите поле-источник\"\r\n        },\r\n        \"list\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"list\",\r\n            \"help\": \"Overwrite_field\",\r\n            \"description\": \"Перезаписать поле\",\r\n            \"values\": {\r\n              \"0\": \"Да\",\r\n              \"1\": \"Нет\"\r\n            }\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cur_field2\"\r\n        }\r\n    ]\r\n}',	'calc',	'calc_2.tpl',	'',	1,	'[1]',	1),
(5,	'Копировать поля из связанной таблицы',	'$line[\'{$cur_field}\'] = $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'];\r\n{if $cur_field_type == 6 || $cur_field_type == 9}\r\n    $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n    if (strpos($user_agent, \"MSIE\") !== false) {\r\n        $exp = \"\\n\\r\";\r\n    } else {\r\n        $exp = \"\\r\\n\";    \r\n    }\r\n    $files = explode($exp, $line[\'{$gen_field.field}\']);\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$cur_field|replace:\'f\':\'\'}, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname);\r\n        save_data_file({$gen_field.field|replace:\'f\':\'\'}, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n{/if}',	'{\r\n    \"title\": \"Копировать поля из связанной таблицы\",\r\n    \"help\": \"tpl_link_to_field\",\r\n    \"parameters\": {\r\n        \"cur_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field\",\r\n            \"description\": \"Выберите поле, в которое сохранять значение\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_result_field\"\r\n        },\r\n        \"gen_field\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"gen_field\",\r\n            \"description\": \"Выберите таблицу, из которой тянуть значение\",\r\n            \"field_description\": \"Выберите поле, из которого тянуть значение\",\r\n            \"only_parent\": \"true\",\r\n            \"help\": \"tpl_sel_table\",\r\n            \"field_help\": \"tpl_sel_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"gen_field\"\r\n        }\r\n    ]\r\n}',	'calc',	'calc_5.tpl',	'',	3,	'[1]',	1),
(6,	'Дата: Установить текущую дату',	'$line[\'{$cur_field}\'] = date(\'Y-m-d H:i:s\');',	'{\r\n    \"title\": \"Дата: Установить текущую дату\",\r\n    \"help\": \"tpl_cur_date\",\r\n    \"parameters\": {\r\n        \"cur_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field\",\r\n            \"description\": \"Выберите поле, в которое сохранять дату\",\r\n            \"type_field\": 2,\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_cur_date_field\"\r\n        }        \r\n    }\r\n}',	'button',	'cur_date.tpl',	'',	1,	'[]',	6),
(7,	'Копировать поле',	'{if $list eq 0}\r\n{if $cur_field1_type eq 6 || $cur_field1_type eq 9}\r\n$user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\nif (strpos($user_agent, \"MSIE\") !== false) {\r\n    $exp = \"\\n\\r\";\r\n} else {\r\n    $exp = \"\\r\\n\";    \r\n}\r\n$files = explode($exp, $line[\'{$cur_field2}\']);\r\nforeach ($files as $fname) {\r\n    $orig_fpath = get_file_path({$cur_field2|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n    save_data_file({$cur_field1|replace:\'f\':\'\'}, $line[\'id\'], $fname, file_get_contents($orig_fpath));\r\n}\r\n{/if}\r\n$line[\'{$cur_field1}\'] = $line[\'{$cur_field2}\'];\r\n{else}\r\nif (!$line[\'{$cur_field1}\']) {\r\n{if $cur_field1_type eq 6 || $cur_field1_type eq 9}\r\n    $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n    if (strpos($user_agent, \"MSIE\") !== false) {\r\n        $exp = \"\\n\\r\";\r\n    } else {\r\n        $exp = \"\\r\\n\";    \r\n    }\r\n    $files = explode($exp, $line[\'{$cur_field2}\']);\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$cur_field2|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n        save_data_file({$cur_field1|replace:\'f\':\'\'}, $line[\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n{/if}\r\n    $line[\'{$cur_field1}\'] = $line[\'{$cur_field2}\'];\r\n}\r\n{/if}',	'{\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            \"description\": \"Выберите поле-получатель\"\r\n        },\r\n        \"cur_field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field2\",\r\n            \"help\": \"source_field\",\r\n            \"description\": \"Выберите поле-источник\"\r\n        },\r\n        \"list\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"list\",\r\n            \"help\": \"Overwrite_field\",\r\n            \"description\": \"Перезаписать поле\",\r\n            \"values\": {\r\n              \"0\": \"Да\",\r\n              \"1\": \"Нет\"\r\n            }\r\n        }\r\n    }\r\n}',	'button',	'change_field.tpl',	'',	1,	'[]',	1),
(8,	'Копировать поля из связанной таблицы',	'$line[\'{$cur_field}\'] = $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'];\r\n{if $cur_field_type == 6 || $cur_field_type == 9}\r\n    $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n    if (strpos($user_agent, \"MSIE\") !== false) {\r\n        $exp = \"\\n\\r\";\r\n    } else {\r\n        $exp = \"\\r\\n\";    \r\n    }\r\n    $files = explode($exp, $line[\'{$gen_field.field}\']);\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$cur_field|replace:\'f\':\'\'}, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname);\r\n        save_data_file({$gen_field.field|replace:\'f\':\'\'}, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n{/if}',	'{\r\n    \"title\": \"Копировать поля из связанной таблицы\",\r\n    \"help\": \"tpl_link_to_field\",\r\n    \"parameters\": {\r\n        \"cur_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field\",\r\n            \"description\": \"Выберите поле, в которое сохранять значение\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_to_field\"\r\n        },\r\n        \"gen_field\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"gen_field\",\r\n            \"description\": \"Выберите таблицу, из которой тянуть значение\",\r\n            \"field_description\": \"Выберите поле, из которого тянуть значение\",\r\n            \"only_parent\": \"true\",\r\n            \"help\": \"tpl_table_copy_line\",\r\n            \"field_help\": \"source_field\"\r\n        }\r\n    }\r\n}',	'button',	'change_field_on_link.tpl',	'',	3,	'[]',	1),
(10,	'Дата: Копировать дату или время',	'if(\'{$put_value}\' == 1){\r\n    $line[\'{$put_field}\'] = date(\'d.m.Y\', strtotime($line[\'{$cond_field}\']));\r\n} else if(\'{$put_value}\' == 2){\r\n    $line[\'{$put_field}\'] = date(\'H:i:s\', strtotime($line[\'{$cond_field}\']));\r\n}',	'{\r\n    \"title\": \"Дата: Копировать дату или время\",\r\n    \"help\": \"tpl_date_to_field\",\r\n    \"parameters\": {\r\n        \"cond_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cond_field\",\r\n            \"type_field\": [2,12],\r\n            \"description\": \"Выберите поле, из которого будут тянутся данные\",\r\n            \"help\": \"tpl_source_field\"\r\n        },\r\n        \"put_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"put_field\",\r\n            \"type_field\": \"3\",\r\n            \"description\": \"Выберите поле, которое должно быть заполнено данными\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_to_field\"\r\n        },\r\n        \"put_value\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"put_value\",\r\n            \"values\":{\"1\":\"Дата\",\"2\":\"Время\"},\r\n            \"description\": \"Выберите данные, которые должны быть переданы\",\r\n            \"help\": \"tpl_list_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cond_field\"\r\n        }\r\n    ]\r\n}',	'calc',	'calc_8.tpl',	'',	1,	'[1]',	13),
(11,	'Текст: Найти и заменить текст',	'$line[\'{$cond_field}\'] = str_replace(\'{$give_value}\', \'{$put_value}\', $line[\'{$cond_field}\']);',	'{\n    \"title\": \"Текст: Найти и заменить текст\",\n    \"help\": \"tpl_replace\",\n    \"parameters\": {\n        \"cond_field\": {\n            \"type\": \"Field\",\n            \"name\": \"cond_field\",\n            \"type_field\": \"3\",\n            \"description\": \"Выберите поле, которое должно быть изменено\",\n            \"help\": \"tpl_put_field\"\n        },\n        \"give_value\": {\n            \"type\": \"String\",\n            \"name\": \"give_value\",\n            \"description\": \" Введите значение для поиска\",\n            \"help\": \"tpl_search_value\"\n        },\n        \"put_value\": {\n            \"type\": \"String\",\n            \"name\": \"put_value\",\n            \"description\": \"Введите значение для замены\",\n            \"help\": \"tpl_replace_value\"\n        }\n    },\n    \"events\": [\n        {\n            \"type\": 3,\n            \"field\": \"cond_field\"\n        }\n    ]\n}',	'calc',	'calc_9.tpl',	'',	1,	'[1]',	7),
(12,	'Среднее значение по подтаблице',	'$filter = sql_select_array(FILTERS_TABLE, \'id = \', get_filter_id({$filter|default:0}));\r\n$result = data_select_field(\r\n    $table_id, \r\n    \'COUNT(id) AS cnt, SUM({$cond_field}) AS sum\', \r\n    \'status = 0\' . ($event[\'type\'] === \'delete\' ? \' AND id != \' . $ID : \'\') . \r\n    \' AND {$cond_table.link_field_id} = \' . $line[\'{$cond_table.link_field_id}\'][\'raw\'] . \r\n    ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\')\r\n);\r\nif ($row = sql_fetch_assoc($result)) {\r\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'sum\'] / $row[\'cnt\'];\r\n} else {\r\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = 0;\r\n}',	'{\r\n    \"title\": \"Среднее значение по подтаблице\",\r\n    \"help\": \"tpl_average\",\r\n    \"parameters\": {\r\n        \"cond_table\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"cond_table\",\r\n            \"only_parent\": \"false\",\r\n            \"type_field\": [1,3],\r\n            \"description\": \"Выберите таблицу, в которую будет записан результат\",\r\n            \"field_description\": \"Выберите поле, в которое будет записан результат\",\r\n            \"help\": \"tpl_sel_table\",\r\n            \"field_help\": \"tpl_sel_field\"\r\n        },\r\n        \"filter\": {\r\n            \"type\": \"Filter\",\r\n            \"name\": \"filter\",\r\n            \"description\": \"Выберите фильтр\",\r\n            \"help\": \"tpl_sel_filter\"\r\n        },\r\n        \"cond_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cond_field\",\r\n            \"type_field\": \"1\",\r\n            \"description\": \"Выберите поле, по которому будет считаться среднее арифметическое\",\r\n            \"help\": \"tpl_source_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cond_field\"\r\n        },\r\n        {\r\n            \"type\": 4,\r\n            \"field\": \"\"\r\n        },\r\n        {\r\n            \"type\": 7,\r\n            \"field\": \"\"\r\n        }\r\n    ]\r\n}',	'calc',	'calc_10.tpl',	'',	2,	'[2]',	2),
(13,	'Текст: Работа с ФИО',	'$array = explode(\" \", $line[\'{$cond_field}\']);\nif (\'{$put_value}\' === \'1\') {\n    $line[\'{$cond_field}\'] = $array[\'1\'];\n} else {\n    $line[\'{$cond_field}\'] = $array[\'1\'] . \' \' . $array[\'2\'];\n}',	'{\n    \"title\": \"Текст: Работа с ФИО\",\n    \"help\": \"tpl_explode_name\",\n    \"parameters\": {\n        \"cond_field\": {\n            \"type\": \"Field\",\n            \"name\": \"cond_field\",\n            \"type_field\": \"3\",\n            \"description\": \"Выберите поле, содержащее ФИО\",\n            \"help\": \"tpl_source_field\"\n        },\n        \"put_value\": {\n            \"type\": \"List\",\n            \"name\": \"put_value\",\n            \"values\":{\"1\":\"Имя\",\"2\":\"Имя и отчество\"},\n            \"description\": \"Выберите данные, которые останутся в поле.\",\n            \"help\": \"tpl_option\"\n        }\n    },\n    \"events\": [\n        {\n            \"type\": 3,\n            \"field\": \"cond_field\"\n        }\n    ]\n}',	'calc',	'calc_11.tpl',	'',	1,	'[1]',	8),
(14,	'Отмена редактирования поля',	'$line[\'{$field}\'] = $event[\'changed\'][substr(\'{$field}\',1)][\'old\'];',	'{\r\n    \"title\": \"Отмена редактирования поля\",\r\n    \"help\": \"tpl_disabled_edit_field\",\r\n    \"parameters\": {\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Запрет редактирования поля\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_cancel_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"field\"\r\n        }\r\n    ]\r\n}',	'calc',	'disabled_edit_field.tpl',	'',	6,	'[1]',	1),
(15,	'Перевод суммы из числа в текст',	'$line[\'{$field2}\'] = num2str($line[\'{$field1}\'], \"\", \"\", 0, 0, 0, \"\");\r\n',	'{\r\n    \"title\": \"Перевод суммы из числа в текст\",\r\n    \"help\": \"tpl_num_to_text\",\r\n    \"parameters\": {\r\n        \"field1\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field1\",\r\n            \"type_field\": \"1\",\r\n            \"description\": \"Выберите исходное поле(число)\",\r\n            \"no_system\": true,\r\n            \"help\": \"source_field\"\r\n        },\r\n        \"field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field2\",\r\n            \"type_field\": \"3\",\r\n            \"description\": \"Выберите поле записи суммы (текст)\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_to_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"field1\"\r\n        }\r\n    ]\r\n}',	'calc',	'intsum_to_stringsum.tpl',	'',	1,	'[1]',	14),
(20,	'Сохранение пользователя, который измненил поле',	'$line[\'{$value}\'] = $user[\'id\'];',	'{\n    \"title\": \"Сохранение пользователя, который измненил поле\",\n    \"help\": \"tpl_save_user_changed_field\",\n    \"parameters\": {\n        \"value\": {\n            \"type\": \"Field\",\n            \"name\": \"value\",\n            \"type_field\": 7,\n            \"description\": \"Выберите поле, в которое сохранять значение\",\n            \"no_system\": true,\n            \"help\": \"tpl_to_field\"\n        }\n    },\n    \"events\": [\n        {\n            \"type\": 1,\n            \"field\": \"\"\n        }\n    ]\n}',	'calc',	'calc_17.tpl',	'',	1,	'[1]',	15),
(21,	'Число: Математическая формула',	'$string = \'{$flex_data}\';\nif ($string) {\n    {literal}$string = str_replace(\'{f\', \'$line[\\\'f\', $string);\n    $string = str_replace(\'}\', \'\\\']\', $string);{/literal}\n    $string = \'$line[\\\'{$result}\\\'] = \' . $string . \';\';\n    eval($string);\n}',	'{\r\n     \"title\": \"Число: Математическая формула\",\r\n     \"help\": \"tpl_math_formulas\",\r\n     \"parameters\": {\r\n         \"field1\": {\r\n             \"type\": \"Field\",\r\n             \"name\": \"field1\",\r\n             \"description\": \"Выберите поле\",\r\n             \"type_field\": 1,\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\": \"tpl_func_field\"\r\n         },\r\n         \"result\": {\r\n             \"type\": \"Field\",\r\n             \"name\": \"result\",\r\n             \"description\": \"Выберите поле записи результата\",\r\n             \"type_field\": 1,\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}',	'calc',	'math_functions.tpl',	'',	1,	'[1]',	6),
(22,	'Сброс нумерации с нового года',	'if ($event[\'line_id\'] === \'new\') {\r\n    $res = data_select($table_id, \'status=0 and id!=\' . $line[\'id\'] . \' ORDER BY id DESC LIMIT 1\');\r\n    $row = sql_fetch_assoc($res);\r\n    if (date(\'Y\', strtotime($row[\'add_time\'])) >= date(\'Y\')) {\r\n        $line[\'{$field}\'] = $row[\'{$field}\'] + 1;\r\n    } else {\r\n        $line[\'{$field}\'] = 1;\r\n    }\r\n}',	'{\r\n    \"title\": \"Сброс нумерации с нового года\",\r\n    \"help\": \"tpl_calc_numeric\",\r\n    \"parameters\": {\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Выберите поле\",\r\n            \"type_field\": 1,\r\n            \"help\": \"tpl_reset_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 1,\r\n            \"field\": \"\"\r\n        }\r\n    ]\r\n}\r\n',	'calc',	'autonum.tpl',	'',	6,	'[1]',	2),
(24,	'Очистить поле',	'{if $field1_type eq \"5\" || $field1_type eq 1 || $field1_type eq 7}\n    $line[\'{$field1}\'] = 0;\n{else}\n    $line[\'{$field1}\'] = \'\';\n{/if}',	'{\r\n    \"title\": \"Очистить поле\",\r\n    \"help\": \"tpl_clear_field\",\r\n    \"parameters\": {\r\n        \"field1\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field1\",\r\n            \"description\": \"Выберите поле\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_clear_field\"\r\n        }\r\n    }\r\n}',	'button',	'field_clearing.tpl',	'',	1,	'[]',	3),
(25,	'Копировать поле из основной записи в подтаблицу',	'data_update({$subtable.table}, EVENTS_ENABLE, \r\n    [\'{$subtable.field}\' => $line[\'{$field}\']{if $subtable.field_type == 5}[\'raw\']{/if}], \r\n    \'status = 0 AND {$subtable.link_field_id} = \' . $line[\'id\']\r\n);\r\n{if $subtable.field_type == 6 || $subtable.field_type == 9}\r\n$user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\nif (strpos($user_agent, \"MSIE\") !== false) {\r\n    $exp = \"\\n\\r\";\r\n} else {\r\n    $exp = \"\\r\\n\";    \r\n}\r\n$files = explode($exp, $line[\'{$field}\']);\r\n$res = data_select_field({$subtable.table}, \'id\',\'status=0 and {$subtable.link_field_id} =\' . $line[\'id\']);\r\nwhile ($row = sql_fetch_assoc($res)) {\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$field|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n        save_data_file({$subtable.field|replace:\'f\':\'\'}, $row[\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n}\r\n{/if}',	'{\r\n    \"title\": \"Копировать поле из основной записи в подтаблицу\",\r\n    \"help\": \"tpl_from_table_in_subtable\",\r\n    \"parameters\": {\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Выберите поле, из которого тянуть данные\",\r\n            \"help\": \"source_field\"\r\n        },\r\n        \"subtable\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"subtable\",\r\n            \"description\": \"Выберите подтаблицу\",\r\n            \"field_description\": \"Выберите поле, в которое тянуть значение\",\r\n            \"no_system\": true,\r\n            \"only_subtables\": true,\r\n            \"help\": \"tpl_table_copy_line\",\r\n            \"field_help\": \"tpl_to_field\"\r\n        }\r\n    }\r\n}',	'button',	'from_table_in_subtable.tpl',	'',	2,	'[]',	1),
(26,	'Отправить шаблон рассылки',	'$true = send_template({$send_tpl}, \"id = \" . $line[\'id\']);\r\n if ($true) {\r\n    $display_message .= $lang[\'Mailer_started\'] .\" <a style=\'font-size:16px;\' href=edit_mailing.php>\";\r\n    $display_message .= $lang[\'Active_mailer\'] . \"</a>\";\r\n    display_notification($display_message, 1);\r\n}',	'{\r\n    \"title\": \"Отправить шаблон рассылки\",\r\n    \"help\": \"tpl_send\",\r\n    \"parameters\": {\r\n        \"send_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"send_tpl\",\r\n            \"description\": \"Шаблон рассылки\",\r\n            \"tpl_type\": \"send\",\r\n            \"help\": \"tpl_send_template\"\r\n        }\r\n    }\r\n}',	'button',	'tpl_send.tpl',	'',	4,	'[]',	1),
(27,	'Изменить поля в связанной таблице',	'$line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] = $line[\'{$cur_field}\'];',	'{\r\n    \"title\": \"Изменить поля в связанной таблице\",\r\n    \"help\": \"tpl_link_to_table\",\r\n    \"parameters\": {\r\n        \"cur_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field\",\r\n            \"description\": \"Выберите поле, из которого тянуть значение\",\r\n            \"help\": \"source_field\"\r\n        },\r\n        \"gen_field\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"gen_field\",\r\n            \"description\": \"Выберите таблицу, в которое сохранять\",\r\n            \"field_description\": \"Выберите поле, в которое сохранять\",\r\n            \"only_parent\": \"true\",\r\n            \"help\": \"tpl_to_table\",\r\n            \"field_help\": \"tpl_to_field\"\r\n        }\r\n    }\r\n}',	'button',	'tpl_link_to_table.tpl',	'',	3,	'[]',	2),
(28,	'Копировать значение поля до изменения',	'$line[\'{$field}\'] = $event[\'changed\'][substr(\'{$field2}\',1)][\'old\'];',	'{\r\n    \"title\": \"Копировать значение поля до изменения\",\r\n    \"help\": \"tpl_save_old_value\",\r\n    \"parameters\": {\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Выберите поле записи\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_put_field\"\r\n        },\r\n        \"field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field2\",\r\n            \"description\": \"Выберите поле, чье старое значение нужно сохранить\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_old_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"field2\"\r\n        }\r\n    ]\r\n}',	'calc',	'save_old_value.tpl',	'',	6,	'[1]',	3),
(29,	'Текст: Найти и заменить текст',	'$line[\'{$cond_field}\'] = str_replace(\'{$give_value}\', \'{$put_value}\', $line[\'{$cond_field}\']);',	'{\r\n    \"title\": \"Текст: Найти и заменить текст\",\r\n    \"help\": \"tpl_replace\",\r\n    \"parameters\": {\r\n        \"cond_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cond_field\",\r\n            \"type_field\": \"3\",\r\n            \"description\": \"Выберите поле, которое должно быть изменено\",\r\n            \"help\": \"tpl_put_field\"\r\n        },\r\n        \"give_value\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"give_value\",\r\n            \"description\": \" Введите значение для поиска\",\r\n            \"help\": \"tpl_search_value\"\r\n        },\r\n        \"put_value\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"put_value\",\r\n            \"description\": \"Введите значение для замены\",\r\n            \"help\": \"tpl_replace_value\"\r\n        }\r\n    }\r\n}',	'button',	'tpl_replace.tpl',	'',	1,	'[]',	9),
(30,	'Дата: Изменить дату',	'$line[\'{$cur_field3}\'] = date(\'Y-m-d H:i:s\', strtotime($line[\'{$cur_field1}\']) {$operand} $line[\'{$cur_field2}\'] * 3600 * 24);',	'{\r\n    \"title\": \"Дата: Изменить дату\",\r\n    \"help\": \"tpl_add_to_date\",\r\n    \"parameters\": {\r\n        \"cur_field1\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field1\",\r\n            \"description\": \"Выберите исходную дату\",\r\n            \"type_field\": [2,12],\r\n            \"help\": \"tpl_source_field\"\r\n        },\r\n        \"operand\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"operand\",\r\n            \"description\": \"Выберите опцию\",\r\n            \"values\": {\r\n              \"+\": \"Прибавить\",\r\n              \"-\": \"Отнять\"\r\n            },\r\n            \"help\": \"tpl_option\"\r\n        },\r\n        \"cur_field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field2\",\r\n            \"description\": \"Выберите поле с числом дней\",\r\n            \"type_field\": 1,\r\n            \"help\": \"tpl_field_days\"\r\n        },\r\n        \"cur_field3\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field3\",\r\n            \"description\": \"Выберите поле записи результата\",\r\n            \"type_field\": 2,\r\n            \"help\": \"tpl_to_field\"\r\n        }\r\n    }\r\n}',	'button',	'tpl_add_to_date.tpl',	'',	1,	'[]',	8),
(31,	'Копировать поле из основной записи в подтаблицу',	'data_update({$subtable.table}, {if $operand}EVENTS_ENABLE, {/if}\r\n    [\'{$subtable.field}\' => $line[\'{$field}\']{if $subtable.field_type == 5}[\'raw\']{/if}], \r\n    \'status = 0 AND {$subtable.link_field_id} = \' . $line[\'id\']\r\n);\r\n{if $subtable.field_type == 6 || $subtable.field_type == 9}\r\n$user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\nif (strpos($user_agent, \"MSIE\") !== false) {\r\n    $exp = \"\\n\\r\";\r\n} else {\r\n    $exp = \"\\r\\n\";    \r\n}\r\n$files = explode($exp, $line[\'{$field}\']);\r\n$res = data_select_field({$subtable.table}, \'id\',\'status=0 and {$subtable.link_field_id} =\' . $line[\'id\']);\r\nwhile ($row = sql_fetch_assoc($res)) {\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$field|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n        save_data_file({$subtable.field|replace:\'f\':\'\'}, $row[\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n}\r\n{/if}',	'{\r\n    \"title\": \"Копировать поле из основной записи в подтаблицу\",\r\n    \"help\": \"tpl_from_table_in_subtable\",\r\n    \"parameters\": {\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Выберите поле, из которого тянуть данные\",\r\n            \"help\": \"source_field\"\r\n        },\r\n        \"subtable\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"subtable\",\r\n            \"description\": \"Выберите подтаблицу\",\r\n            \"field_description\": \"Выберите поле, в которое тянуть значение\",\r\n            \"no_system\": true,\r\n            \"only_subtables\": true,\r\n            \"help\": \"tpl_table_copy_line\",\r\n            \"field_help\": \"tpl_to_field\"\r\n        },\r\n        \"operand\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"operand\",\r\n            \"description\": \"Запуск вычислений\",\r\n            \"values\": {\r\n              \"1\": \"Да\",\r\n              \"2\": \"Нет\"\r\n            },\r\n            \"help\": \"tpl_event_on\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"field\"\r\n        }\r\n    ]\r\n}',	'calc',	'from_table_in_subtable.tpl',	'',	2,	'[4]',	3),
(33,	'Удалить запись',	'if ($line[\'status\'] != 2) {\r\n    $true = data_update(\r\n        $table_id,\r\n        array(\r\n            \'status\' => 2\r\n        ),\r\n        \'status IN (0,1) AND id = \' . $line[\'id\']\r\n    );\r\n}',	'{\r\n    \"title\": \"Удаление записи\",\r\n    \"help\": \"note_delete\"\r\n}',	'button',	'null.tpl',	'',	5,	'[]',	1),
(34,	'Архивировать запись',	'if ($line[\'status\'] != 1) {\r\n    $true = data_update(\r\n        $table_id,\r\n        array(\r\n            \'status\' => 1\r\n        ),\r\n        \'status IN (0,2) AND id = \' . $line[\'id\']\r\n    );\r\n}',	'{\r\n    \"title\": \"Архивация записи\",\r\n    \"help\": \"note_archive\"\r\n}',	'button',	'null.tpl',	'',	5,	'[]',	2),
(35,	'Восстановить запись',	'if ($line[\'status\'] != 0) {\n    $true = data_update(\n        $table_id,\n        array(\n            \'status\' => 0\n        ),\n        \'status IN (1,2) AND id = \' . $line[\'id\']\n    );\n}',	'{\n    \"title\": \"Восстановить запись\",\n    \"help\": \"note_restore\"\n}',	'button',	'null.tpl',	'',	5,	'[]',	3),
(36,	'Количество записей в подтаблице',	'$filter = sql_select_array(FILTERS_TABLE, \'id = \', get_filter_id({$filter|default:0}));\r\n$result = data_select_field(\r\n    $table_id,\r\n    \'COUNT(id) AS cnt\',\r\n    \'status = 0\' . ($event[\'type\'] === \'delete\' ? \' AND id != \' . $ID : \'\') . \r\n    \' AND {$cond_table.link_field_id} = \' . $line[\'{$cond_table.link_field_id}\'][\'raw\'] . \r\n    ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\')\r\n);\r\n$row = sql_fetch_assoc($result);\r\n{if $operand == 1}\r\n    $filter_str = \'\';\r\n    if (isset($filter)) {\r\n        $filter_str = \'&filter=\' . $filter[\'id\'];\r\n    }\r\n    $line_link = substr(\"{$cond_table.link_field_id}\", 1);\r\n    $url = $config[\'site_url\'] . \"/fields.php?table=\" . $table_id . \"&rel_search&rel_table={$cond_table.table}&rel_line=\" . \r\n    $line[\'{$cond_table.link_field_id}\'][\'raw\'] . \"&rel_field=\" . $line_link . $filter_str;\r\n    {if $cond_table.field_type eq 3}\r\n        $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = \"<a href=\'\" . $url . \"\'>\" . $row[\'cnt\'] . \"</a>\";\r\n    {else} \r\n        $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'cnt\'];\r\n    {/if}\r\n{else}\r\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'cnt\'];\r\n{/if}',	'{\r\n    \"title\": \"Количество записей в подтаблице\",\r\n    \"help\": \"subnotes_count\",\r\n    \"parameters\": {\r\n        \"cond_table\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"cond_table\",\r\n            \"only_parent\": \"true\",\r\n            \"description\": \"Выберите таблицу, в которую будет записан результат\",\r\n            \"type_field\": [1,3],\r\n            \"help\": \"main_talbe\",\r\n            \"field_description\": \"Выберите поле, в которое будет записан результат\",\r\n            \"field_help\": \"main_table_field\"\r\n        },\r\n        \"filter\": {\r\n            \"type\": \"Filter\",\r\n            \"name\": \"filter\",\r\n            \"description\": \"Выберите фильтр\",\r\n            \"help\": \"filter_help\"\r\n        },\r\n        \"operand\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"operand\",\r\n            \"description\": \"Добавить ссылку на записи\",\r\n            \"values\": {\r\n              \"1\": \"Да\",\r\n              \"2\": \"Нет\"\r\n            },\r\n            \"help\": \"link_in_res_table\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 1,\r\n            \"field\": \"\"\r\n        },\r\n        {\r\n            \"type\": 4,\r\n            \"field\": \"\"\r\n        },\r\n        {\r\n            \"type\": 7,\r\n            \"field\": \"\"\r\n        }\r\n    ]\r\n}',	'calc',	'subnotes_count.tpl',	'',	2,	'[2]',	4),
(37,	'Удалить запись',	'if ($line[\'status\'] != 2) {\n    $true = data_update(\n        $table_id,\n        array(\n            \'status\' => 2\n        ),\n        \'status IN (0,1) AND id = \' . $line[\'id\']\n    );\n}',	'{\n    \"title\": \"Удалить запись\",\n    \"help\": \"note_delete\",\n    \"events\": [\n        {\n            \"type\": 1,\n            \"field\": \"\"\n        }\n    ]\n}',	'calc',	'null.tpl',	'',	5,	'[1]',	1),
(38,	'Архивировать запись',	'if ($line[\'status\'] != 1) {\n    $true = data_update(\n        $table_id,\n        array(\n            \'status\' => 1\n        ),\n        \'status IN (0,2) AND id = \' . $line[\'id\']\n    );\n}',	'{\n    \"title\": \"Архивировать запись\",\n    \"help\": \"note_archive\",\n    \"events\": [\n        {\n            \"type\": 1,\n            \"field\": \"\"\n        }\n    ]\n}',	'calc',	'null.tpl',	'',	5,	'[1]',	2),
(39,	'Восстановить запись',	'if ($line[\'status\'] != 0) {\n    $true = data_update(\n        $table_id,\n        array(\n            \'status\' => 0\n        ),\n        \'status IN (1,2) AND id = \' . $line[\'id\']\n    );\n}',	'{\n    \"title\": \"Восстановить запись\",\n    \"help\": \"note_restore\",\n    \"events\": [\n        {\n            \"type\": 1,\n            \"field\": \"\"\n        }\n    ]\n}',	'calc',	'null.tpl',	'',	5,	'[1]',	3),
(40,	'Сохранить шаблон печати в файл',	'$form = sql_select_array(FORMS_TABLE,\r\n    \'id = \' . get_form_id({$print_tpl})\r\n);\r\nif ($form[\'type_form\'] == 2 && $form[\'pdf_import\'] == 1) {\r\n    include $config[\'site_path\'] . \"/include/mpdf/mpdf.php\";\r\n    $mpdf = new mPDF(\"\", \"A4\" . $form[\'orientation\'], \"\", \"\", $form[\'margin_left\'], $form[\'margin_right\'], $form[\'margin_top\'], $form[\'margin_bottom\']);\r\n    $mpdf->SetDisplayMode(\'fullwidth\');\r\n    $file_name = $form[\'doc_name\'] . \'.pdf\';\r\n    $form_text = \'\';\r\n    if ($form[\'head_form\'] != \'\') {\r\n        $mpdf->WriteHTML(form_template($form, $line, \"head_form\"));\r\n    }\r\n    $mpdf->WriteHTML(form_template($form, $line, \"body_form\"));\r\n    if ($form[\'foot_form\'] != \'\') {\r\n        $mpdf->WriteHTML(form_template($form, $line, \"foot_form\"));\r\n    }\r\n    $form_text = $mpdf->Output($file_name, \"S\");\r\n    $save_link = substr(\"{$field}\", 1);\r\n    save_data_file($save_link, $ID, $file_name, $form_text);\r\n    $line[\'{$field}\'] = $file_name;\r\n} else {\r\n    if ($form[\'type_form\'] == 5) {\r\n        $file_name = $form[\'doc_name\'] . \'.docx\';\r\n    } else if ($form[\'type_form\'] == 3) {\r\n        $file_name = $form[\'doc_name\'] . \'.rtf\';\r\n    } else if ($form[\'type_form\'] == 4) {\r\n        $file_name = $form[\'doc_name\'] . \'.odt\';\r\n    } else if ($form[\'type_form\'] == 6) {\r\n        $file_name = $form[\'doc_name\'] . \'.xls\';\r\n    }\r\n    $save_link = substr(\"{$field}\", 1);\r\n    save_data_file($save_link, $ID, $file_name, form_template($form, $line, \'body_form\'));\r\n    $line[\'{$field}\'] = $file_name;\r\n}',	'{\r\n    \"title\": \"Сохранить шаблон печати в файл\",\r\n    \"help\": \"tpl_print_save\",\r\n    \"parameters\": {\r\n        \"print_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"print_tpl\",\r\n            \"description\": \"Выберите шаблон печати\",\r\n            \"tpl_type\": \"print\",\r\n            \"help\": \"tpl_print_calc_save\"\r\n        },\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Выберите поле для сохранения шаблона\",\r\n            \"type_field\": \"6\",\r\n            \"help\": \"tpl_print_calc_field\"\r\n        }\r\n    }\r\n}',	'button',	'save_tpl_in_field.tpl',	'',	4,	'[]',	3),
(41,	'Распечатать шаблон печати',	'$print_tpl = get_form_id({$print_tpl});\r\n$_SESSION[$ses_id][\'sel\' . $table_id][] = $ID;\r\n?>\r\n<script>\r\nvar url = \'<?=$config[\"site_url\"]?>/forms.php?table=<?=$table_id?>&mode=print&form=<?=$print_tpl?>\';\r\nwindow.open(url);\r\n</script>\r\n<?php',	'{\r\n    \"title\": \"Распечатать шаблон печати\",\r\n    \"help\": \"tpl_print\",\r\n    \"parameters\": {\r\n        \"print_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"print_tpl\",\r\n            \"description\": \"Выберите шаблон печати\",\r\n            \"tpl_type\": \"print\",\r\n            \"help\": \"tpl_print_help\"\r\n        }\r\n    }\r\n}',	'button',	'tpl_print.tpl',	'',	4,	'[]',	2),
(42,	'Копировать поле из подтаблицы в основную запись',	'$filter = sql_select_array(FILTERS_TABLE, \'id = \', get_filter_id({$filter|default:0}));\r\n$result = data_select_field(\r\n    $table_id, \r\n    \'{$cond_field}\',\r\n    \'status = 0\' . ($event[\'type\'] === \'delete\' ? \' AND id != \' . $ID : \'\') . \r\n    \' AND {$cond_table.link_field_id} = \' . $line[\'{$cond_table.link_field_id}\'][\'raw\'] . \r\n    ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\') . \r\n    \' ORDER BY id {if $record == \"last\"}DESC{/if} LIMIT 1\');\r\n$row = sql_fetch_assoc($result);\r\n{if $add_value == \'yes\'}\r\nif ($line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] != \'\') {\r\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] .= \"{$add_delim}\" . $row[\'{$cond_field}\'];\r\n} else {\r\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'{$cond_field}\'];\r\n}\r\n{else}\r\n$line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'{$cond_field}\'];\r\n{/if}\r\n{if $cond_field_type == 6 || $cond_field_type == 9}\r\n$user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\nif (strpos($user_agent, \"MSIE\") !== false) {\r\n    $exp = \"\\n\\r\";\r\n} else {\r\n    $exp = \"\\r\\n\";    \r\n}\r\n$files = explode($exp, $line[\'{$cond_field}\']);\r\nforeach ($files as $fname) {\r\n    $orig_fpath = get_file_path({$cond_field|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n    save_data_file({$cond_table.field|replace:\'f\':\'\'}, $line[\'id\'], $fname, file_get_contents($orig_fpath));\r\n}\r\n{/if}',	'{\r\n    \"title\": \"Копировать поле из подтаблицы в основную запись\",\r\n    \"help\": \"last_record_data\",\r\n    \"parameters\": {\r\n        \"cond_table\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"cond_table\",\r\n            \"only_parent\": \"true\",\r\n            \"description\": \"Выберите таблицу, в которую будет записан результат\",\r\n            \"field_description\": \"Выберите поле, в которое будет записан результат\",\r\n            \"help\": \"tpl_parent_table\",\r\n            \"field_help\": \"tpl_parent_field\"\r\n         },\r\n        \"filter\": {\r\n            \"type\": \"Filter\",\r\n            \"name\": \"filter\",\r\n            \"description\": \"Выберите фильтр\",\r\n            \"help\": \"tpl_filter\"\r\n        },\r\n        \"cond_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cond_field\",\r\n            \"description\": \"Выберите поле\",\r\n            \"help\": \"tpl_field_out\"\r\n        },\r\n        \"record\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"record\",\r\n            \"description\": \"Выберите запись\",\r\n            \"help\": \"tpl_record\",\r\n            \"values\": {\r\n                \"last\": \"последняя\",\r\n                \"first\": \"первая\"\r\n            }\r\n        },\r\n        \"add_value\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"add_value\",\r\n            \"description\": \"Добавлять к существующему значению\",\r\n            \"help\": \"tpl_add_value\",\r\n            \"values\": {\r\n                \"no\": \"нет\",\r\n                \"yes\": \"да\"\r\n            }\r\n        },\r\n        \"add_delim\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"add_delim\",\r\n            \"description\": \"Разделитель при добавлении\",\r\n            \"help\": \"tpl_add_delim\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cond_field\"\r\n        },\r\n        {\r\n            \"type\": 4,\r\n            \"field\": \"\"\r\n        },\r\n        {\r\n            \"type\": 7,\r\n            \"field\": \"\"\r\n        }\r\n    ]\r\n}',	'calc',	'last_record_data.tpl',	'',	2,	'[2]',	5),
(43,	'Копировать поле из подтаблицы в основную запись',	'$result = data_select_field({$sub_table.subTable}, \'{$sub_table.subField}\',\r\n    \'status = 0 AND {$sub_table.link_field_id} = \' . $ID . \' ORDER BY id {if $sub_record == \"last\"}DESC{/if} LIMIT 1\');\r\n$row = sql_fetch_assoc($result);\r\n{if $add_value == \'yes\'}\r\nif ($line[\'{$res_field}\'] != \'\') {\r\n    $line[\'{$res_field}\'] .= \"{$add_delim}\" . $row[\'{$sub_table.subField}\'];\r\n} else {\r\n    $line[\'{$res_field}\'] = $row[\'{$sub_table.subField}\'];\r\n}\r\n{else}\r\n$line[\'{$res_field}\'] = $row[\'{$sub_table.subField}\'];\r\n{/if}\r\n{if $res_field_type == 6 || $res_field_type == 9}\r\n    $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n    if (strpos($user_agent, \"MSIE\") !== false) {\r\n        $exp = \"\\n\\r\";\r\n    } else {\r\n        $exp = \"\\r\\n\";    \r\n    }\r\n    $files = explode($exp, $line[\'{$cond_field}\']);\r\n    foreach ($files as $fname) {\r\n        $orig_fpath = get_file_path({$sub_table.subField|replace:\'f\':\'\'}, $line[\'id\'], $fname);\r\n        save_data_file({$res_field|replace:\'f\':\'\'}, $line[\'id\'], $fname, file_get_contents($orig_fpath));\r\n    }\r\n{/if}',	'{\r\n    \"title\": \"Копировать поле из подтаблицы в основную запись\",\r\n    \"help\": \"last_record_data\",\r\n    \"parameters\": {\r\n        \"sub_table\": {\r\n            \"type\": \"SubField\",\r\n            \"name\": \"sub_table\",\r\n            \"description\": \"Выберите подтаблицу\",\r\n            \"sub_description\": \"Выберите поле подтаблицы\",\r\n            \"help\": \"tpl_sub_table\",\r\n            \"field_help\": \"tpl_sub_field\"\r\n        },\r\n        \"sub_record\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"sub_record\",\r\n            \"description\": \"Выберите запись подтаблицы\",\r\n            \"help\": \"tpl_record\",\r\n            \"values\": {\r\n                \"last\": \"последняя\",\r\n                \"first\": \"первая\"\r\n            }\r\n        },\r\n        \"res_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"res_field\",\r\n            \"description\": \"Выберите поле, в которое будет записан результат\",\r\n            \"help\": \"tpl_field_in\"\r\n        },\r\n        \"add_value\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"add_value\",\r\n            \"description\": \"Добавлять к существующему значению\",\r\n            \"help\": \"tpl_add_value\",\r\n            \"values\": {\r\n                \"no\": \"нет\",\r\n                \"yes\": \"да\"\r\n            }\r\n        },\r\n        \"add_delim\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"add_delim\",\r\n            \"description\": \"Разделитель при добавлении\",\r\n            \"help\": \"tpl_add_delim\"\r\n        }\r\n    }\r\n}',	'button',	'last_record_data.tpl',	'',	2,	'[2]',	2),
(50,	'Копировать/создать запись',	'$sel_table = get_table_id({$correlation[\'table\']|default:0});\r\n$sel_fields = [\r\n    {foreach from = $correlation[\'to_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\n$sel_def_vals = [\r\n    {foreach from = $correlation[\'def_val\'] item = value}\'{$value}\', {/foreach}\r\n];\r\n$cur_fields = [\r\n    {foreach from = $correlation[\'cur_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\nif ($sel_table > 0) {\r\n    $ins_arr = [];\r\n    foreach ($sel_fields as $num => $field_id) {\r\n        if (!$field_id) continue;\r\n        $def_val = $sel_def_vals[$num];\r\n        if ($def_val) {\r\n             $ins_arr[\'f\' . $field_id] = $def_val;\r\n        } else {\r\n             if (is_array($line[\'f\' . $cur_field[$num]])) {\r\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\r\n             } else {\r\n                 if ($cur_fields[$num] !== \'id\') {\r\n                     $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\r\n                 } else {\r\n                     $ins_arr[\'f\' . $field_id] = $line[$cur_fields[$num]];\r\n                 }                \r\n             }\r\n        }\r\n        $field_check = sql_select_array(FIELDS_TABLE, \'id=\' . $field_id);\r\n        if ($field_check[\'type_field\'] == 6 || $field_check[\'type_field\'] == 9) { \r\n            $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n            if (strpos($user_agent, \"MSIE\") !== false) {      \r\n                $exp = \"\\n\\r\";      \r\n            } else {         \r\n                $exp = \"\\r\\n\";    \r\n            }   \r\n            $files = explode($exp, $def_val);      \r\n            foreach ($files as $fname) {        \r\n                $orig_fpath = get_file_path($cur_fields[$num], $line[\'id\'], $fname); \r\n                save_data_file($field_id, $line[\'id\'], $fname, file_get_contents($orig_fpath));  \r\n            }\r\n        }      \r\n    }\r\n    if (count($ins_arr) > 0) {\r\n        if ($new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr)) {\r\n{if $correlation[\'save_field\']}\r\n            $line[\'f{$correlation[\'save_field\']}\'] = $new_id;\r\n            data_update($table_id, EVENTS_ENABLE, [\'f{$correlation[\'save_field\']}\' => $new_id], \'id = \' . $ID);\r\n{/if}\r\n{if $addit neq \'stop\'}\r\n{if $addit eq \'go_edit\'}\r\n            if (isset($_REQUEST[\'parameters\'])) {\r\n                echo \'<script type=\"text/javascript\">\r\n                    window.opener.location.href = \"\' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'&edit_mode=on\";\r\n                    window.close();\r\n                    </script>\';\r\n            } else {\r\n                header(\'Location: \' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'&edit_mode=on\'); exit;\r\n            }\r\n{else}\r\n            if (isset($_REQUEST[\'parameters\'])) {\r\n                echo \'<script type=\"text/javascript\">\r\n                    window.opener.location.href = \"\' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'\";\r\n                    window.close();\r\n                    </script>\';\r\n            } else {\r\n                header(\'Location: \' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id); exit;\r\n            }\r\n{/if}\r\n{/if}\r\n        } else {\r\n            $error_trace[] = true;\r\n        }\r\n    } else {\r\n        $error_trace[] = true;\r\n    }\r\n} else {\r\n    $error_trace[] = true;\r\n}',	'{\r\n    \"title\": \"Копировать запись\",\r\n    \"help\": \"tpl_copy_line\",\r\n    \"parameters\": {\r\n        \"correlation\": {\r\n            \"type\": \"Correlation\",\r\n            \"name\": \"correlation\",\r\n            \"description\": \"Таблица-получатель\",\r\n            \"help\": \"tpl_table_copy_line\",\r\n            \"save_field\": true\r\n        },\r\n        \"addit\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"addit\",\r\n            \"description\": \"Опция\",\r\n            \"help\": \"tpl_help\",\r\n            \"default_value\": \"stop\",\r\n            \"values\": {\r\n              \"go_edit\": \"Переходить на созданную запись в режиме редактирования\",\r\n              \"go\": \"Переходить на созданную запись\",\r\n              \"stop\": \"Оставаться в текущей записи\"\r\n            }\r\n        }\r\n    }\r\n}',	'button',	'copy_line.tpl',	'',	7,	'[]',	4),
(51,	'Сохранить шаблон печати в файл',	'$form = sql_select_array(FORMS_TABLE,\r\n    \'id = \' . get_form_id({$print_tpl})\r\n);\r\nif ($form[\'type_form\'] == 2 && $form[\'pdf_import\'] == 1) {\r\n    include $config[\'site_path\'] . \"/include/mpdf/mpdf.php\";\r\n    $mpdf = new mPDF(\"\", \"A4\" . $form[\'orientation\'], \"\", \"\", $form[\'margin_left\'], $form[\'margin_right\'], $form[\'margin_top\'], $form[\'margin_bottom\']);\r\n    $mpdf->SetDisplayMode(\'fullwidth\');\r\n    $file_name = $form[\'doc_name\'] . \'.pdf\';\r\n    $form_text = \'\';\r\n    if ($form[\'head_form\'] != \'\') {\r\n        $mpdf->WriteHTML(form_template($form, $line, \"head_form\"));\r\n    }\r\n    $mpdf->WriteHTML(form_template($form, $line, \"body_form\"));\r\n    if ($form[\'foot_form\'] != \'\') {\r\n        $mpdf->WriteHTML(form_template($form, $line, \"foot_form\"));\r\n    }\r\n    $form_text = $mpdf->Output($file_name, \"S\");\r\n    $save_link = substr(\"{$field}\", 1);\r\n    save_data_file($save_link, $ID, $file_name, $form_text);\r\n    $line[\'{$field}\'] = $file_name;\r\n} else {\r\n    if ($form[\'type_form\'] == 5) {\r\n        $file_name = $form[\'doc_name\'] . \'.docx\';\r\n    } else if ($form[\'type_form\'] == 3) {\r\n        $file_name = $form[\'doc_name\'] . \'.rtf\';\r\n    } else if ($form[\'type_form\'] == 4) {\r\n        $file_name = $form[\'doc_name\'] . \'.odt\';\r\n    } else if ($form[\'type_form\'] == 6) {\r\n        $file_name = $form[\'doc_name\'] . \'.xls\';\r\n    }\r\n    $save_link = substr(\"{$field}\", 1);\r\n    save_data_file($save_link, $ID, $file_name, form_template($form, $line, \'body_form\'));\r\n    $line[\'{$field}\'] = $file_name;\r\n}',	'{\r\n    \"title\": \"Сохранить шаблон печати в файл\",\r\n    \"help\": \"tpl_print_save\",\r\n    \"parameters\": {\r\n        \"print_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"print_tpl\",\r\n            \"description\": \"Выберите шаблон печати\",\r\n            \"tpl_type\": \"print\",\r\n            \"help\": \"tpl_print_calc_save\"\r\n        },\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Выберите поле для сохранения шаблона\",\r\n            \"type_field\": \"6\",\r\n            \"help\": \"tpl_print_calc_field\"\r\n        }\r\n    }\r\n}',	'calc',	'save_tpl_in_field.tpl',	'',	4,	'[1]',	3),
(60,	'Создать запись в подтаблице',	'$sel_table = get_table_id({$correlation[\'table\']|default:0});\r\n$sel_fields = [\r\n    {foreach from = $correlation[\'to_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\n$sel_def_vals = [\r\n    {foreach from = $correlation[\'def_val\'] item = value}\'{$value}\', {/foreach}\r\n];\r\n$cur_fields = [\r\n    {foreach from = $correlation[\'cur_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\n$link_field = get_field_id({$correlation[\'link_field_id\']|default:0});\r\nif ($sel_table > 0) {\r\n    $ins_arr = [];\r\n    foreach ($sel_fields as $num => $field_id) {\r\n        if (!$field_id) continue;\r\n        $def_val = $sel_def_vals[$num];\r\n        if ($def_val) {\r\n             $ins_arr[\'f\' . $field_id] = $def_val;\r\n        } else {\r\n             if (is_array($line[$cur_field[$num]])) {\r\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\r\n             } else {\r\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\r\n             }\r\n        }\r\n    }\r\n    if ($link_field) {\r\n        $ins_arr[\'f\' . $link_field] = $ID;\r\n    }\r\n    if (count($ins_arr) > 0) {\r\n        data_insert($sel_table, EVENTS_ENABLE, $ins_arr);\r\n    } else {\r\n        $error_trace[] = true;\r\n    }\r\n} else {\r\n    $error_trace[] = true;\r\n}',	'{\r\n    \"title\": \"Создать запись в подтаблице\",\r\n    \"help\": \"tpl_copy_line\",\r\n    \"parameters\": {\r\n        \"correlation\": {\r\n            \"type\": \"Correlation\",\r\n            \"name\": \"correlation\",\r\n            \"description\": \"Таблица-получатель\",\r\n            \"help\": \"tpl_table_copy_line\",\r\n            \"only_subtables\": true\r\n        }\r\n    }\r\n}',	'button',	'copy_line_subtable.tpl',	'',	2,	'[]',	0),
(61,	'Распечатать шаблон печати',	'$print_tpl = get_form_id({$print_tpl});\r\n$_SESSION[$ses_id][\'sel\' . $table_id][] = $ID;\r\n?>\r\n<script>\r\nvar url = \'<?=$config[\"site_url\"]?>/forms.php?table=<?=$table_id?>&mode=print&form=<?=$print_tpl?>\';\r\nwindow.open(url);\r\n</script>',	'{\r\n    \"title\": \"Распечатать шаблон печати\",\r\n    \"help\": \"tpl_print\",\r\n    \"parameters\": {\r\n        \"print_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"print_tpl\",\r\n            \"description\": \"Выберите шаблон печати\",\r\n            \"tpl_type\": \"print\",\r\n            \"help\": \"tpl_print_help\"\r\n        }\r\n    }\r\n}',	'calc',	'tpl_print.tpl',	'',	4,	'[1]',	2),
(62,	'Изменить поле',	'{if $put_field_type eq 2}\n$line[\'{$put_field.field}\'] = date(\'Y-m-d H:i:s\', strtotime(\'{$put_field.set_value}\'));\n{else}\n$line[\'{$put_field.field}\'] = \'{$put_field.set_value}\';\n{/if}',	'{\r\n    \"title\": \"Изменить поле\",\r\n    \"help\": \"tpl_field_change\",\r\n    \"parameters\": {\r\n        \"put_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"put_field\",\r\n            \"description\": \"Выберите поле для замены значения\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_put_field\",\r\n            \"set_value\": \"true\",\r\n            \"set_value_description\": \"Введите значение, которое должно быть присвоено полю\",\r\n            \"set_help\": \"tpl_set_value\"\r\n        }\r\n    }\r\n}',	'button',	'field_change.tpl',	'',	1,	'[]',	2),
(74,	'Копировать значение поля до изменения',	'$line[\'{$field}\'] = $event[\'changed\'][substr(\'{$field2}\',1)][\'old\'];',	'{\r\n    \"title\": \"Копировать значение поля до изменения\",\r\n    \"help\": \"tpl_save_old_value\",\r\n    \"parameters\": {\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"description\": \"Выберите поле записи\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_put_field\"\r\n        },\r\n        \"field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field2\",\r\n            \"description\": \"Выберите поле, чье старое значение нужно сохранить\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_old_field\"\r\n        }\r\n    }\r\n}',	'button',	'save_old_value.tpl',	'',	6,	'[1]',	1),
(75,	'Число: Математическая формула',	'$string = \'{$flex_data}\';\nif ($string) {\n    {literal}$string = str_replace(\'{f\', \'$line[\\\'f\', $string);\n    $string = str_replace(\'}\', \'\\\']\', $string);{/literal}\n    $string = \'$line[\\\'{$result}\\\'] = \' . $string . \';\';\n    eval($string);\n}',	'{\r\n     \"title\": \"Число: Математическая формула\",\r\n     \"help\": \"tpl_math_formulas\",\r\n     \"parameters\": {\r\n         \"field1\": {\r\n             \"type\": \"Field\",\r\n             \"name\": \"field1\",\r\n             \"description\": \"Выберите поле\",\r\n             \"type_field\": 1,\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\": \"tpl_func_field\"\r\n         },\r\n         \"result\": {\r\n             \"type\": \"Field\",\r\n             \"name\": \"result\",\r\n             \"description\": \"Выберите поле записи результата\",\r\n             \"type_field\": 1,\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    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"field1\"\r\n        }\r\n    ]\r\n}\r\n',	'button',	'math_functions.tpl',	'',	1,	'[]',	5),
(76,	'Отправить шаблон рассылки',	'$true = send_template({$send_tpl}, \"id = \" . $line[\'id\']);\r\n if ($true) {\r\n    $display_message .= $lang[\'Mailer_started\'] .\" <a style=\'font-size:16px;\' href=edit_mailing.php>\";\r\n    $display_message .= $lang[\'Active_mailer\'] . \"</a>\";\r\n    display_notification($display_message, 1);\r\n}',	'{\r\n    \"title\": \"Отправить шаблон рассылки\",\r\n    \"help\": \"tpl_send\",\r\n    \"parameters\": {\r\n        \"send_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"send_tpl\",\r\n            \"description\": \"Шаблон рассылки\",\r\n            \"tpl_type\": \"send\",\r\n            \"help\": \"tpl_send_template\"\r\n        }\r\n    }\r\n}',	'calc',	'tpl_send.tpl',	'',	4,	'[1]',	1),
(77,	'Изменить поля в связанной таблице',	'$line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'] = $line[\'{$cur_field}\'];',	'{\r\n    \"title\": \"Изменить поля в связанной таблице\",\r\n    \"help\": \"tpl_link_to_table\",\r\n    \"parameters\": {\r\n        \"cur_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field\",\r\n            \"description\": \"Выберите поле, из которого тянуть значение\",\r\n            \"help\": \"source_field\"\r\n        },\r\n        \"gen_field\": {\r\n            \"type\": \"TableField\",\r\n            \"name\": \"gen_field\",\r\n            \"description\": \"Выберите таблицу, в которое сохранять\",\r\n            \"field_description\": \"Выберите поле, в которое сохранять\",\r\n            \"only_parent\": \"true\",\r\n            \"help\": \"tpl_to_table\",\r\n            \"field_help\": \"tpl_to_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cur_field\"\r\n        }\r\n    ]\r\n}',	'calc',	'tpl_link_to_table.tpl',	'',	3,	'[1]',	2),
(78,	'Копировать/создать запись',	'$sel_table = get_table_id({$correlation[\'table\']|default:0});\r\n$sel_fields = [\r\n    {foreach from = $correlation[\'to_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\n$sel_def_vals = [\r\n    {foreach from = $correlation[\'def_val\'] item = value}\'{$value}\', {/foreach}\r\n];\r\n$cur_fields = [\r\n    {foreach from = $correlation[\'cur_field\'] item = field_id}get_field_id({$field_id|default:0}), {/foreach}\r\n];\r\nif ($sel_table > 0) {\r\n    $ins_arr = [];\r\n    foreach ($sel_fields as $num => $field_id) {\r\n        if (!$field_id) continue;\r\n        $def_val = $sel_def_vals[$num];\r\n        if ($def_val) {\r\n             $ins_arr[\'f\' . $field_id] = $def_val;\r\n        } else {\r\n             if (is_array($line[\'f\' . $cur_field[$num]])) {\r\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\r\n             } else {\r\n                 if ($cur_fields[$num] !== \'id\') {\r\n                     $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\r\n                 } else {\r\n                     $ins_arr[\'f\' . $field_id] = $line[$cur_fields[$num]];\r\n                 }                \r\n             }\r\n        }\r\n        $field_check = sql_select_array(FIELDS_TABLE, \'id=\' . $field_id);\r\n        if ($field_check[\'type_field\'] == 6 || $field_check[\'type_field\'] == 9) { \r\n            $user_agent = $_SERVER[\"HTTP_USER_AGENT\"];\r\n            if (strpos($user_agent, \"MSIE\") !== false) {      \r\n                $exp = \"\\n\\r\";      \r\n            } else {         \r\n                $exp = \"\\r\\n\";    \r\n            }   \r\n            $files = explode($exp, $def_val);      \r\n            foreach ($files as $fname) {        \r\n                $orig_fpath = get_file_path($cur_fields[$num], $line[\'id\'], $fname); \r\n                save_data_file($field_id, $line[\'id\'], $fname, file_get_contents($orig_fpath));  \r\n            }\r\n        }      \r\n    }\r\n    if (count($ins_arr) > 0) {\r\n        if ($new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr)) {\r\n{if $correlation[\'save_field\']}\r\n            $line[\'f{$correlation[\'save_field\']}\'] = $new_id;\r\n            data_update($table_id, EVENTS_ENABLE, [\'f{$correlation[\'save_field\']}\' => $new_id], \'id = \' . $ID);\r\n{/if}\r\n        } \r\n    }\r\n}',	'{\r\n    \"title\": \"Копировать запись\",\r\n    \"help\": \"tpl_copy_line\",\r\n    \"parameters\": {\r\n        \"correlation\": {\r\n            \"type\": \"Correlation\",\r\n            \"name\": \"correlation\",\r\n            \"description\": \"Таблица-получатель\",\r\n            \"help\": \"tpl_table_copy_line\",\r\n            \"save_field\": true\r\n        },\r\n        \"addit\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"addit\",\r\n            \"description\": \"Опция\",\r\n            \"help\": \"tpl_help\",\r\n            \"default_value\": \"stop\",\r\n            \"values\": {\r\n              \"go_edit\": \"Переходить на созданную запись в режиме редактирования\",\r\n              \"go\": \"Переходить на созданную запись\",\r\n              \"stop\": \"Оставаться в текущей записи\"\r\n            }\r\n        }\r\n    }\r\n}',	'calc',	'copy_line.tpl',	'',	7,	'[1]',	4),
(79,	'Очистить поле',	'{if $field1_type eq \"5\" || $field1_type eq 1 || $field1_type eq 7}\n    $line[\'{$field1}\'] = 0;\n{else}\n    $line[\'{$field1}\'] = \'\';\n{/if}',	'{\n    \"title\": \"Очистить поле\",\n    \"help\": \"tpl_clear_field\",\n    \"parameters\": {\n        \"field1\": {\n            \"type\": \"Field\",\n            \"name\": \"field1\",\n            \"description\": \"Выберите поле\",\n            \"no_system\": true,\n            \"help\": \"tpl_clear_field\"\n        }\n    },\n    \"events\": [\n        {\n            \"type\": 1,\n            \"field\": \"\"\n        }\n    ]\n}',	'calc',	'field_clearing.tpl',	'',	1,	'[1]',	3),
(80,	'Отправить шаблон смс рассылки',	'$true = send_template({$send_tpl}, \"id = \" . $line[\'id\']);\r\n if ($true) {\r\n    $display_message .= $lang[\'Mailer_started\'] .\" <a style=\'font-size:16px;\' href=edit_smsing.php>\";\r\n    $display_message .= $lang[\'Active_mailer\'] . \"</a>\";\r\n    display_notification($display_message, 1);\r\n}',	'{\r\n    \"title\": \"Отправить шаблон рассылки\",\r\n    \"help\": \"tpl_send\",\r\n    \"parameters\": {\r\n        \"send_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"send_tpl\",\r\n            \"description\": \"Шаблон смс рассылки\",\r\n            \"tpl_type\": \"sms\",\r\n            \"help\": \"tpl_send_sms_template\"\r\n        }\r\n    }\r\n}',	'calc',	'tpl_send_sms.tpl',	'',	4,	'[1]',	1),
(81,	'Отправить шаблон смс рассылки',	'$true = send_template({$send_tpl}, \"id = \" . $line[\'id\']);\r\n if ($true) {\r\n    $display_message .= $lang[\'Mailer_started\'] .\" <a style=\'font-size:16px;\' href=edit_smsing.php>\";\r\n    $display_message .= $lang[\'Active_mailer\'] . \"</a>\";\r\n    display_notification($display_message, 1);\r\n}',	'{\r\n    \"title\": \"Отправить шаблон рассылки\",\r\n    \"help\": \"tpl_send\",\r\n    \"parameters\": {\r\n        \"send_tpl\": {\r\n            \"type\": \"Template\",\r\n            \"name\": \"send_tpl\",\r\n            \"description\": \"Шаблон смс рассылки\",\r\n            \"tpl_type\": \"sms\",\r\n            \"help\": \"tpl_send_sms_template\"\r\n        }\r\n    }\r\n}',	'button',	'tpl_send_sms.tpl',	'',	4,	'[1]',	1),
(82,	'Дата: Вычислить день недели',	'if ($line[\'{$cur_field}\'] == \'0000-00-00 00:00:00\'){\r\n    $line[\'{$put_field}\'] = \'\';\r\n} else {\r\n    $week_name = [\r\n        \'Воскресенье\',\r\n        \'Понедельник\',\r\n        \'Вторник\',\r\n        \'Среда\',\r\n        \'Четверг\',\r\n        \'Пятница\',\r\n        \'Суббота\',\r\n    ];\r\n    $week_day = date(\'w\', strtotime($line[\'{$cur_field}\']));\r\n    $line[\'{$put_field}\'] = $week_name[$week_day];\r\n}',	'{\r\n    \"title\": \"Дата: Вычислить день недели\",\r\n    \"help\": \"761\",\r\n    \"parameters\": {\r\n        \"cond_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field\",\r\n            \"type_field\": [2,12],\r\n            \"description\": \"Выберите поле, из которого будет тянуться дата\",\r\n            \"help\": \"762\"\r\n        },\r\n        \"put_field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"put_field\",\r\n            \"description\": \"Выберите поле, которое должно быть заполнено данными\",\r\n            \"no_system\": true,\r\n            \"help\": \"763\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cur_field\"\r\n        }\r\n    ]\r\n}',	'calc',	'week_day.tpl',	'',	1,	'[1]',	13),
(83,	'Текст: Обработка ФИО',	'$array = explode(\" \", $line[\'{$field1}\']);\n$name1 = \'\';\n$name2 = \'\';\n$surname = \'\';\n$i = 0;\n$sex = 0;\n$lit_sname = {literal}\'{Фамилия}\'{/literal};\n$lit_sname_s = {literal}\'{Ф}\'{/literal};\n$lit_name1 = {literal}\'{Имя}\'{/literal};\n$lit_name1_s = {literal}\'{И}\'{/literal};\n$lit_name2 = {literal}\'{Отчество}\'{/literal};\n$lit_name2_s = {literal}\'{О}\'{/literal};\n$pattern = \'{$flex_data}\';\nif (count($array) > 1){\n // определение поля по отчеству\n foreach($array as $value){\n  if (mb_strlen($value )> 2){\n   $s1 = mb_substr($value,-3);\n   if ($s1 == \'вна\' || $s1 == \'чна\'){\n    $sex = 2;\n    break;\n   }\n   elseif ($s1 == \'вич\' || $s1 == \'ьич\'){\n    $sex = 1;\n   }\n  }\n }\n $ich = 0;\n // количество вхождений -ич\n foreach($array as $value){\n  if (mb_strlen($value )> 2){\n   if (mb_substr($value,-2) == \'ич\'){\n    $ich++;\n   }\n  }\n }\n // определение фамилии\n foreach($array as $value){\n  if (empty($surname) && mb_strlen($value) > 2){\n   $s1 = mb_substr($value,-2);\n   $s2 = \'\';\n   if (mb_strlen($value) > 2){\n    $s2 = mb_substr($value,-3);\n   }\n   $s3 = \'\';\n   if (mb_strlen($value) > 3){\n    $s3 = mb_substr($value,-4);\n   }\n   if ($s1 == \'ин\' || $s1 == \'ев\' || $s1 == \'ов\' || $s1 == \'ан\' || $s1 == \'ын\' || $s1 == \'ых\' || $s1 == \'ян\' || $s1 == \'ко\' || $s1 == \'ук\' || $s1 == \'ик\' || $s1 == \'те\' || $s1 == \'ни\' || $s1 == \'юк\'\n    || $s2 == \'ина\' || $s2 == \'ына\' || $s2 == \'ева\' || $s2 == \'ова\' || $s2 == \'янц\' || $s2 == \'нок\' || $s2 == \'дзе\' || $s2 == \'ава\' || $s2 == \'сон\'\n    || $s3 == \'ских\' || $s3 == \'ский\' || $s3 == \'ская\' || $s3 == \'тейн\' || $s3 == \'блюм\' || $s3 == \'баум\' || $s3 == \'берг\' || $s3 == \'вили\' || $s3 == \'заде\' || $s3 == \'оглу\'){\n    $surname = $value;\n    $i++;\n    break;\n   }\n   elseif ($s1 == \'ич\' && (($ich == 1 && $sex == 2) || ($ich == 2 && $sex == 1))){\n    $surname = $value;\n    $i++;\n    break;\n   }\n  }\n }\n // определение отчества\n foreach($array as $value){\n  if ($value != $surname && empty($name2) && mb_strlen($value )> 3){\n   $s1 = mb_substr($value,-3);\n   if ($s1 == \'вич\' || $s1 == \'ьич\' || $s1 == \'вна\' || $s1 == \'чна\'){\n    $name2 = $value;\n    $i++;\n    break;\n   }\n  }\n }\n // определение имени\n if (count($array) > $i){\n  foreach($array as $value){\n   if ($value != $name2 && $value != $surname){\n    $name1 = $value;\n    $i++;\n    break;\n   }\n  }\n }\n // допределение фамилии\n if (count($array) > $i){\n  foreach($array as $value){\n   if ($value != $name1 && $value != $name2){\n    $surname = $value;\n    break;\n   }\n  }\n }\n}\nelse{\n $surname = $line[\'{$field1}\'];\n}\n// падеж\n$case = \'{$case_value}\';\n// падеж для полной фамилии\nif (!empty($surname) && $sex > 0 && $case != \'1\' && mb_strpos($pattern, $lit_sname) !== false){\n $s1 = mb_substr($surname,-2);\n if ($sex == 1) {\n  switch ($case) {\n   case \'2\':\n    if ($s1 == \'ов\' || $s1 == \'ев\' || $s1 == \'ин\' || $s1 == \'ын\' || $s1 == \'ых\' || $s1 == \'ан\' || $s1 == \'ян\' || $s1 == \'нц\' || $s1 == \'ик\' || $s1 == \'ук\' || $s1 == \'юк\' \n     || $s1 == \'он\' || $s1 == \'йн\' || $s1 == \'юм\' || $s1 == \'ич\' || $s1 == \'рг\' ){\n     $surname = $surname . \'а\';\n    }\n    elseif ($s1 == \'ий\'){\n     $surname = mb_substr($surname, 0, -2) . \'ого\';\n    }\n    break;\n   case \'3\':\n    if ($s1 == \'ов\' || $s1 == \'ев\' || $s1 == \'ин\' || $s1 == \'ын\' || $s1 == \'ых\' || $s1 == \'ан\' || $s1 == \'ян\' || $s1 == \'нц\' || $s1 == \'ик\' || $s1 == \'ук\' || $s1 == \'юк\' \n     || $s1 == \'он\' || $s1 == \'йн\' || $s1 == \'юм\' || $s1 == \'ич\' || $s1 == \'рг\' ){\n     $surname = $surname . \'у\';\n    }\n    elseif ($s1 == \'ий\'){\n     $surname = mb_substr($surname, 0, -2) . \'ому\';\n    }\n    break;\n   case \'4\':\n    if ($s1 == \'ов\' || $s1 == \'ев\' || $s1 == \'ин\' || $s1 == \'ын\' || $s1 == \'ых\' || $s1 == \'ан\' || $s1 == \'ян\' || $s1 == \'нц\' || $s1 == \'ик\' || $s1 == \'ук\' || $s1 == \'юк\' \n     || $s1 == \'он\' || $s1 == \'йн\' || $s1 == \'юм\' || $s1 == \'ич\' || $s1 == \'рг\' ){\n     $surname = $surname . \'а\';\n    }\n    elseif ($s1 == \'ий\'){\n     $surname = mb_substr($surname, 0, -2) . \'ого\';\n    }\n    break;\n   case \'5\':\n    if ($s1 == \'ов\' || $s1 == \'ев\' || $s1 == \'ин\' || $s1 == \'ын\'){\n     $surname = $surname . \'ым\';\n    }\n    elseif ($s1 == \'ян\' || $s1 == \'ан\' || $s1 == \'юк\' || $s1 == \'ук\' || $s1 == \'юм\' || $s1 == \'ик\' || $s1 == \'он\' || $s1 == \'йн\' || $s1 == \'рг\'){\n     $surname = $surname . \'ом\';\n    }\n    elseif ($s1 == \'ич\' || $s1 == \'нц\'){\n     $surname = $surname . \'ем\';\n    }\n    elseif ($s1 == \'ий\'){\n     $surname = mb_substr($surname, 0, -2) . \'им\';\n    }\n    break;\n   case \'6\':\n    if ($s1 == \'ов\' || $s1 == \'ев\' || $s1 == \'ин\' || $s1 == \'ын\' || $s1 == \'ых\' || $s1 == \'ан\' || $s1 == \'ян\' || $s1 == \'нц\' || $s1 == \'ик\' || $s1 == \'ук\' || $s1 == \'юк\' \n     || $s1 == \'он\' || $s1 == \'йн\' || $s1 == \'юм\' || $s1 == \'ич\' || $s1 == \'рг\' ){\n     $surname = $surname . \'е\';\n    }\n    elseif ($s1 == \'ий\'){\n     $surname = mb_substr($surname, 0, -2) . \'ом\';\n    }\n    break;\n  }\n }\n else {\n  if ($case == \'4\') {\n   if ($s1 == \'ва\' || $s1 == \'на\'){\n    $surname = mb_substr($surname, 0, -1) . \'у\';\n   }\n   elseif ($s1 == \'ая\'){\n    $surname = mb_substr($surname, 0, -2) . \'ую\';\n   }\n  }\n  else {\n   if ($s1 == \'ва\' || $s1 == \'на\'){\n    $surname = mb_substr($surname, 0, -1) . \'ой\';\n   }\n   elseif ($s1 == \'ая\'){\n    $surname = mb_substr($surname, 0, -2) . \'ой\';\n   }\n  }\n }\n}\n// падеж для полного имени\nif (!empty($name1) && $case != \'1\' && $sex > 0 && mb_strpos($pattern, $lit_name1) !== false){\n $s1 = mb_substr($name1,-1);\n if ($sex == 1) {\n  if ($s1 === \'й\' || $s1 === \'ь\') {\n   switch ($case) {\n    case \'2\':\n    case \'4\':\n     $name1 = mb_substr($name1, 0, -1) . \'я\';\n     break;\n    case \'3\':\n     $name1 = mb_substr($name1, 0, -1) . \'ю\';\n     break;\n    case \'5\':\n     $name1 = mb_substr($name1, 0, -1) . \'ем\';\n     break;\n    case \'6\':\n     if (mb_substr($name1,-2) === \'ий\'){ \n      $name1 = mb_substr($name1, 0, -1) . \'и\';\n     }\n     else {\n      $name1 = mb_substr($name1, 0, -1) . \'е\';\n     }\n     break;\n   }\n  }\n  elseif ($s1 === \'я\') {\n   switch ($case) {\n    case \'2\':\n     $name1 = mb_substr($name1, 0, -1) . \'и\';\n     break;\n    case \'3\':\n    case \'6\':\n     $name1 = mb_substr($name1, 0, -1) . \'е\';\n     break;\n    case \'4\':\n     $name1 = mb_substr($name1, 0, -1) . \'ю\';\n     break;\n    case \'5\':\n     $name1 = mb_substr($name1, 0, -1) . \'ёй\';\n     break;\n   }\n  }\n  elseif ($s1 === \'л\') {\n   switch ($case) {\n    case \'2\':\n    case \'4\':\n     $name1 = mb_substr($name1, 0, -2) . \'ла\';\n     break;\n    case \'3\':\n     $name1 = mb_substr($name1, 0, -2) . \'лу\';\n     break;\n    case \'5\':\n     $name1 = mb_substr($name1, 0, -2) . \'лом\';\n     break;\n    case \'6\':\n     $name1 = mb_substr($name1, 0, -2) . \'ле\';\n     break;\n   }\n  }\n  else {\n   switch ($case) {\n    case \'2\':\n    case \'4\':\n     $name1 = $name1 . \'а\';\n     break;\n    case \'3\':\n     $name1 = $name1 . \'у\';\n     break;\n    case \'5\':\n     $name1 = $name1 . \'ом\';\n     break;\n    case \'6\':\n     $name1 = $name1 . \'е\';\n     break;\n   }\n  }\n }\n else {\n  if ($s1 === \'я\') {\n   switch ($case) {\n    case \'2\':\n    case \'3\':\n    case \'6\':\n     $name1 = mb_substr($name1, 0, -1) . \'и\';\n     break;\n    case \'4\':\n     $name1 = mb_substr($name1, 0, -1) . \'ю\';\n     break;\n    case \'5\':\n     $name1 = mb_substr($name1, 0, -1) . \'ей\';\n     break;\n   }\n  }\n  elseif ($s1 === \'ь\') {\n   switch ($case) {\n    case \'2\':\n    case \'3\':\n    case \'6\':\n     $name1 = mb_substr($name1, 0, -1) . \'и\';\n     break;\n     break;\n    case \'5\':\n     $name1 = $name1 . \'ю\';\n     break;\n   }\n  }\n  else {\n   switch ($case) {\n    case \'2\':\n     $name1 = mb_substr($name1, 0, -1) . \'ы\';\n     break;\n    case \'3\':\n    case \'6\':\n     $name1 = mb_substr($name1, 0, -1) . \'е\';\n     break;\n    case \'4\':\n     $name1 = mb_substr($name1, 0, -1) . \'у\';\n     break;\n    case \'5\':\n     $name1 = mb_substr($name1, 0, -1) . \'ой\';\n     break;\n   }\n  }\n }\n}\n// падеж для полного отчества\nif (!empty($name2) && $case != \'1\' && mb_strpos($pattern, $lit_name2) !== false){\n $s1 = mb_substr($name2,-2);\n switch ($case) {\n  case \'2\':\n   if ($s1 == \'ич\'){\n    $name2 = $name2 . \'а\';\n   }\n   elseif ($s1 == \'на\'){\n    $name2 = mb_substr($name2, 0, -1) . \'ы\';\n   }\n   break;\n  case \'3\':\n   if ($s1 == \'ич\'){\n    $name2 = $name2 . \'у\';\n   }\n   elseif ($s1 == \'на\'){\n    $name2 = mb_substr($name2, 0, -1) . \'е\';\n   }\n   break;\n  case \'4\':\n   if ($s1 == \'ич\'){\n    $name2 = $name2 . \'а\';\n   }\n   elseif ($s1 == \'на\'){\n    $name2 = mb_substr($name2, 0, -1) . \'у\';\n   }\n   break;\n  case \'5\':\n   if ($s1 == \'ич\'){\n    $name2 = $name2 . \'ем\';\n   }\n   elseif ($s1 == \'на\'){\n    $name2 = mb_substr($name2, 0, -1) . \'ой\';\n   }\n   break;\n  case \'6\':\n   if ($s1 == \'ич\'){\n    $name2 = $name2 . \'е\';\n   }\n   elseif ($s1 == \'на\'){\n    $name2 = mb_substr($name2, 0, -1) . \'е\';\n   }\n   break;\n }\n}\n// шаблон\n$s = $pattern;\nif (mb_strpos($s, $lit_sname) !== false){\n $s = str_replace($lit_sname, $surname, $s);\n}\nif (mb_strpos($s, $lit_sname_s) !== false){\n $s = str_replace($lit_sname_s, mb_substr($surname,0,1), $s);\n}\nif (mb_strpos($s, $lit_name1) !== false){\n $s = str_replace($lit_name1, $name1, $s);\n}\nif (mb_strpos($s, $lit_name1_s) !== false){\n $s = str_replace($lit_name1_s, mb_substr($name1,0,1), $s);\n}\nif (mb_strpos($s, $lit_name2) !== false){\n $s = str_replace($lit_name2, $name2, $s);\n}\nif (mb_strpos($s, $lit_name2_s) !== false){\n $s = str_replace($lit_name2_s, mb_substr($name2,0,1), $s);\n}\n$line[\'{$result}\'] = $s;\n',	'{\n     \"title\": \"Текст: Обработка ФИО\",\n     \"help\": \"764\",\n     \"parameters\": {\n         \"field1\": {\n             \"type\": \"Field\",\n             \"name\": \"field1\",\n             \"description\": \"Выберите поле с ФИО\",\n             \"type_field\": 3,\n             \"help\": \"765\"\n         },\n         \"func_row\": {\n             \"type\": \"String\",\n             \"name\": \"func_row\",\n             \"description\": \"Поле записи шаблона\",\n             \"textarea\": true,\n             \"help\": \"767\"\n         },\n         \"result\": {\n             \"type\": \"Field\",\n             \"name\": \"result\",\n             \"description\": \"Выберите поле записи результата\",\n             \"type_field\": 3,\n             \"help\": \"766\"\n         },\n        \"put_value\": {\n            \"type\": \"List\",\n            \"name\": \"case_value\",\n            \"values\":{\"1\":\"Именительный\",\"2\":\"Родительный\",\"3\":\"Дательный\",\"4\":\"Винительный\",\"5\":\"Творительный\",\"6\":\"Предложный\"},\n            \"description\": \"Выберите падеж\",\n            \"help\": \"768\"\n        },\n         \"flex_data\": {\n             \"type\": \"String\",\n             \"name\": \"flex_data\",\n             \"description\": \"\",\n             \"help\": \"tpl_flex_string\"\n         }\n     },\n    \"events\": [\n        {\n            \"type\": 3,\n            \"field\": \"field1\"\n        }\n    ]\n}',	'calc',	'fio_processing.tpl',	'',	1,	'[1]',	9),
(84,	'Дата: Разница между датами',	'if ($line[\'{$field}\'] == \'0000-00-00 00:00:00\' || $line[\'{$field1}\'] == \'0000-00-00 00:00:00\'){\r\n    $line[\'{$value}\'] = 0;\r\n} else {\r\n{if $case_type == 2}\r\n    $datetime1 = strtotime($line[\'{$field}\']);\r\n    $datetime2 = strtotime($line[\'{$field1}\']);\r\n    $interval = $datetime2 - $datetime1;\r\n{if $case_measure == 1}\r\n    $total = $interval;\r\n{elseif $case_measure == 2}\r\n    $total = $interval / 60;\r\n{elseif $case_measure == 3}\r\n    $total = $interval / 3600;\r\n{elseif $case_measure == 4}\r\n    $total = $interval / (3600 * 24);\r\n{elseif $case_measure == 5}\r\n    $total = $interval / (3600 * 24 * 7);\r\n{elseif $case_measure == 6}\r\n    $total = $interval / (3600 * 24 * 30);\r\n{elseif $case_measure == 7}\r\n    $total = $interval / (3600 * 24 * 365.25);\r\n{/if}\r\n    $line[\'{$value}\'] = round($total, {$decimal_places|default:0}, PHP_ROUND_HALF_DOWN);\r\n{else}\r\n    $datetime1 = date_create($line[\'{$field}\']);\r\n    $datetime2 = date_create($line[\'{$field1}\']);\r\n    $interval = date_diff($datetime1, $datetime2);\r\n    $date = $interval->format(\'%a д. %H ч. %I м.\');\r\n    $result = str_replace(\'0 д.,\', \'\', $date);\r\n    $result = str_replace(\'0 м.,\', \'\', $result);\r\n    $result = str_replace(\'0 ч.,\', \'\', $result);\r\n    $line[\'{$value}\'] = $result;\r\n{/if}\r\n}',	'{\r\n    \"title\": \"Дата: Разница между датами\",\r\n    \"help\": \"tpl_diff_dates\",\r\n    \"parameters\": {\r\n        \"field\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field\",\r\n            \"type_field\": [2,12],\r\n            \"description\": \"Выберите поле первой даты\",\r\n            \"help\": \"tpl_first_field\"\r\n        },\r\n        \"field1\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"field1\",\r\n            \"type_field\": [2,12],\r\n            \"description\": \"Выберите поле второй даты\",\r\n            \"help\": \"tpl_second_field\"\r\n        },\r\n        \"case_type\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"case_type\",\r\n            \"values\":{\"1\":\"Текст\",\"2\":\"Число\"},\r\n            \"description\": \"Записывать результат в виде\",\r\n            \"help\": \"tpl_to_field\"\r\n },\r\n        \"value\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"value\",\r\n            \"type_field\": [1,3],\r\n            \"description\": \"Выберите поле, в которое сохранять значение\",\r\n            \"no_system\": true,\r\n            \"help\": \"tpl_to_field\"\r\n        },\r\n        \"case_measure\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"case_measure\",\r\n            \"values\":{\"1\":\"Секунд\",\"2\":\"Минут\",\"3\":\"Часов\",\"4\":\"Дней\",\"5\":\"Недель\",\"6\":\"Месяцев\",\"7\":\"Лет\"},\r\n            \"description\": \"Выберите единицу измерения\",\r\n            \"help\": \"tpl_to_field\"\r\n },\r\n         \"decimal_places\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"decimal_places\",\r\n            \"description\": \"Знаков после запятой\",\r\n            \"help\": \"tpl_to_field\"\r\n        }\r\n   },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"field\"\r\n        },\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"field1\"\r\n        }\r\n    ]\r\n}',	'calc',	'date_diff.tpl',	'',	1,	'[1]',	12),
(85,	'Изменить поле',	'{if $put_field_type eq 2}\n {if $case_type eq 2}\n  $line[\'{$put_field.field}\'] = date(\'Y-m-d H:i:s\');\n {else}\n  $line[\'{$put_field.field}\'] = date(\'Y-m-d H:i:s\', strtotime(\'{$put_field.set_value}\'));\n {/if}\n{else}\n $line[\'{$put_field.field}\'] = \'{$put_field.set_value}\';\n{/if}',	'{\n    \"title\": \"Изменить поле\",\n    \"help\": \"tpl_field_change\",\n    \"parameters\": {\n        \"put_field\": {\n            \"type\": \"Field\",\n            \"name\": \"put_field\",\n            \"description\": \"Выберите поле для замены значения\",\n            \"no_system\": true,\n            \"help\": \"tpl_put_field\",\n            \"set_value\": \"true\",\n            \"set_value_description\": \"Введите значение, которое должно быть присвоено полю\",\n            \"set_help\": \"tpl_set_value\"\n        },\n        \"case_type\": {\n            \"type\": \"List\",\n            \"name\": \"case_type\",\n            \"values\":{\"1\":\"Ввод даты\",\"2\":\"Текущая дата\"},\n            \"description\": \"Вариант ввода\",\n            \"help\": \"tpl_to_field\"\n }\n    },\n    \"events\": [\n        {\n            \"type\": 1,\n            \"field\": \"\"\n        }\n    ]\n}',	'calc',	'field_change.tpl',	'',	1,	'[1]',	2),
(86,	'Дата: Изменить дату',	'$orig_date = strtotime($line[\'{$cur_field1}\']);\r\n$number = {if $number_value}{$number_value}{else}$line[\'{$cur_field2}\']{/if};\r\n{if $case_measure == 1}\r\n$new_date = $orig_date {$operand} $number;\r\n{elseif $case_measure == 2}\r\n$new_date = $orig_date {$operand} $number * 60;\r\n{elseif $case_measure == 3}\r\n$new_date = $orig_date {$operand} $number * 3600;\r\n{elseif $case_measure == 4}\r\n$new_date = $orig_date {$operand} $number * 3600 * 24;\r\n{elseif $case_measure == 5}\r\n$new_date = $orig_date {$operand} $number * 3600 * 24 * 7;\r\n{elseif $case_measure == 6}\r\n$new_date = mktime(date(\'H\', $orig_date), date(\'i\', $orig_date), date(\'s\', $orig_date), date(\'n\', $orig_date) {$operand} $number, date(\'j\', $orig_date), date(\'Y\', $orig_date));\r\n{elseif $case_measure == 7}\r\n$new_date = mktime(date(\'H\', $orig_date), date(\'i\', $orig_date), date(\'s\', $orig_date), date(\'n\', $orig_date), date(\'j\', $orig_date), date(\'Y\', $orig_date) {$operand} $number);\r\n{/if}\r\n$line[\'{$cur_field3}\'] = date(\'Y-m-d H:i:s\', $new_date);',	'{\r\n    \"title\": \"Дата: Изменить дату\",\r\n    \"help\": \"tpl_add_to_date\",\r\n    \"parameters\": {\r\n        \"cur_field1\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field1\",\r\n            \"description\": \"Выберите исходную дату\",\r\n            \"type_field\": [2,12],\r\n            \"help\": \"tpl_date_field\"\r\n        },\r\n        \"operand\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"operand\",\r\n            \"description\": \"Выберите опцию\",\r\n            \"values\": {\r\n              \"+\": \"Прибавить\",\r\n              \"-\": \"Отнять\"\r\n            },\r\n            \"help\": \"tpl_option\"\r\n        },\r\n         \"number_value\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"number_value\",\r\n            \"description\": \"Выберите число\",\r\n            \"help\": \"tpl_to_field\"\r\n        },\r\n        \"cur_field2\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field2\",\r\n            \"description\": \"либо Выберите поле с числом\",\r\n            \"type_field\": 1,\r\n            \"help\": \"tpl_sel_field\"\r\n        },\r\n        \"case_measure\": {\r\n            \"type\": \"List\",\r\n            \"name\": \"case_measure\",\r\n            \"values\":{\"1\":\"Секунд\",\"2\":\"Минут\",\"3\":\"Часов\",\"4\":\"Дней\",\"5\":\"Недель\",\"6\":\"Месяцев\",\"7\":\"Лет\"},\r\n            \"description\": \"Выберите единицу измерения\",\r\n            \"help\": \"tpl_to_field\"\r\n },\r\n        \"cur_field3\": {\r\n            \"type\": \"Field\",\r\n            \"name\": \"cur_field3\",\r\n            \"description\": \"Выберите поле для записи результата\",\r\n            \"type_field\": 2,\r\n            \"help\": \"tpl_result_field\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cur_field1\"\r\n        },\r\n        {\r\n            \"type\": 3,\r\n            \"field\": \"cur_field2\"\r\n        }\r\n    ]\r\n}',	'calc',	'date_change.tpl',	'',	1,	'[1]',	11);
EOT;
run_query($sql_query);