示例:destoon文章 (DestoonDemoSkycaiji)

  更新日期:2018-07-20 14:44
实现destoon文章入库功能,您可参考该代码开发更多destoon应用,使用时需在数据绑定》CMS路径结尾加上@destoon

应用代码

<?php
/**
 * 示例:destoon文章
 * 您可参考代码自行开发destoon的更多功能
 * 您可以使用 thinkphp3.2的函数
 */
namespace Release\Cms;
class DestoonDemoSkycaijiCms extends BaseCms{
	public $siteurl;//cms站点网址
	public function cms_db_destoon($cmsPath){
		define('IN_DESTOON',true);
		$dbFile=realpath($cmsPath.'/config.inc.php');
		//转换成thinkphp数据库配置
		include $dbFile;//导入本地cms配置文件
		$cmsDb=array(
			'db_type'  => $CFG['database'],
			'db_user'  => $CFG['db_user'],
			'db_pwd'   => $CFG['db_pass'],
			'db_host'  => $CFG['db_host'],
			'db_port'  => 3306,
			'db_name'  => $CFG['db_name'],
			'db_charset'  => $CFG['db_charset'],
			'db_prefix'  => $CFG['tb_pre']
		);
		$this->siteurl=$CFG['url'];
		return $cmsDb;
	}
	//参数
	public $_params=array(
		'author' => array (
			'name' => '作者账号',
			'tag' => 'select',
			'option' => 'function:param_option_author',
			'require'=>1,
		),
		'category' => array (
			'name' => '分类',
			'tag' => 'select',
			'option' => 'function:param_option_category',
			'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){
		$newArticle=array(
			'catid'=>$params['category'],
			'areaid'=>0,
			'level'=>0,
			'title'=>$params['title'],
			'style'=>'',
			'fee'=>0,
			'subtitle'=>'',
			'introduce'=>mb_substr(strip_tags($params['content']), 0,60,'utf-8'),
			'tag'=>'',
			'keyword'=>'',
			'pptword'=>'',
			'author'=>'',
			'copyfrom'=>'',
			'fromurl'=>'',
			'voteid'=>'',
			'hits'=>0,
			'comments'=>0,
			'thumb'=>'',
			'username'=>$params['author'],
			'addtime'=>time(),
			'editor'=>$params['author'],
			'edittime'=>time(),
			'ip'=>'',
			'template'=>'',
			'status'=>3,
			'islink'=>0,
			'linkurl'=>'',
			'filepath'=>'',
			'note'=>''
		);
		$articleId=$this->db()->table('__ARTICLE_21__')->add($newArticle);//添加文章并返回id
		if($articleId>0){
			$target='show.php?itemid='.$articleId;
			$this->db()->table('__ARTICLE_21__')->where(array('itemid'=>$articleId))->save(array('linkurl'=>$target));
			$this->db()->table('__ARTICLE_DATA_21__')->add(array('itemid'=>$articleId,'content'=>$params['content']));
			$target=$this->siteurl.'news/'.$target;
			return array('id'=>$articleId,'target'=>$target);
		}else{
			return array('id'=>0,'error'=>'文章入库失败');
		}
	}
	/*
	 * 参数选项:作者
	 * 必须返回键值对形式的数组
	 */
	public function param_option_author(){
		$usersDb=$this->db()->table('__MEMBER__')->where('admin=1')->select();
		$userList=array();
		foreach ($usersDb as $user){
			$uname=$user['username'];
			$userList[$uname]=$uname;
		}
		return $userList;
	}
	
	/*
	 * 参数选项:分类
	 * 必须返回键值对形式的数组
	 */
	public function param_option_category(){
		$catsDb=$this->db()->table('__CATEGORY__')->where('moduleid=21')->select();//文章分类
		$catList=array();
		foreach ($catsDb as $cat){
			$catList[$cat['catid']]=$cat['catname'];
		}
		return $catList;
	}
}
?>