<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

//判断是否有ajax请求
$act = !empty($_GET['act']) ? $_GET['act'] : '';
$cache_id = sprintf('%X', crc32($_SESSION['user_name'] . '-' . $_CFG['lang']));

if($site_type=='web'){
    /* meta information */
    $smarty->assign('keywords',        htmlspecialchars($_CFG['shop_keywords']));
    $smarty->assign('description',     htmlspecialchars($_CFG['shop_desc']));
    $smarty->assign('main_domain',         MAIN_DOMAIN);  // 当前位置
    $smarty->assign('data_dir',        DATA_DIR);       // 数据目录
    $smarty->assign('http',         $GLOBALS['ecs']->http());

    $smarty->assign('news_list',        		get_news($site_id));
    $smarty->assign('rooms_list',        		get_rooms($site_id));
    $smarty->assign('facility_list',        		get_facility($site_id));
    $smarty->assign('fac_list',        		get_fac($site_id));


    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('hs_web') . 
            " WHERE hs_id = " . $site_id;
    $hsweb = $GLOBALS['db']->getRow($sql);
    $smarty->assign('site',$hsweb);       


	$smarty->display('web.html');
}else{
	$smarty->assign('keywords',        htmlspecialchars($_CFG['shop_keywords']));
	    $smarty->assign('description',     htmlspecialchars($_CFG['shop_desc']));
	    $smarty->assign('main_domain',         MAIN_DOMAIN);  // 当前位置
	    $smarty->assign('data_dir',        DATA_DIR);       // 数据目录
		$smarty->assign('http',         $GLOBALS['ecs']->http());
	    $smarty->assign('ad1_list',        		get_rand5_ad1($site_id));
	    $smarty->assign('rnd5_portals_1',         get_rand5_portals(1));
	    $smarty->assign('rnd5_portals_2',         get_rand5_portals(2));
	    $smarty->assign('rnd5_portals_3',         get_rand5_portals(3));
	    $smarty->assign('ad2_list',        		get_rand5_ad2($site_id));
	    $smarty->assign('ad3_list',        		get_rand5_ad3($site_id));
	    $smarty->assign('ad4_list',        		get_all_ad4($site_id));
	    $smarty->assign('footer_text',        	$_CFG['footer_text']);
	    $smarty->assign('category',        	    $category);
	
	    if($site_id>0){ 
		    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('portal') .
			    " WHERE id = " . $site_id;
		    $pt = $GLOBALS['db']->getRow($sql);
		    $smarty->assign('pt',$pt);       
		}
		
	$smarty->display('portal.html');
}
/*------------------------------------------------------ */
//-- PRIVATE FUNCTIONS
/*------------------------------------------------------ */


function get_rooms($id)
{
        $sql = 'SELECT * ' .
               ' FROM ' . $GLOBALS['ecs']->table('hs_room') .
               ' WHERE hs_id= '.$id.
               ' ORDER BY rand() LIMIT 5';

        $res = $GLOBALS['db']->getAll($sql);

		return $res;
}

function get_news($id)
{
        $sql = "SELECT * " .
               " FROM " . $GLOBALS['ecs']->table('hs_news') .
               " WHERE enable=1 and hs_id= ".$id." AND (start_time<='".date("Y-m-d")."' and end_time>='".date("Y-m-d")."')".
               " ORDER BY id desc LIMIT 3";

        $res = $GLOBALS['db']->getAll($sql);

		return $res;
}

function get_facility($id)
{
        $sql = 'SELECT * ' .
               ' FROM ' . $GLOBALS['ecs']->table('hs_gallery') .
               ' WHERE hs_id ='.$id.' '.
               ' ORDER BY rand() LIMIT 20';

        $res = $GLOBALS['db']->getAll($sql);
		return $res;
}

function get_fac($id)
{
        $sql = 'SELECT * ' .
               ' FROM ' . $GLOBALS['ecs']->table('facility') .
               ' WHERE is_show =1 order by sort_order,id';

        $res = $GLOBALS['db']->getAll($sql);

        $sql = 'SELECT facility ' .
               ' FROM ' . $GLOBALS['ecs']->table('hs_web') .
               ' WHERE hs_id = '.$id." ";
		$site_fac=$GLOBALS['db']->getOne($sql);
		$arr_fac=explode(",",$site_fac);

		for($i=0;$i<count($res);$i++){
			if(in_array($res[$i]['id'],$arr_fac)){
				$res[$i]['selected']=1;				
			}else{
				$res[$i]['selected']=0;								
			}
		}
		return $res;
}

function get_rand5_portals($type=1)
{
        $sql = 'SELECT * ' .
               ' FROM ' . $GLOBALS['ecs']->table('portal') .
               ' WHERE type = '. $type .
               ' ORDER BY rand() LIMIT 10';

        $portals_res = $GLOBALS['db']->getAll($sql);

		return $portals_res;
}

function get_rand5_ad1($id)
{
        $sql = "SELECT a.*,b.* " .
               " FROM " . $GLOBALS['ecs']->table('user_ad') ."a,".$GLOBALS['ecs']->table('hs_web')." b ,".$GLOBALS['ecs']->table('users')." c ".
               " WHERE a.user_id=b.hs_id and a.user_id=c.user_id AND a.pt_id= ".$id ." AND a.block&1 = 1 and c.status=0 and a.dstamp >='".date("Y-m-d")."'".
               " ORDER BY rand() ";
        $ad1_res = $GLOBALS['db']->getAll($sql);
		return $ad1_res;
}

function get_rand5_ad2($id)
{
        $sql = "SELECT a.*,b.* " .
               " FROM " . $GLOBALS['ecs']->table('user_ad') ."a,".$GLOBALS['ecs']->table('hs_web')." b ,".$GLOBALS['ecs']->table('users')." c ".
               " WHERE a.user_id=b.hs_id and a.user_id=c.user_id AND a.pt_id= ".$id ." AND a.block&2 = 2 and c.status=0 and a.dstamp >='".date("Y-m-d")."'".
               " ORDER BY rand() LIMIT 5";

        $ad1_res = $GLOBALS['db']->getAll($sql);

		return $ad1_res;
}

function get_rand5_ad3($id)
{
        $sql = "SELECT a.*,b.* " .
               " FROM " . $GLOBALS['ecs']->table('user_ad') ."a,".$GLOBALS['ecs']->table('hs_web')." b ,".$GLOBALS['ecs']->table('users')." c ".
               " WHERE a.user_id=b.hs_id and a.user_id=c.user_id AND a.pt_id= ".$id ." AND a.block&4 = 4 and c.status=0 and a.dstamp >='".date("Y-m-d")."'".
               " ORDER BY rand() LIMIT 10";

        $ad_res = $GLOBALS['db']->getAll($sql);

	    $count = count($ad_res);
	    for ($i=0; $i<$count; $i++)
	    {
			$sql="SELECT * FROM ".$GLOBALS['ecs']->table('hs_room')." WHERE hs_id=".$ad_res[$i]['id'];
	        $rm_res = $GLOBALS['db']->getAll($sql);			
			$ad_res[$i]['room']=$rm_res;        	
			$sql="SELECT * FROM ".$GLOBALS['ecs']->table('facility')." WHERE id in (".$ad_res[$i]['facility'].")";
	        $fac_res = $GLOBALS['db']->getAll($sql);			
			$ad_res[$i]['fac_list']=$fac_res;        	

        }
		return $ad_res;
}

function get_all_ad4($id)
{
        $sql = "SELECT a.*,b.* " .
               " FROM " . $GLOBALS['ecs']->table('user_ad') ."a,".$GLOBALS['ecs']->table('hs_web')." b ,".$GLOBALS['ecs']->table('users')." c ".
               " WHERE a.user_id=b.hs_id and a.user_id=c.user_id AND a.pt_id= ".$id ." AND a.block&8 = 8 and c.status=0 and a.dstamp >='".date("Y-m-d")."'".
               " ORDER BY rand()";

        $ad_res = $GLOBALS['db']->getAll($sql);


		return $ad_res;
}

?>
