2011-11-16 10:03:18 +00:00
|
|
|
<?php
|
|
|
|
class message
|
|
|
|
{
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static function post($db,$pid,$rid,$title,$msg){
|
|
|
|
$rev = array();
|
|
|
|
try{
|
|
|
|
$sql = "INSERT INTO messages_content (body) values ('$msg') RETURNING id";
|
|
|
|
$sth = $db->prepare($sql);
|
|
|
|
$sth->execute();
|
|
|
|
$msgbody = $sth->fetch(PDO::FETCH_ASSOC);
|
|
|
|
$msgid = $msgbody['id'];
|
|
|
|
if(!is_array($rid))
|
|
|
|
{
|
|
|
|
$sql = "INSERT INTO messages (pid,rid,msgid,title) VALUES ('$pid','$rid','$msgid','$title')";
|
|
|
|
$sth = $db->prepare($sql);
|
|
|
|
if($sth->execute())
|
|
|
|
{
|
|
|
|
$rev[$rid]['msg'] = "发送成功";
|
|
|
|
}else{
|
|
|
|
$rev[$rid]['msg'] = "发送失败";
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
foreach ($rid as $v)
|
|
|
|
{
|
|
|
|
$sql = "INSERT INTO messages (pid,rid,msgid,title) VALUES ('$pid','$v','$msgid','$title')";
|
|
|
|
$sth = $db->prepare($sql);
|
|
|
|
if($sth->execute())
|
|
|
|
{
|
|
|
|
$rev[$v]['msg'] = "发送成功";
|
|
|
|
}else{
|
|
|
|
$rev[$v]['msg'] = "发送失败";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch (Exception $e){
|
|
|
|
$rev['error'] = $e->getMessage();
|
|
|
|
}
|
|
|
|
return $rev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getNew($db,$uid,$type="member"){
|
|
|
|
$rev = array();
|
|
|
|
try{
|
|
|
|
$sql = "SELECT * FROM messages m
|
|
|
|
LEFT JOIN messages_logs ml ON m.id=ml.mid
|
2011-11-17 09:37:17 +00:00
|
|
|
WHERE ml.uid IS NULL AND m.status>=0 ";
|
2011-11-16 10:03:18 +00:00
|
|
|
if($type=="member")
|
|
|
|
{
|
|
|
|
$sql .= " AND m.rid IN (0,$uid)";
|
|
|
|
}else if($type=="admin"){
|
|
|
|
$sql .= " AND m.rid IN (-1,0,$uid)";
|
|
|
|
}
|
|
|
|
|
2012-05-10 06:38:36 +00:00
|
|
|
$sql.=" order by sendtime desc";
|
|
|
|
|
2011-11-16 10:03:18 +00:00
|
|
|
$result = $db->query($sql);
|
|
|
|
|
|
|
|
$rows = $result->fetchAll();
|
|
|
|
|
|
|
|
$rev['count'] = count($rows);
|
|
|
|
$rev['rows'] = $rows;
|
|
|
|
|
|
|
|
}catch (Exception $e){
|
|
|
|
$rev['error'] = $e->getMessage();
|
|
|
|
}
|
|
|
|
return $rev;
|
|
|
|
}
|
|
|
|
|
2011-11-17 09:37:17 +00:00
|
|
|
//返回单条消息
|
2011-11-16 10:03:18 +00:00
|
|
|
static function getOne($db,$uid,$id){
|
|
|
|
$rev = array();
|
|
|
|
try{
|
2011-11-17 09:45:28 +00:00
|
|
|
$sql = "SELECT m.id,m.pid,m.rid,m.sendtime,m.status,m.msgid,m.title,mc.body,u.realname,ml.uid FROM messages m
|
2011-11-16 10:03:18 +00:00
|
|
|
LEFT JOIN messages_content mc ON m.msgid=mc.id
|
|
|
|
LEFT JOIN users u on u.id=m.pid
|
|
|
|
LEFT JOIN messages_logs ml ON m.id=ml.mid
|
|
|
|
WHERE m.id=$id";
|
|
|
|
$result = $db->query($sql);
|
|
|
|
$row = $result->fetch();
|
|
|
|
|
|
|
|
if(empty($row['uid']))
|
|
|
|
{
|
|
|
|
$sql = "INSERT INTO messages_logs (uid,mid) VALUES ('$uid','$id')";
|
|
|
|
@$db->exec($sql);
|
|
|
|
}
|
|
|
|
$rev['info'] = $row;
|
|
|
|
}catch (Exception $e){
|
|
|
|
$rev['error'] = $e->getMessage();
|
|
|
|
}
|
|
|
|
return $rev;
|
|
|
|
}
|
2011-11-17 09:37:17 +00:00
|
|
|
|
|
|
|
//标记为已读
|
|
|
|
static function read($db,$uid,$mid){
|
|
|
|
$sql = "INSERT INTO messages_logs (uid,mid) VALUES ('$uid','$mid')";
|
|
|
|
if(@$db->exec($sql))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//标记为已处理(不再推送)
|
|
|
|
static function over($db,$mid){
|
|
|
|
$id = "";
|
|
|
|
if(!is_array($mid))
|
|
|
|
{
|
|
|
|
$id = $mid;
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
$id = join(",",$mid);
|
|
|
|
}
|
|
|
|
$sql = "UPDATE messages SET status=-1 WHERE id IN ($id)";
|
|
|
|
if(@$db->exec($sql))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-16 10:03:18 +00:00
|
|
|
}
|