之前发表过一篇《PHPCMS V9单页面添加组图字段》的文章,是在后台添加或编辑单页面内容处增加图组功能,然后前台单页面调用图组。今天要说的是在栏目管理处,也就是添加或修改栏目时,有上传栏目图组的功能。两者都源自系统本身的图组,实现上有类似的地方。
实现流程:
一、栏目表中添加栏目多图字段
二、系统扩展函数文件phpcms/libs/functions/extention.func.php中添加函数catimages()
/**
* WY ADD AT 2018/05/14
* 后台栏目管理中添加组图上传
* 返回多图上传
* @param $field 多图字段名
* @param $catid 当前栏目ID
* @param $value 多图字段值
*/
function catimages($field,$catid,$value) {
$list_str = '';
if($value) {
$value = string2array(new_html_entity_decode($value));
if(is_array($value)) {
foreach($value as $_k=>$_v) {
$list_str .= "<div id='image_{$field}_{$_k}' style='padding:1px'><input type='text' name='{$field}_url[]' value='{$_v[url]}' style='width:310px;' ondblclick='image_priview(this.value);' class='input-text'> <input type='text' name='{$field}_alt[]' value='{$_v[alt]}' style='width:160px;' class='input-text'> <a href=\"javascript:remove_div('image_{$field}_{$_k}')\">".L('remove_out', '', 'content')."</a></div>";
}
}
} else {
$list_str .= "<center><div class='onShow' id='nameTip'>".L('upload_pic_max', '', 'content')." <font color='red'>50</font> ".L('tips_pics', '', 'content')."</div></center>";
}
$string = '<input name="info['.$field.']" type="hidden" value="1">
<fieldset class="blue pad-10">
<legend>'.L('pic_list').'</legend>';
$string .= $list_str;
$string .= '<div id="'.$field.'" class="picList"></div>
</fieldset>
<div class="bk10"></div>
';
if(!defined('IMAGES_INIT')) {
$str = '<script type="text/javascript" src="statics/js/swfupload/swf2ckeditor.js"></script>';
define('IMAGES_INIT', 1);
}
$str = '<script language="javascript" type="text/javascript" src="statics/js/content_addtop.js"></script>';
$authkey = upload_key("50,gif|jpg|jpeg|png|bmp,1");
$string .= $str."<div class='picBut cu'><a href='javascript:void(0);' onclick=\"javascript:flashupload('{$field}_images', '".L('attachment_upload')."','{$field}',change_images,'50,gif|jpg|jpeg|png|bmp,1','content','{$catid}','{$authkey}')\"/> ".L('select_pic')." </a></div>";
return $string;
}
三、后台栏目管理控制器phpcms/modules/admin/category.php中,添加栏目和修改栏目的方法中分别添加栏目多图上传处理:
添加栏目public function add()中的$_POST['info']['siteid'] = $this->siteid;前加入如下代码:
//取得图片列表 wy add at 2018-05-14
$catpics = $_POST['catpics_url'];
//取得图片说明
$catpics_alt = isset($_POST['catpics_alt']) ? $_POST['catpics_alt'] : array();
$catpicsarray = $temp = array();
if(!empty($catpics)) {
foreach($catpics as $key=>$pic) {
$temp['url'] = $pic;
$temp['alt'] = str_replace(array('"',"'"),'`',$catpics_alt[$key]);
$catpicsarray[$key] = $temp;
}
}
$catpicsarray = array2string($catpicsarray);
$_POST['info']['catpics'] = $catpicsarray;
修改栏目public function edit()中的$this->db->update($_POST['info'],array('catid'=>$catid,'siteid'=>$this->siteid));前加入如下代码:
//取得图片列表 wy add at 2018-05-14
$catpics = $_POST['catpics_url'];
//取得图片说明
$catpics_alt = isset($_POST['catpics_alt']) ? $_POST['catpics_alt'] : array();
$catpicsarray = $temp = array();
if(!empty($catpics)) {
foreach($catpics as $key=>$pic) {
$temp['url'] = $pic;
$temp['alt'] = str_replace(array('"',"'"),'`',$catpics_alt[$key]);
$catpicsarray[$key] = $temp;
}
}
$catpicsarray = array2string($catpicsarray);
$_POST['info']['catpics'] = $catpicsarray;
四、在栏目(含单网页)添加、修改的模板页面(phpcms\modules\admin\templates目录下的category_add.tpl.php、category_edit.tpl.php、category_page_add.tpl.php、category_page_edit.tpl.php)中,分别添加如下代码:
category_add.tpl.php文件第77行加入如下代码:
<!-- wy add at 2018-05-14 栏目上传多图 start-->
<tr>
<th width="80">组图:</th>
<td>
<?php echo catimages('catpics',$catid,''); ?>
</td>
</tr>
<!-- 单页上传多图 end -->
category_edit.tpl.php文件第67行加入如下代码:
<!-- wy add at 2018-05-14 栏目上传多图 start-->
<tr>
<th width="80">组图:</th>
<td>
<?php echo catimages('catpics',$catid,$catpics); ?>
</td>
</tr>
<!-- 单页上传多图 end -->
category_page_add.tpl.php文件第60行加入如下代码:
<!-- wy add at 2018-05-14 栏目上传多图 start-->
<tr>
<th width="80">组图:</th>
<td>
<?php echo catimages('catpics',$catid,''); ?>
</td>
</tr>
<!-- 单页上传多图 end -->
category_page_edit.tpl.php文件第46行加入如下代码:
<!-- wy add at 2018-05-14 栏目上传多图 start-->
<tr>
<th width="80">组图:</th>
<td>
<?php echo catimages('catpics',$catid,$catpics); ?>
</td>
</tr>
<!-- 单页上传多图 end -->
五、更新缓存,并在添加栏目或修改栏目时上传多图
六,在前台模板中调用
只调用当前栏目组图:
{loop string2array($CATEGORYS[$catid][catpics]) $vimages}
<img alt="{$vimages['alt']}" src="{$vimages['url']}" />
{/loop}
调用当前栏目下每个子栏目组图:
{loop subcat($catid) $c}
<div>
{loop string2array($c[catpics]) $vimages}
<img alt="{$vimages['alt']}" src="{$vimages['url']}" />
{/loop}
</div>
{/loop}