<?php
define('IN_SMT',true);
define('CURSCRIPT','group');
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";

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

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

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

if(!submit_check(CURSCRIPT.'_submit')){

	require_once DIR_NAV.'/plugin/group/include/functions.php';
	require_once MYMPS_SMARTY."/include.php";
	
	$group_class = get_group_class();
	$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('group_class',$group_class);
	$smarty -> assign('area_class',$area_class);
	
	require_once MYMPS_DATA.'/grouplevel.inc.php';
	$smarty -> assign('glevel',$glevel);
	unset($area_class,$glevel);
	
	if($id) {
		
		$group  = $db -> getRow("SELECT * FROM `{$db_mymps}group` WHERE groupid = '$id' AND glevel > '0'");
		if(!$group['groupid']) write_msg('该团购活动不存在或者尚未通过审核！','olmsg');
		$group['remaindate'] = intval(($group['enddate'] - $timestamp)/(86400));
		
		$data = '';
		@include MYMPS_DATA.'/caches/area_option_static.php';
		$group['areaname'] = $data ? $data[$group['areaid']]['areaname'] : $db -> getOne("SELECT areaname FROM `{$db_mymps}area` WHERE areaid = '$group[areaid]'");
		$data = NULL;
		
		/*团购介绍内链处理*/
		$group['content'] = replace_insidelink($group['content'],'group');
		$smarty -> assign('group',$group);
		
		$share = array();
		$share['title'] = urlencode($group['gname']);
		$share['url']	= plugin_url(CURSCRIPT,array('id'=>$group['groupid']));
		$smarty -> assign('share',$share);
		unset($share);
		
		$loc = get_group_location($group['cate_id'],$group['gname']);
		$page_title = $loc['page_title'];
		$location	= $loc['location'];
		$smarty -> assign('page_title',$page_title);
		$smarty -> assign('location',$location);
		unset($page_title,$location,$loc,$group);
		
		$smarty -> assign('hotgroup',get_groups('15',1));
		$smarty -> display(DIR_NAV.'/plugin/'.CURSCRIPT.'/template/view.html');
		
	} else {
		
		$where = "WHERE glevel > '0'";
		if($cate_id) $where .= " AND cate_id = '$cate_id'";
		if($areaid) $where .= " AND areaid = '$areaid'";
		
		$rows_num = $db -> getOne("SELECT COUNT(groupid) FROM `{$db_mymps}group` $where");
		$param = setParam(array('cateid','areaid'));
		$group = page1("SELECT * FROM `{$db_mymps}group` $where ORDER BY displayorder DESC");
		foreach($group as $k => $v){
			$list[$v['groupid']]['groupid'] = $v['groupid'];
			$list[$v['groupid']]['gname'] = $v['gname'];
			$list[$v['groupid']]['des'] = clear_html($v['des']);
			$list[$v['groupid']]['enddate'] = $v['enddate'];
			$list[$v['groupid']]['meetdate'] = $v['meetdate'];
			$list[$v['groupid']]['gaddress'] = $v['gaddress'];
			$list[$v['groupid']]['glevel'] = $v['glevel'];
			$list[$v['groupid']]['signintotal'] = $v['signintotal'];
			$list[$v['groupid']]['commenturl'] = $v['commenturl'];
			$list[$v['groupid']]['pre_picture'] = $v['pre_picture'];
			$list[$v['groupid']]['uri'] = plugin_url('group',array('id'=>$v['groupid']));
		}
		
		$smarty -> assign('list',$list);
		$smarty -> assign('page_view',page2());
		$smarty -> assign('hotgroup',get_groups('15',1));
		
		$loc = get_group_location($cate_id);
		$page_title = (!$cat_id && !$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);
		
		$smarty -> display(DIR_NAV.'/plugin/'.CURSCRIPT.'/template/index.html');
		unset($param,$rows_num,$group,$loc,$seo);
	}

} else {
	
	$sname = $sname ? mhtmlspecialchars($sname) : '';
	$id = isset($id) ? intval($id) : '';
	$qqmsn = isset($qqmsn) ? mhtmlspecialchars($qqmsn) : '';
	$tel =  isset($tel) ? mhtmlspecialchars($tel) : '';
	$signinip = GetIP();
	$sex = isset($sex) ? mhtmlspecialchars($sex) : '';
	$message = isset($message) ? textarea_post_change($message) : '';
	$totalnumber = isset($totalnumber) ? intval($totalnumber) : '';
	$age = isset($age) ? mhtmlspecialchars($age) : '';
	
	if(empty($id)) write_msg('您报名的团购活动不存在！');
	if(empty($sname)) write_msg('姓名不能为空！');
	
	if(!$randcode = mymps_chk_randcode($checkcode)){
		write_msg('验证码输入错误，请返回重新输入');
		exit;
	}
	
	$db -> query("UPDATE `{$db_mymps}group` SET signintotal = signintotal + 1 WHERE groupid = '$id'");
	$db -> query("INSERT INTO `{$db_mymps}group_signin` (groupid,sname,sex,tel,age,qqmsn,signinip,dateline,totalnumber,message) VALUES ('$id','$sname','$sex','$tel','$age','$qqmsn','$signinip','$timestamp','$totalnumber','$message')");
	write_msg('报名提交成功，我们会尽快与您取得联系！',plugin_url(CURSCRIPT,array('id'=>$id)));
	
}

is_object($db) && $db->Close();
$mymps_global = $cate_id = $areaid = $pluginsettings = $smarty = $db = NULL;

function get_group_location($cate_id=0,$str=''){
	global $db,$db_mymps,$group_class;
	
	$raquo = $GLOBALS['mymps_global']['cfg_raquo'];
	$location   = '当前位置：<a href="'.$GLOBALS['mymps_global']['SiteUrl'].'">'.$GLOBALS['mymps_global']['SiteName'].'</a>'.' <code>'.$raquo.'</code> '.' <a href="'.plugin_url(CURSCRIPT,array('cate_id'=>0)).'">团购活动</a>';
	$page_title = '团购活动 - '.$GLOBALS['mymps_global']['SiteName'];
	
	if(!empty($cate_id)){
		$page_title =  htmlspecialchars($group_class[$cate_id]['cate_name']) . ' - ' . $page_title;
		$location   .= ' <code> '.$raquo.' </code> <a href="' . $group_class[$cate_id]['cate_uri'] . '">' .
		htmlspecialchars($group_class[$cate_id]['cate_name']).'</a>';
	}
	
	$page_title = $GLOBALS['mymps_global']['SiteCity'].($GLOBALS['areaid'] ? get_areaname($GLOBALS['areaid']) : '').$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,$group_class);
    return $cur;
}
?>