$sql .= " and (groupid = " . $_SESSION['curuser']['groupid'] . " OR (groupid = 0 AND (resellerid='" . $_SESSION['curuser']['resellerid'] . "' OR resellerid=0)))"; } else { $sql .= " where (groupid = " . $_SESSION['curuser']['groupid'] . " OR (groupid = 0 AND (resellerid='" . $_SESSION['curuser']['resellerid'] . "' OR resellerid=0)))"; } } else { if (strpos(strtolower($sql), 'where')) { $sql .= " and groupid = " . $_SESSION['curuser']['groupid']; } else { $sql .= " where groupid = " . $_SESSION['curuser']['groupid']; } } } elseif ($_SESSION['curuser']['usertype'] == 'reseller') { if (strpos(strtolower($sql), 'where')) { $sql .= " and resellerid = " . $_SESSION['curuser']['resellerid']; } else { $sql .= " where resellerid = " . $_SESSION['curuser']['resellerid']; } } } if (!function_exists('mb_convert_encoding')) { echo "Please install php-mbstring!"; exit; } ob_start(); header("charset=uft-8"); header('Content-type: application/force-download'); header('Content-Transfer-Encoding: Binary'); header('Content-disposition: attachment; filename=astercc.csv'); echo astercrm::exportDataToCSV($sql); ob_end_flush(); unset($_SESSION['export_sql']);
function archiveCDR($archiveDate) { global $db, $locate, $config; $objResponse = new xajaxResponse(); $date = date("Y-m-d"); $end_date = date("Y-m-d", strtotime("{$date} - {$archiveDate} month")); if ($config['system']['useHistoryCdr'] == 1) { $table = 'historycdr'; } else { $table = 'mycdr'; } $sql = "SELECT calldate FROM {$table} WHERE calldate < '" . $end_date . "' ORDER BY calldate ASC LIMIT 1"; $start_date = $db->getOne($sql); if ($start_date == '') { $objResponse->addAlert($locate->Translate('no cdr data early than') . " " . $archiveDate . " " . $locate->Translate('months')); $objResponse->addAssign("divMsg", "style.visibility", "hidden"); $objResponse->addClear("msgZone", "innerHTML"); return $objResponse->getXML(); } $file_dir = $config['system']['upload_file_path'] . "cdr_archive"; if (!is_dir($file_dir)) { if (!mkdir($file_dir)) { $objResponse->addAlert($locate->Translate('cant create archive directory')); $objResponse->addAssign("divMsg", "style.visibility", "hidden"); $objResponse->addClear("msgZone", "innerHTML"); return $objResponse->getXML(); } } $start_date = split('\\ ', $start_date); $start_date = $start_date['0']; $file_name = $start_date . "_to_" . $end_date; if (!($handle = fopen($file_dir . "/" . $file_name . ".csv", 'x'))) { $objResponse->addAlert($locate->Translate('cant create archive file')); $objResponse->addAssign("divMsg", "style.visibility", "hidden"); $objResponse->addClear("msgZone", "innerHTML"); return $objResponse->getXML(); } $sql = "SELECT * FROM {$table} WHERE calldate < '" . $end_date . "' ORDER BY calldate ASC"; $archiveData = astercrm::exportDataToCSV($sql); if (!fwrite($handle, $archiveData)) { $objResponse->addAlert($locate->Translate('cant create archive file')); $objResponse->addAssign("divMsg", "style.visibility", "hidden"); $objResponse->addClear("msgZone", "innerHTML"); return $objResponse->getXML(); } fclose($handle); system("tar zcf " . $file_dir . "/" . $file_name . ".tar.gz " . $file_dir . "/" . $file_name . ".csv", $r); if ($r === false) { $final_file = $file_dir . "/" . $file_name . ".csv"; } else { $final_file = $file_dir . "/" . $file_name . ".tar.gz"; unlink($file_dir . "/" . $file_name . ".csv"); } $objResponse->addAlert($locate->Translate('archive success') . ", " . $locate->Translate('file save in') . ": " . $final_file); $sql = "DELETE FROM {$table} WHERE calldate < '" . $end_date . "'"; $res = $db->query($sql); if ($res == 1) { $objResponse->addAlert($locate->Translate('clear cdr date success')); } else { $objResponse->addAlert($locate->Translate('clear cdr date failed')); } $html = createGrid(0, ROWSXPAGE); $objResponse->addAssign("divMsg", "style.visibility", "hidden"); $objResponse->addClear("msgZone", "innerHTML"); $objResponse->addAssign("grid", "innerHTML", $html); return $objResponse->getXML(); //echo $file_name;exit; }
if (strpos(strtolower($sql), 'where')) { if ($table != '') { //判断是否传了主表名 $sql .= " and {$table}.groupid = " . $_SESSION['curuser']['groupid']; $filename = $table; } else { $sql .= " and groupid = " . $_SESSION['curuser']['groupid']; } } else { if ($table != '') { //判断是否传了主表名 $sql .= " where {$table}.groupid = " . $_SESSION['curuser']['groupid']; $filename = $table; } else { $sql .= " where groupid = " . $_SESSION['curuser']['groupid']; } } } if ($table != '') { //判断是否传了主表名 $filename = $table . '.csv'; } //echo $sql;exit; ob_start(); header("charset=uft-8"); header('Content-type: application/force-download'); header('Content-Transfer-Encoding: Binary'); header('Content-disposition: attachment; filename=' . $filename); echo astercrm::exportDataToCSV($sql, $table); ob_end_flush(); unset($_SESSION['export_sql']);