示例:yzmcms文章 (YzmcmsDemoSkycaiji)

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

应用代码

<?php
/**
 * 示例:yzmcms文章
 * 您可参考代码自行开发yzmcms的更多功能
 * 您可以使用 thinkphp3.2的函数
 */
namespace Release\Cms;
class YzmcmsDemoSkycaijiCms extends BaseCms{
	public $siteurl;//cms站点网址
	//参数
	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,
		),
		'seotitle' => array (
			'name' => 'SEO标题',
			'tag' => 'select',
			'option' => 'function:param_option_fields',
			'require'=>1,
		),
		'content' => array (
			'name' => '文章内容',
			'tag' => 'select',
			'option' => 'function:param_option_fields',
			'require'=>1,
		),
		'cover' => array (
			'name' => '封面图',
			'tag' => 'select',
			'option' => 'function:param_option_fields',
			'require'=>0,
		),
	);
	public function cms_db_yzmcms($cmsPath){
		$config=include $cmsPath.'/common/config/config.php';
		$cmsDb=array(
			'db_type'  => $config['db_type'],
			'db_user'  => $config['db_user'],
			'db_pwd'   => $config['db_pwd'],
			'db_host'  => $config['db_host'],
			'db_port'  => $config['db_port'],
			'db_name'  => $config['db_name'],
			'db_charset'  => 'utf8',
			'db_prefix'  => $config['db_prefix']
		);
		return $cmsDb;
	}
	public function init_extend(){
		$siteurl=$this->db()->table('__CONFIG__')->where("`name`='site_url'")->find();
		$this->siteurl=$siteurl['value'];
	}
	/*
	 * 导入数据
	* 必须以数组形式返回:
	* id(必填)表示入库返回的自增id或状态
	* target(可选)记录入库的数据位置(发布的网址等)
	* desc(可选)记录入库的数据位置附加信息
	* error(可选)记录入库失败的错误信息
	* 入库的信息可在“已采集数据”中查看
	* return array('id'=>0,'target'=>'','desc'=>'','error'=>'');
	*/
	public function runImport($params){
		$user=$this->db()->table('__ADMIN__')->where(array('adminname'=>$params['author']))->find();
		if(empty($user)){
			return array('id'=>0,'error'=>'用户不存在');
		}
		
		$cat=$this->db()->table('__CATEGORY__')->where(array('catid'=>$params['category']))->find();
		
		$newArticle=array(
			'catid'=>$params['category'],
	        'userid'=>$user['adminid'],
	        'username'=>$user['adminname'],
	        'nickname'=>$user['nickname'],
	        'title'=>$params['title'],
	        'seo_title'=>$params['seotitle'],
	        'inputtime'=>time(),
	        'updatetime'=>time(),
	        'keywords'=>'',
	        'description'=>mb_substr(trim(strip_tags($params['content'])), 0,100,'utf-8'),
	        'click'=>'',
	        'content'=>$params['content'],
	        'copyfrom'=>'',
	        'thumb'=>$params['cover'],
	        'articledir'=>'',
	        'url'=>'',
	        'flag'=>'',
	        'status'=>1,
	        'system'=>1,
	        'listorder'=>10,
	        'groupids_view'=>0,
	        'readpoint'=>0
		);
		$id=$this->db()->table('__ARTICLE__')->add($newArticle);
		if($id>0){
			$url=$this->siteurl.$cat['catdir'].'/'.$id.'.html';//文章url
			$this->db()->table('__ARTICLE__')->where(array('id'=>$id))->save(array('url'=>$url));
			
			return array('id'=>$id,'target'=>$url);
		}else{
			return array('id'=>0,'error'=>'添加文章失败');
		}
	}
	/*
	 * 参数选项:作者
	 * 必须返回键值对形式的数组
	 */
	public function param_option_author(){
		$usersDb=$this->db()->table('__ADMIN__')->select();
		$userList=array();
		foreach ($usersDb as $user){
			$uname=auto_convert2utf8($user['adminname']);
			$userList[$uname]=$uname;
		}
		return $userList;
	}
	
	/*
	 * 参数选项:分类
	 * 必须返回键值对形式的数组
	 */
	public function param_option_category(){
		$catsDb=$this->db()->table('__CATEGORY__')->where('modelid=1')->select();//分类
		$catList=array();
		foreach ($catsDb as $cat){
			$catList[$cat['catid']]=auto_convert2utf8($cat['catname']);
		}
		return $catList;
	}
}
?>