pbootcms产品文章入库 

类型
发布插件 » cms程序
标识
PbcmsCaijiSrso
下载
168次
评论
0条
更新
2020-07-12
开发者
蜀戎网络工作室

最新版,@pbcms

使用教程

https://www.srso.cn/fy/304.html  

<?php
/**
 * 示例:pboot文章
 * 您可参考代码自行开发pboot cms的更多功能
 * 您可以使用 thinkphp5的函数
 */
namespace plugin\release\cms;
class PbcmsCaijiSrso extends BaseCms{
	public function cms_db_pbcms($cmsPath){
		$dbFile=realpath($cmsPath.'/config/database.php');
		$cmsDb=array();
		if(file_exists($dbFile)){
			$dbFile=include $dbFile;
			$dbFile=$dbFile['database'];
			if(is_array($dbFile)){
				//使用sqlite必须开启pdo_sqlite
				$cmsDb['db_type']=stripos($dbFile['type'], 'sqlite')!==false?'sqlite':'mysql';
				$cmsDb['db_name']=$cmsDb['db_type']=='sqlite'?($cmsPath.$dbFile['dbname']):$dbFile['dbname'];
				$cmsDb['db_host']=$dbFile['host'];
				$cmsDb['db_user']=$dbFile['user'];
				$cmsDb['db_pwd']=$dbFile['passwd'];
				$cmsDb['db_charset']='utf8';
				$cmsDb['db_port']=$dbFile['port'];
				$cmsDb['db_prefix']='ay_';//固定的前缀
			}
		}
		return $cmsDb;
	}
	//参数
	public $_params = array (
		'author' => array (
			'name' => '作者账号',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_author',
		),
		'category' => array (
			'name' => '分类',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_category',
		),
		'title' => array (
			'name' => '文章标题',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'content' => array (
			'name' => '文章内容',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'data' => array (
			'name' => '日期',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'ico' => array (
			'name' => '略缩图',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'pics' => array (
			'name' => '多图轮播',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'source' => array (
			'name' => '来源',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'tags' => array (
			'name' => 'tags',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'gjc' => array (
			'name' => 'seo关键词',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'ms' => array (
			'name' => 'seo描述',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'fw' => array (
			'name' => '访问数量',
			'require' => 0,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
	);

	/*
	 * 导入数据
	* 必须以数组形式返回:
	* id(必填)表示入库返回的自增id或状态
	* target(可选)记录入库的数据位置(发布的网址等)
	* desc(可选)记录入库的数据位置附加信息
	* error(可选)记录入库失败的错误信息
	* 入库的信息可在“已采集数据”中查看
	* return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	*/
	public function runImport($params){
		$newPost=array(
			'acode'=>'cn',
			'scode'=>$params['category'],
			'subscode'=>'',
			'title'=>$params['title'],
			'titlecolor'=>'#333333',
			'subtitle'=>'',
			'filename'=>'',
			'author'=>$params['author'],
			'source'=>$params['source'],
			'outlink'=>'',
			'date'=>date('Y-m-d H:i:s'),
			'ico'=>$params['ico'],
			'pics'=>$params['pics'],
			'content'=>$params['content'],
			'tags'=>$params['tags'],
			'enclosure'=>'',
			'keywords'=>$params['gjc'],
			'description'=>$params['ms'],
			'sorting'=>255,
			'status'=>1,
			'istop'=>0,
			'isrecommend'=>0,
			'isheadline'=>0,
			'visits'=>$params['fw'],
			'likes'=>0,
			'oppose'=>0,
			'create_user'=>$params['author'],
			'update_user'=>$params['author'],
			'create_time'=>date('Y-m-d H:i:s'),
			'update_time'=>date('Y-m-d H:i:s')
		);
		
		$postId=$this->db()->table('__CONTENT__')->insert($newPost,false,true);//添加文章并返回id
		if($postId>0){
			$target='新闻:'.$postId;
			return array('id'=>$postId,'target'=>$target);
		}else{
			return array('id'=>0,'error'=>'文章入库失败');
		}
	}
	/*
	 * 参数选项:作者
	 * 必须返回键值对形式的数组
	 */
	public function param_option_author(){
		$usersDb=$this->db()->table('__USER__')->limit(100)->select();
		$userList=array();
		foreach ($usersDb as $user){
			$userList[$user['username']]=$user['username'];
		}
		return $userList;
	}
	
	/*
	 * 参数选项:分类
	 * 必须返回键值对形式的数组
	 */
	public function param_option_category(){
		// $catsDb=$this->db()->table('__CONTENT_SORT__')->where("contenttpl='news.html'")->limit(100)->select();//文章分类
		$catsDb=$this->db()->table('__CONTENT_SORT__')->limit(100)->select();//文章分类
		$catList=array();
		foreach ($catsDb as $cat){
			$catList[$cat['scode']]=$cat['name'];
		}
		return $catList;
	}
}
?>