<?php
define('IN_SMT',true);
define('CURSCRIPT','coupon');
define('IN_MYMPS',TRUE);
define('DIR_NAV',dirname(__FILE__));

require_once DIR_NAV.'/include/global.php';
require_once DIR_NAV."/data/config.php";

ifsiteopen();
require_once MYMPS_DATA."/config.db.php";
require_once MYMPS_INC."/db.class.php";

$cate_id = isset($cate_id) ? intval($cate_id) : '';
$areaid  = isset($areaid)  ? intval($areaid)  : '';
$id  	 = isset($id)  	   ? intval($id)	  : '';
$orderby = isset($orderby) ? trim($orderby)	  : '';
$data 	 = $pluginsettings	=	'';
$action  = isset($action)  ? trim($action)	  : '';

if(!in_array($orderby,array('prints','dateline','hit'))) $orderby = 'hit';

@include MYMPS_DATA.'/caches/plugin.php';
$pluginsettings = $data;
unset($data);

!ifplugin(CURSCRIPT) && exit('管理员已禁用或未安装优惠券插件...');

require_once MYMPS_SMARTY."/include.php";
require_once DIR_NAV.'/plugin/coupon/include/functions.php';

$couponclass  = $coupon_class = get_coupon_class();
$coupon_class = is_array($coupon_class) ? array_merge(
							array(
								  0=>array(
										   'cate_id'=>0,
										   'cate_name'=>'全部',
										   'cate_uri'=>plugin_url(CURSCRIPT,array('cate_id'=>0))
										   )
								  )
							,$coupon_class
							) : array();

$area_class = cat_list('area',0,0,false,1);
$area_class	= array_merge(array('0'=>array('areaid'=>'','areaname'=>'全部')),$area_class);
if(is_array($area_class)){
	foreach($area_class as $areakey => $areaval){
		$area_class[$areakey]['uri'] = plugin_url(CURSCRIPT,array('cate_id'=>$cate_id,'areaid'=>$areaval['areaid']));
		$area_class[$areakey]['select'] = $areaval['areaid'] == $areaid ? '1' : 0;
	}
}

$advertisement	= get_advertisement('other');//获得全局广告
$adveritems		= $advertisement['items'];
$advertisement	= $advertisement['all'];

tpl_assign();

$smarty -> assign('coupon_class',$coupon_class);
$smarty -> assign('area_class',$area_class);
unset($coupon_class,$area_class);

$counts = $db -> getAll("SELECT cate_id,count(id) AS num FROM {$db_mymps}coupon AS i WHERE status = '1' AND grade > '0' GROUP BY cate_id ");
$count = array();
$count['total'] = $db->getOne("SELECT count(id) FROM {$db_mymps}coupon WHERE status = '1' AND grade > '0'");
foreach($counts as $k=>$v){
	$count[$v['cate_id']] = !empty($v['num']) ? $v['num'] : 0 ;
}
$smarty -> assign('count',$count);
unset($counts);

if($id) {
	
	$coupon  = $db -> getRow("SELECT * FROM `{$db_mymps}coupon` WHERE id = '$id' AND status = '1' AND grade > '0'");
	if(!$coupon['id']) write_msg('该优惠券已失效或者尚未通过审核！','olmsg');
	
	if($action == 'print'){
		
		$db -> query("UPDATE `{$db_mymps}coupon` SET prints = prints + 1 WHERE id = '$id'");
		$smarty -> assign('coupon',$coupon);
		$smarty -> display(DIR_NAV.'/plugin/'.CURSCRIPT.'/template/print.html');
	
	} else {

		$db -> query("UPDATE `{$db_mymps}coupon` SET hit = hit + 1 WHERE id = '$id'");
		/*优惠券内容内链处理*/
		$coupon['content'] = replace_insidelink($coupon['content'],'coupon');
		$smarty -> assign('coupon',$coupon);
		
		$space = $db -> getRow("SELECT tname,address,busway,tel FROM `{$db_mymps}member` WHERE userid = '$coupon[userid]'");
		$uid = $db -> getOne("SELECT id FROM `{$db_mymps}member` WHERE userid = '$coupon[userid]'");
		$space['uri'] = Rewrite('store',array('uid'=>$uid,'action'=>'index'));
		$space['tname'] = $space['tname'] ? $space['tname'] : $space['userid'];
		$space['address'] = $space['address'] ?  $space['address'] : $space['busway'];
		$smarty -> assign('space',$space);
		unset($space);
		
		$loc = get_coupon_location($coupon['cate_id'],$coupon['title']);
		$page_title = $loc['page_title'];
		$location	= $loc['location'];
		
		$smarty -> assign('page_title',$page_title);
		$smarty -> assign('location',$location);
		unset($page_title,$location,$loc);
		
		$smarty -> display(DIR_NAV.'/plugin/'.CURSCRIPT.'/template/view.html');
		
	}
	
	unset($coupon);
	
} else {
	
	$where = "WHERE status = '1' AND grade > '0'";
	if($cate_id) $where .= " AND cate_id = '$cate_id'";
	if($areaid) $where .= " AND areaid = '$areaid'";
	
	$rows_num = $db->getOne("SELECT count(id) FROM {$db_mymps}coupon $where");
	$param = setParam(array('cateid','areaid','orderby'));
	$coupon = page1("SELECT * FROM `{$db_mymps}coupon` $where ORDER BY $orderby DESC");
	foreach($coupon as $k => $v){
		$list[$v['id']]['id'] = $v['id'];
		$list[$v['id']]['title'] = $v['title'];
		$list[$v['id']]['des'] = $v['des'];
		$list[$v['id']]['enddate'] = $v['enddate'];
		$list[$v['id']]['begindate'] = $v['begindate'];
		$list[$v['id']]['prints'] = $v['prints'];
		$list[$v['id']]['pre_picture'] = $v['pre_picture'];
		$list[$v['id']]['sup'] = $v['sup'];
		$list[$v['id']]['uri'] = plugin_url('coupon',array('id'=>$v['id']));
	}
	
	foreach(array('hit'=>'默认','prints'=>'打印','dateline'=>'最新') as $k => $v){
		$orderby_url[$k]['selected'] = $orderby == $k ? 1 : 0;
		$orderby_url[$k]['name'] = $v;
		$orderby_url[$k]['url'] = plugin_url(CURSCRIPT,array('cate_id'=>$cate_id,'areaid'=>$areaid,'orderby'=>$k));
	}
	$smarty -> assign('orderby_url',$orderby_url);
	unset($orderby_url);
	
	$smarty -> assign('list',$list);
	$smarty -> assign('page_view',page2());
	
	$loc = get_coupon_location($cate_id);
	$page_title = (empty($cate_id) && empty($areaid)) ? ($pluginsettings[CURSCRIPT]['seotitle'] ? $pluginsettings[CURSCRIPT]['seotitle'] : $loc['page_title']) : $loc['page_title'];
	$location	= $loc['location'];
	$smarty -> assign('page_title',$page_title);
	$smarty -> assign('location',$location);
	unset($page_title,$location,$loc);
	
	$seo = array();
	$seo['keywords'] 	= $pluginsettings[CURSCRIPT]['seokeywords'];
	$seo['description'] = $pluginsettings[CURSCRIPT]['seodescription'];
	$smarty -> assign('seo',$seo);
	unset($seo);
	
	$currentlocate = $areaname.$couponclass[$cate_id]['cate_name'].'优惠券';
	$smarty -> assign('currentlocate',$currentlocate);
	unset($currentlocate,$couponclass,$areaname);
	
	$smarty -> display(DIR_NAV.'/plugin/'.CURSCRIPT.'/template/index.html');
	unset($param,$rows_num,$coupon);
}

is_object($db) && $db->Close();
$mymps_global = $pluginsettings  = $smarty = $db = $area_class = $id = $cate_id = $areaid = $orderby = $where = $seo = $list = $count = NULL;

function get_coupon_location($cate_id=0,$str=''){
	global $db,$db_mymps,$couponclass,$areaid,$areaname,$mymps_global;
	
	$raquo = $mymps_global['cfg_raquo'];
	$location   = '当前位置：<a href="'.$mymps_global['SiteUrl'].'">'.$GLOBALS['mymps_global']['SiteName'].'</a>'.' <code>'.$raquo.'</code> '.' <a href="'.plugin_url(CURSCRIPT,array('cate_id'=>0)).'">优惠券</a>';
	$page_title = '优惠券 - '.$mymps_global['SiteName'];
	
	if(!empty($cate_id)){
		$page_title =  htmlspecialchars($couponclass[$cate_id]['cate_name']) . ' - ' . $page_title;
		$location   .= ' <code> '.$raquo.' </code> <a href="' . $couponclass[$cate_id]['cate_uri'] . '">' .
		htmlspecialchars($couponclass[$cate_id]['cate_name']).'</a>';
	}
	
	$areaname = $mymps_global['SiteCity'].($areaid ? get_areaname($areaid) : '');
	$page_title = $areaname.$page_title;
	
	if (!empty($str)){
        $page_title = $str.' - '.$page_title;
        $location   .= ' <code>'.$raquo.'</code> &nbsp;' .$str;
    }
	
	$cur = array('page_title'=>$page_title,'location'=>$location);
	unset($page_title,$cat,$location,$type,$couponclass);
    return $cur;
}
?>