示例:twcms文章 

类型
发布插件 » CMS程序
标识
TwcmsDemoSkycaiji
下载
286次
更新
2019-02-22
实现通王企业系统文章入库功能,您可参考该代码开发更多twcms应用,使用时需在数据绑定»CMS路径结尾加上@twcms
<?php
/**
 * 示例:twcms文章
 * 您可参考代码自行开发twcms的更多功能
 * 您可以使用 thinkphp5的函数
 */
namespace plugin\release\cms;
class TwcmsDemoSkycaiji extends BaseCms{
	public $siteurl;//cms站点网址
	//初始化扩展
	public function init_extend(){
		$config=$this->db()->table('__RUNTIME__')->where("`k`='cfg'")->find();
		$config=json_decode($config['v'],true);
		$this->siteurl=trim($config['weburl'],'\/\\');//cms网址
	}
	/**
	 * 自定义数据库配置
	 * 绑定数据时需输入cms路径并在路径结尾加上@CMS程序名
	 */
	public function cms_db_twcms($cmsPath){
		$dbFile=realpath($cmsPath.'/twcms/config/config.inc.php');
		//转换成thinkphp数据库配置
		include_once $dbFile;//导入本地cms配置文件
		$config=$_ENV['_config'][db];
		$cmsDb=array(
			'db_type'  => $config['type'],
			'db_user'  => $config['master']['user'],
			'db_pwd'   => $config['master']['password'],
			'db_host'  => $config['master']['host'],
			'db_port'  => 3306,
			'db_name'  => $config['master']['name'],
			'db_charset'  => $config['master']['charset'],
			'db_prefix'  => $config['master']['tablepre']
		);
		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',
		),
	);
	
	/*
	 * 导入数据
	* 必须以数组形式返回:
	* id(必填)表示入库返回的自增id或状态
	* target(可选)记录入库的数据位置(发布的网址等)
	* desc(可选)记录入库的数据位置附加信息
	* error(可选)记录入库失败的错误信息
	* 入库的信息可在“已采集数据”中查看
	* return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	*/
	public function runImport($params){
		$userId=$this->db()->table('__USER__')->where(array('username'=>$params['author']))->value('uid');		
		if($userId<=0){
			return array('id'=>0,'error'=>'作者账号:'.$params['author'].' 不存在');
		}
		
		$newArticle=array(
			'cid'=>$params['category'],
	        'title'=>$params['title'],
	        'color'=>'',
	        'alias'=>'',
	        'tags'=>'',
	        'intro'=>'',
	        'pic'=>'',
	        'uid'=>$userId,
	        'author'=>$params['author'],
	        'source'=>'',
	        'dateline'=>time(),
	        'lasttime'=>time(),
	        'ip'=>'0',
	        'iscomment'=>'0',
	        'comments'=>'0',
	        'imagenum'=>'0',
	        'filenum'=>'0',
	        'flags'=>'',
	        'seo_title'=>'',
	        'seo_keywords'=>'',
	        'seo_description'=>''
		);
		
		$articleId=$this->db()->table('__CMS_ARTICLE__')->insert($newArticle,false,true);//添加文章并返回id
		if($articleId>0){
			//返回成功内容网址
			$this->db()->table('__CMS_ARTICLE_DATA__')->insert(array('id'=>$articleId,'content'=>$params['content']));//添加到内容表
			$this->db()->table('__CMS_ARTICLE_VIEWS__')->insert(array('id'=>$articleId,'cid'=>$params['category'],'views'=>0));//添加到分类关系
			
			$target=$this->siteurl."/index.php?show--cid-{$params['category']}-id-{$articleId}.html";
			return array('id'=>$articleId,'target'=>$target);
		}else{
			return array('id'=>0,'error'=>'文章入库失败');
		}
		return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	}

	/*
	 * 参数选项:作者
	 * 必须返回键值对形式的数组
	 */
	public function param_option_author(){
		$usersDb=$this->db()->table('__USER__')->select();
		$userList=array();
		foreach ($usersDb as $user){
			$username=auto_convert2utf8($user['username']);
			$userList[$username]=$username;
		}
		return $userList;
	}
	
	/*
	 * 参数选项:分类
	 * 必须返回键值对形式的数组
	 */
	public function param_option_category(){
		$catsDb=$this->db()->table('__CATEGORY__')->where('mid=2')->select();
		$catList=array();
		foreach ($catsDb as $cat){
			$catList[$cat['cid']]=$cat['name'];
		}
		return $catList;
	}
}
?>
非常抱歉!该插件已停止更新仅供示例参考,cms程序更新可能会导致插件功能失效,请自行修改代码以适应您的需求