<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
 * PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_ads.php
 * 摘　　要：广告
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
if (!getAdminPri('8107'))exit(NO_AUTH_LOGIN);
$do = trim($_GET['do']);
$post = filter_site_url_encode($_POST);
$tpl -> assign("do",$do);
switch($do){
	case 'edit':
		$ads_id = abs(intval($_GET['adsid']));
		if(empty($ads_id)){
			$sp->bgmsg('请选择要修改的广告', '?action=ads&do=show&page=' . $_GET['page']);
		}
		$sql = "SELECT ads_id, ads_name, ads_type, ads_text, ads_begintime, ads_endtime, 
		ads_pass, ads_place_id, ##__ads_place.place_name FROM ##__ads LEFT JOIN ##__ads_place ON ##__ads.ads_place_id = ##__ads_place.place_id  WHERE ads_id = '$ads_id'";
		$ads = $db->fetch($sql,  true);
		$ads['ads_text'] = unserialize_deep($ads['ads_text']);
		$ads = filter_site_url_decode($ads);
		if(empty($ads))$sp->bgmsg('不存在次广告信息，请重新操作', '?action=ads&do=show&page=' .to_int($_GET['page']));
		$tpl->assign('ads', $ads);
		$tpl->assign('curpage',to_int($_GET['page']));
	case 'add':
		$pn = $do == 'edit' ? '修改广告' : '添加广告';
		/* 查询 所有的 广告位*/
		$sql = 'SELECT place_id, place_name, place_width, place_height FROM ##__ads_place ORDER BY place_id DESC';
		$places = $db->fetch($sql);
		$tpl->assign('places',$places);
		break;
	case 'oper':
		$ads_id = abs(intval($post['adsid']));
		$url = empty($ads_id) ? '?action=ads&do=add' : '?action=ads&do=edit&adsid=' . $ads_id;
		$data['ads_name'] = $post['name'];
		if(empty($data['ads_name'])){
			$sp->bgmsg('广告名称不能为空', $url);
		}
		$data['ads_type'] = $post['type'];
		$data['ads_place_id'] = $post['place'];
		if(empty($data['ads_place_id'])){
			$sp->bgmsg('请您选择广告位', $url);
		}
		$data['ads_begintime'] = strtotime($post['begintime']);
		$data['ads_endtime'] = strtotime($post['endtime']);
		if(empty($data['ads_begintime']) || empty($data['ads_endtime'])){
			$sp->bgmsg('广告时间不能为空', $url);
		}
		if($data['ads_begintime'] >= $data['ads_endtime']){
			$sp->bgmsg('结束时间必须大于开始时间', $url);
		}
		$data['ads_pass'] = abs(intval($post['pass']));
		if($data['ads_type'] == 'image'){
			/* image */
			$data_text['image_src'] = $post['image_text'];
			$image_type = substr($data_text['image_src'],strlen($data_text['image_src'])-3);
			
			$image_types = array('jpg','png', 'gif');
			if(empty($image_type) || !in_array($image_type, $image_types)){
				$sp->bgmsg('图片路径不能为空，并且图片格式只能是 .jpg 、.png 、.gif 结尾', $url);
			}
			$data_text['image_link'] = $post['image_link'];
		}elseif($data['ads_type'] == 'flash'){
			/* flash */
			$data_text['flash_src'] = $post['flash_text'];
			if(empty($data_text['flash_src']) || substr($data_text['flash_src'],strlen($data_text['flash_src'])-3) != 'swf'){
				$sp->bgmsg('Flash路径不能为空，并且Flash格式只能是 .swf 结尾', $url);
			}
			$data_text['flash_link'] = $post['flash_link'];
		}elseif($data['ads_type'] == 'text'){
			/* 文本 */
			$data_text['text_text'] = $post['text_text'];
			if(empty($data_text['text_text'])){
				$sp->bgmsg('文本内容不能为空', $url);
			}
		}elseif ($data['ads_type'] == 'a'){
			/* a 标签 */
			$data_text['a_text'] = $post['a_text'];
			if(empty($data_text['a_text'])){
				$sp->bgmsg('链接内容不能为空', $url);
			}
			$data_text['a_link'] = $post['a_link'];
			if(empty($data_text['a_link'])){
				$sp->bgmsg('连接地址不能为空', $url);
			}
		}else{
			$sp->bgmsg('您选择的类型不正确，请重新选择', $url);
		}
		$data_text['ads_desc'] = $post['desc'];
		$data['ads_text'] = serialize_deep($data_text);
		php188StaticCache::getInstance()->remove('phpwms_ads_place');
		if(empty($ads_id)){
			/* 添加 */
			$mes = $db->insert($data, 'ads') ? '添加成功' : '添加失败';
		}else{
			/*修改*/
			$mes = $db->update($data,'ads', array('ads_id'=>$ads_id)) ? '修改成功' : '修改失败';
		}
		$mes = empty($mes) ? '操作失败' : $mes;
		$sp->bgmsg($mes, '?action=ads&do=show&page=' . $_GET['page']);
		break;
	case 'delete':
		$id = $_REQUEST['adsid'];
		if(is_array($id)){
			$ids = array_filter($id);
			$id = '';
			foreach($ids as $v){
				$id .=  abs(intval($v)) . ',';
			}
			$id = substr($id, 0, strlen($id) - 1);
		}else{
			$id = abs(intval($id));
		}
		if(empty($id)){
			$sp->bgmsg('请选择正确的广告位', '?action=ads&do=show&page=' . $_GET['page']);
		}
		$sql = "DELETE FROM ##__ads WHERE ads_id IN	($id)";
		$mes = $db->query($sql) ? '删除成功' : '删除失败';
		php188StaticCache::getInstance()->remove('phpwms_ads_place');
		$sp->bgmsg($mes, '?action=ads&do=show&page=' . $_GET['page']);
		break;
	case 'detail':
		$ads_id = abs(intval($_GET['adsid']));
		if(empty($ads_id)){
			exit('请您选择要查看的广告');
		}
		$sql = "SELECT ads_id, ads_name, ads_type, ads_text, ads_begintime, ads_endtime, 
		ads_pass, ads_place_id, place_width, place_height, ##__ads_place.place_name FROM ##__ads LEFT JOIN ##__ads_place ON ##__ads.ads_place_id = ##__ads_place.place_id  WHERE ads_id = '$ads_id'";
		$ads = $db->fetch($sql,  true);
		if(empty($ads)){
			exit('没有此数据，请你重新核实');
		}
		$ads['ads_text'] = unserialize_deep($ads['ads_text']);
		echo get_ads_string($ads);
		exit();
	default:
		$pn = '广告列表';
		/*得到广告*/
		$sql = 'SELECT COUNT(*) as count FROM ##__ads';
		$res = $db->fetch ( $sql, true );
		$count = intval($res ['count']);
		$sql = "SELECT ads_id, ads_name, ads_type, ads_text, ads_begintime, ads_endtime, 
		ads_pass, ads_place_id, ##__ads_place.place_name FROM ##__ads LEFT JOIN ##__ads_place ON ##__ads.ads_place_id = ##__ads_place.place_id ORDER BY ads_id DESC";
		$result = page_for_db_extend($count, $sql,'?action=ads&do=show', 10);
		foreach ($result['data'] as $k=>$ads){
			$result['data'][$k]['ads_text'] = unserialize_deep($ads['ads_text']);
		}
		$curtime = time();
		$tpl->assign('result', $result);
		$tpl->assign('curtime', $curtime);
		break;
}
$tpl -> assign("admin_nav",shownav($pn));
$tpl -> display('ads.tpl');
?>