<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware,use is subject to license terms
*
* $Id: helper_notification.php 34003 2013-09-18 04:31:14Z nemohou $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class helper_notification {
//添加通知
public static function notification_add($touid,$type,$note,$notevars = array(),$system = 0,$category = -1) {
global $_G;
if(!($tospace = getuserbyuid($touid))) {
return false;
}
space_merge($tospace,'field_home');
$filter = empty($tospace['privacy']['filter_note'])?array():array_keys($tospace['privacy']['filter_note']);
if($filter && (in_array($type.'|0',$filter) || in_array($type.'|'.$_G['uid'],$filter))) {
return false;
}
if($category == -1) {//默认值
$category = 0;//重新设置为0
$categoryname = '';//分类名
if($type == 'follow' || $type == 'follower') {
switch ($type) {
case 'follow' : $category = 5; break;//收听
case 'follower' : $category = 6; break;//听众
}
$categoryname = $type;//分类名
} else {
foreach($_G['notice_structure'] as $key => $val) {
if(in_array($type,$val)) {
switch ($key) {
case 'mypost' : $category = 1; break;//我的帖子
case 'interactive' : $category = 2; break;//坛友互动
case 'system' : $category = 3; break;//系统提醒
case 'manage' : $category = 4; break;//管理工作
default : $category = 0;//应用消息
}
$categoryname = $key;//分类名
break;
}
}
}
} else {//$category值不为-1的情况
switch ($category) {
case 1 : $categoryname = 'mypost'; break;//我的帖子
case 2 : $categoryname = 'interactive'; break;//坛友互动
case 3 : $categoryname = 'system'; break;//系统提醒
case 4 : $categoryname = 'manage'; break;//管理工作
case 5 : $categoryname = 'follow'; break;//收听
case 6 : $categoryname = 'follower'; break;//听众
default : $categoryname = 'app';//应用消息
}
}
if($category == 0) {//应用消息
$categoryname = 'app';
} elseif($category == 1 || $category == 2) {//我的帖子、坛友互动
$categoryname = $type;
}
/*
* zhuoweida 回复了您的帖子 多城公积金贷款额度吃紧背后:非法套现频发 查看
* 通知内容中用户名部分
* 默认执行
*/
//默认执行
$notevars['actor'] = "<a href="home.php?mod=space&uid=$_G[uid]">".$_G['member']['username']."</a>";
if(!is_numeric($type)) {
$vars = explode(':',$note);
if(count($vars) == 2) {
$notestring = lang('plugin/'.$vars[0],$vars[1],$notevars);//通知内容
} else {
$notestring = lang('notification',$notevars);//通知内容
}
$frommyapp = false;
} else {
$frommyapp = true;
$notestring = $note;//通知内容
}
$oldnote = array();//定义一个旧的通知内容数组
//from_id:来源对象id,from_idtype:来源对象类型
if($notevars['from_id'] && $notevars['from_idtype']) {
//通过from_id、from_idtype、touid作为条件来获取最新一条通知内容
$oldnote = C::t('home_notification')->fetch_by_fromid_uid($notevars['from_id'],$notevars['from_idtype'],$touid);
}
//from_num:来源量
if(empty($oldnote['from_num'])) $oldnote['from_num'] = 0;//来源量设置为0
$notevars['from_num'] = $notevars['from_num'] ? $notevars['from_num'] : 1;
//通知数组
$setarr = array(
'uid' => $touid,//通知用户ID
'type' => $type,//通知类型:"doing"记录,"friend"好友请求,"sharenotice"好友分享,"post"话题回复
'new' => 1,//通知是否为新:"1"为新通知,"0"为通知已读
'authorid' => $_G['uid'],//作者用户ID
'author' => $_G['username'],//用户名
'note' => $notestring,//通知内容
'dateline' => $_G['timestamp'],//通知产生的时间戳
'from_id' => $notevars['from_id'],//来源对象id
'from_idtype' => $notevars['from_idtype'],//来源对象类型
'from_num' => ($oldnote['from_num']+$notevars['from_num']),//来源量
'category' => $category //提醒分类 1我的帖子 2坛友互动 3系统消息 4管理工作 0应用消息
);
//系统提醒的情况
if($system) {
$setarr['authorid'] = 0; //作者用户ID设置为0
$setarr['author'] = ''; //用户名设置为空
}
$pkId = 0;
if($oldnote['id']) {//旧通知内容的id
C::t('home_notification')->update($oldnote['id'],$setarr);//更新为最新的通知内容
$pkId = $oldnote['id'];//主键id值
} else {
$oldnote['new'] = 0;
$pkId = C::t('home_notification')->insert($setarr,true);//插入操作
}
$banType = array('task');
//云状态的情况
if($_G['setting']['cloud_status'] && !in_array($type,$banType)) {
$noticeService = Cloud::loadClass('Service_Client_Notification');
if($oldnote['id']) {
$noticeService->update($touid,$pkId,$setarr['from_num'],$setarr['dateline'],$note);
} else {
$extra = $type == 'post' ? array('pId' => $notevars['pid']) : array();
$extra['notekey'] = $note;
$noticeService->add($touid,$setarr['authorid'],$setarr['author'],$setarr['from_id'],$setarr['from_idtype'],$setarr['note'],$extra);
}
}
//新通知
if(empty($oldnote['new'])) {
C::t('common_member')->increase($touid,array('newprompt' => 1));//更新数据
$newprompt = C::t('common_member_newprompt')->fetch($touid);//获取新通知
if($newprompt) {
$newprompt['data'] = unserialize($newprompt['data']);//反序列化数据
if(!empty($newprompt['data'][$categoryname])) {
$newprompt['data'][$categoryname] = intval($newprompt['data'][$categoryname]) + 1;
} else {
$newprompt['data'][$categoryname] = 1;
}
C::t('common_member_newprompt')->update($touid,array('data' => serialize($newprompt['data'])));
} else {
C::t('common_member_newprompt')->insert($touid,array($categoryname => 1));
}
//邮件函数类库
require_once libfile('function/mail');
//邮件标题
$mail_subject = lang('notification','mail_to_user');
//发邮件通知
sendmail_touser($touid,$mail_subject,$notestring,$frommyapp ? 'myapp' : $type);
}
if(!$system && $_G['uid'] && $touid != $_G['uid']) {
C::t('home_friend')->update_num_by_uid_fuid(1,$_G['uid'],$touid);
}
}
public static function manage_addnotify($type,$from_num = 0,$langvar = array()) {
global $_G;
$notifyusers = dunserialize($_G['setting']['notifyusers']);
$notifytypes = explode(',',$_G['setting']['adminnotifytypes']);
$notifytypes = array_flip($notifytypes);
$notearr = array('from_id' => 1,'from_idtype' => $type,'from_num' => $from_num);
if($langvar) {
$langkey = $langvar['langkey'];
$notearr = array_merge($notearr,$langvar);
} else {
$langkey = 'manage_'.$type;
}
foreach($notifyusers as $uid => $user) {
if($user['types'][$notifytypes[$type]]) {
helper_notification::notification_add($uid,$langkey,$notearr,1,4);
}
}
}
public function get_categorynum($newprompt_data) {
global $_G;
$categorynum = array();
if(empty($newprompt_data) || !is_array($newprompt_data)) {
return array();
}
foreach($newprompt_data as $key => $val) {
if(in_array($key,array('follow','follower'))) {
continue;
}
if(in_array($key,$_G['notice_structure']['mypost'])) {//我的帖子的情况
$categorynum['mypost'] += $val;
} elseif(in_array($key,$_G['notice_structure']['interactive'])) {//坛友互动的情况
$categorynum['interactive'] += $val;
}else{
$categorynum[$key] = $val;//其它情况
}
}
return $categorynum;
}
//更新新提醒数目
public function update_newprompt($uid,$type) {
global $_G;
if($_G['member']['newprompt_num']) {//我的帖子或坛友互动新提醒总数量
$tmpprompt = $_G['member']['newprompt_num'];//临时新提醒总数量
$num = 0;//累计器
$updateprompt = 0;//更新提醒数的标识,默认值为0
//$tmpprompt[$type]等价于$_G['member']['newprompt_num'][$type]
if(!empty($tmpprompt[$type])) {//当前分类下(我的帖子或坛友互动)当前类型下(如:atpostpcomment等)的提醒数量不为空的情况
unset($tmpprompt[$type]);//释放将当前分类下当前类型下提醒数量
$updateprompt = true;//将标识更新为true
}
//通过循环重新统计新提醒消息总数,将当前分类下当前类型下的新提醒数量清空后再重新统计一遍非当前类型下的新提醒消息总量
//$tmpprompt:各种类型的新提醒数量
foreach($tmpprompt as $key => $val) {
$num += $val;//新提醒消息数量累加
}
//如果重新统计的新提醒数目不为空
if($num) {
if($updateprompt) {//允许更新的标识
//用户新提醒数量表
C::t('common_member_newprompt')->update($uid,array('data' => serialize($tmpprompt)));
//更新用户主表中的新提醒数目
C::t('common_member')->update($uid,array('newprompt'=>$num));
}
} else {//如果重新统计的新提醒数目为空
//删除用户新提醒数目表中关于当前登录用户的有关信息
C::t('common_member_newprompt')->delete($_G['uid']);
//更新用户主表中的新提醒数目为0
C::t('common_member')->update($_G['uid'],array('newprompt'=>0));
}
}
}
}
?>