<?php

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = '{if $listerine == 3}// Выберите способ ввода - текущая дата
$line["{$cur_field3}"] = date("Y-m-d H:i:s");
{else}
{if $listerine == 1}// Выберите способ ввода - число
$number = {$number_value|default:0};
{elseif $listerine == 2}// Выберите способ ввода - поле с числом
$number = $line["{$cur_field2}"] ? $line["{$cur_field2}"] : 0;
{/if}
$orig_date = strtotime($line["{$cur_field1}"]);
{if $case_measure == 4}
$new_date = $orig_date {$operand} $number;
{elseif $case_measure == 2}
$new_date = $orig_date {$operand} $number * 60;
{elseif $case_measure == 3}
$new_date = $orig_date {$operand} $number * 3600;
{elseif $case_measure == 1}
$new_date = $orig_date {$operand} $number * 3600 * 24;
{elseif $case_measure == 5}
$new_date = $orig_date {$operand} $number * 3600 * 24 * 7;
{elseif $case_measure == 6}
$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));
{elseif $case_measure == 7}
$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);
{/if}
$line["{$cur_field3}"] = date("Y-m-d H:i:s", $new_date);
{/if}', params = '{
    "title": "Дата: Изменить дату",
    "help": "tpl_add_to_date",
    "parameters": {
        "listerine": {
            "type": "List",
            "name": "listerine",
            "description": "Выберите способ ввода:",
            "values":{"1":"число","2":"поле с числом","3":"текущая дата"},
            "no_system": true,
            "help": "tpl_cur_date_field"
        },
        "number_value": {
            "type": "String",
            "name": "number_value",
            "description": "Выберите число",
            "help": "tpl_to_field"
        },
        "cur_field1": {
            "type": "Field",
            "name": "cur_field1",
            "description": "Поле-источник",
            "type_field": [2,12],
            "help": "tpl_date_field"
        },
        "operand": {
            "type": "List",
            "name": "operand",
            "description": "Выберите опцию",
            "values": {
                "+": "Прибавить",
                "-": "Отнять"
            },
            "help": "tpl_option"
        },
        "cur_field2": {
            "type": "Field",
            "name": "cur_field2",
            "description": "Выберите поле с числом",
            "type_field": 1,
            "help": "tpl_sel_field"
        },
        "case_measure": {
            "type": "List",
            "name": "case_measure",
            "values":{"1":"Дней","2":"Минут","3":"Часов","4":"Секунд","5":"Недель","6":"Месяцев","7":"Лет"},
            "description": "Выберите единицу измерения",
            "help": "tpl_to_field"
        },
        "cur_field3": {
            "type": "Field",
            "name": "cur_field3",
            "description": "Поле-получатель",
            "type_field": 2,
            "help": "tpl_result_field"
        }
    },
    "events": [
        {
            "type": 3,
            "field": "cur_field1"
        },
        {
            "type": 3,
            "field": "cur_field2"
        }
    ]
}' WHERE id = 86
EOT;
run_query($sql_query);
