继续上篇的《 phpcms后台批量上传添加图片文章方法详解(一) 》的修改。
四、增加批量添加插入数据库函数addall_content()
打开“phpcms\model\content_model.class.php”文件,搜索“public function add_content”函数,在该函数的后面添加以下代码:
/**
* 添加内容
*
* @param $datas
* @param $isimport 是否为外部接口导入
*/
public function addall_content($data,$isimport = 0) {
if($isimport) $data = new_addslashes($data);
$this->search_db = pc_base::load_model('search_model');
$modelid = $this->modelid;
require_once CACHE_MODEL_PATH.'content_input.class.php';
require_once CACHE_MODEL_PATH.'content_update.class.php';
$content_input = new content_input($this->modelid);
$inputinfo = $content_input->get($data,$isimport);
$systeminfo = $inputinfo['system'];
$modelinfo = $inputinfo['model'];
if($data['inputtime'] && !is_numeric($data['inputtime'])) {
$systeminfo['inputtime'] = strtotime($data['inputtime']);
} elseif(!$data['inputtime']) {
$systeminfo['inputtime'] = SYS_TIME;
} else {
$systeminfo['inputtime'] = $data['inputtime'];
}
//读取模型字段配置中,关于日期配置格式,来组合日期数据
$this->fields = getcache('model_field_'.$modelid,'model');
$setting = string2array($this->fields['inputtime']['setting']);
extract($setting);
if($fieldtype=='date') {
$systeminfo['inputtime'] = date('Y-m-d');
}elseif($fieldtype=='datetime'){
$systeminfo['inputtime'] = date('Y-m-d H:i:s');
}
if($data['updatetime'] && !is_numeric($data['updatetime'])) {
$systeminfo['updatetime'] = strtotime($data['updatetime']);
} elseif(!$data['updatetime']) {
$systeminfo['updatetime'] = SYS_TIME;
} else {
$systeminfo['updatetime'] = $data['updatetime'];
}
$systeminfo['username'] = $data['username'] ? $data['username'] : param::get_cookie('admin_username');
$systeminfo['sysadd'] = defined('IN_ADMIN') ? 1 : 0;
//自动提取摘要
if(isset($_POST['add_introduce']) && $systeminfo['description'] == '' && isset($modelinfo['content'])) {
$content = stripslashes($modelinfo['content']);
$introcude_length = intval($_POST['introcude_length']);
$systeminfo['description'] = str_cut(str_replace(array("'","\r\n","\t",'[page]','[/page]','“','”',' '), '', strip_tags($content)),$introcude_length);
$inputinfo['system']['description'] = $systeminfo['description'] = addslashes($systeminfo['description']);
}
//自动提取缩略图
if(isset($_POST['auto_thumb']) && $systeminfo['thumb'] == '' && isset($modelinfo['content'])) {
$content = $content ? $content : stripslashes($modelinfo['content']);
$auto_thumb_no = intval($_POST['auto_thumb_no'])-1;
if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
$systeminfo['thumb'] = $matches[3][$auto_thumb_no];
}
}
$systeminfo['description'] = str_replace(array('/','\\','#','.',"'"),' ',$systeminfo['description']);
$systeminfo['keywords'] = str_replace(array('/','\\','#','.',"'"),' ',$systeminfo['keywords']);
//主表
$tablename = $this->table_name = $this->db_tablepre.$this->model_tablename;
$inputinfo = $content_input->get($data,$isimport);
$title01 = $inputinfo['system']['title01'];
$title02 = $inputinfo['system']['title02'];
$title03 = $inputinfo['system']['title03'];
$title04 = $inputinfo['system']['title04'];
$title05 = $inputinfo['system']['title05'];
$suolve01 = $inputinfo['system']['suolve01'];
$suolve02 = $inputinfo['system']['suolve02'];
$suolve03 = $inputinfo['system']['suolve03'];
$suolve04 = $inputinfo['system']['suolve04'];
$suolve05 = $inputinfo['system']['suolve05'];
$catid = $inputinfo['system']['catid'];
$systeminfo1 = array('catid' => $catid,'title' => $title01,'thumb' => $suolve01);
$systeminfo2 = array('catid' => $catid,'title' => $title02,'thumb' => $suolve02);
$systeminfo3 = array('catid' => $catid,'title' => $title03,'thumb' => $suolve03);
$systeminfo4 = array('catid' => $catid,'title' => $title04,'thumb' => $suolve04);
$systeminfo5 = array('catid' => $catid,'title' => $title05,'thumb' => $suolve05);
$id = $modelinfo['id'] = $this->insert($systeminfo1,true);
$id1 = $modelinfo['id'] = $this->insert($systeminfo2,true);
$id2 = $modelinfo['id'] = $this->insert($systeminfo3,true);
$id3 = $modelinfo['id'] = $this->insert($systeminfo4,true);
$id4 = $modelinfo['id'] = $this->insert($systeminfo5,true);
//$id = $modelinfo['id'] = $this->insert($systeminfo,true);
//print_r( $systeminfo);
$this->update($systeminfo,array('id'=>$id));
$this->update($systeminfo,array('id'=>$id1));
$this->update($systeminfo,array('id'=>$id2));
$this->update($systeminfo,array('id'=>$id3));
$this->update($systeminfo,array('id'=>$id4));
//更新URL地址
if($data['islink']==1) {
$urls[0] = trim_script($_POST['linkurl']);
$urls[0] = str_replace(array('select ',')','\\','#',"'"),' ',$urls[0]);
} else {
$urls = $this->url->show($id, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
$urls1 = $this->url->show($id1, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
$urls2 = $this->url->show($id2, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
$urls3 = $this->url->show($id3, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
$urls4 = $this->url->show($id4, 0, $systeminfo['catid'], $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
}
$this->table_name = $tablename;
$this->update(array('url'=>$urls[0]),array('id'=>$id));
$this->update(array('url'=>$urls1[0]),array('id'=>$id1));
$this->update(array('url'=>$urls2[0]),array('id'=>$id2));
$this->update(array('url'=>$urls3[0]),array('id'=>$id3));
$this->update(array('url'=>$urls4[0]),array('id'=>$id4));
//附属表
$this->table_name = $this->table_name.'_data';
$this->insert($modelinfo);
//添加统计
$this->hits_db = pc_base::load_model('hits_model');
$hitsid = 'c-'.$modelid.'-'.$id;
$hitsid1 = 'c-'.$modelid.'-'.$id1;
$hitsid2 = 'c-'.$modelid.'-'.$id2;
$hitsid3 = 'c-'.$modelid.'-'.$id3;
$hitsid4 = 'c-'.$modelid.'-'.$id4;
$this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));
$this->hits_db->insert(array('hitsid'=>$hitsid1,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));
$this->hits_db->insert(array('hitsid'=>$hitsid2,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));
$this->hits_db->insert(array('hitsid'=>$hitsid3,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));
$this->hits_db->insert(array('hitsid'=>$hitsid4,'catid'=>$systeminfo['catid'],'updatetime'=>SYS_TIME));
if($data['status']==99) {
//更新到全站搜索
$this->search_api($id,$inputinfo);
}
//更新栏目统计数据
$this->update_category_items($systeminfo['catid'],'add',1);
//调用 update
$content_update = new content_update($this->modelid,$id);
$content_update1 = new content_update($this->modelid,$id1);
$content_update2 = new content_update($this->modelid,$id2);
$content_update3 = new content_update($this->modelid,$id3);
$content_update4 = new content_update($this->modelid,$id4);
//合并后,调用update
$merge_data = array_merge($systeminfo,$modelinfo);
$merge_data['posids'] = $data['posids'];
$content_update->update($merge_data);
$content_update1->update($merge_data);
//发布到审核列表中
if(!defined('IN_ADMIN') || $data['status']!=99) {
$this->content_check_db = pc_base::load_model('content_check_model');
$check_data = array(
'checkid'=>'c-'.$id.'-'.$modelid,
'catid'=>$systeminfo['catid'],
'siteid'=>$this->siteid,
'title'=>$systeminfo['title'],
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'status'=>$data['status'],
);
$this->content_check_db->insert($check_data);
}
//END发布到审核列表中
if(!$isimport) {
$html = pc_base::load_app_class('html', 'content');
$urls['data']['system']['id'] = $id;
$urls1['data']['system']['id'] = $id1;
$urls2['data']['system']['id'] = $id2;
$urls3['data']['system']['id'] = $id3;
$urls4['data']['system']['id'] = $id4;
if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);
if($urls1['content_ishtml'] && $data['status']==99) $html->show($urls1[1],$urls['data']);
if($urls2['content_ishtml'] && $data['status']==99) $html->show($urls2[1],$urls['data']);
if($urls3['content_ishtml'] && $data['status']==99) $html->show($urls3[1],$urls['data']);
if($urls4['content_ishtml'] && $data['status']==99) $html->show($urls4[1],$urls['data']);
$catid = $systeminfo['catid'];
}
//发布到其他栏目
if($id && isset($_POST['othor_catid']) && is_array($_POST['othor_catid'])) {
$linkurl = $urls[0];
$r = $this->get_one(array('id'=>$id));
foreach ($_POST['othor_catid'] as $cid=>$_v) {
$this->set_catid($cid);
$mid = $this->category[$cid]['modelid'];
if($modelid==$mid) {
//相同模型的栏目插入新的数据
$inputinfo['system']['catid'] = $systeminfo['catid'] = $cid;
$newid = $modelinfo['id'] = $this->insert($systeminfo,true);
$this->table_name = $tablename.'_data';
$this->insert($modelinfo);
if($data['islink']==1) {
$urls = $_POST['linkurl'];
$urls = str_replace(array('select ',')','\\','#',"'"),' ',$urls);
} else {
$urls = $this->url->show($newid, 0, $cid, $systeminfo['inputtime'], $data['prefix'],$inputinfo,'add');
}
$this->table_name = $tablename;
$this->update(array('url'=>$urls[0]),array('id'=>$newid));
//发布到审核列表中
if($data['status']!=99) {
$check_data = array(
'checkid'=>'c-'.$newid.'-'.$mid,
'catid'=>$cid,
'siteid'=>$this->siteid,
'title'=>$systeminfo['title'],
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'status'=>1,
);
$this->content_check_db->insert($check_data);
}
if($urls['content_ishtml'] && $data['status']==99) $html->show($urls[1],$urls['data']);
} else {
//不同模型插入转向链接地址
$newid = $this->insert(
array('title'=>$systeminfo['title'],
'style'=>$systeminfo['style'],
'thumb'=>$systeminfo['thumb'],
'keywords'=>$systeminfo['keywords'],
'description'=>$systeminfo['description'],
'status'=>$systeminfo['status'],
'catid'=>$cid,'url'=>$linkurl,
'sysadd'=>1,
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'updatetime'=>$systeminfo['updatetime'],
'islink'=>1
),true);
$this->table_name = $this->table_name.'_data';
$this->insert(array('id'=>$newid));
//发布到审核列表中
if($data['status']!=99) {
$check_data = array(
'checkid'=>'c-'.$newid.'-'.$mid,
'catid'=>$systeminfo['catid'],
'siteid'=>$this->siteid,
'title'=>$systeminfo['title'],
'username'=>$systeminfo['username'],
'inputtime'=>$systeminfo['inputtime'],
'status'=>1,
);
$this->content_check_db->insert($check_data);
}
}
$hitsid = 'c-'.$mid.'-'.$newid;
$this->hits_db->insert(array('hitsid'=>$hitsid,'catid'=>$cid,'updatetime'=>SYS_TIME));
}
}
//END 发布到其他栏目
//更新附件状态
if(pc_base::load_config('system','attachment_stat')) {
$this->attachment_db = pc_base::load_model('attachment_model');
$this->attachment_db->api_update('','c-'.$systeminfo['catid'].'-'.$id,2);
}
//生成静态
if(!$isimport && $data['status']==99) {
//在添加和修改内容处定义了 INDEX_HTML
if(defined('INDEX_HTML')) $html->index();
if(defined('RELATION_HTML')) $html->create_relation_html($catid);
}
return $id;
}
完成后效果如下图:
五、在图片模型中添加批量添加字段
进入网站后台---内容---模型管理--图片模型--字段管理,添加相应的批量添加字段,这里的字段值一定要和“content_addall.tpl.php”和“content.php”中的值一样,否则会出现错误,完成后效果如图所示:
六、将单条添加中的批量上传字段进行隐藏处理
为了能够达到更好的用户体验,需要将单条添加的时候,批量上传中用到的字段隐藏掉,打开“phpcms\modules\content\templates\content_add.tpl.php”文件,搜索“<th width="80"><?php if($info['star']){ ?> <font color="red">*</font><?php } ?> <?php echo $info['name']?>”,在句话<tr>的上面增加判断,效果图下图:
写到最后:
至此PHPCMS后台批量添加图片模型文章的方法已经完成,只不过还有一点细节的部分需要调整,比如在添加完五条信息之后,在返回进行编辑的时候,会出现信息为空的现象,这点不会影响批量添加的使用,有兴趣的同学可以再进行修改,以达到更佳完美的效果图,作为济南网站建设公司的一员,尽量保证我们所做的每个网站的后台最适宜用户的使用,就比如这个批量添加的功能,如果您不给客户添加上,可能会浪费自己或者客户很多时间,而且客户也会觉得自己花钱建设的网站没有达到完美。