示例:xiunobbs发帖 (XiunobbsDemoSkycaiji)

  更新日期:2018-08-02 21:15
实现xiunobbs修罗论坛发帖功能,您可参考该代码开发更多xiunobbs应用

应用代码

<?php
/**
 * 示例:Xiunobbs发帖
 * 您可参考代码自行开发Xiunobbs的更多功能
 * 您可以使用 thinkphp3.2的函数
 */
namespace Release\Cms;
class XiunobbsDemoSkycaijiCms 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' => '作者账号',
			'tag' => 'text',
			'require'=>1,
		),
		'forum' => array (
			'name' => '版块ID',
			'tag' => 'select',
			'option' => 'function:param_option_forum',
			'require'=>1,
		),
		'title' => array (
			'name' => '帖子标题',
			'tag' => 'select',
			'option' => 'function:param_option_fields',
			'require'=>1,
		),
		'content' => array (
			'name' => '帖子内容',
			'tag' => 'select',
			'option' => 'function:param_option_fields',
			'require'=>1,
		),
	);

	/*
	 * 导入数据
	* 必须以数组形式返回:
	* 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__')->add($newThread);//添加主题并返回id
		if($tid>0){
			$this->db()->table('__MYTHREAD__')->add(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__')->add($newPost);//添加帖子并返回id
			$this->db()->table('__THREAD__')->where(array('tid'=>$tid))->save(array('firstpid'=>$pid,'lastpid'=>$pid));
			
			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;
	}
}
?>