示例:dedecms织梦文章 

类型
发布插件 » CMS程序
标识
DedecmsDemoSkycaiji
下载
926次
更新
2019-07-18
实现织梦文章入库功能,您可参考该代码开发更多dedecms应用
<?php
/**
 * 示例:dedecms文章
 * 您可参考代码自行开发dedecms的更多功能
 * 您可以使用 thinkphp5的函数
 */
namespace plugin\release\cms;
class DedecmsDemoSkycaiji extends BaseCms{
	public $siteurl;//cms站点网址
	//初始化扩展
	public function init_extend(){
		$siteurls=$this->db()->table('__SYSCONFIG__')->where("`varname` in ('cfg_basehost','cfg_cmspath')")->column('value','varname');
		$this->siteurl=rtrim($siteurls['cfg_basehost'].$siteurls['cfg_cmspath'],'\/\\');
	}
	//参数
	public $_params = array (
		'typeid' => array (
			'name' => '栏目id',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_typeid',
		),
		'title' => array (
			'name' => '文章标题',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'content' => array (
			'name' => '文章内容',
			'require' => 1,
			'tag' => 'select',
			'option' => 'function:param_option_fields',
		),
		'author' => array (
			'name' => '作者',
			'require' => 0,
			'tag' => 'select',
			'option' => array (
				'admin' => 'admin',
				'网络' => '网络',
				'佚名' => '佚名',
			),
		),
		'desc' => array (
			'name' => '自动摘要',
			'require' => 0,
			'tag' => 'radio',
		),
		'cover' => array (
			'name' => '自动封面',
			'require' => 0,
			'tag' => 'radio',
		),
	);
	
	/*
	 * 导入数据
	 * 必须以数组形式返回:
	 * id(必填)表示入库返回的自增id或状态
	 * target(可选)记录入库的数据位置(发布的网址等)
	 * desc(可选)记录入库的数据位置附加信息
	 * error(可选)记录入库失败的错误信息
	 * 入库的信息可在“已采集数据”中查看
	 * return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	 */
	public function runImport($params){
		$newArticle=array(
			'typeid'=>$params['typeid'],
			'typeid2'=>0,
			'arcrank'=>0,
			'channel'=>1,
			'senddate'=>time(),
			'sortrank'=>time(),
			'mid'=>1,
		);
		$articleId=$this->db()->table('__ARCTINY__')->insert($newArticle,false,true);//返回文章id
		if($articleId>0){
			$newArticle['id']=$articleId;
			$newArticle['title']=$params['title'];
			$newArticle['writer']=$params['author'];
			$newArticle['pubdate']=time();
			$newArticle['flag']='';
			$newArticle['ismake']=-1;
			$newArticle['litpic']='';
			$newArticle['description']='';
			if($params['desc']){
				//自动摘要
				$newArticle['description']=mb_substr(trim(preg_replace('/\s+/',' ',strip_tags($params['content']))),0,200,'utf-8');
			}
			
			if($params['cover']){
				//生成封面
				if(preg_match('/<img[^<>]*src=[\'\"]([^\"\'<>]+)[\'\"]/i',$params['content'],$mcover)){
					$newArticle['litpic']=$mcover[1];
				}
			}
			
			$this->db()->table('__ARCHIVES__')->insert($newArticle);//添加文章
			
			$newAddon=array(
				'aid'=>$articleId,
				'typeid'=>$newArticle['typeid'],
				'body'=>$params['content'],
			);
			$this->db()->table('__ADDONARTICLE__')->insert($newAddon);//添加文章附加
			
			$target=$this->siteurl.'/plus/view.php?aid='.$articleId;
			return array('id'=>$articleId,'target'=>$target);
		}else{
			$error='文章入库失败';
			return array('id'=>0,'error'=>'文章入库失败');
		}
	}
	/*
	 * 文章主栏目选项
	 * 必须返回键值对形式的数组
	 */
	public function param_option_typeid(){
		$typeDb=$this->db()->table('__ARCTYPE__')->where('topid=1')->order('sortrank asc')->select();
		$typeList=array();
		foreach ($typeDb as $v){
			$typeList[$v['id']]=auto_convert2utf8($v['typename']);
		}
		return $typeList;
	}
}
?>
非常抱歉!该插件已停止更新仅供示例参考,cms程序更新可能会导致插件功能失效,请自行修改代码以适应您的需求