westdc-zf1/application/models/message.php

129 lines
2.9 KiB
PHP

<?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
WHERE ml.uid IS NULL AND m.status>=0 ";
if($type=="member")
{
$sql .= " AND m.rid IN (0,$uid)";
}else if($type=="admin"){
$sql .= " AND m.rid IN (-1,0,$uid)";
}
$sql.=" order by sendtime desc";
$result = $db->query($sql);
$rows = $result->fetchAll();
$rev['count'] = count($rows);
$rev['rows'] = $rows;
}catch (Exception $e){
$rev['error'] = $e->getMessage();
}
return $rev;
}
//返回单条消息
static function getOne($db,$uid,$id){
$rev = array();
try{
$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
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;
}
//标记为已读
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;
}
}
}