<?php
$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = 'if ($line[\'{$cond_table.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        \'count(id) as cnt\',\r\n        \'status = 0\' . ($event[\'type\'] === \'delete\' ? \' AND id != \' . $ID : \'\') . \r\n        \' AND {$cond_table.link_field_id} = \' . (int)$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 && $cond_table.field_type == 3}\r\n    $filter_str = isset($filter) ? \'&filter=\' . $filter[\'id\'] : \'\';\r\n    $url = $config[\'site_url\'] . \"/fields.php?table=\" . $table_id . \"&rel_search&rel_table=\" . get_table_id({$cond_table.table}) . \"&rel_line=\" . \r\n        $line[\'{$cond_table.link_field_id}\'][\'raw\'] . \"&rel_field=\" . get_field_id({$cond_table.link_field_id|substr:1}) . $filter_str;\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}\r\nif ($event[\'changed\'][{$cond_table.link_field_id|substr:1}][\'old\'] > 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        \'count(id) as cnt\',\r\n        \'status = 0 AND {$cond_table.link_field_id} = \' . (int)$event[\'changed\'][{$cond_table.link_field_id|substr:1}][\'old\'] . \r\n        ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\')\r\n    );\r\n    $row = sql_fetch_assoc($result);\r\n{if $operand == 1 && $cond_table.field_type == 3}\r\n    $filter_str = isset($filter) ? \'&filter=\' . $filter[\'id\'] : \'\';\r\n    $url = $config[\'site_url\'] . \"/fields.php?table=\" . $table_id . \"&rel_search&rel_table=\" . get_table_id({$cond_table.table}) . \"&rel_line=\" . \r\n        $event[\'changed\'][{$cond_table.link_field_id|substr:1}][\'old\'] . \"&rel_field=\" . get_field_id({$cond_table.link_field_id|substr:1}) . $filter_str;\r\n    data_update({$cond_table.table}, [\'{$cond_table.field}\' => \"<a href=\'\" . $url . \"\'>\" . $row[\'cnt\'] . \"</a>\"], \'id = \', $event[\'changed\'][{$cond_table.link_field_id|substr:1}][\'old\']);\r\n{else}\r\n    data_update({$cond_table.table}, [\'{$cond_table.field}\' => $row[\'cnt\']], \'id = \', $event[\'changed\'][{$cond_table.link_field_id|substr:1}][\'old\']);\r\n{/if}\r\n}',
params = '{\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\": 3,\r\n            \"field\": \"cond_table\"\r\n        },\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}'
WHERE id = 36
EOT;
run_query($sql_query);