<?php
/*
 *Coupright B2Bbuilder
 *Powered by http://www.b2b-builder.com
 *Auther:Brad zhang
 *Des: company cat
 *Date:2008-11-14
 */
include_once("../includes/global.php");
$script_tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$sctiptName = array_pop($script_tmp);
include_once("auth.php");
//=======================================
$add_type=empty($_GET['add_type'])?1:$_GET['add_type'];
if(!empty($_POST["Submit"])&&empty($_GET["edit"])&&$add_type==2)
{
	$p=$_POST;
	include('../lib/allchar.php');
	
	if(!empty($p['province']))
	{
		$sql="select a.city_id,a.city from ".CITY." a,".PROVINCE." b WHERE a.province_id=b.province_id and b.province='$p[province]'";
		$db->query($sql);
		$re=$db->getRows();
		foreach($re as $v)
		{
			$v['web_title']=str_replace('keyword',$v['city'],$p['web_title']);
			$v['web_keyword']=str_replace('keyword',$v['city'],$p['web_keyword']);
			$v['web_des']=str_replace('keyword',$v['city'],$p['web_des']);
			$v['copyright']=str_replace('keyword',$v['city'],$p['copyright']);
			$v['des']=str_replace('keyword',$v['city'],$p['des']);
			$domain=c($v['city']);
			
			$sql="insert into ".DOMAIN." 
			(`dtype`,`domain`,`con`,`con2`,`des`,web_title,web_keyword,web_des,copyright,template) 
			values 
			('1','$domain','$p[province]','$v[city]','$v[des]','$v[web_title]','$v[web_keyword]','$v[web_des]','$v[copyright]','$p[template]')";
			$re=$db->query($sql);
		}
	}
	else
	{
		$pc=getCity();
		foreach($pc as $v)
		{
			$v['web_title']=str_replace('keyword',$v['province'],$p['web_title']);
			$v['web_keyword']=str_replace('keyword',$v['province'],$p['web_keyword']);
			$v['web_des']=str_replace('keyword',$v['province'],$p['web_des']);
			$v['copyright']=str_replace('keyword',$v['province'],$p['copyright']);
			$v['des']=str_replace('keyword',$v['province'],$p['des']);
			$domain=c($v['province']);
			
			$sql="insert into ".DOMAIN." 
			(`dtype`,`domain`,`con`,`des`,web_title,web_keyword,web_des,copyright,template) 
			values 
			('1','$domain','$v[province]','$v[des]','$v[web_title]','$v[web_keyword]','$v[web_des]','$v[copyright]','$p[template]')";
			$re=$db->query($sql);
			
			foreach($v['city'] as $sv)
			{
				$sv['web_title']=str_replace('keyword',$sv['city'],$p['web_title']);
				$sv['web_keyword']=str_replace('keyword',$sv['city'],$p['web_keyword']);
				$sv['web_des']=str_replace('keyword',$sv['city'],$p['web_des']);
				$sv['copyright']=str_replace('keyword',$sv['city'],$p['copyright']);
				$sv['des']=str_replace('keyword',$sv['city'],$p['des']);
				$domain=c($sv['city']);
				
				$sql="insert into ".DOMAIN." 
				(`dtype`,`domain`,`con`,`con2`,`des`,web_title,web_keyword,web_des,copyright,template) 
				values 
				('1','$domain','$v[province]','$sv[city]','$sv[des]','$sv[web_title]','$sv[web_keyword]','$sv[web_des]','$sv[copyright]','$p[template]')";
				$re=$db->query($sql);
			}
		}
	}
	

	if($re)
		msg("sub_domain_list.php");
}
if(!empty($_POST["Submit"])&&empty($_GET["edit"])&&$add_type==1)
{
	$p=$_POST;
	$sql="insert into ".DOMAIN." 
	(`dtype`,`domain`,`con`,`con2`,`des`,logo,web_title,web_keyword,web_des,copyright,template) 
	values 
	('1','$p[domain]','$p[province]','$p[city]','$p[des]','$p[logo]','$p[web_title]','$p[web_keyword]','$p[web_des]','$p[copyright]','$p[template]')";
	$re=$db->query($sql);
	if($re)
		msg("sub_domain_list.php");
}
if(!empty($_POST["Submit"])&&!empty($_GET["edit"]))
{
	$p=$_POST;
	$p["isopen"]=$p["isopen"]?1:0;
	$sql="update ".DOMAIN." set `domain`='$p[domain]',`con`='$p[province]',`con2`='$p[city]',`des`='$p[des]',`isopen`='$p[isopen]',web_title='$p[web_title]',web_keyword='$p[web_keyword]',web_des='$p[web_des]',copyright='$p[copyright]',template='$p[template]',logo='$p[logo]' where id='$_GET[edit]'";
	$re=$db->query($sql);
	if($re)
		msg("sub_domain_list.php");
}
if(!empty($_GET["edit"]))
{
	$sql="select * from ".DOMAIN." where id='$_GET[edit]'";
	$db->query($sql);
	$de=$db->fetchRow();
}
//=========================================
function read_dir($dir)
{
	$i=0;
	$handle = opendir($dir); 
	$rdir=array();
	while ($filename = readdir($handle))
	{ 
		if($filename!="."&&$filename!="..")
		{
		  if(is_dir($dir.$filename))
		  { 
		  	 if(substr($filename,0,5)!='user_'&&substr($filename,0,8)!='special_'&&substr($filename,0,5)!='email')
		   	 	$rdir[]=$filename;
		  }
	   }
	}
	return $rdir;
}
?>
<!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>
<TITLE><?php echo lang_show('admin_system');?></TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<link href="main.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<script src="../script/my_lightbox.js" language="javascript"></script>
<script type="text/javascript" src="../script/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
function getHTML(v,type)
{	
	var url = '../ajax_back_end.php';
	var sj = Math.random();
	if(type==1)
		var pars = 'shuiji=' + sj+'&prov_id='+v;
	if(type==2)
		var pars = 'shuiji=' + sj+'&catid='+v;
	$.post(url, pars,showResponse);
	function showResponse(originalRequest)
	{
		if(type==1)
			var ob="city";
		else
			var ob="subcat";
		var tempStr = 'var MyMe = ' + originalRequest; 
		var i=0;var j=0;
		eval(tempStr);
		for(var s in MyMe)
		{
			++j;
		}
		document.getElementById(ob).options.length =j+1;
		for(var k in MyMe)
		{
			var cityId=MyMe[k][0];
			var ciytName=MyMe[k][1];
			i++;
			document.getElementById(ob).options[i].value = cityId;
			document.getElementById(ob).options[i].text = ciytName;
	　	}	
	 }
　}
</script>
<div class="bigbox">
	<div class="bigboxhead tab" style=" width:90%">
		<span class="cbox"><a href="sub_domain_list.php"><?php echo lang_show('man_sub_station');?></a></span>
		<span class="cbox on"><a href=""><?php echo lang_show('add_type');?></a></span>
	</div>

	<div class="bigboxbody" style="margin-top:-1px;">
<form method="post" action="">
<?php if(!empty($config['baseurl'])){?>
<table width="100%" align="center" cellpadding="4" cellspacing="0" >
  <?php if(empty($_GET['editid'])){ ?>
  <tr>
    <td width="13%" align="right"  class="body_left">&nbsp;</td>
    <td>
      <label>
        <input onClick="window.location='add_sub_domain.php?add_type=1';" checked="checked" type="radio" class="radio" name="add_type" value="1">
        单个添加
        </label>　
      
      <label>
        <input onClick="window.location='add_sub_domain.php?add_type=2';" <?php if($_GET['add_type']==2) echo 'checked="checked"';?> type="radio" class="radio" name="add_type" value="2">
        批量生成
        </label>
    </td>
    </tr>
  <?php } ?>
  <tr>
      <td class="body_left" align="left" >省份城市</td>
      <td><label>
        <select name="province" id="province" onChange="getHTML(this.value,1)" class="lselect">
          <option value="">所有省份</option>
          <?php
		echo get_province($de['con']);
		?>
          </select>
        </label>
        
        --
        
        <select name="city" id="city" class="rselect">
          <option value=""><?php echo lang_show('all_city');?></option>
          <?php
	  if(!empty($de["con2"]))
	  {
	  ?>
          <option value="<?php echo $de["con2"];?>" selected="selected"><?php echo $de["con2"];?></option>
          <?php } ?> 
          </select>        <span class="bz"><?php echo lang_show('prov_des');?></span></td>
      </tr>
    <tr>
      <td align="left" >分站指定模板</td>
      <td>
        <select name="template" id="template" class="select">
          <?php
		  $dir=read_dir('../templates/');
		  foreach($dir as $v)
		  {
		  	if(substr($v,0,1)!='.')
			{
				if($v==$de['template'])
					$sl="selected";
				else
					$sl=NULL;
				echo "<option value='$v' $sl>$v</option>";
			}
		  }
		  ?></select>		  </td>
      </tr>
	<?php if($_GET['add_type']!=2){ ?>
      <tr>
    <td align="left"  ><?php echo lang_show('sub_domain');?></td>
    <td>
      http://<input value="<?php echo $de['domain'];?>" name="domain" type="text" id="domain" size="10" maxlength="10">.<?php echo $config['baseurl'];?><span class="bz"><?php echo lang_show('domain_des');?></span></td>
    </tr>
	 
      <tr>
        <td align="left">LOGO</td>
        <td><input class="text" value="<?php echo $de['logo'];?>" id="logo" name="logo" type="text">
          [<a href="javascript:uploadfile('<?php echo lang_show('upload_logo');?>','logo',180,60)"><?php echo lang_show('upload');?></a>] 
          [<a href="javascript:preview('logo');"><?php echo lang_show('review');?></a>]
          [<a onclick="javascript:$('#logo').val('');" href="#"><?php echo lang_show('del');?></a>]</td>
        </tr> 
	  <?php } ?>
      <tr>
      <td align="left"><?php echo lang_show('substationtitle');?></td>
      <td><input class="text" value="<?php echo $de['web_title'];?>" type="text" name="web_title" id="web_title">     <?php if($_GET['add_type']==2){?>
        <?php echo lang_show('provinces_key');?>  <?php } ?></td>
      </tr>
    <tr>
      <td align="left" ><?php echo lang_show('substationkey');?></td>
      <td><input value="<?php echo $de['web_keyword'];?>" class="text" type="text" name="web_keyword" id="web_keyword">
        <?php if($_GET['add_type']==2){?>
        <?php echo lang_show('provinces_key');?>
        <?php } ?></td>
      </tr>
    <tr>
      <td align="left" ><?php echo lang_show('substationdes');?></td>
      <td><input value="<?php echo $de['web_des'];?>" class="text" type="text" name="web_des" id="web_des">
        <?php if($_GET['add_type']==2){?>
        <?php echo lang_show('provinces_key');?> 
        <?php } ?></td>
      </tr>
      <tr>
    <td align="left" ><?php echo lang_show('des');?></td>
    <td class="bz" >
      <textarea name="des" class="text" style="height:100px;"><?php echo $de['des'];?></textarea>
      <?php if($_GET['add_type']==2){?>
      <?php echo lang_show('provinces_key');?>
      <?php } ?></td>
  </tr>
      <tr>
        <td align="left" >Copyright</td>
        <td class="bz" ><textarea name="copyright" class="text" style="height:100px;"><?php echo $de['copyright'];?></textarea>
          <?php if($_GET['add_type']==2){?>
          <?php echo lang_show('provinces_key');?>
          <?php } ?></td>
      </tr>
  <?php if(!empty($_GET['edit'])){ ?>
   <tr>
    <td align="left" ><?php echo lang_show('is_open');?></td>
    <td><input name="isopen" type="checkbox" class="checkbox" id="isopen" value="1" <?php if($de['isopen'])echo "checked";?>></td>
  </tr>
  <?php } ?>
  <tr>
    <td align="left">&nbsp;</td>
    <td >
      <input class="btn" type="submit" name="Submit" value="<?php echo lang_show('submit');?>">   </td>
  </tr>
</table>
</form>
<?php
}
else
{
	admin_msg('system_config.php','请先设置网站基本路径');
}
?>
</div>
</div>

</BODY>
</HTML>
