推荐使用
siteurl=$this->db()->table('__options__')->where(array('option_name'=>'siteurl'))->value('option_value');
}
//参数
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){
$newpost=array(
'post_date'=>date('y-m-d h:i:s',time()),
'post_date_gmt'=>gmdate('y-m-d h:i:s',time()),
'post_content' => $params['content'],
'post_title' => $params['title'],
'post_status' => 'publish',
'post_type' => 'post',
'comment_status' => 'open',
'ping_status' => 'open',
'post_modified'=>date('y-m-d h:i:s',time()),
'post_modified_gmt'=>gmdate('y-m-d h:i:s',time()),
);
$newpost['post_author']=$this->db()->table('__users__')->where(array('user_login'=>$params['author']))->value('id');//设置作者id
$postid=$this->db()->table('__posts__')->insert($newpost,false,true);//添加文章并返回id
if($postid>0){
//返回成功内容网址
$this->db()->table('__term_relationships__')->insert(array('object_id'=>$postid,'term_taxonomy_id'=>$params['category']));//添加到关系表
$target=$this->siteurl.'/?p='.$postid;
return array('id'=>$postid,'target'=>$target);
}else{
return array('id'=>0,'error'=>'文章入库失败');
}
}
/*
* 参数选项:作者
* 必须返回键值对形式的数组
*/
public function param_option_author(){
$usersdb=$this->db()->table('__users__')->limit(100)->select();
$userlist=array();
foreach ($usersdb as $user){
$userlist[$user['user_login']]=$user['user_login'];
}
return $userlist;
}
/*
* 参数选项:分类
* 必须返回键值对形式的数组
*/
public function param_option_category(){
$catsdb=$this->db()->table('__terms__')->select();
$catlist=array();
foreach ($catsdb as $cat){
$catlist[$cat['term_id']]=$cat['name'];
}
return $catlist;
}
}
?>