在一些论坛里,用户可以在贴子里写上另一个用户的用户名,并加一个@前缀,这样对方就会收到通知来看这个帖子。
下面是虎扑论坛里的一个例子:
下面我们尝试在Discuz系统的记录里加上这个功能。
1。 在source/language/lang_notification.php加入:
'doing_mention' => '{actor} 在 < a href="{url}" target="_blank">记录 中提到了您',
2。 在source/include/spacecp/spacecp_doing.php里下面一段:
if($updo['uid'] != $_G['uid']) {
notification_add($updo['uid'], 'doing', 'doing_reply', array(
'url'=>"home.php?mod=space&uid=$updo[uid]&do=doing&view=me&doid=$updo[doid]&highlight=$newid",
'from_id'=>$updo['doid'],
'from_idtype'=>'doid'));
updatecreditbyaction('comment', 0, array(), 'doing'.$updo['doid']);
}
之后加入:
if (preg_match_all("/( |\s|^)@(\S+)( |\s|$)/", $message, $matches)) {
$result = array_unique($matches[2]);
foreach($result as $ausername) {
if(($auid = C::t('common_member')->fetch_uid_by_username($ausername))) {
if (($auid != $_G['uid']) && ($auid != $updo['uid'])) {
if (!isblacklist($auid)) notification_add($auid, 'comment', 'doing_mention', array(
'url'=>"home.php?mod=space&uid=$auid&do=doing&view=me&doid=$updo[doid]&highlight=$newid",
'from_id'=>$updo['doid'],
'from_idtype'=>'doid'));
}
}
}
}
下面是效果图:
以上只是讨论下做法,不足为据。 类似的可以将这个功能引入日志及其评论中,在这里就不讨论了。
注:本文中的代码里的某些<符号后人为加了一个不应该有的空格,以避免Discuz在保存日志时自动改变日志内容。
5/1/2016补充:经网友sesemule提醒,发现原来的代码只处理了记录里的回复部分,没处理记录本身。要支持记录本身的话,还要做下面的改动:
3。 在source/include/spacecp/spacecp_doing.php里下面一句:
C::t('common_member_field_home')->update($_G['uid'], $setarr);
之后加入:
if (preg_match_all("/( |\s|^)@(\S+)( |\s|$)/", $message, $matches)) {
$result = array_unique($matches[2]);
foreach($result as $ausername) {
if(($auid = C::t('common_member')->fetch_uid_by_username($ausername))) {
if ($auid != $_G['uid']) {
if (!isblacklist($auid)) notification_add($auid, 'comment', 'doing_mention', array(
'url'=>"home.php?mod=space&uid=$auid&do=doing&view=me&doid=$newdoid&highlightdoing=$newdoid",
'from_id'=>$updo['doid'],
'from_idtype'=>'doid'));
}
}
}
}
4。 在source/include/space/space_doing.php里将下面这句
$uids = $_GET['highlight'] ? array() : array($space['uid']);
改成:
$uids = ($_GET['highlight'] || $_GET['highlightdoing']) ? array() : array($space['uid']);
5。 在source/include/space/space_doing.php里下面这段:
$query = C::t('home_doing')->fetch_all_search($start, $perpage, 1, $uids, '', $searchkey, '', '' ,'', 1, $doid, $f_index);
foreach($query as $value) {
之后加入:
if($_GET['highlightdoing'] && $value['doid'] == $_GET['highlightdoing']) {
$value['style'] .= 'color:#F60;';
}
6。在template/default/home/space_doing.htm里将下面这句
改成:
相关代码下载: http://www.bian-wang.com/upload/txgz_doing_at_notification_160502a.zip
5/16/2016补充:应网友sesemule的要求,把这个功能扩展到日志及其评论:
1)加汉字:在文件 source/language/lang_notification.php 中加入:
'blog_mention' => '{actor} 在 < a href="{url}" target="_blank">日志 中提到了您',
'blogcomment_mention' => '{actor} 在 < a href="{url}" target="_blank">日志评论 中提到了您',
2)日志的通知:在文件 source/include/spacecp/spacecp_blog.php 里的下面这段
if($newblog = blog_post($_POST, $blog)) {
if(empty($blog) && $newblog['topicid']) {
$url = 'home.php?mod=space&uid='.$_G['uid'].'&do=topic&topicid='.$newblog['topicid'].'&view=blog&quickforward=1';
} else {
$url = 'home.php?mod=space&uid='.$newblog['uid'].'&do=blog&quickforward=1&id='.$newblog['blogid'];
}
后加入
if (preg_match_all("/( |\s|^)@(\S+)( |\s|$)/", $_POST['message'], $matches)) {
$n_url = 'home.php?mod=space&uid='.$newblog['uid'].'&do=blog&id='.$newblog['blogid'];
$result = array_unique($matches[2]);
foreach($result as $ausername) {
if(($auid = C::t('common_member')->fetch_uid_by_username($ausername))) {
if ($auid != $_G['uid']) {
if (!isblacklist($auid)) notification_add($auid, 'comment', 'blog_mention', array('url'=>$n_url));
}
}
}
}
3)日志评论的通知:
3a)新评论:在文件 source/include/spacecp/spacecp_comment.php 里的下面这段
$cidarr = add_comment($message, $id, $idtype, $cid);
后加入
if ($idtype == 'blogid') {
if (preg_match_all("/( |\s|^)@(\S+)( |\s|$)/", $message, $matches)) {
if($cidarr['cid'] != 0 && $comment = C::t('home_comment')->fetch($cidarr['cid'], $_G['uid'])) {
$n_url = "home.php?mod=space&uid=$comment[uid]&do=blog&id=$id&cid=$cidarr[cid]";
$result = array_unique($matches[2]);
foreach($result as $ausername) {
if(($auid = C::t('common_member')->fetch_uid_by_username($ausername))) {
if ($auid != $_G['uid']) {
if (!isblacklist($auid)) notification_add($auid, 'comment', 'blogcomment_mention', array('url'=>$n_url));
}
}
}
}
}
}
3b)修改评论: 在文件 source/include/spacecp/spacecp_comment.php 里的下面这段
C::t('home_comment')->update($comment['cid'], array('message'=>$message, 'status'=>$comment_status));
后加入
if ($comment['idtype'] == 'blogid') {
if (preg_match_all("/( |\s|^)@(\S+)( |\s|$)/", $message, $matches)) {
$n_url = "home.php?mod=space&uid=$comment[uid]&do=blog&id=$comment[id]&cid=$cid";
$result = array_unique($matches[2]);
foreach($result as $ausername) {
if(($auid = C::t('common_member')->fetch_uid_by_username($ausername))) {
if ($auid != $_G['uid']) {
if (!isblacklist($auid)) notification_add($auid, 'comment', 'blogcomment_mention', array('url'=>$n_url));
}
}
}
}
}