<?php
$sql_query = 'INSERT INTO ' . $config['table_prefix'] . 'calc_template (`id`, `name`, `calculate_tpl`, `params`, `type`, `tpl_name`, `javascript_tpl`, `tpl_category`, `calc_types`, `calc_num`) VALUES ';
$sql_query .= <<<'EOT'
(122,	'Изменить следующую строку в подтаблице',	'if($event[\'type\'] != \'save\'){\r\n    global $calc_fields_changes;\r\n    $par_table_id = get_table_id({$display_none|default:-1});\r\n    $link_field_id = sql_fetch_row(sql_select_field(SUBTABLES_TABLE, \'link_field_id\', \'table_id = \' . $par_table_id . \' AND link_table_id = \' . $table_id))[0];\r\n    $par_link_id = is_array($line[\'f\' . $link_field_id]) ? $line[\'f\' . $link_field_id][\'raw\'] : $line[\'f\' . $link_field_id];\r\n    if ($par_link_id > 0) {\r\n        data_update($table_id, [\'{$put_field.field}\' => \'{$put_field.set_value}\'], \'status = 0 AND f\' . $link_field_id . \' = \', $par_link_id, \' AND id > \', $line[\'id\'], \' ORDER BY id LIMIT 1\');\r\n        $line_id = data_select_array($table_id, \'status = 0 AND f\' . $link_field_id . \' = \', $par_link_id, \' AND id > \', $line[\'id\'], \' ORDER BY id LIMIT 1\')[\'id\'];\r\n        $field_id = substr(\'{$put_field.field}\', 1);\r\n        $calc_fields_changes[$table_id][$field_id][$line_id][\'{$put_field.field}\'] = \'{$put_field.set_value}\';\r\n    }\r\n}',	'{\r\n    \"title\": \"Изменить следующую строку в подтаблице\",\r\n    \"help\": \"change_next_record_in_a_subtable\",\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        \"display_none\": {\r\n            \"type\": \"String\",\r\n            \"name\": \"display_none\",\r\n            \"description\": \"display_none\",\r\n            \"help\": \"\"\r\n        }\r\n    },\r\n    \"events\": [\r\n        {\r\n            \"type\": 1\r\n        }\r\n    ]\r\n}',	'calc',	'change_next_record_in_a_subtable.tpl',	'',	2,	'[2]',	6)
EOT;
run_query($sql_query);
