<?php
$result = run_query("SELECT record_table FROM " . $config["table_prefix"] . "module_asterisk_servers");
$row = sql_fetch_assoc($result);
$table_id = $row['record_table'];
if ($table_id) {
    $result = run_query("SELECT id FROM " . $config["table_prefix"] . "buttons WHERE table_id=" . $table_id . " and name='Выгрузка записи'");
    $row = sql_fetch_assoc($result);
    $button_id = $row['id'];
    if (!$button_id) {
        $result = run_query("SELECT id FROM " . $config["table_prefix"] . "fields WHERE table_id=" . $table_id . " and name_field='Запись'");
        $row = sql_fetch_assoc($result);
        $rec_field_id = $row['id'];
        $result = run_query("SELECT id FROM " . $config["table_prefix"] . "fields WHERE table_id=" . $table_id . " and name_field='Номер'");
        $row = sql_fetch_assoc($result);
        $phone_field_id = $row['id'];
        $result = run_query("SELECT id FROM " . $config["table_prefix"] . "fields WHERE table_id=" . $table_id . " and name_field='Дата и время начала'");
        $row = sql_fetch_assoc($result);
        $date_field_id = $row['id'];
        if ($rec_field_id and $phone_field_id and $date_field_id) {
            $calculate = "\$sel = \$_REQUEST[''sel''] ? \$_REQUEST[''sel''] : array(\$line[''id'']);
if (count(\$sel)==1)
{
    \$fname = \$config[''site_path''].''/modules/asterisk/records/''.substr(\$line[''f" . $rec_field_id . "''],36,4).''.mp3'';
    \$fsize = filesize(\$fname);
    if (\$fsize) header(''Content-Length: ''.\$fsize);
    \$name = \$line[''id''].''_''.\$line[''f" . $phone_field_id . "''].''_''.date(''Ymd'', strtotime(\$line[''f" . $date_field_id . "''])).''.mp3'';
    header(''Content-Disposition: attachment; filename=\"''.\$name.''\"'');
    header(''Content-type: audio/mpeg'');
    echo file_get_contents(\$fname);
}
else
{
    global \$my_rec_num, \$my_zip;
    \$my_rec_num++;
    if (\$my_rec_num==1)
    {
        \$my_zip = new ZipArchive;
        \$my_zip->open(\$config[''site_path''].''/temp/records.zip'', ZipArchive::CREATE);
    }
    \$name = \$line[''id''].''_''.\$line[''f" . $phone_field_id . "''].''_''.date(''Ymd'', strtotime(\$line[''f" . $date_field_id . "''])).''.mp3'';
    \$my_zip->addFile(\$config[''site_path''].''/modules/asterisk/records/''.substr(\$line[''Запись''],36,4).''.mp3'', \$name);
    if (\$my_rec_num==count(\$sel))
    {
        \$my_zip->close();
        \$fname = \$config[''site_path''].''/temp/records.zip'';
        \$fsize = filesize(\$fname);
        if (\$fsize) header(''Content-Length: ''.\$fsize);
        header(''Content-Disposition: attachment; filename=\"records.zip\"'');
        header(''Content-type: application/zip'');
        echo file_get_contents(\$fname);
        unlink(\$fname);
    }
}";
            run_query("INSERT INTO " . $config["table_prefix"] . "buttons (table_id, name, type, num) VALUES ($table_id, 'Выгрузка записи', 1, 1)");
            $button_id = sql_insert_id();
            run_query("INSERT INTO " . $config["table_prefix"] . "acc_buttons (button_id, group_id, access) VALUES ($button_id, 1, 1)");
            run_query("INSERT INTO " . $config["table_prefix"] . "calc (table_id, name, calculate) VALUES ($table_id, 'Button $button_id','$calculate')");
            $calc_id = sql_insert_id();
            run_query("INSERT INTO " . $config["table_prefix"] . "calc_cond (calc_id, type, param) VALUES ($calc_id, 24, $button_id)");
        }
    }
}
?>