<?php
/**
*@Purpose:	
*		1. Search header.
*		2. "Previous" and "next" button.
*		3. total CDRs statistics.
*File name: cdrHeader.php
*Author: Li Long
*Create: No20040316-1
*Modifications:
*No.1
*Name:Cui Ticun
*Date:2004-5-27
*Description:Support SMLog
*/
//=============================================================================
if($DEBUG)
{
	echo "<BR>***********************************************************<BR>";
	echo "serverName=$serverName, sysId=$sysId,cdrType=$cdrType<BR>";
	echo "queryField=$queryField<BR>";
	echo "keyWord=$keyWord<BR>";
	echo "preTbNum=$preTbNum, preDispNum=$preDispNum<BR>";
	echo "cntTbNum=$cntTbNum, cntDispNum=$cntDispNum<BR>";
	echo "nextTbNum=$nextTbNum, nextDispNum=$nextDispNum<BR>";
	echo "***************************************************************<BR>";
}

global $OMC_server;
$dbName	= "CDR_DB";
$cdrDbConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], $dbName);

$dispNumOfPerPage = 30;
$sortFld = isset($sortFld)?$sortFld:"releaseTime DESC ";
if(!isset($cntTbNum))		$cntTbNum	= 0;
if(!isset($cntDispNum))	 	$cntDispNum	= 0;

if(isset($pageNext)){
	if($DEBUG)	echo "NEXT<BR>";
	if(isset($nextTbNum)){
		$cntTbNum		= intval($nextTbNum);
		$cntTbNum_1	= intval($nextTbNum);
	}else{
		$nextTbNum	= 0;
	}
	if(isset($nextDispNum))
		$cntDispNum	= intval($nextDispNum);
	else
		$nextDispNum	= 0;
}
if(isset($pagePrevious)){
	if($DEBUG)	echo "PREVIOUS<BR>";
	if(isset($preTbNum)){
		$cntTbNum		= intval($preTbNum);
		$cntTbNum_1	= intval($preTbNum);
	}else
		$preTbNum		= $cntTbNum;
	if(isset($preDispNum))
		$cntDispNum	= intval($preDispNum);
	else
		$preDispNum	= $cntDispNum;
}
if($DEBUG){ 
	echo "cntTbNum=$cntTbNum<BR>";
	echo "cntDispNum=$cntDispNum<BR>";
}

$rlsTb="cdrReleaseConf";
$srcTb="cdrServiceConf";
$typeTb="cdrTypeConf";

if($DEBUG){
	$currentReleaseTime	= date("Y-m-d H:i:s");
	echo "cntDate=$cntDate, currentReleaseTime=$currentReleaseTime<BR>";
}
for($i=0;$i<24;$i++){
	$beginTime[$i]	= date("y-m-d H:i:s",mktime(date("H")-$i,0,0,date("m"),date("d"),date("Y")));
	$endTime[$i]	= date("y-m-d H:i:s",mktime(date("H")-$i,59,59,date("m"),date("d"),date("Y")));
	$selHour[$i]	= date("H",mktime(date("H")-$i,0,0,date("m"),date("d"),date("Y")));
	$tbName[$i]		= "cdrFrom".$serverName."_".$selHour[$i];
	if($DEBUG)	echo "beginTime[$i]=$beginTime[$i],	endTime[$i]=$endTime[$i],	 tbName[$i]=$tbName[$i]<BR>";
}

/*************   Get the CDR count of passed hours ***************************/

if(!isset($keyWord)){

	for($i=0;$i<24;$i++){
		$recordCount[$i]	 = 0;
	}
	for($i=0;$i<=date("G");$i++){
		$hour		= date("H",mktime($i,0,0,date("m"),date("d"),date("Y")));
		$tmpTbName	= "cdrFrom".$serverName."_".$hour;
/****************changed for SMLog by Cui Ticun start*************************/
		if($cdrType==18||$cdrType==19||$cdrType==20||$cdrType==21){ 		
			$selSql		= "SELECT count(*) FROM $tmpTbName WHERE recordType=$cdrType";
		}else{
			/*
			if($sysId == 320)
			{
				echo "mscType=**$mscType**<br>";
				$mscType=trim($mscType);
				if($mscType == "call")
				{
					$selSql.="SELECT count(*) FROM $tmpTbName WHERE (recordType>=0 AND recordType<=5) ";
				}
				else if($mscType == "sm")
				{
					$selSql.= "SELECT count(*) FROM $tmpTbName WHERE (recordType>=6 AND recordType<=7) ";
				}
				else
				{	*/
					$selSql		= "SELECT count(*) FROM $tmpTbName";
				//}	
			//}
			
		}
/****************changed for SMLog by Cui Ticun end*************************/
		if($DEBUG) 
		echo "<BR>$selSql<BR>";
		$selRes = @mysqli_query($cdrDbConn,$selSql);
		$selRow = @mysqli_fetch_array($selRes);
		//echo mysqli_error($pubConn);
		if($selRow[0]=="")
			$recordCount[$i]	 = 0;
		else
			$recordCount[$i]	 = $selRow[0];
		if($DEBUG)	echo "recordCount[$i]	 = $recordCount[$i]<BR>";
	}
}
/***********   Get the CDR count of passed hours end.        *****************/

/********  Get the next table number and next display number start  **********/

if($DEBUG){
	echo "<BR>************ Next ***************<BR>";
	echo "tbName[$cntTbNum]=$tbName[$cntTbNum]<BR>";
	echo "cntTbNum=$cntTbNum,cntDispNum=$cntDispNum<BR>";
}
$displayNum = 0;
$nextTbNum	= $cntTbNum;


while((($displayNum-$cntDispNum)<$dispNumOfPerPage)&&($nextTbNum<24)){
/****************changed for SMLog by Cui Ticun start*************************/
	
	if($cdrType==18||$cdrType==19||$cdrType==20||$cdrType==21){ 
		if(isset($keyWord)){
			$WHERE		= " $queryField like '%$keyWord%'";
			$WHERE		= "WHERE ".$WHERE." AND recordType=$cdrType";
			$selSql			= "SELECT count(*) FROM $tbName[$nextTbNum] $WHERE";
		}else{
			$selSql		= "SELECT count(*) FROM $tbName[$nextTbNum] WHERE recordType=$cdrType";
		}
	}else{
		if(isset($keyWord)){
			if($queryField=="callingNumber")
				$rrType=6;
			else
				$rrType=7;
			
				if($sysId == 400){
					$WHERE		= " $queryField like '%$keyWord%' or (recordType=$rrType and MSISDN like '%$keyWord%') ";
				}else{
					$WHERE		= " $queryField like '%$keyWord%' or (recordType=$rrType and servedMSISDN like '%$keyWord%') ";
				}

			$WHERE		= "WHERE ".$WHERE;
			$selSql			= "SELECT count(*) FROM $tbName[$nextTbNum] $WHERE ";
		}else{
			$selSql		= "SELECT count(*) FROM $tbName[$nextTbNum]";
		}
		//echo "sysId=$sysId,mscType=**$mscType**<br>";
		if($sysId == 320)
		{
			
			if($mscType == "call")
			{
				//$selSql.=" AND (recordType>=0 AND recordType<=5) ";	
				if(isset($keyWord)){
					if($queryField=="callingNumber")
						$rrType=6;
					else
						$rrType=7;
					$WHERE		= " ($queryField like '%$keyWord%' or (recordType=$rrType and servedMSISDN like '%$keyWord%')) AND (recordType>=0 AND recordType<=5) ";
					$WHERE		= "WHERE ".$WHERE;
					$selSql			= "SELECT count(*) FROM $tbName[$nextTbNum] $WHERE ";
				}else{
					$selSql		= "SELECT count(*) FROM $tbName[$nextTbNum] WHERE recordType>=0 AND recordType<=5 ";
				}
			}
			else if($mscType == "sm")
			{
				//$selSql.=" AND (recordType>=6 AND recordType<=7) ";
				if(isset($keyWord)){
					if($queryField=="callingNumber")
						$rrType=6;
					else
						$rrType=7;
					$WHERE		= " ($queryField like '%$keyWord%' or (recordType=$rrType and servedMSISDN like '%$keyWord%')) AND (recordType>=6 AND recordType<=7) ";
					$WHERE		= "WHERE ".$WHERE;
					$selSql			= "SELECT count(*) FROM $tbName[$nextTbNum] $WHERE ";
				}else{
					$selSql		= "SELECT count(*) FROM $tbName[$nextTbNum] WHERE recordType>=6 AND recordType<=7 ";
				}
			}
			else
			{	
				if(isset($keyWord)){
					if($queryField=="callingNumber")
						$rrType=6;
					else
						$rrType=7;
					$WHERE		= " $queryField like '%$keyWord%' or (recordType=$rrType and serviceCentre  like '%$keyWord%') ";
					$WHERE		= "WHERE ".$WHERE;
					$selSql			= "SELECT count(*) FROM $tbName[$nextTbNum] $WHERE ";
				}else{
					$selSql		= "SELECT count(*) FROM $tbName[$nextTbNum]";
				}
			}	
		}
	}
/****************changed for SMLog by Cui Ticun end**************************/
if($sysId==375)
	$selSql=" SELECT count(*) FROM $tbName[$nextTbNum] WHERE msisdn like '%$keyWord%' ";
if($sysId==386)
	$selSql=" SELECT count(*) FROM $tbName[$nextTbNum] WHERE nodeName like '%$keyWord%' ";
if($sysId==380)
{
	if($eirType == "imei")
		$selSql=" SELECT count(*) FROM $tbName[$nextTbNum] WHERE servedIMEI like '%$keyWord%' ";
	else
		$selSql=" SELECT count(*) FROM $tbName[$nextTbNum] WHERE servedIMSI like '%$keyWord%' ";
}
//echo "selSql=$selSql<br>";
if($DEBUG)	{	echo "selSql=$selSql<br>";}
	$selRes = @mysqli_query($cdrDbConn,$selSql);
	$selRow = @mysqli_fetch_array($selRes);
	//echo mysqli_error($cdrDbConn);
	if($selRow[0]=="")
		$recordNum[$nextTbNum]	= 0;
	else
		$recordNum[$nextTbNum]	= $selRow[0];
	if($DEBUG)	echo "recordNum[$nextTbNum]=$recordNum[$nextTbNum]<BR>";
	$displayNum += $selRow[0];
	//echo "dispNumOfPerPage=$dispNumOfPerPage;displayNum=$displayNum<BR>";
	if(($cntDispNum+$dispNumOfPerPage)>$displayNum){
		$nextTbNum++;
	}
	if($DEBUG)	echo "displayNum = $displayNum, nextTbNum=$nextTbNum<BR>";
}

if(($nextTbNum==24||$displayNum<$dispNumOfPerPage))
	unset($nextFlag);
else
	$nextFlag	= 1;

/********************changed for SMLog by Cui Ticun start**********************/
/********************deal with when $displayNum==$dispNumOfPerPage*************/

if($displayNum==$dispNumOfPerPage&&$nextTbNum!=24){
	
	$tmpTbNum=$nextTbNum+1;
	do{
		if($cdrType==18||$cdrType==19||$cdrType==20||$cdrType==21){ 
			if(isset($keyWord)){
				$WHERE		= " $queryField like '%$keyWord%'";
				$WHERE		= "WHERE ".$WHERE." AND recordType=$cdrType";
				$selSql			= "SELECT count(*) FROM $tbName[$tmpTbNum] $WHERE";
			}else{
				$selSql		= "SELECT count(*) FROM $tbName[$tmpTbNum] WHERE recordType=$cdrType";
			}
		}else{
			if(isset($keyWord)){
				$WHERE		= " $queryField like '%$keyWord%'";
				$WHERE		= "WHERE ".$WHERE;
				$selSql			= "SELECT count(*) FROM $tbName[$tmpTbNum] $WHERE ";
			}else{
				$selSql		= "SELECT count(*) FROM $tbName[$tmpTbNum]";
			}
		}
		if($DEBUG)echo "selSql=$selSql,		";
		$selRes = @mysqli_query($cdrDbConn,$selSql);
		$selRow = @mysqli_fetch_array($selRes);
//		echo mysqli_error($pubConn);
		if($selRow[0]=="")
			$selRow[0]	= 0;		
		$tmpTbNum++;
	}while(!$selRow[0]&&$tmpTbNum<24);
}
if($tmpTbNum==24)
	unset($nextFlag);
/********************changed for SMLog by Cui Ticun end**********************/

if($DEBUG){
	echo "==========================<BR>";
	echo "recordNum[$nextTbNum]=$recordNum[$nextTbNum]<BR>";
	echo "cntDispNum=$cntDispNum<BR>";
	echo "displayNum=$displayNum<BR>";
	echo "==========================<BR>";
}

if($nextTbNum == $cntTbNum){
	//echo "IF<BR>";
	$nextDispNum	= $cntDispNum+$dispNumOfPerPage;
}else{
	//echo "ELSE<BR>";
	$nextDispNum	= $cntDispNum+$dispNumOfPerPage+$recordNum[$nextTbNum]-$displayNum;
}
//if(($recordNum[$nextTbNum]-$cntDispNum)>$dispNumOfPerPage){
if($DEBUG){
	echo "nextTbNum=$nextTbNum, nextDispNum=$nextDispNum<BR>";
	echo "********************************<BR>";
}

if($displayNum==0){
	echo "No record.<BR>";
	exit();
}
/***********   Get the next table number and display number end.    **********/

/********   Get the previous table number and display number start	  ********/
if($DEBUG){
	echo "<BR>************ Previous ***************<BR>";
	echo "tbName[$cntTbNum]=$tbName[$cntTbNum]<BR>";
	echo "cntTbNum=$cntTbNum, cntDispNum=$cntDispNum<BR>";
}
if(($cntTbNum!=0)||($cntDispNum!=0)){
	$previousFlag=1;
}
if(isset($previousFlag)){
	$preTbNum		= $cntTbNum;
	if($cntDispNum >= $dispNumOfPerPage){
		$preDispNum	= $cntDispNum - $dispNumOfPerPage;
	}else{
		$leftDisplay	= $dispNumOfPerPage - $cntDispNum;
		if($preTbNum==0)	$leftDisplay = 0;
		while(($leftDisplay>0)&&($preTbNum>0)){
			$preTbNum	= $preTbNum-1;
/****************changed for SMLog by Cui Ticun start**************************/
			if($cdrType==18||$cdrType==19||$cdrType==20||$cdrType==21){ 
				if(isset($keyWord)){
					$WHERE		= " $queryField like '%$keyWord%'";
					$WHERE		= "WHERE ".$WHERE." AND recordType=$cdrType";
					$selSql			= "SELECT count(*) FROM $tbName[$preTbNum] $WHERE";
				}else{
					$selSql		= "SELECT count(*) FROM $tbName[$preTbNum] WHERE recordType=$cdrType";
				}
			}else{
				if(isset($keyWord)){
					$WHERE		= " $queryField like '%$keyWord%'";
					$WHERE		= "WHERE ".$WHERE;
					$selSql			= "SELECT count(*) FROM $tbName[$preTbNum] $WHERE";
				}else{
					$selSql		= "SELECT count(*) FROM $tbName[$preTbNum]";
				}
			}
/****************changed for SMLog by Cui Ticun end**************************/
			if($DEBUG)	echo "selSql=$selSql<BR>";
			$selRes = @mysqli_query($cdrDbConn,$selSql);
			$selRow = @mysqli_fetch_array($selRes);
			if($selRow[0]=="")
				$selRow[0]  = 0;			
			$leftDisplay	= $leftDisplay - $selRow[0];
			//echo mysqli_error($pubConn);
		}
		if( $leftDisplay!=$dispNumOfPerPage ){
			$preDispNum	= abs($leftDisplay);
		}else{
			$preDispNum	= 0;
		}
	}
}
if($DEBUG){
	echo "preTbNum=$preTbNum,preDispNum=$preDispNum<BR>";
	echo "********************************<BR>";
}
/********   Get the previous table number and display number end.    *********/
?>

<!--------------------TABLE START::   Display CDR count ---------------------->
<?php
if(!isset($keyWord)){
	$cntDate		= date("Y-m-d");
	echo "$cntDate:<br>";
	if($cdrType!=20){		
		echo "<TABLE border=1 width=100% cellpadding=2 cellspacing=0 bordercolor=#666666 bordercolordark=#FFFFFF bgcolor=#FFFFFF>";
		echo "<TR bgcolor=\"#E6E6E6\" align=\"center\">";
		echo "<td>Period</td>";
		$field	= "queryField=$queryField";
		$sys		= "serverName=$serverName&&sysId=$sysId&&cdrType=$cdrType";
		for($i=0;$i<12;$i++){
			if($recordCount[$i] > 0){
				$tmpTbNum	= date("G")-$i;
				$cnt	= "cntTbNum=$tmpTbNum&&cntTbNum_1=$tmpTbNum&&cntDispNum=0";
				$cntPageLink	= $field."&&".$sys."&&".$cnt;
				if($DEBUG)	echo "cntPageLink=$cntPageLink<BR>";
				echo "<td><a href=\"$PHP_SELF?$cntPageLink\">$i</a></td>";
			}else{
				echo "<td>$i</td>";
			}
		}
		echo "</TR>";

		echo "<TR align=\"center\">";
		echo "<TD>Total</TD>";
		for($i=0;$i<12;$i++){
			echo "<TD>$recordCount[$i]</TD>";
		}
		echo "</TR>";
		
		
		echo "<TR bgcolor=\"#E6E6E6\" align=\"center\">";
		echo "<td>Period</td>";
		for($i=12;$i<24;$i++){
			if($recordCount[$i] > 0){
				$tmpTbNum	= date("G")-$i;
				$cnt	= "cntTbNum=$tmpTbNum&&cntTbNum_1=$tmpTbNum&&cntDispNum=0";
				$cntPageLink	= $field."&&".$sys."&&".$cnt;
				if($DEBUG)	echo "cntPageLink=$cntPageLink<BR>";
				echo "<td><a href=\"$PHP_SELF?$cntPageLink\">$i</a></td>";
			}else{
				echo "<td>$i</td>";
			}
		}
		echo "</TR>";

		echo "<TR align=\"center\">";
		echo "<TD>Total</TD>";
		for($i=12;$i<24;$i++){
			echo "<TD>$recordCount[$i]</TD>";
		}
		echo "</TR>";
		
		
		
		echo "</TABLE>";
	}
}
?>
<!--------------------TABLE END::   Display CDR count ------------------------>

<!-----------TABLE START::   Display "Previous" and "Next" button ------------>
<TABLE border="0" width="100%" cellpadding="2" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF">      
  <TR >     
	<TD align=left>
		Period: 
		<?php
			$periodStartArray	= explode("_",$tbName[$cntTbNum]);
			$periodStart	= $periodStartArray[1];
			$periodEndArray	= explode("_",$tbName[$nextTbNum]);
			$periodEnd		= $periodEndArray[1];
			if(!isset($periodEnd)){
				$periodEnd	= $periodStart;
			}
			echo "$periodStart - $periodEnd"; 
		?>
	</TD>
    <TD align="right">
		<?PHP
			// NEW: Display a link for the previous set of records.  
			$next	 = " nextTbNum=$nextTbNum && nextDispNum=$nextDispNum ";
			$previous	= " preTbNum=$preTbNum && preDispNum=$preDispNum ";
			$flag		= " pagePrevious=1 ";
			$field	= " queryField=$queryField ";
			$sys		= " serverName=$serverName&&sysId=$sysId&&cdrType=$cdrType ";
			$key		= " keyWord=$keyWord ";
			$msc_type= " mscType=$mscType ";
			if(isset($keyWord)){
				$previousPageLink = $previous." && ".$next." && ".$flag." && ".$field." && ".$sys." && ".$msc_type." && ".$key;
			}else{
				$previousPageLink = $previous." && ".$next." && ".$flag." && ".$field." && ".$sys." && ".$msc_type;
			}
			if($DEBUG)	echo "¡¾PREVIOUS¡¿$previousPageLink";
			if (isset($previousFlag)) {  
				echo "<a href=\"$PHP_SELF?$previousPageLink\">"; 
				echo "<IMG alt=$strPrevious src=\"../../images/pre_page.gif\" border=\"0\"></a>";
			}
			/*
			else{
				echo "<font color=\"#808080\"><IMG alt=$strPrevious src=\"../../images/pre_page.gif\" border=\"0\"></font>";
			} 
			*/

			// NEW: Display a link for the next set of records.  
			$flag		= " pageNext=1 ";
			if(isset($keyWord))
				$nextPageLink = $previous." && ".$next." && ".$flag." && ".$field." && ".$sys." && ".$msc_type." && ".$key;
			else
				$nextPageLink = $previous." && ".$next." && ".$flag." && ".$field." && ".$sys." && ".$msc_type;
			if($DEBUG)	echo "¡¾NEXT¡¿$nextPageLink";
			if (isset($nextFlag)){  
				echo "<a href=\"$PHP_SELF?$nextPageLink\">";  
				//echo "&nbsp;>>&nbsp;$strNext</a>";  
				echo "<IMG alt=$strNext src=\"../../images/next_page.gif\" border=\"0\"></a>";
			}
			/*
			else{
				//echo "<font color=\"#808080\">&nbsp;>>&nbsp;$strNext</font>";  
				echo "<font color=\"#808080\"><IMG alt=$strNext src=\"../../images/next_page.gif\" border=\"0\"></font>";
			} 
			*/
			if ( (!isset($previousFlag)) && (!isset($nextFlag)) ){
				echo "<BR>";
			}

		?>
	<TD>
  <TR>
</TABLE>
<!------------TABLE END::   Display "Previous" and "Next" button ------------->
