示例:xiunobbs发帖 

类型
发布插件 » CMS程序
标识
XiunobbsDemoSkycaiji
下载
584次
更新
2019-02-22
实现xiunobbs修罗论坛发帖功能,您可参考该代码开发更多xiunobbs应用
<?php
/**
 * 示例:Xiunobbs发帖
 * 您可参考代码自行开发Xiunobbs的更多功能
 * 您可以使用 thinkphp5的函数
 */
namespace plugin\release\cms;
class XiunobbsDemoSkycaiji extends BaseCms{
	public function cms_db_xiunobbs($cmsPath){
		$dbFile=realpath($cmsPath.'/conf/conf.php');
		//转换成thinkphp数据库配置
		$config=include $dbFile;
		
		$config=$config['db'][$config['db']['type']]['master'];
		$cmsDb=array(
			'db_type'  => 'mysql',
			'db_user'  => $config['user'],
			'db_pwd'   => $config['password'],
			'db_host'  => $config['host'],
			'db_port'  => 3306,
			'db_name'  => $config['name'],
			'db_charset'  => $config['charset'],
			'db_prefix'  => $config['tablepre']
		);
		
		return $cmsDb;
	}
	//参数
	public $_params = array (
		'author' => array (
			'name' => '作者账号',
			'require' => 1,
			'tag' => 'text',
		),
		'forum' => array (
			'name' => '版块ID',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_forum',
		),
		'title' => array (
			'name' => '帖子标题',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'content' => array (
			'name' => '帖子内容',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
	);

	/*
	 * 导入数据
	* 必须以数组形式返回:
	* id(必填)表示入库返回的自增id或状态
	* target(可选)记录入库的数据位置(发布的网址等)
	* desc(可选)记录入库的数据位置附加信息
	* error(可选)记录入库失败的错误信息
	* 入库的信息可在“已采集数据”中查看
	* return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	*/
	public function runImport($params){
		$uid=$this->db()->table('__USER__')->where(array('username'=>$params['author']))->find();
		if(!empty($uid)){
			$uid=$uid['uid'];
		}else{
			return array('id'=>0,'error'=>'用户不存在');
		}
		
		$newThread=array(
	        'fid'=>$params['forum'],
			'top'=>0,
	        'uid'=>$uid,
			'userip'=>'',
	        'subject'=>$params['title'],
			'create_date'=>time(),
			'last_date'=>time(),
			'views'=>0,
			'posts'=>0,
			'images'=>0,
			'files'=>0,
			'mods'=>0,
			'closed'=>0,
			'firstpid'=>0,
			'lastuid'=>0,
			'lastpid'=>0,
		);
		
		$tid=$this->db()->table('__THREAD__')->insert($newThread,false,true);//添加主题并返回id
		if($tid>0){
			$this->db()->table('__MYTHREAD__')->insert(array('uid'=>$uid,'tid'=>$tid));//我的主题
			
			$newPost=array(
				'tid'=>$tid,
				'uid'=>$uid,
				'isfirst'=>1,
				'create_date'=>time(),
				'userip'=>0,
				'images'=>0,
				'files'=>0,
				'doctype'=>1,
				'quotepid'=>0,
				'message'=>strip_tags($params['content']),
				'message_fmt'=>$params['content'],
			);
			$pid=$this->db()->table('__POST__')->insert($newPost,false,true);//添加帖子并返回id
			$this->db()->table('__THREAD__')->where(array('tid'=>$tid))->update(array('firstpid'=>$pid,'lastpid'=>$pid));
			$this->db()->table('__FORUM__')->where('fid',$params['forum'])->setInc('threads',1);
			return array('id'=>$tid,'target'=>'主题:'.$tid);
		}else{
			return array('id'=>0,'error'=>'添加主题失败');
		}
	}
	
	/*
	 * 参数选项:版块
	 * 必须返回键值对形式的数组
	 */
	public function param_option_forum(){
		$forumsDb=$this->db()->table('__FORUM__')->select();
		$forumList=array();
		foreach ($forumsDb as $forum){
			$forumList[$forum['fid']]=auto_convert2utf8($forum['name']);
		}
		return $forumList;
	}
}
?>
非常抱歉!该插件已停止更新仅供示例参考,cms程序更新可能会导致插件功能失效,请自行修改代码以适应您的需求