<?php
/***********************************************************************************/
/* MassCRM客户关系管理系统							   */
/* ========================      						   */
/* 欢迎使用 MassCRM ,在您开始使用之前,请务必完整阅读本软件附带的全部说明档	   */
/***********************************************************************************/
/* Welcome to http://www.massboxes.com & http://masscrm.massboxes.com		   */
/***********************************************************************************/
	function checkRole($table_name,$module_num,$bu_auto_id,$role_name){
	 	global $smart;
	 	global $boxesDB;
	 			
	 	$boxesDB->setTableName("boxes_user");
		$sql="where bu_auto_id='".$bu_auto_id."'";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
		
		$bu_role=$boxes_check[0][bu_role];
		
		$boxesDB->setTableName("boxes_role");
		$sql="where br_name='".$bu_role."'";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
	 	
		$br_value=$boxes_check[0][br_value];
	 	
	 	$boxesDB->setTableName("boxes_function");
		$sql="where (bf_module_num='$module_num' and bf_ename='".$role_name."')";
		$boxesDB->setCondition($sql);
		$boxes_check=$boxesDB->selectResult();
	 	
		$bf_auto_id=$boxes_check[0][bf_auto_id];
		
		if(!$bu_role or !$br_value or ($bf_auto_id and substr($br_value,$bf_auto_id-1,1)!=1)){
			$error="请求失败：你没有模块“".$boxes_check[0][bf_module]."”“".$boxes_check[0][bf_name]."”的权限！";
			echo"<script>alert(\"$error\");self.close();</script>";
			exit;
		}
	}
	
	include "../account/".$_REQUEST[account].".boxes.inc.php";
	require_once "../includes/config.inc.php";
	require_once('../adodb/adodb.inc.php');
	require_once "../includes/BiaoCommon.class.php";
	$conn = &ADONewConnection('mysql');
	$conn->debug=false;
	$conn->PConnect($MYSQL_HOSTNAME, $MYSQL_USERNAME, $MYSQL_PASSWORD, $DATABASE);
	$boxesDB=new BiaoCommon($conn); 
	
	$table_name=$_REQUEST[table_name];
	$module_name=$_REQUEST[module_name];
	$module_num=$_REQUEST[module_num];
	$bu_auto_id=$_REQUEST[bu_auto_id];
	
	if(!$table_name){
		echo"<script>alert(\"参数传递不全！按确定退出！\");</script>";
		exit;
	}
	$boxesDB->setTableName("field_display_table");
	$sql="where table_name='".$table_name."'";
	$boxesDB->setCondition($sql);
	$boxes_check=$boxesDB->selectResult();
	if(!count($boxes_check)){
		echo"<script>alert(\"系统设置出问题啦！\");</script>";
		exit;
	}
	$prefix=$boxes_check[0][prefix];
	
	$auto_id=$prefix."auto_id";
	$title=$table_name."_title";
	$title=$$title;
/*############################################################*/	
	$sort_by_field	=	$_REQUEST[sort_by_field];
	$sort_order		=	$_REQUEST[sort_order];
	if(!$sort_order){
		$sort_order="desc";
	}
		
	if(!$sort_by_field)	
		$index="order by $auto_id ".$sort_order;
	else{ 
		$index="order by $sort_by_field ".$sort_order;
	}
/*############################################################*/
	
	$operator=$_REQUEST["operator"];
	
	checkRole($table_name,$module_num,$bu_auto_id,"EXPORT");
	
	$boxesDB->setTableName("field_display_table");
	$sql="where (table_name='".$table_name."' and search_is_display='1') order by search_display_number asc";
	$boxesDB->setCondition($sql);
	$boxes_check=$boxesDB->selectResult();
	$id=$boxes_check[0][prefix]."id";
	for($i=0;$i<count($boxes_check);$i++){
		$row=$boxes_check[$i];
		$field_name="search_".$row[field_name];
		if($_REQUEST[$field_name]){
			$condition.="$row[field_name] like '%".$_REQUEST[$field_name]."%' and ";//全部都是模糊查找
		}
	}

	if($condition){//去掉最后一个and	
		$condition=substr($condition,0,strlen($condition)-4);
		$condition="where ".$condition." ";
	}
	
	$condition.=$index;	
	
	$boxesDB->setTableName($table_name);
	$boxesDB->setCondition($condition);
	$boxes_result=$boxesDB->selectResult();
	
	if(count($boxes_result)>$PRINT_MAX){
		echo"<script>alert('记录太多了，已经超过了设置最大记录 $PRINT_MAX 条！');self.close();</script>";
		exit;
	}
	
	$boxesDB->setTableName("field_display_table");
	$boxesDB->setSelectField("*");
	$sql="where (table_name='$table_name' and print_list_is_display='1') order by print_list_display_number asc";
	$boxesDB->setCondition($sql);
	$boxes_top=$boxesDB->selectResult();
	
	header("Content-Type: application/vnd.ms-excel"); 
	header("Content-Disposition: attachment; filename=$title.xls"); 
	header("Pragma: no-cache"); 
	header("Expires: 0");	
?>
	<style type="text/css">
	<!--
	.style1 {
		font-size: 18px;
		font-family: "黑体";
	}
	-->
	</style>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	<table width='640'  border='1' cellspacing='0' cellpadding='0'>
	  <tr>
		<td height='30' colspan='<?echo count($boxes_top);?>'><div align='center' class="style1"><? echo"$title";?></div></td>
	  </tr>
		<tr height=25>
		<?
		for($i=0;$i<count($boxes_top);$i++){
	 		$row=$boxes_top[$i];
	 	?>
	 	<td><div align="center"><strong><?echo"$row[display_name]";?></strong></div></td>
	 	<?	
		}	 	
	 	?>
	  </tr>
	 <?
	 for($i=0;$i<count($boxes_result);$i++){
	 	$row=$boxes_result[$i];
	 ?>
	  <tr height=25>
	  	<?
		for($j=0;$j<count($boxes_top);$j++){
			$row_field=$boxes_top[$j];
	  	?>
	  	<td><div align="center"><? echo $row[$row_field[field_name]] ;?></div></td>
	  	<?
		}
		?>
	  </tr>
	 <?
	  }
	 ?>
	</table>
