{*
 Шаблон вычисления "Экспорт записей в CSV"
*}

$exp_table_id = (int)$line['{$exp_table_field}'];
// готовим массив записей для экспорта
{if $filter}
$filter_value = sql_fetch_row(sql_select_field(FILTERS_TABLE, 'value', 'id = ', $line['{$filter}']))[0];
if ($filter_value) {
    $filter_value = '(' . filter_tpl_replace($filter_value) . ')';
    if (strpos($filter_value, '`status`') === false) {
        $filter_value .= ' AND status = 0';
    }
} else {
    $filter_value = 'status = 0';
}
{else}
$filter_value = 'status = 0';
{/if}
{if $date1 || $date2}
$date1 = {if $date1}$line['{$date1}']{else}'0000-00-00 00:00:00'{/if};
$date2 = {if $date2}str_replace(['0000', '00:00:00'], ['9999', '23:59:59'], $line['{$date2}']){else}'9999-00-00 00:00:00'{/if};
$filter_value .= " AND add_time BETWEEN '$date1' AND '$date2'";
{/if}
$sel = [];
$result = data_select_field($exp_table_id, 'id', $filter_value);
while ($row = sql_fetch_assoc($result)) {
    $sel[] = $row['id'];
}
if ($sel) {
    $_SESSION[$ses_id]['sel' . $exp_table_id] = $sel;
    // экспортируем данные
    $exp_file = 'temp/export' . $exp_table_id . '.csv';
{if $exp_template}
    $exp_template = sql_fetch_assoc(sql_select_field(EXPORT_TPL_TABLE, 'exp_config', "table_id = ",  $exp_table_id, " AND name = '{$exp_template}'"));
    $exp_config = unserialize($exp_template['exp_config']);
    export_data($exp_table_id, $exp_config, $exp_file, 'none');
{else}
    export_data($exp_table_id, '', $exp_file, 'none');
{/if}
    $content = file_get_contents($exp_file);
    unlink($exp_file);
    // сохраняем результат в поле
    $fname = $lang['Table'] . $exp_table_id . '.csv';
    $line['{$exp_file_field}'] = $fname;
    save_data_file({$exp_file_field|replace:'f':''}, $ID, $fname, $content);
} else {
    $error_trace[] = 'No data';
}