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

159 篇文章 3 订阅
144 篇文章 1 订阅

定义文件:\source\class\extend\extend_thread_allowat.php

相关类定义如下:

class extend_thread_allowat extends extend_thread_base {

	public $atlist;
	public $allowat;

	public function before_newthread($parameters) {

		if($this->group['allowat']) {
			$this->atlist = $atlist_tmp = array();
			preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
			$atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
			if(!empty($atlist_tmp)) {
				if(!$this->setting['at_anyone']) {
					foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
						$this->atlist[$row['followuid']] = $row['fusername'];
					}
					if(count($this->atlist) < $this->group['allowat']) {
						$query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
						foreach($query as $row) {
							$this->atlist[$row['fuid']] = $row['fusername'];
						}
					}
				} else {
					foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
						$this->atlist[$row['uid']] = $row['username'];
					}
				}
			}
			if($this->atlist) {
				foreach($this->atlist as $atuid => $atusername) {
					$atsearch[] = "/@".str_replace('/', '\/', preg_quote($atusername))." /i";
					$atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
				}
				$this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
				$this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);
			}
		}
	}

	public function after_newthread() {
		if($this->group['allowat'] && $this->atlist) {
			foreach($this->atlist as $atuid => $atusername) {
				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)));
			}
			set_atlist_cookie(array_keys($this->atlist));
		}
	}

	public function before_newreply($parameters) {
		if($this->group['allowat']) {
			$atlist_tmp = $ateduids = array();
			preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
			$atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
			$atnum = $maxselect = 0;
			foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {
				$atnum ++;
				$ateduids[$row[uid]] = $row['uid'];
			}
			$maxselect = $this->group['allowat'] - $atnum;
			if($maxselect > 0 && !empty($atlist_tmp)) {
				$at_anyone = $this->setting['at_anyone'];
				if(empty($at_anyone)) {
					foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
						if(!in_array($row['followuid'], $ateduids)) {
							$this->atlist[$row[followuid]] = $row['fusername'];
						}
						if(count($this->atlist) == $maxselect) {
							break;
						}
					}
					if(count($this->atlist) < $maxselect) {
						$query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
						foreach($query as $row) {
							if(!in_array($row['followuid'], $ateduids)) {
								$this->atlist[$row[fuid]] = $row['fusername'];
							}
						}
					}
				} else {
					foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
						if(!in_array($row['uid'], $ateduids)) {
							$this->atlist[$row[uid]] = $row['username'];
						}
						if(count($this->atlist) == $maxselect) {
							break;
						}
					}
				}
			}
			if($this->atlist) {
				foreach($this->atlist as $atuid => $atusername) {
					$atsearch[] = "/@$atusername /i";
					$atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
				}
				$this->param['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
				$this->param['message'] = substr($this->param['message'], 0, strlen($this->param['message']) - 1);
			}
		}
	}

	public function after_newreply() {
		if($this->group['allowat'] && $this->atlist) {
			foreach($this->atlist as $atuid => $atusername) {
				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)));
			}
			set_atlist_cookie(array_keys($this->atlist));
		}
	}

	public function before_editpost($parameters) {
		if($this->group['allowat']) {
			$this->atlist = $atlist_tmp = $ateduids = array();
			$atnum = $maxselect = 0;
			foreach(C::t('home_notification')->fetch_all_by_authorid_fromid($this->member['uid'], $this->thread['tid'], 'at') as $row) {
				$atnum ++;
				$ateduids[$row[uid]] = $row['uid'];
			}
			$maxselect = $this->group['allowat'] - $atnum;
			preg_match_all("/@([^\r\n]*?)\s/i", $parameters['message'].' ', $atlist_tmp);
			$atlist_tmp = array_slice(array_unique($atlist_tmp[1]), 0, $this->group['allowat']);
			if($maxselect > 0 && !empty($atlist_tmp)) {
				if(empty($this->setting['at_anyone'])) {
					foreach(C::t('home_follow')->fetch_all_by_uid_fusername($this->member['uid'], $atlist_tmp) as $row) {
						if(!in_array($row['followuid'], $ateduids)) {
							$this->atlist[$row[followuid]] = $row['fusername'];
						}
						if(count($this->atlist) == $maxselect) {
							break;
						}
					}
					if(count($this->atlist) < $maxselect) {
						$query = C::t('home_friend')->fetch_all_by_uid_username($this->member['uid'], $atlist_tmp);
						foreach($query as $row) {
							if(!in_array($row['followuid'], $ateduids)) {
								$this->atlist[$row[fuid]] = $row['fusername'];
							}
						}
					}
				} else {
					foreach(C::t('common_member')->fetch_all_by_username($atlist_tmp) as $row) {
						if(!in_array($row['uid'], $ateduids)) {
							$this->atlist[$row[uid]] = $row['username'];
						}
						if(count($this->atlist) == $maxselect) {
							break;
						}
					}
				}
				if($this->atlist) {
					foreach($this->atlist as $atuid => $atusername) {
						$atsearch[] = "/@$atusername /i";
						$atreplace[] = "[url=home.php?mod=space&uid=$atuid]@{$atusername}[/url] ";
					}
					$parameters['message'] = preg_replace($atsearch, $atreplace, $parameters['message'].' ', 1);
					$parameters['message'] = substr($parameters['message'], 0, strlen($parameters['message']) - 1);
				}
			}
		}
	}

	public function after_editpost() {
		if($this->group['allowat'] && $this->atlist) {
			foreach($this->atlist as $atuid => $atusername) {
				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)));
			}
			set_atlist_cookie(array_keys($this->atlist));
		}
	}
}

由以上代码定义我们看到程序处理@功能分别有三处:发帖、回帖、编辑;

首先以正则表达式方式从内容文本中抽取@会员信息存入atlist,并将内容文本中的@会员解析出会员的空间地址([url]标签),然后在所有的after_xxx方法中向被@的会员发送通知,这里需要说明一点是:before_xxx方法是内容入库前的操作,after_xxx方法是内容入库后的操作!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Discuz! 是一款流行的论坛系统,支持插件开发,以下是一个简单的插件开发实例: 1. 创建插件目录 在 Discuz! 的 plugin 目录下创建一个新的目录,例如 myplugin。 2. 创建插件文件 在 myplugin 目录下创建一个新的文件,例如 myplugin.inc.php。这个文件是插件的主文件,需要包含以下代码: ``` <?php if(!defined('IN_DISCUZ')) { exit('Access Denied'); } class plugin_myplugin { // 插件代码 } ?> ``` 3. 注册插件 在 myplugin.inc.php 文件中,使用以下代码注册插件: ``` <?php if(!defined('IN_DISCUZ')) { exit('Access Denied'); } class plugin_myplugin { function __construct() { // 注册插件 global $_G; $_G['cache']['plugin']['myplugin'] = array( 'name' => '我的插件', 'version' => '1.0', 'description' => '这是我的第一个 Discuz! 插件', 'author' => '我的名字', 'pluginurl' => '', 'url' => '', 'lang' => '', 'modules' => array() ); } } ?> ``` 4. 实现插件功能 在插件的主文件中,实现插件的功能。例如,在 myplugin.inc.php 文件中加入以下代码: ``` <?php if(!defined('IN_DISCUZ')) { exit('Access Denied'); } class plugin_myplugin { function __construct() { // 注册插件 global $_G; $_G['cache']['plugin']['myplugin'] = array( 'name' => '我的插件', 'version' => '1.0', 'description' => '这是我的第一个 Discuz! 插件', 'author' => '我的名字', 'pluginurl' => '', 'url' => '', 'lang' => '', 'modules' => array() ); } function global_footer() { // 在页面底部添加一段文本 echo '<div>这是我的第一个 Discuz! 插件</div>'; } } ?> ``` 这段代码将在页面底部添加一段文本,展示插件的功能。 5. 安装插件 将 myplugin 目录上传到 Discuz! 的 plugin 目录下,然后在后台插件管理页面中安装插件即可。 这是一个简单的 Discuz! 插件开发实例,你可以根据自己的需求,实现不同的插件功能

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值