<?php
if(!defined('MOD'))  die("access die");
purviewCheck('money', 'plugin');
$action = $_GET['action'];
$action = $action ? $action : 'manage';
switch($action)
{
    case 'manage':
        $data = qdb::fetchAll("select * from ".TABLEPRE."ad ");
		$temp = qdb::fetchAll("select count(pid) as num, id from ".TABLEPRE."ad_mapping group by id ");
		$temp1 = array();
		foreach($temp as $key=>$val){
			$temp1[$val['id']] = $val['num'];
		}
		foreach($data as &$val){
			$val['total'] = intval($temp1[$val['id']]);
			$val['url'] = W_P . "cache/js/money_" . $val['name'] . ".js";
		}
		unset($val);
	break;
	
	case 'cache':
		checkAdminReferer();
		$data = qdb::fetchAll("select id from ".TABLEPRE."ad ");
		foreach($data as $val){
			money_js_cache($val['id']);
		}
		qbase::showmsg("操作成功", 'default', 'plugin.php?plugin=money&action=manage');
	break;
	
	case 'delete':
		checkAdminReferer();
		$id = intval($_GET['id']);
		$item = qdb::queryFirst("select * from ".TABLEPRE."ad where id = '{$id}' ");
		if(!empty($item)){
			qdb::query("delete from ".TABLEPRE."ad where id = '{$id}'");
			qdb::query("delete from ".TABLEPRE."ad_mapping where id = '{$id}'");
			qbase::writetofile(C_P . "/js/money_" . $item['name'] . '.js', '');
			qbase::get_object('event', 'ad', array('action'=>'delete', 'data'=>$item));
		}
	break;
	
	case 'js':
		checkAdminReferer();
		$id = intval($_GET['id']);
		money_js_cache($id);
		qbase::showmsg("操作成功", 'default', 'plugin.php?plugin=money&action=manage');
	break;
	
	case 'add':
	case 'edit':
		if($_POST['myFormAc'] == 'update'){
			$param = array();
			$param['name'] = trim($_POST['name']);
			$param['description'] = trim($_POST['description']);
			$param['width'] = intval($_POST['width']);
			$param['num'] = intval($_POST['num']);
			$id = intval($_POST['id']);
			if($id > 0){
				unset($param['name']);
				$sql = qbase::createSql(TABLEPRE.'ad', $param, 'update') . " where id = '{$id}' ";
			}else{
				$num = qdb::queryAlone("select count(*) as num from ".TABLEPRE."ad where name = '{$param['name']}' ");
				if($num > 0) qbase::showmsg("此广告位的唯一标识已被使用，请换一个");
				$sql = qbase::createSql(TABLEPRE.'ad', $param, 'insert');
			}
			qdb::query($sql);
			qbase::showmsg("操作成功", 'default', 'plugin.php?plugin=money&action=manage');
		}else{
			$_output = array();
			if($action == 'edit'){
				$id = intval($_GET['id']);
				$_output = qdb::queryFirst("select * from ".TABLEPRE."ad where id = '{$id}' ");
				if(empty($_output)) qbase::showmsg("error request");
			}
		}
	break;
	
    case 'manage_ad':
		$id = intval($_GET['id']);
		$where = $id > 0 ? " where m.id = '{$id}' " : "";
        $data = qdb::fetchAll("select m.*, a.name from ".TABLEPRE."ad_mapping as m left join ".TABLEPRE."ad as a on m.id = a.id {$where} order by m.ordering desc, m.pid desc");
		foreach($data as &$val){
			if($val['closetime'] == 0){
				$val['closetime_tip'] = '无限期';
			}else{
				$val['closetime_tip'] = $val['closetime'] < time() ? "<span style='color:red'>已过期</span>" : qbase::formatTime('Y-n-d', $val['closetime']);
			}
		}
		unset($val);
    break;	
	
	case 'add_ad':
	case 'edit_ad':
		if($_POST['myFormAc'] == 'update'){
			$param = array();
			$param['id'] = intval($_GET['id']);
			$param['modifytime'] = time();
			$param['ordering'] = intval($_GET['ordering']);
			$param['subject'] = trim($_POST['subject']);
			$param['type'] = trim($_POST['type']);
			$param['closetime'] = strtotime(trim($_POST['closetime']));
			$param['status'] = intval($_POST['status']);
			$param['width'] = intval($_POST['width']);
			$param['height'] = intval($_POST['height']);
			$param['word'] = trim($_POST['word']);
			$param['src'] = trim($_POST['src']);
			$param['link'] = trim($_POST['link']);
			$param['code'] = trim($_POST['code']);
			$param['filter_type'] = intval($_POST['filter_type']);
			$param['filter_val'] = trim($_POST['filter_val']);
			if(empty($param['subject'])) qbase::showmsg("请填写广告名字");
			switch($param['type']){
				case 'word':
					$param['width'] = $param['height'] = $param['src'] = $param['code'] = '';
					if(empty($param['word']) || empty($param['link'])) qbase::showmsg("请填写完整广告选项");
				break;
				
				case 'img':
				case 'flash':
					$param['word'] = $param['code'] = '';
					if(empty($param['link']) || empty($param['width']) || empty($param['height']) || empty($param['src'])) qbase::showmsg("请填写完整广告选项");
				break;
				
				case 'code':
					$param['word'] = $param['src'] = '';
					if(empty($param['code']) || empty($param['width']) || empty($param['height']) ) qbase::showmsg("请填写完整广告选项");
				break;

				case 'iframe':
					$param['word'] = $param['code'];
					if(empty($param['src']) || empty($param['width']) || empty($param['height']) ) qbase::showmsg("请填写完整广告选项");
				break;

				case 'newwin':
					$param['word'] = $param['src'] = '';
					if(empty($param['link']) || empty($param['width']) || empty($param['height'])) qbase::showmsg("请填写完整广告选项");
				break;
				
				default:
					qbase::showmsg("error request");
				break;
			}
			if($action == 'add_ad'){
				$param['dateline'] = time();
				$sql = qbase::createSql(TABLEPRE.'ad_mapping', $param, 'insert');
				$pid = qdb::query($sql, true);
				$id = $param['id'];
			}else{
				unset($param['id']);
				$pid = intval($_POST['pid']);
				$id = qdb::queryAlone("select id from ".TABLEPRE."ad_mapping where pid = '{$pid}' ");
				if($id){
					$sql = qbase::createSql(TABLEPRE.'ad_mapping', $param, 'update') . " where pid = '{$pid}' ";
					qdb::query($sql);
				}
			}
			if($param['type'] == 'code'){
				$html = trim(stripslashes($param['code']));
				create_iframe_html($pid, $html);
			}
			money_js_cache($id);
			qbase::showmsg("操作成功", 'default', 'plugin.php?plugin=money&action=manage');
		}else{
			$_output = array();
			if($action == 'edit_ad'){
				$pid = intval($_GET['pid']);
				$_output = qdb::queryFirst("select * from ".TABLEPRE."ad_mapping where pid = '{$pid}' ");
				if(empty($_output)) qbase::showmsg("error request");
				$_output['code'] = htmlspecialchars($_output['code']);
				$_output['closetime'] = $_output['closetime'] ? qbase::formatTime('Y-m-d', $_output['closetime']) : 0;
			}else{
				$id = intval($_GET['id']);
				$_output['closetime'] = qbase::formatTime('Y-m-d', time() + 3600*24*30);
			}
		}
	break;
	
	case 'delete_ad':
		checkAdminReferer();
		$pid = intval($_GET['pid']);
		$id = qdb::queryAlone("select id from ".TABLEPRE."ad_mapping where pid = '{$pid}' ");
		if($id){
			qdb::query("delete from ".TABLEPRE."ad_mapping where pid = '{$pid}' ");
			money_js_cache($id);
		}
		qbase::showmsg("操作成功", 'default', 'plugin.php?plugin=money&action=manage_ad&id=' . $id);
	break;
}
include qbase::template($path . 'plugin/money');

function money_js_cache($id){
	$item = qdb::queryFirst("select * from ".TABLEPRE."ad where id = '{$id}' ");
	if(empty($item)) return false;
	$limit = " ";
	if($item['num'] > 0) $limit = " limit {$item['num']} ";
	$data = qdb::fetchAll("select pid, subject, type, word, src, link, width, height, code, filter_type, filter_val,modifytime from ".TABLEPRE."ad_mapping where id = '{$id}' and status > 0 and closetime > {$_SERVER['REQUEST_TIME']} order by ordering desc {$limit}");
	$js = '';
	foreach($data as $val){
		$js .= "qmeCms.adManager.init(". qbase::jsonencode($val) .");\n"; 
		if($val['type'] == 'code') create_iframe_html($val['pid'], $val['code']);
	}
	qbase::writetofile(C_P . "/js/money_" . $item['name'] . '.js', $js);
	qbase::get_object('event', 'ad', array('action'=>'cache', 'data'=>$item));
}

function create_iframe_html($pid, $html){
	global $_SET;
	$mpre = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset={$_SET['def_charset']}\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\" /><style type=\"text/css\">*{padding:0;margin:0;}</style><base target='_blank' /></head><body>";
	$mend = "</body></html>";
	qbase::writetofile(C_P . "/money/mm_" . $pid . ".html", $mpre . $html . $mend);
	clearstatcache();
}
?>
