Phpcms V9 文章或图片模型增加相关下载功能

2022-12-30 浏览:92
Phpcms V9 文章或图片模型增加相关下载功能
评论:(0)复制地址

1、在文章或图片模型增加新字段:

      字段类型:万能字段 

      字段名:relation_download

      字段别名: 相关下载

      相关参数:

<input type='hidden' name='info[relation_download]' id='relation_download' value='{FIELD_VALUE}' style='50' >
<ul class="list-dot" id="relation_download_text"></ul>
<div>
<input type='button' value="添加相关" onclick="omnipotent('selectid','?m=content&c=content&a=public_relationdownlist&modelid=2','添加相关下载',1)" class="button" style="width:66px;">
<span class="edit_content">
<input type='button' value="显示已有" onclick="show_relation_download(3,2,{ID})" class="button" style="width:66px;">
</span>
</div>


微信截图_20221230164523.jpg


2、打开statics/js/content_addtop.js,在function show_relation(modelid,id) {}下面新增以下代码


//移除相关下载
function remove_relationdownload(sid,id) {
	var relation_ids = $('#relation_download').val();
	if(relation_ids !='' ) {
		$('#'+sid).remove();
		var r_arr = relation_ids.split('|');
		var newrelation_ids = '';
		$.each(r_arr, function(i, n){
			if(n!=id) {
				if(i==0) {
					newrelation_ids = n;
				} else {
				 newrelation_ids = newrelation_ids+'|'+n;
				}
			}
		});
		$('#relation_download').val(newrelation_ids);
	}
}
//显示相关下载
function show_relation_download(modelid,target_modelid,id) {
//$.getJSON("?m=content&c=content&a=public_getjson_ids2&modelid="+modelid+"&id="+id, function(json){
$.getJSON("?m=content&c=content&a=public_getjson_ids2&modelid="+modelid+"&target_modelid="+target_modelid+"&id="+id, function(json){
	var newrelation_ids2 = '';
	if(json==null) {
		alert('没有添加相关下载');
		return false;
	}
	$.each(json, function(i, n){
		newrelation_ids2 += "<li id='"+n.sid+"'>·<span>"+n.title+"</span><a href='javascript:;' class='close' onclick=\"remove_relationdownload('"+n.sid+"',"+n.id+")\"></a></li>";
	});

	$('#relation_download_text').html(newrelation_ids2);
}); 
}


3、打开phpcms/modules/content/content.php,在public function public_getjson_ids() {}下面增加以下代码


/**
	 * 相关下载选择
	 */
	public function public_relationdownlist() {
		pc_base::load_sys_class('format','',0);
		$show_header = '';
		$model_cache = getcache('model','commons');
		if(!isset($_GET['modelid'])) {
			showmessage(L('please_select_modelid'));
		} else {
			$page = intval($_GET['page']);
			
			$modelid = intval($_GET['modelid']);
			$this->db->set_model($modelid);
			$where = '';
			if($_GET['catid']) {
				$catid = intval($_GET['catid']);
				$where .= "catid='$catid'";
			}
			$where .= $where ?  ' AND status=99' : 'status=99';
			
			if(isset($_GET['keywords'])) {
				$keywords = trim($_GET['keywords']);
				$field = $_GET['field'];
				if(in_array($field, array('id','title','keywords','description'))) {
					if($field=='id') {
						$where .= " AND `id` ='$keywords'";
					} else {
						$where .= " AND `$field` like '%$keywords%'";
					}
				}
			}
			$infos = $this->db->listinfo($where,'',$page,12);
			$pages = $this->db->pages;
			include $this->admin_tpl('relationdownlist');
		}
	}
	public function public_getjson_ids2() {
		$modelid = intval($_GET['modelid']);
		$target_modelid = intval($_GET['target_modelid']); 
		$id = intval($_GET['id']);
		$this->db->set_model($modelid);
		$tablename = $this->db->table_name;
		$this->db->table_name = $tablename.'_data';
		$r = $this->db->get_one(array('id'=>$id),'relation_download');
		if($r['relation_download']) {
			$relation = str_replace('|', ',', $r['relation_download']);		
			$relation = trim($relation,',');
			$where = "id IN($relation)";
			$infos = array();
			$this->db->set_model($target_modelid);
			$this->model = getcache('model', 'commons');
			$this->db->table_name = $this->db->db_tablepre.$this->model[$target_modelid]['tablename'];
			$datas = $this->db->select($where,'id,title');
			foreach($datas as $_v) {
				$_v['sid'] = 'd'.$_v['id'];
				if(strtolower(CHARSET)=='gbk') $_v['title'] = iconv('gbk', 'utf-8', $_v['title']);
				$infos[] = $_v;
			}
			echo json_encode($infos);
		}
	}


4、在phpcms/modules/content/templates 文件夹中创建新文件relationdownlist.tpl.php,代码如下

<?php
defined('IN_ADMIN') or exit('No permission resources.');
include $this->admin_tpl('header','admin');
?>
<div class="pad-10">
<form name="searchform" action="" method="get" >
<input type="hidden" value="content" name="m">
<input type="hidden" value="content" name="c">
<input type="hidden" value="public_relationdownlist" name="a">
<input type="hidden" value="<?php echo $modelid;?>" name="modelid">
<table width="100%" cellspacing="0" class="search-form">
    <tbody>
		<tr>
		<td align="center">
		<div class="explain-col">
				<select name="field">
					<option value='title' <?php if($_GET['field']=='title') echo 'selected';?>><?php echo L('title');?></option>
					<option value='keywords' <?php if($_GET['field']=='keywords') echo 'selected';?> ><?php echo L('keywords');?></option>
					<option value='description' <?php if($_GET['field']=='description') echo 'selected';?>><?php echo L('description');?></option>
					<option value='id' <?php if($_GET['field']=='id') echo 'selected';?>>ID</option>
				</select>
				<?php echo form::select_category('',$catid,'name="catid"',L('please_select_category'),$modelid,0,1);?>
				<input name="keywords" type="text" value="<?php echo stripslashes($_GET['keywords'])?>" style="width:330px;" class="input-text" />
				<input type="submit" name="dosubmit" class="button" value="<?php echo L('search');?>" />
	</div>
		</td>
		</tr>
    </tbody>
</table>
</form>
<div class="table-list">
    <table width="100%" cellspacing="0" >
        <thead>
            <tr>
            <th ><?php echo L('title');?></th>
			<th width="100"><?php echo L('belong_category');?></th>
            <th width="100"><?php echo L('addtime');?></th>
            </tr>
        </thead>
    <tbody>
	<?php foreach($infos as $r) { ?>
	<tr onclick="select_list(this,'<?php echo safe_replace($r['title']);?>',<?php echo $r['id'];?>)" class="cu" title="<?php echo L('click_to_select');?>">
		<td align='left' ><?php echo $r['title'];?></td>
		<td align='center'><?php echo $this->categorys[$r['catid']]['catname'];?></td>
		<td align='center'><?php echo format::date($r['inputtime']);?></td>
	</tr>
	 <?php }?>
	    </tbody>
    </table>
   <div id="pages"><?php echo $pages;?></div>
</div>
</div>
<style type="text/css">
 .line_ff9966,.line_ff9966:hover td{
	background-color:#FF9966;
}
 .line_fbffe4,.line_fbffe4:hover td {
	background-color:#fbffe4;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
	function select_list(obj,title,id) {
		var relation_ids = parent.window.$('#relation_download').val();
		var sid = 'd<?php echo $modelid;?>'+id;
		if($(obj).attr('class')=='line_ff9966' || $(obj).attr('class')==null) {
			$(obj).attr('class','line_fbffe4');
			parent.window.$('#'+sid).remove();
			if(relation_ids !='' ) {
				var r_arr = relation_ids.split('|');
				var newrelation_ids = '';
				$.each(r_arr, function(i, n){
					if(n!=id) {
						if(i==0) {
							newrelation_ids = n;
						} else {
						 newrelation_ids = newrelation_ids+'|'+n;
						}
					}
				});
				parent.window.$('#relation_download').val(newrelation_ids);
			}
		} else {
			$(obj).attr('class','line_ff9966');
			var str = "<li id='"+sid+"'>·<span>"+title+"</span><a href='javascript:;' class='close' onclick=\"remove_relationdownload('"+sid+"',"+id+")\"></a></li>";
			//window.top.$('#relation_text').append(str);
			parent.window.$('#relation_download_text').append(str);
			if(relation_ids =='' ) {
				parent.window.$('#relation_download').val(id);
			} else {
				relation_ids = relation_ids+'|'+id;
				parent.window.$('#relation_download').val(relation_ids);
			}
		}
}
</SCRIPT>
</body>
</html>


5、更新系统缓存,添加或修改文章或图片


6、前端模板调用代码


{if $relation_download!=''}
					{php $rel = explode('|',$relation_download);}
					{loop $rel $id}
					{pc:get sql="select title,id,url,thumb from v9_download where id=$id"}
					{loop $data $r}
					<li><a href="{$r['url']}" target="_blank"><span class="refe-img"><img src="{$r[thumb]}" alt="{$r[title]}" /></span><span class="pro-name">{$r[title]}</span></a></li> 
					{/loop}
					{/pc}
					{/loop}
					{/if}


评论:(0)复制地址
发布:苗景云 | 分类:IT技术&设计 | Tags:phpcms

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。