<?php
$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = '{if $field == $field1}\r\n$line[\'{$value}\'] = 0;\r\n{else}\r\nif ($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({if $field == \'currentDate\'}date(\'Y-m-d H:i:s\'){else}$line[\'{$field}\']{/if});\r\n    $datetime2 = strtotime({if $field1 == \'currentDate\'}date(\'Y-m-d H:i:s\'){else}$line[\'{$field1}\']{/if});\r\n    $interval = $datetime2 - $datetime1;\r\n{if $case_measure == 1}\r\n    $line[\'{$value}\'] = $interval;\r\n{elseif $case_measure == 2}\r\n    $line[\'{$value}\'] = $interval / 60;\r\n{elseif $case_measure == 3}\r\n    $line[\'{$value}\'] = $interval / 3600;\r\n{elseif $case_measure == 4}\r\n    $line[\'{$value}\'] = $interval / (3600 * 24);\r\n{elseif $case_measure == 5}\r\n    $line[\'{$value}\'] = $interval / (3600 * 24 * 7);\r\n{elseif $case_measure == 6}\r\n    $line[\'{$value}\'] = $interval / (3600 * 24 * 30);\r\n{elseif $case_measure == 7}\r\n    $line[\'{$value}\'] = $interval / (3600 * 24 * 365.25);\r\n{/if}\r\n{else}\r\n    $datetime1 = date_create({if $field == \'currentDate\'}date(\'Y-m-d H:i:s\'){else}$line[\'{$field}\']{/if});\r\n    $datetime2 = date_create({if $field == \'currentDate\'}date(\'Y-m-d H:i:s\'){else}$line[\'{$field1}\']{/if});\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{/if}',
params = '{\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    },\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}'
WHERE id = 84
EOT;
run_query($sql_query);