示例:帝国cms文章(仅限动态页面) (EmpirecmsDemoSkycaiji)

  更新日期:2018-08-08 21:28
实现Empirecms文章入库功能(只支持动态页面),您可参考该代码开发更多Empirecms应用

应用代码

<?php
/**
 * 示例:empirecms文章
 * 您可参考代码自行开发empirecms的更多功能
 * 您可以使用 thinkphp3.2的函数
 */
namespace Release\Cms;
class EmpirecmsDemoSkycaijiCms extends BaseCms{
	public $newsurl;//根目录
	//参数
	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,
		),
	);

	public function init_extend(){
		include $this->cmsPath.'/e/config/config.php';
		$this->newsurl=$public_r['newsurl'];
	}
	/*
	 * 导入数据
	* 必须以数组形式返回:
	* id(必填)表示入库返回的自增id或状态
	* target(可选)记录入库的数据位置(发布的网址等)
	* desc(可选)记录入库的数据位置附加信息
	* error(可选)记录入库失败的错误信息
	* 入库的信息可在“已采集数据”中查看
	* return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	*/
	public function runImport($params){
		$uid=$this->db()->table('__ENEWSUSER__')->where(array('username'=>$params['author']))->find();
		if(!empty($uid)){
			$uid=$uid['userid'];
		}else{
			return array('id'=>0,'error'=>'用户不存在');
		}
		$cat=$this->db()->table('__ENEWSCLASS__')->where(array('classid'=>$params['category']))->find();//分类
		$cat=$this->auto_convert2utf8($cat);
		if($cat['tbname']=='article'){
			//添加文章
			$newArticle=array(
				'classid'=>$params['category'],
				'ttid'=>0,
				'onclick'=>0,
				'plnum'=>0,
				'totaldown'=>0,
				'newspath'=>date('Y-m-d',time()),
				'filename'=>'',
				'userid'=>$uid,
				'username'=>$params['author'],
				'firsttitle'=>0,
				'isgood'=>0,
				'ispic'=>0,
				'istop'=>0,
				'isqf'=>0,
				'ismember'=>0,
				'isurl'=>0,
				'truetime'=>time(),
				'lastdotime'=>time(),
				'havehtml'=>1,
				'groupid'=>0,
				'userfen'=>0,
				'titlefont'=>'',
				'titleurl'=>'',
				'stb'=>1,
				'fstb'=>1,
				'restb'=>1,
				'keyboard'=>'',
				'title'=>$params['title'],
				'newstime'=>time(),
				'titlepic'=>'',
				'eckuid'=>0,
				'ftitle'=>'',
				'smalltext'=>'',
				'writer'=>'',
				'befrom'=>'',
				'newstext'=>'',
				'diggtop'=>0
			);
			
			$aid=$this->db()->table('__ECMS_ARTICLE__')->add($newArticle);
			if($aid>0){
				//存储到文件中
				$newstext=date('Y',time()).'/'.date('md',time()).'/'.md5($aid);
				\Think\Storage::put($this->cmsPath.'/d/txt/'.$newstext.'.php','<? exit();?>'.$params['content']);
				
				$this->db()->table('__ECMS_ARTICLE__')->where(array('id'=>$aid))->save(array(
					'filename'=>$aid,
					'titleurl'=>$this->newsurl.'e/action/ShowInfo.php?classid='.$params['category'].'&id='.$aid,
					'newstext'=>$newstext
				));
				//附加表
				$this->db()->table('__ECMS_ARTICLE_DATA_1__')->add(array(
					'id'=>$aid,
					'classid'=>$params['category'],
					'keyid'=>'',
					'dokey'=>1,
					'newstempid'=>0,
					'closepl'=>0,
					'haveaddfen'=>0,
					'infotags'=>''
				));
				
				$this->db()->table('__ECMS_ARTICLE_INDEX__')->add(array(
					'id'=>$aid,
					'classid'=>$params['category'],
					'checked'=>1,
					'newstime'=>time(),
					'truetime'=>time(),
					'lastdotime'=>time(),
					'havehtml'=>1
				));
				
				return array('id'=>$aid,'target'=>'文章:'.$aid);
			}else{
				return array('id'=>0,'error'=>'添加文章失败');
			}
		}else{
			//添加新闻
			$newNews=array(
				'classid'=>$params['category'],
				'ttid'=>0,
				'onclick'=>0,
				'plnum'=>0,
				'totaldown'=>0,
				'newspath'=>date('Y-m-d',time()),
				'filename'=>'',
				'userid'=>$uid,
				'username'=>$params['author'],
				'firsttitle'=>0,
				'isgood'=>0,
				'ispic'=>0,
				'istop'=>0,
				'isqf'=>0,
				'ismember'=>0,
				'isurl'=>0,
				'truetime'=>time(),
				'lastdotime'=>time(),
				'havehtml'=>1,
				'groupid'=>0,
				'userfen'=>0,
				'titlefont'=>'',
				'titleurl'=>'',
				'stb'=>1,
				'fstb'=>1,
				'restb'=>1,
				'keyboard'=>'',
				'title'=>$params['title'],
				'newstime'=>time(),
				'titlepic'=>'',
				'eckuid'=>0,
				'ftitle'=>'',
				'smalltext'=>'',
				'diggtop'=>0
			);
			
			$nid=$this->db()->table('__ECMS_NEWS__')->add($newNews);
			if($nid>0){
				$this->db()->table('__ECMS_NEWS__')->where(array('id'=>$nid))->save(array(
					'filename'=>$nid,
					'titleurl'=>$this->newsurl.'e/action/ShowInfo.php?classid='.$params['category'].'&id='.$nid
				));
				
				//附加表
				$this->db()->table('__ECMS_NEWS_DATA_1__')->add(array(
					'id'=>$nid,
					'classid'=>$params['category'],
					'keyid'=>'',
					'dokey'=>1,
					'newstempid'=>0,
					'closepl'=>0,
					'haveaddfen'=>0,
					'infotags'=>'',
					'writer'=>'',
					'befrom'=>'',
					'newstext'=>$params['content']
				));
			
				$this->db()->table('__ECMS_NEWS_INDEX__')->add(array(
						'id'=>$nid,
						'classid'=>$params['category'],
						'checked'=>1,
						'newstime'=>time(),
						'truetime'=>time(),
						'lastdotime'=>time(),
						'havehtml'=>1
				));
			
				return array('id'=>$nid,'target'=>'新闻:'.$nid);
			}else{
				return array('id'=>0,'error'=>'添加文章失败');
			}
		}
	}
	/*
	 * 参数选项:作者
	 * 必须返回键值对形式的数组
	 */
	public function param_option_author(){
		$usersDb=$this->db()->table('__ENEWSUSER__')->where('groupid=1')->select();
		$userList=array();
		foreach ($usersDb as $user){
			$uname=auto_convert2utf8($user['username']);
			$userList[$uname]=$uname;
		}
		return $userList;
	}
	
	/*
	 * 参数选项:分类
	 * 必须返回键值对形式的数组
	 */
	public function param_option_category(){
		$catsDb=$this->db()->table('__ENEWSCLASS__')->where("islast=1 and tbname in ('news','article')")->select();//文章分类
		$catList=array();
		foreach ($catsDb as $cat){
			$catList[$cat['classid']]=auto_convert2utf8($cat['classname']);
		}
		return $catList;
	}
}
?>