westdc-zf1/application/models/message.php

95 lines
2.2 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";
if($type=="member")
{
$sql .= " AND m.rid IN (0,$uid)";
}else if($type=="admin"){
$sql .= " AND m.rid IN (-1,0,$uid)";
}
$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.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;
}
}