<?php
$sql_prefix = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query1 = <<<'EOT'
`calculate_tpl` = 'if ($event[\'type\'] === \'delete\') {\n    $deleteCond = "AND id <> $ID";\n}\n$filter = (int)"{$filter}";\n$filter = sql_select_array(FILTERS_TABLE, \'id=\' . $filter);\nif ($filter[\'value\']) {\n    $filter[\'value\'] = filter_tpl_replace($filter[\'value\']);\n} else {\n    $filter[\'value\'] = \'1 = 1\';\n}\n$pre = data_select_field(\n    $table_id,\n    "COUNT(id) AS cnt",\n    "status = 0 " . $deleteCond . " AND {$cond_table.link_field_id} = " . $line[\'{$cond_table.link_field_id}\'][\'raw\'] . \n    " AND " . $filter[\'value\']\n);\n$res = sql_fetch_assoc($pre);\n\nif ({$operand} == 1) {\n    $filter_str = \'\';\n    if (isset($filter)) {\n        $filter_str = \'&filter=\' . $filter[\'id\'];\n    }\n    $line_link = substr("{$cond_table.link_field_id}", 1);\n    $url = $config[\'site_url\'] . "/fields.php?table=" . $table_id . "&rel_search&rel_table={$cond_table.table}&rel_line=" . \n    $line[\'{$cond_table.link_field_id}\'][\'raw\'] . "&rel_field=" . $line_link . $filter_str;\n    {if $cond_table.field_type eq 3}\n        $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = "<a href=\'" . $url . "\'>" . $res[\'cnt\'] . "</a>";\n    {else} \n        $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $res[\'cnt\'];\n    {/if}\n} else {\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $res[\'cnt\'];\n}'
WHERE id = 36
EOT;
$sql_query2 = <<<'EOT'
`calculate_tpl` = ' $cur_field2 = \'{$cur_field2}\';\n$cur_field2 = str_replace(\'f\', \'\', $cur_field2);\n$cur_field1 = \'{$cur_field1}\';\n$cur_field1 = str_replace(\'f\', \'\', $cur_field1);\n{if $cur_field_type == 6 || $cur_field_type == 9}\n   $user_agent = $_SERVER["HTTP_USER_AGENT"];\n   if (strpos($user_agent, "MSIE") !== false) {\n       $exp = "\\n\\r";\n   } else {\n       $exp = "\\r\\n";    \n   }\n   $files = explode($exp, $line[\'{$cur_field2}\']);\n   foreach ($files as $fname) {\n       $orig_fpath = get_file_path($cur_field2, $line[\'id\'], $fname);\n       save_data_file($cur_field1, $line[\'id\'], $fname, file_get_contents($orig_fpath));\n   }\n{/if}\nif ({$list} == 0) {\n    $line[\'{$cur_field1}\'] = $line[\'{$cur_field2}\'];\n} else {\n    if (!$line[\'{$cur_field1}\']) {\n        $line[\'{$cur_field1}\'] = $line[\'{$cur_field2}\'];\n    }\n}'
WHERE id = 2
EOT;
run_query($sql_prefix . $sql_query1);
run_query($sql_prefix . $sql_query2);
$sql_query1 = <<<'EOT'
`calculate_tpl` = '$line[\'{$cur_field}\'] = $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'];\n$cur_field2 = \'{$gen_field.field}\';\n$cur_field2 = str_replace(\'f\', \'\', $cur_field2);\n$cur_field1 = \'{$cur_field}\';\n$cur_field1 = str_replace(\'f\', \'\', $cur_field1);\n{if $cur_field_type == 6 || $cur_field_type == 9}\n   $user_agent = $_SERVER["HTTP_USER_AGENT"];\n   if (strpos($user_agent, "MSIE") !== false) {\n       $exp = "\\n\\r";\n   } else {\n       $exp = "\\r\\n";    \n   }\n   $files = explode($exp, $line[\'{$gen_field.field}\']);\n   foreach ($files as $fname) {\n       $orig_fpath = get_file_path($cur_field2, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname);\n       save_data_file($cur_field1, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname, file_get_contents($orig_fpath));\n   }\n{/if}'
WHERE id = 5
EOT;
$sql_query2 = <<<'EOT'
`calculate_tpl` = '$line[\'{$cur_field}\'] = $line[\'{$gen_field.link_field_id}\'][\'{$gen_field.field}\'];\n$cur_field2 = \'{$gen_field.field}\';\n$cur_field2 = str_replace(\'f\', \'\', $cur_field2);\n$cur_field1 = \'{$cur_field}\';\n$cur_field1 = str_replace(\'f\', \'\', $cur_field1);\n{if $cur_field == 6 || $cur_field == 9}\n   $user_agent = $_SERVER["HTTP_USER_AGENT"];\n   if (strpos($user_agent, "MSIE") !== false) {\n       $exp = "\\n\\r";\n   } else {\n       $exp = "\\r\\n";    \n   }\n   $files = explode($exp, $line[\'{$gen_field.field}\']);\n   foreach ($files as $fname) {\n       $orig_fpath = get_file_path($cur_field2, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname);\n       save_data_file($cur_field1, $line[\'{$gen_field.link_field_id}\'][\'id\'], $fname, file_get_contents($orig_fpath));\n   }\n{/if}'
WHERE id = 8
EOT;
run_query($sql_prefix . $sql_query1);
run_query($sql_prefix . $sql_query2);
$sql_query1 = <<<'EOT'
`calculate_tpl` = '$cur_field2 = \'{$field}\';\n$cur_field2 = str_replace(\'f\', \'\', $cur_field2);\n$cur_field1 = \'{$subtable.field}\';\n$cur_field1 = str_replace(\'f\', \'\', $cur_field1);\n{if $subtable.field_type == 6 || $subtable.field_type == 9}\n   $user_agent = $_SERVER["HTTP_USER_AGENT"];\n   if (strpos($user_agent, "MSIE") !== false) {\n       $exp = "\\n\\r";\n   } else {\n       $exp = "\\r\\n";    \n   }\n   $files = explode($exp, $line[\'{$field}\']);\n   $res = data_select_field({$subtable.table}, \'id\',\'status=0 and {$subtable.link_field_id} =\' . $line[\'id\']);\n   while ($row = sql_fetch_assoc($res)) {\n       foreach ($files as $fname) {\n           $orig_fpath = get_file_path($cur_field2, $line[\'id\'], $fname);\n           save_data_file($cur_field1, $row[\'id\'], $fname, file_get_contents($orig_fpath));\n       }\n   }\n{/if}\ndata_update(\n    {$subtable.table},\n    array(\'{$subtable.field}\' => $line[\'{$field}\']),\n    \'status = 0 AND {$subtable.link_field_id} = \' . $line[\'id\']\n);'
WHERE id = 25
EOT;
$sql_query2 = <<<'EOT'
`calculate_tpl` = 'if ({$operand} == 1) {\n    data_update(\n        {$subtable.table},\n        EVENTS_ENABLE,\n        array(\'{$subtable.field}\' => $line[\'{$field}\']),\n        \'status = 0 AND {$subtable.link_field_id} = \' . $line[\'id\']\n    );\n} else {\n    data_update(\n        {$subtable.table},\n        array(\'{$subtable.field}\' => $line[\'{$field}\']),\n        \'status = 0 AND {$subtable.link_field_id} = \' . $line[\'id\']\n    );\n}\n$cur_field2 = \'{$field}\';\n$cur_field2 = str_replace(\'f\', \'\', $cur_field2);\n$cur_field1 = \'{$subtable.field}\';\n$cur_field1 = str_replace(\'f\', \'\', $cur_field1);\n{if $subtable.field_type == 6 || $subtable.field_type == 9}\n   $user_agent = $_SERVER["HTTP_USER_AGENT"];\n   if (strpos($user_agent, "MSIE") !== false) {\n       $exp = "\\n\\r";\n   } else {\n       $exp = "\\r\\n";    \n   }\n   $files = explode($exp, $line[\'{$field}\']);\n   $res = data_select_field({$subtable.table}, \'id\',\'status=0 and {$subtable.link_field_id} =\' . $line[\'id\']);\n   while ($row = sql_fetch_assoc($res)) {\n       foreach ($files as $fname) {\n           $orig_fpath = get_file_path($cur_field2, $line[\'id\'], $fname);\n           save_data_file($cur_field1, $row[\'id\'], $fname, file_get_contents($orig_fpath));\n       }\n   }\n{/if}'
WHERE id = 31
EOT;
run_query($sql_prefix . $sql_query1);
run_query($sql_prefix . $sql_query2);
$sql_query1 = <<<'EOT'
`calculate_tpl` = 'if ($event[\'type\'] === \'delete\') {\n    $deleteCond = " AND id <> $ID";\n}\n$filter = sql_select_array(FILTERS_TABLE, \'id={$filter}\');\n$result = data_select_field($table_id, \'{$cond_field}\',\n    \'status = 0 AND {$cond_table.link_field_id} = \' . intval($line[\'{$cond_table.link_field_id}\'][\'raw\']) . $deleteCond .\n    ($filter[\'value\'] ? \' AND (\' . filter_tpl_replace($filter[\'value\']) . \')\' : \'\') . \' ORDER BY id {if $record == "last"}DESC{/if} LIMIT 1\');\n$row = sql_fetch_assoc($result);\n{if $add_value == \'yes\'}\nif ($line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] != \'\') {\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] .= "{$add_delim}" . $row[\'{$cond_field}\'];\n} else {\n    $line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'{$cond_field}\'];\n}\n{else}\n$line[\'{$cond_table.link_field_id}\'][\'{$cond_table.field}\'] = $row[\'{$cond_field}\'];\n{/if}\n$cur_field2 = \'{$cond_field}\';\n$cur_field2 = str_replace(\'f\', \'\', $cur_field2);\n$cur_field1 = \'{$cond_table.field}\';\n$cur_field1 = str_replace(\'f\', \'\', $cur_field1);\n{if $cond_table.field_type == 6 || $cond_table.field_type == 9}\n   $user_agent = $_SERVER["HTTP_USER_AGENT"];\n   if (strpos($user_agent, "MSIE") !== false) {\n       $exp = "\\n\\r";\n   } else {\n       $exp = "\\r\\n";    \n   }\n   $files = explode($exp, $line[\'{$cond_field}\']);\n   foreach ($files as $fname) {\n       $orig_fpath = get_file_path($cur_field2, $line[\'id\'], $fname);\n       save_data_file($cur_field1, $line[\'id\'], $fname, file_get_contents($orig_fpath));\n   }\n{/if}'
WHERE id = 42
EOT;
$sql_query2 = <<<'EOT'
`calculate_tpl` = '$result = data_select_field({$sub_table.subTable}, \'{$sub_table.subField}\',\n    \'status = 0 AND {$sub_table.link_field_id} = \' . $ID . \' ORDER BY id {if $sub_record == "last"}DESC{/if} LIMIT 1\');\n$row = sql_fetch_assoc($result);\n{if $add_value == \'yes\'}\nif ($line[\'{$res_field}\'] != \'\') {\n    $line[\'{$res_field}\'] .= "{$add_delim}" . $row[\'{$sub_table.subField}\'];\n} else {\n    $line[\'{$res_field}\'] = $row[\'{$sub_table.subField}\'];\n}\n{else}\n$line[\'{$res_field}\'] = $row[\'{$sub_table.subField}\'];\n{/if}\n$cur_field2 = \'{$sub_table.subField}\';\n$cur_field2 = str_replace(\'f\', \'\', $cur_field2);\n$cur_field1 = \'{$res_field}\';\n$cur_field1 = str_replace(\'f\', \'\', $cur_field1);\n{if $res_field == 6 || $res_field == 9}\n   $user_agent = $_SERVER["HTTP_USER_AGENT"];\n   if (strpos($user_agent, "MSIE") !== false) {\n       $exp = "\\n\\r";\n   } else {\n       $exp = "\\r\\n";    \n   }\n   $files = explode($exp, $line[\'{$cond_field}\']);\n   foreach ($files as $fname) {\n       $orig_fpath = get_file_path($cur_field2, $line[\'id\'], $fname);\n       save_data_file($cur_field1, $line[\'id\'], $fname, file_get_contents($orig_fpath));\n   }\n{/if}'
WHERE id = 43
EOT;
run_query($sql_prefix . $sql_query1);
run_query($sql_prefix . $sql_query2);
$sql_query1 = <<<'EOT'
`calculate_tpl` = '$sel_table = (int)\'{$correlation[\'table\']}\';\n$sel_fields = [\n    {foreach from = $correlation[\'to_field\'] item = field_id}(int)\'{$field_id}\', {/foreach}\n];\n$sel_def_vals = [\n    {foreach from = $correlation[\'def_val\'] item = field_id}\'{$field_id}\', {/foreach}\n];\n$cur_fields = [\n    {foreach from = $correlation[\'cur_field\'] item = field_id}\'{$field_id}\', {/foreach}\n];\nif ($sel_table > 0) {\n    $ins_arr = [];\n    foreach ($sel_fields as $num => $field_id) {\n        if (!$field_id) continue;\n        $def_val = $sel_def_vals[$num];\n        if ($def_val) {\n             $ins_arr[\'f\' . $field_id] = $def_val;\n        } else {\n             if (is_array($line[\'f\' . $cur_field[$num]])) {\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\n             } else {\n                 if ($cur_fields[$num] !== \'id\') {\n                     $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\n                 } else {\n                     $ins_arr[\'f\' . $field_id] = $line[$cur_fields[$num]];\n                 }                \n             }\n        }\n        $field_check = sql_select_array(FIELDS_TABLE, \'id=\' . $field_id);\n        if ($field_check[\'type_field\'] == 6 || $field_check[\'type_field\'] == 9) { \n            $user_agent = $_SERVER["HTTP_USER_AGENT"];\n            if (strpos($user_agent, "MSIE") !== false) {      \n                $exp = "\\n\\r";      \n            } else {         \n                $exp = "\\r\\n";    \n            }   \n            $files = explode($exp, $def_val);      \n            foreach ($files as $fname) {        \n                $orig_fpath = get_file_path($cur_fields[$num], $line[\\\'id\\\'], $fname); \n                save_data_file($field_id, $line[\\\'id\\\'], $fname, file_get_contents($orig_fpath));  \n            }\n        }      \n    }\n    if (count($ins_arr) > 0) {\n        if ($new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr)) {\n{if $correlation[\'save_field\']}\n            $line[\'f{$correlation[\'save_field\']}\'] = $new_id;\n            data_update($table_id, EVENTS_ENABLE, [\'f{$correlation[\'save_field\']}\' => $new_id], \'id = \' . $ID);\n{/if}\n{if $addit neq \'stop\'}\n{if $addit eq \'go_edit\'}\n            if (isset($_REQUEST[\'parameters\'])) {\n                echo \'<script type="text/javascript">\n                    window.opener.location.href = "\' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'&edit_mode=on";\n                    window.close();\n                    </script>\';\n            } else {\n                header(\'Location: \' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'&edit_mode=on\'); exit;\n            }\n{else}\n            if (isset($_REQUEST[\'parameters\'])) {\n                echo \'<script type="text/javascript">\n                    window.opener.location.href = "\' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id . \'";\n                    window.close();\n                    </script>\';\n            } else {\n                header(\'Location: \' . $config[\'site_root\'] . \'/view_line2.php?table=\' . $sel_table . \'&line=\' . $new_id); exit;\n            }\n{/if}\n{/if}\n        } else {\n            $error_trace[] = true;\n        }\n    } else {\n        $error_trace[] = true;\n    }\n} else {\n    $error_trace[] = true;\n}\n'
WHERE id = 50
EOT;
$sql_query2 = <<<'EOT'
`calculate_tpl` = '$sel_table = (int)\'{$correlation[\'table\']}\';\n$sel_fields = [\n    {foreach from = $correlation[\'to_field\'] item = field_id}(int)\'{$field_id}\', {/foreach}\n];\n$sel_def_vals = [\n    {foreach from = $correlation[\'def_val\'] item = field_id}\'{$field_id}\', {/foreach}\n];\n$cur_fields = [\n    {foreach from = $correlation[\'cur_field\'] item = field_id}\'{$field_id}\', {/foreach}\n];\nif ($sel_table > 0) {\n    $ins_arr = [];\n    foreach ($sel_fields as $num => $field_id) {\n        if (!$field_id) continue;\n        $def_val = $sel_def_vals[$num];\n        if ($def_val) {\n             $ins_arr[\'f\' . $field_id] = $def_val;\n        } else {\n             if (is_array($line[\'f\' . $cur_field[$num]])) {\n                 $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]][\'raw\'];\n             } else {\n                 if ($cur_fields[$num] !== \'id\') {\n                     $ins_arr[\'f\' . $field_id] = $line[\'f\' . $cur_fields[$num]];\n                 } else {\n                     $ins_arr[\'f\' . $field_id] = $line[$cur_fields[$num]];\n                 }                \n             }\n        }\n        $field_check = sql_select_array(FIELDS_TABLE, \'id=\' . $field_id);\n        if ($field_check[\'type_field\'] == 6 || $field_check[\'type_field\'] == 9) { \n            $user_agent = $_SERVER["HTTP_USER_AGENT"];\n            if (strpos($user_agent, "MSIE") !== false) {      \n                $exp = "\\n\\r";      \n            } else {         \n                $exp = "\\r\\n";    \n            }   \n            $files = explode($exp, $def_val);      \n            foreach ($files as $fname) {        \n                $orig_fpath = get_file_path($cur_fields[$num], $line[\\\'id\\\'], $fname); \n                save_data_file($field_id, $line[\\\'id\\\'], $fname, file_get_contents($orig_fpath));  \n            }\n        }      \n    }\n    if (count($ins_arr) > 0) {\n        $new_id = data_insert($sel_table, EVENTS_ENABLE, $ins_arr);\n{if $correlation[\'save_field\']}\n        $line[\'f{$correlation[\'save_field\']}\'] = $new_id;\n{/if}\n    }\n}\n'
WHERE id = 78
EOT;
run_query($sql_prefix . $sql_query1);
run_query($sql_prefix . $sql_query2);
