<?php
/********************************************************************
 *                           DirCMS会员模块公用函数
 *                            -------------------
 *   begin                : 2010/5/30
 *   copyright            : (C) 2009 DirCMS
 *   email                : uuturn@126.com
 *
 *   $Id: global.func.php,v 1.0.0 2010/5/30 1:04:01 M Exp $
 ********************************************************************/
function member_tlp($tlp)
{
	!file_exists(DIRCMS_ROOT.'member/template/'.$tlp.'.tlp.php') && fatal_error($LANG['DIRCMS_TLPFILE_ERROR']);
	return DIRCMS_ROOT.'member/template/'.$tlp.'.tlp.php';
}

function create_sn()
{
	return date("YmdHis").str_pad( mt_rand(1,99999 ),5,"0",STR_PAD_LEFT);
}

function pay_fee($amount,$fee)
{
    $pay_fee=0;
    if (strpos($fee,'%')!==false)
    {
        $val=floatval($fee)/100;
        $pay_fee=$val>0?$amount*$val:0;
    }
    else
    {
        $pay_fee=floatval($fee);
    }
    return round($pay_fee,2);
}

function get_payment($code)
{
	global $db;
    $sql="SELECT * FROM " .DB_PRE."pay_method WHERE `code` = '$code' AND `enabled` = '1'";
    $info=$db->fetch_one($sql);
	$cfg=$info['config'];
    if(is_string($cfg) )
    {
        $arr=string2array($cfg);
        $config=array();

        foreach($arr as $key =>$val)
        {
            $config[$key] = $val['value'];
        }
        return $config;
    }
    else
    {
        return false;
    }

}

function createYp() /////// 更新商家首页，关于我们，地图，联系我们，留言板 2011-1-25
{
	global $DIRCMS,$db,$_userid,$_gradeid;
	$id=$_userid=intval($_userid);
	
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息
//print_r($pics);exit();
	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);

	$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');

	// 更新首页
	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> ';
	ob_start();
	if(in_array($gradeid,$templatepriv))
	{
		include space_tlp($template,'index');
	}
	else
	{
		include template('company');
	}
	if(create_html(DIRCMS_ROOT.'company/company_'.$id.'.html'))
	{
		createAbout();
		createCon();
		createMsg();
		createMap();
		createProcduct(0);
		createNews(0);
		createCoupon(0);
	}
}

function createCon() /////// 更新联系我们
{
	global $DIRCMS,$db,$_userid,$_gradeid,$colname;
	$id=$_userid=intval($_userid);
	
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息

	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);

	$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');

	// 留言
	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > 留言板';
	$page=max(isset($page)?intval($page):1,1);
	ob_start();
	if(in_array($gradeid,$templatepriv))
	{
		include space_tlp($template,'contact');
	}
	else
	{
		include template('company_contact');
	}
	create_html(DIRCMS_ROOT.'company/contact_'.$id.'.html');
}

function createMsg() /////// 更新留言
{
	global $DIRCMS,$db,$_userid,$_gradeid,$colname;
	$id=$_userid=intval($_userid);
	
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息

	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);

	$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');

	// 留言
	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > 留言板';
	$page=max(isset($page)?intval($page):1,1);
	ob_start();
	if(in_array($gradeid,$templatepriv))
	{
		include space_tlp($template,'guestbook');
	}
	else
	{
		include template('company_guestbook');
	}
	create_html(DIRCMS_ROOT.'company/guestbook_'.$id.'.html');
}

function createMap() /////// 更新商家地图
{
	global $DIRCMS,$db,$_userid,$_gradeid,$colname;
	$id=$_userid=intval($_userid);
	
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息

	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);

	$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');

	// 地图
	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > 商家地图';
	ob_start();
	if(in_array($gradeid,$templatepriv))
	{
		include space_tlp($template,'map');
	}
	else
	{
		include template('company_map');
	}
	create_html(DIRCMS_ROOT.'company/map_'.$id.'.html');
}

function createAbout() /////// 更新关于我们 2011-1-25
{
	global $DIRCMS,$db,$_userid,$_gradeid,$colname;
	$id=$_userid=intval($_userid);
	
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息

	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);

	$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');

	// 关于我们
	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > 关于我们';
	ob_start();
	if(in_array($gradeid,$templatepriv))
	{
		include space_tlp($template,'about');
	}
	else
	{
		include template('company_about');
	}
	create_html(DIRCMS_ROOT.'company/about_'.$id.'.html');
}

function createProcduct($id)/////// 更新商家产品 2011-1-25
{
	global $DIRCMS,$db,$_userid,$_gradeid,$colname;
	$_userid=intval($_userid);
	$id=intval($id);
	$colname=intval($colname);
	if(!$_userid)return false;

	if($id)
	{
		$data=$db->fetch_one("SELECT * FROM ".DB_PRE."co_product WHERE id=".$id);
		@extract($data);unset($data);
	}
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息

	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);
	
	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > <a href="'.$DIRCMS['site_url'].'company/product_'.$_userid.'_'.$colname.'_1.html">'.$menu[5]['name'].'</a>'.' > '.$product;

	$producttype=cache_read('product_type'.$_userid.'.cache.php',DIRCMS_ROOT.'company/cache/product/');

	if($id)
	{
		// 更新内容
		$type=$producttype?$producttype[$colname]:'无分类';
		ob_start();
		$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');
		if(in_array($_gradeid,$templatepriv))
		{
			include space_tlp($template,'showproduct');
		}
		else
		{
			include template('company_showproduct');
		}
		
		create_html(DIRCMS_ROOT.'company/showproduct_'.$id.'.html');
	}
	else
	{
		// 更新列表
		$id=$_userid;
		if($colname)
		{
			$count=$db->fetch_one("SELECT count(*) as num FROM ".DB_PRE."co_product WHERE userid=".$id." AND typeid=".intval($colname));
		}
		else
		{
			$count=$db->fetch_one("SELECT count(*) as num FROM ".DB_PRE."co_product WHERE userid=".$id);
		}
		$t=$colname;
		$count=$count['num'];
		$pagecount=max(ceil($count/PAGESIZE),1);
		$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > '.$menu[3]['name'].'';
		for($i=1;$i<=$pagecount;$i++)
		{
			if($colname)
			{
				if($pagecount==1)
				{
					$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'.html">尾页</a> ';
				}
				else
				{
					$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'_'.max($i-1,1).'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'_'.min($pagecount,$i+1).'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.intval($colname).'_'.$pagecount.'.html">尾页</a> ';
				}
			}
			else
			{
				if($pagecount==1)
				{
					$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'.html">尾页</a> ';
				}
				else
				{
					$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.max($i-1,1).'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.min($pagecount,$i+1).'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/product_'.$id.'_'.$pagecount.'.html">尾页</a> ';
				}
			}
			$page=intval($i)?max(1,intval($i)):1;
			ob_start();
			$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');
			if(in_array($_gradeid,$templatepriv))
			{
				include space_tlp($template,'product');
			}
			else
			{
				include template('company_product');
			}
			$colname=$t;
			if($colname)
			{
				if($i==1)
				{
					create_html(DIRCMS_ROOT.'company/product_'.$_userid.'_'.intval($colname).'.html');
				}
				else
				{
					create_html(DIRCMS_ROOT.'company/product_'.$_userid.'_'.intval($colname).'_'.$i.'.html');
				}
			}
			else
			{
				if($i==1)
				{
					create_html(DIRCMS_ROOT.'company/product_'.$_userid.'.html');				
				}
				else
				{
					create_html(DIRCMS_ROOT.'company/product_'.$_userid.'_'.$i.'.html');
				}
			}
		}
	}
}

function createNews($id) /////// 更新商家新闻 2011-1-25
{
	global $DIRCMS,$db,$_userid,$_gradeid;
	$_userid=intval($_userid);
	$id=intval($id);
	if(!$_userid)return false;

	if($id)
	{
		$data=$db->fetch_one("SELECT * FROM ".DB_PRE."co_news WHERE id=".$id);
		@extract($data);unset($data);
	}
	
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息

	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);

	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > <a href="'.$DIRCMS['site_url'].'company/news_'.$_userid.'_1.html">'.$menu[3]['name'].'</a>'.' > '.$title;
	if($id)
	{
		// 更新内容
		ob_start();
		$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');
		if(in_array($_gradeid,$templatepriv))
		{
			include space_tlp($template,'shownews');
		}
		else
		{
			include template('company_shownews');
		}
		
		create_html(DIRCMS_ROOT.'company/shownews_'.$id.'.html');
	}
	else
	{
		// 更新列表
		$id=$_userid;
		$count=$db->fetch_one("SELECT count(*) as num FROM ".DB_PRE."co_news WHERE userid=".$id);
		$count=$count['num'];
		$pagecount=max(ceil($count/PAGESIZE),1);
		$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > '.$menu[3]['name'].'';
		for($i=1;$i<=$pagecount;$i++)
		{
			if($pagecount==1)
			{
				$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'.html">尾页</a> ';
			}
			else
			{
				$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'_'.max($i-1,1).'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'_'.min($pagecount,$i+1).'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/news_'.$id.'_'.$pagecount.'.html">尾页</a> ';
			}

			$page=intval($i)?max(1,intval($i)):1;
			ob_start();
			$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');

			if(in_array($_gradeid,$templatepriv))
			{
				include space_tlp($template,'news');
			}
			else
			{
				include template('company_news');
			}
			if($i==1)
			{
				create_html(DIRCMS_ROOT.'company/news_'.$_userid.'.html');
			}
			else
			{
				create_html(DIRCMS_ROOT.'company/news_'.$_userid.'_'.$i.'.html');
			}
		}
	}
}

function createCoupon($id) /////// 更新优惠券 2011-1-25
{
	global $DIRCMS,$db,$_userid,$_gradeid;
	$_userid=intval($_userid);
	$id=intval($id);
	if(!$_userid)return false;

	if($id)
	{
		$data=$db->fetch_one("SELECT * FROM ".DB_PRE."co_coupon WHERE id=".$id);
		@extract($data);unset($data);
	}
	
	include_once DIRCMS_ROOT.'include/member.class.php';
	$member=new member();
	$baseinfo=$member->get($_userid); // $baseinfo 数组 存储用户基本信息
	unset($baseinfo['password'],$baseinfo['id']);
	$model=cache_read('cache_membermodel'.$baseinfo['modelid'].'.cache.php',DIRCMS_ROOT.'data/cache_model/'); // 所在模型信息
	$moreinfo=$db->fetch_one("SELECT `".DB_PRE.$model['name']."`.* FROM `".DB_PRE.$model['name']."` WHERE `".DB_PRE.$model['name']."`.`userid`=$_userid"); // $moreinfo 数组 存储用户拓展信息

	$memberinfo=array_merge($baseinfo,$moreinfo); //合并数组 $memberinfo 保存 全部用户信息
	@extract($memberinfo);unset($memberinfo,$baseinfo,$moreinfo);
	
	$menu=cache_read('menu'.intval($_userid).'.cache.php',DIRCMS_ROOT.'company/cache/menu/');
	$menu=$menu?$menu:array (
	  1 => array('name'=>'网站首页','url'=>$DIRCMS['site_url'].'company/company_'.$_userid.'.html'),
	  2 => array('name'=>'公司简介','url'=>$DIRCMS['site_url'].'company/about_'.$_userid.'.html'),
	  3 => array('name'=>'新闻动态','url'=>$DIRCMS['site_url'].'company/news_'.$_userid.'.html'),
	  4 => array('name'=>'优惠信息','url'=>$DIRCMS['site_url'].'company/coupon_'.$_userid.'.html'),
	  5 => array('name'=>'产品展示','url'=>$DIRCMS['site_url'].'company/product_'.$_userid.'.html'),
	  6 => array('name'=>'地图位置','url'=>$DIRCMS['site_url'].'company/map_'.$_userid.'.html'),
	  7 => array('name'=>'联系方式','url'=>$DIRCMS['site_url'].'company/contact_'.$_userid.'.html'),
	  8 => array('name'=>'在线留言','url'=>$DIRCMS['site_url'].'company/guestbook_'.$_userid.'.html'),
	);

	$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > <a href="'.$DIRCMS['site_url'].'company/coupon_'.$_userid.'_1.html">'.$menu[4]['name'].'</a>'.' > '.$title;
	
	if($id)
	{
		// 更新内容
		ob_start();
		$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');
		if(in_array($_gradeid,$templatepriv))
		{
			include space_tlp($template,'showcoupon');
		}
		else
		{
			include template('company_showcoupon');
		}
		
		create_html(DIRCMS_ROOT.'company/showcoupon_'.$id.'.html');
	}
	else
	{
		// 更新列表
		$id=$_userid;
		$count=$db->fetch_one("SELECT count(*) as num FROM ".DB_PRE."co_coupon WHERE userid=".$id);
		$count=$count['num'];
		$pagecount=max(ceil($count/PAGESIZE),1);
		$dir_postion=$position='<a href="'.$DIRCMS['site_url'].'company/company_'.$_userid.'.html" title="店铺首页">店铺首页</a> > '.$menu[4]['name'].'';
		for($i=1;$i<=$pagecount;$i++)
		{
			
			if($pagecount==1)
			{
				$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'.html">尾页</a> ';
			}
			else
			{
				$co_dir_page='共'.$count.'条记录,当前第'.$i.'/'.$pagecount.'页 <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'.html">首页</a> <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'_'.max($i-1,1).'.html">上一页</a> <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'_'.min($pagecount,$i+1).'.html">下一页</a> <a href="'.$DIRCMS['site_url'].'company/coupon_'.$id.'_'.$pagecount.'.html">尾页</a> ';
			}

			$page=intval($i)?max(1,intval($i)):1;
			ob_start();
			$templatepriv=cache_read('templatepriv.cache.php',DIRCMS_ROOT.'company/cache/');
			if(in_array($_gradeid,$templatepriv))
			{
				include space_tlp($template,'coupon');
			}
			else
			{
				include template('company_coupon');
			}
			if($i==1)
			{
				create_html(DIRCMS_ROOT.'company/coupon_'.$_userid.'.html');
			}
			else
			{
				create_html(DIRCMS_ROOT.'company/coupon_'.$_userid.'_'.$i.'.html');
			}
		}
	}
}

function return_url($code,$is_api=0)
{
	global $DIRCMS;
	if($is_api)
	{
		return $DIRCMS['site_url'].'api/AutoReceive.'.$code.'.php';
	}
	else
	{
		return $DIRCMS['site_url'].'member/respond.php?code='.$code;
	}
}

function changeorder($sn)
{
	global $db;
    $sn = trim($sn);
    $row = $db->fetch_one("SELECT * FROM ".DB_PRE."pay_user_account WHERE `sn` = '$sn' AND `ispay` = '1'");
    if(!$row)
    {
	    if($db->query("UPDATE ".DB_PRE."pay_user_account SET `ispay` = '1', `paytime` =".TIME." WHERE `sn` = '$sn'"))
        {
            $r = $db->fetch_one("SELECT * FROM ".DB_PRE."pay_user_account WHERE `sn` = '$sn' AND `ispay` = '1'");
			$amount=floatval($r['quantity']);
			$userid=intval($r['userid']);
			if($db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`amount`=`".DB_PRE."member`.`amount`+{$amount} WHERE `".DB_PRE."member`.`id`={$userid}"))
			{
				return true;
			}
			else
			{	
				return false;
			}
        }
    }
    else
    {
        return false;
    }
}

function uc_call($function,$params=null)
{
    restore_error_handler();
    if(!function_exists($function))
    {
        include_once(DIRCMS_ROOT.'member/uc_client/client.php');
    }
    $rs = call_user_func_array($function,$params);
    set_error_handler('exception_handler');
    return $rs;
}
?>