westdc-zf1/application/models/Stat.php

218 lines
4.6 KiB
PHP
Raw Normal View History

2013-01-12 08:48:40 +00:00
<?php
/**
* Stat 统计
*/
class Stat
{
private $db;//传入PDO对象
//使用到的公共变量
public $tbl_dataorder = "dataorder";
function __construct($db)
{
$this->db = $db;
}
//获得dataorder中的所有用户
public function UserDataorder(){
$dataorder = $this->tbl_dataorder;
$sql = "SELECT d.userid,u.realname
,(SELECT count(dd.id) as offline FROM $dataorder dd WHERE dd.userid=d.userid AND (dd.offlineappid > 0 AND dd.onlineappid < 0) GROUP BY dd.userid) as offline
,(SELECT count(dd.id) as online FROM $dataorder dd WHERE dd.userid=d.userid AND (dd.offlineappid < 0 AND dd.onlineappid > 0) GROUP BY dd.userid) as online
,count(d.id) as total
FROM $dataorder d
LEFT JOIN users u ON u.id=d.userid
WHERE (d.offlineappid > 0 OR d.onlineappid > 0)
GROUP BY d.userid,u.realname
ORDER BY total DESC";
$sth = $this->db->query($sql);
return $sth->fetchAll();
}
//输出下载
public function Download($filename,$content,$filetype="JSON",$zfmvc=""){
if(empty($filename) || empty($content) || empty($filetype))
{
return false;
}
$filetype = strtolower($filetype);
//支持的输出类型
$functions = array(
"json"=>"JsonOutPut",
"xml"=>"XMLOutPut",
"csv"=>"CSVOutPut",
);
$file_type_functions = array();
foreach($functions as $k=>$v)
{
$file_type_functions[] = $k;
}
if(!in_array($filetype,$file_type_functions))
{
return false;
}
$output_body = $this->$functions[$filetype]($content);
$this->pushDownload($output_body,$filename,$filetype,$zfmvc);
}//Download
//输出JSON内容
public function JsonOutPut($content,$numeric = true){
if($numeric == true)
{
return json_encode($content,JSON_NUMERIC_CHECK);
}else{
return json_encode($content);
}
}//JsonOutPut()
//输出XML内容
public function XMLOutPut($content){
}//JsonOutPut()
//输出CSV内容
public function CSVOutPut($data,$head = ""){
$split = ",";
$content = "";
//如果是windows输出一个BOM头
if(strpos($_SERVER["HTTP_USER_AGENT"],"Windows"))
{
$content = "\xEF\xBB\xBF";
}else{
$content = "";
}
if(!empty($head))
{
$content .= join($split,$head)."\r\n";
}
foreach($data as $k=>$v)
{
foreach($v as $kk=>$vv)
{
if(strpos($vv,","))
{
$v[$kk] = "\"".$vv."\"";
}
}
$content .= join($split,$v)."\r\n";
}
return $content;
}//JsonOutPut()
//输出下载
public function pushDownload($content,$filename,$type="",$zfmvc="")
{
if(headers_sent())
{
return false;
}
if(empty($filename) || empty($content))
{
return false;
}
$content_type = $this->ContentTypes($type);
$fsize = strlen($content);
//没有在zf controller 中使用
if(empty($zfmvc))
{
header('Content-Description: File Transfer');
header("Cache-Control: private",false);
header("Content-Type: ".$content_type);
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary\n");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Content-Length: ".$fsize);
ob_clean();
flush();
echo $content;
exit();
}else{
$zfmvc->getResponse()->setHeader('Content-Type', $content_type)
->setHeader('Content-Disposition','attachment; filename="'.$filename.'"')
->setHeader('Content-Length', strlen($content))
->setHeader('Content-Type','application/force-download')
->setHeader('Content-Type','application/download')
->setHeader('Content-Description','File Transfer')
->setHeader('Content-Transfer-Encoding','binary')
->setHeader('Expires',0)
->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
->setHeader('Pragma','public')
->setBody($content);
exit();
}
}//pushDownload
//Content-Type
public function ContentTypes($ext){
$ext = strtolower($ext);
$def = "application/force-download";
if(empty($ext))
{
return $def;
}
$content_types = array(
"csv" => "text/csv",
"pdf"=>"application/pdf",
"exe"=>"application/octet-stream",
"gzip"=>"application/zip",
"doc"=>"application/msword",
"xls"=>"application/vnd.ms-excel",
"ppt"=>"application/vnd.ms-powerpoint",
"gif"=>"image/gif",
"png"=>"image/png",
"jpeg"=>"image/jpg",
"jpg"=>"image/jpg",
"xml"=>"text/xml",
"xsl"=>"text/xml"
);
if(!isset($content_types[$ext]))
{
return $def;
}else{
return $content_types[$ext];
}
}//ContentTypes
}