找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1779|回复: 0

[分享] Discuz!开发之发帖回帖@会员功能代码解析

[复制链接]
发表于 2017-11-14 13:59:37 | 显示全部楼层 |阅读模式 来自 河南省新乡市
定义文件:\source\class\extend\extend_thread_allowat.php
相关类定义如下:
  1. class extend_thread_allowat extends extend_thread_base {  
  2.   
  3.     public $atlist;  
  4.     public $allowat;  
  5.   
  6.     public function before_newthread($parameters) {  
  7.   
  8.         if($this->group['allowat']) {  
  9.             $this->atlist = $atlist_tmp = array();  
  10.             preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);  
  11.             $atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);  
  12.             if(!empty($atlist_tmp)) {  
  13.                 if(!$this->setting['at_anyone']) {  
  14.                     foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {  
  15.                         $this->atlist[$row['followuid']] = $row['fusername'];  
  16.                     }  
  17.                     if(count($this->atlist) < $this->group['allowat']) {  
  18.                         $query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);  
  19.                         foreach($query as $row) {  
  20.                             $this->atlist[$row['fuid']] = $row['fusername'];  
  21.                         }  
  22.                     }  
  23.                 } else {  
  24.                     foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {  
  25.                         $this->atlist[$row['uid']] = $row['username'];  
  26.                     }  
  27.                 }  
  28.             }  
  29.             if($this->atlist) {  
  30.                 foreach($this->atlist as $atuid => $atusername) {  
  31.                     $atsearch[] = "/@".str_replace('/', '\/', preg_quote($atusername))." /i";  
  32.                     $atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";  
  33.                 }  
  34.                 $this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);  
  35.                 $this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);  
  36.             }  
  37.         }  
  38.     }  
  39.   
  40.     public function after_newthread() {  
  41.         if($this->group['allowat'] && $this->atlist) {  
  42.             foreach($this->atlist as $atuid => $atusername) {  
  43.                 notification_add($atuid, 'at', 'at_message', array('from_id' => $this->tid, 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->tid, 'subject' => $this->param['subject'], 'pid' => $this->pid, 'message' => messagecutstr($this->param['message'], 150)));  
  44.             }  
  45.             set_atlist_cookie(array_keys($this->atlist));  
  46.         }  
  47.     }  
  48.   
  49.     public function before_newreply($parameters) {  
  50.         if($this->group['allowat']) {  
  51.             $atlist_tmp = $ateduids = array();  
  52.             preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);  
  53.             $atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);  
  54.             $atnum = $maxselect = 0;  
  55.             foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {  
  56.                 $atnum ++;  
  57.                 $ateduids[$row[uid]] = $row['uid'];  
  58.             }  
  59.             $maxselect = $this->group['allowat'] - $atnum;  
  60.             if($maxselect > 0 && !empty($atlist_tmp)) {  
  61.                 $at_anyone = $this->setting['at_anyone'];  
  62.                 if(empty($at_anyone)) {  
  63.                     foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {  
  64.                         if(!in_array($row['followuid'], $ateduids)) {  
  65.                             $this->atlist[$row[followuid]] = $row['fusername'];  
  66.                         }  
  67.                         if(count($this->atlist) == $maxselect) {  
  68.                             break;  
  69.                         }  
  70.                     }  
  71.                     if(count($this->atlist) < $maxselect) {  
  72.                         $query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);  
  73.                         foreach($query as $row) {  
  74.                             if(!in_array($row['followuid'], $ateduids)) {  
  75.                                 $this->atlist[$row[fuid]] = $row['fusername'];  
  76.                             }  
  77.                         }  
  78.                     }  
  79.                 } else {  
  80.                     foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {  
  81.                         if(!in_array($row['uid'], $ateduids)) {  
  82.                             $this->atlist[$row[uid]] = $row['username'];  
  83.                         }  
  84.                         if(count($this->atlist) == $maxselect) {  
  85.                             break;  
  86.                         }  
  87.                     }  
  88.                 }  
  89.             }  
  90.             if($this->atlist) {  
  91.                 foreach($this->atlist as $atuid => $atusername) {  
  92.                     $atsearch[] = "/@$atusername /i";  
  93.                     $atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";  
  94.                 }  
  95.                 $this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);  
  96.                 $this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);  
  97.             }  
  98.         }  
  99.     }  
  100.   
  101.     public function after_newreply() {  
  102.         if($this->group['allowat'] && $this->atlist) {  
  103.             foreach($this->atlist as $atuid => $atusername) {  
  104.                 notification_add($atuid, 'at', 'at_message', array('from_id' => $this->thread['tid'], 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->thread['tid'], 'subject' => $this->thread['subject'], 'pid' => $this->pid, 'message' => messagecutstr($this->param['message'], 150)));  
  105.             }  
  106.             set_atlist_cookie(array_keys($this->atlist));  
  107.         }  
  108.     }  
  109.   
  110.     public function before_editpost($parameters) {  
  111.         if($this->group['allowat']) {  
  112.             $this->atlist = $atlist_tmp = $ateduids = array();  
  113.             $atnum = $maxselect = 0;  
  114.             foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {  
  115.                 $atnum ++;  
  116.                 $ateduids[$row[uid]] = $row['uid'];  
  117.             }  
  118.             $maxselect = $this->group['allowat'] - $atnum;  
  119.             preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);  
  120.             $atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);  
  121.             if($maxselect > 0 && !empty($atlist_tmp)) {  
  122.                 if(empty($this->setting['at_anyone'])) {  
  123.                     foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {  
  124.                         if(!in_array($row['followuid'], $ateduids)) {  
  125.                             $this->atlist[$row[followuid]] = $row['fusername'];  
  126.                         }  
  127.                         if(count($this->atlist) == $maxselect) {  
  128.                             break;  
  129.                         }  
  130.                     }  
  131.                     if(count($this->atlist) < $maxselect) {  
  132.                         $query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);  
  133.                         foreach($query as $row) {  
  134.                             if(!in_array($row['followuid'], $ateduids)) {  
  135.                                 $this->atlist[$row[fuid]] = $row['fusername'];  
  136.                             }  
  137.                         }  
  138.                     }  
  139.                 } else {  
  140.                     foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {  
  141.                         if(!in_array($row['uid'], $ateduids)) {  
  142.                             $this->atlist[$row[uid]] = $row['username'];  
  143.                         }  
  144.                         if(count($this->atlist) == $maxselect) {  
  145.                             break;  
  146.                         }  
  147.                     }  
  148.                 }  
  149.                 if($this->atlist) {  
  150.                     foreach($this->atlist as $atuid => $atusername) {  
  151.                         $atsearch[] = "/@$atusername /i";  
  152.                         $atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";  
  153.                     }  
  154.                     $parameters['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);  
  155.                     $parameters['message'] = substr($parameters['message'], 0, strlen($parameters['message']) - 1);  
  156.                 }  
  157.             }  
  158.         }  
  159.     }  
  160.   
  161.     public function after_editpost() {  
  162.         if($this->group['allowat'] && $this->atlist) {  
  163.             foreach($this->atlist as $atuid => $atusername) {  
  164.                 notification_add($atuid, 'at', 'at_message', array('from_id' => $this->thread['tid'], 'from_idtype' => 'at', 'buyerid' => $this->member['uid'], 'buyer' => $this->member['username'], 'tid' => $this->thread['tid'], 'subject' => $this->thread['subject'], 'pid' => $this->post['pid'], 'message' => messagecutstr($this->param['message'], 150)));  
  165.             }  
  166.             set_atlist_cookie(array_keys($this->atlist));  
  167.         }  
  168.     }  
  169. }  
复制代码


由以上代码定义我们看到程序处理@功能分别有三处:发帖、回帖、编辑;
首先以正则表达式方式从内容文本中抽取@会员信息存入atlist,并将内容文本中的@会员解析出会员的空间地址([url]标签),然后在所有的after_xxx方法中向被@的会员发送通知,这里需要说明一点是:before_xxx方法是内容入库前的操作,after_xxx方法是内容入库后的操作!

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-5-13 00:24 , Processed in 0.041554 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表