<?php
//
// Created on: 17 Mar 2002
// Usage     : adjhandlist.php?bssid=0
//             [&language="eng|gb"]
//
//
//
//
//
?>
<?php include("nocache.inc"); ?>
<HTML>
<HEAD>
<?php include("header.inc"); ?>
</HEAD>
<SCRIPT language="javascript" type="text/javascript">
window.status = "";
</SCRIPT>
<BODY>

<?php
if (!isset($language))
    $language = "eng";
$parameter="{$language}namearray";
if (!isset($bssid) || !isset($objectid))
{
    echo "The required information for this page is invalid.<br>\n";
    echo "</BODY>\n</HTML>";
    exit();
}

/************************************************
 * Retrieve the Bts Manager ID from object ID
 * AdjHand.11.0.12 --substr--> 11.0.12 --> 11
 ************************************************/
$btsmgrid = substr($objectid,8);
if ($btsmgrid[1] == '.')
{
    $btsmgrid=substr($btsmgrid,0,1);
}
else if ($btsmgrid[2] == '.')
{
    $btsmgrid=substr($btsmgrid,0,2);
}
else
{
    echo strlen($objectid) . "Error Object ID:" . $objectid . "\n";
    return 0;
}

    include("adjhandlist.inc");
    include("meta_table_type.inc");
    include("login_parm.inc");
    $db = mysql_connect($hostname, $username, $password);
    mysql_select_db($dbname,$db);
    //echo "<U><FONT color=#0000FF size=+3pt>=)AdjHand.$btsmgrid.0.*<FONT></U>";
    echo "<U><FONT color=#0000FF size=+3pt>{${$parameter}[title]}<FONT></U>";
    echo "<p>\n";


    /********************************************************
     * Step 1: Find out the next available Adjhand object ID 
     ********************************************************/
    $sqlstring = "select MIN(0+SUBSTRING_INDEX(ObjectId,'.',-1)) from OMCR_AdjHand where bssid=$bssid" .
                 " and (BcchFrequency=-1 or CellGlobalIdentityLaiMcc=0" .
                 " or CellGlobalIdentityLaiMnc=0 or CellGlobalIdentityCi=0 or LocationAreaCodes=0)" .
                 " and ObjectId LIKE 'AdjHand.$btsmgrid.0.%'";
    //echo $sqlstring . "<BR>";
    $result = mysql_query($sqlstring,$db) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
    $row = mysqli_fetch_row($result);
    if ($row[0] == null)
    {
        echo "\nThere are no space for new neighbour cell.";
        return;
    }
    $new_neighbor_id = "AdjHand.$btsmgrid.0.$row[0]";
    mysql_free_result($result);

    /************************************************
     * Step 2: Display the present neighbor list
     ************************************************/

    /************************************************
     * Generate the Neighbor cell list from SQL DB
     ************************************************/
    $sqlstring = "CREATE TEMPORARY TABLE tmp SELECT t1.Label AS NAME," .
                 "t1.MobileCountryCode AS MCC,t1.MobileNetworkCode AS MNC,t1.LocationAreaCode AS LAC," .
                 "t1.CellIdentity AS CI,t1.CellAllocation_0 AS BCCH,t1.BsIdentityCode AS BSIC " .
                 "FROM OMCR_Bts AS t1, OMCR_BSSTREE AS t2 WHERE " .
                 "t2.Presence=1 AND t1.ObjectId=t2.ObjectId AND t1.ObjectId<>'Bts.$btsmgrid.0' AND " .
                 "t1.MobileCountryCode>0 AND t1.MobileNetworkCode>0 AND t1.LocationAreaCode>0 AND " .
                 "t1.CellIdentity>0 AND t1.CellAllocation_0>0 AND t1.BsIdentityCode>=0";
    //echo $sqlstring . "<BR>";
    $result = mysql_query($sqlstring,$db) or
              die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
    $sqlstring = "INSERT INTO tmp SELECT t1.Label AS NAME," .
                 "t1.CellGlobalIdentityLaiMcc AS MCC,t1.CellGlobalIdentityLaiMnc AS MNC,t1.LocationAreaCodes AS LAC," .
                 "t1.CellGlobalIdentityCi AS CI,t1.BcchFrequency AS BCCH,t1.BsIdentityCode AS BSIC " .
                 "FROM OMCR_AdjHand AS t1, OMCR_BSSTREE AS t2 " .
                 "WHERE t2.Presence=1 AND t1.ObjectId=t2.ObjectId AND " .
                 "t1.ObjectId NOT LIKE 'AdjHand.$btsmgrid.0.%' AND t1.CellGlobalIdentityLaiMcc>0 AND " .
                 "t1.CellGlobalIdentityLaiMnc>0 AND t1.LocationAreaCodes>0 AND " .
                 "t1.CellGlobalIdentityCi>0 AND t1.BcchFrequency>0 AND t1.BsIdentityCode>=0";
    //echo $sqlstring . "<BR>";
    $result = mysql_query($sqlstring,$db) or
              die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
    $sqlstring = "SELECT DISTINCT * FROM tmp";
    //echo $sqlstring . "<BR>";
    $result = mysql_query($sqlstring,$db) or
              die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));

/*
    $sqlstring = "select DISTINCT Label,CellGlobalIdentityLaiMcc,CellGlobalIdentityLaiMnc," .
                 "LocationAreaCodes,CellGlobalIdentityCi,BcchFrequency,BsIdentityCode from OMCR_AdjHand " .
                 "where BcchFrequency<>-1 and CellGlobalIdentityLaiMcc<>0 and CellGlobalIdentityLaiMnc<>0 " .
                 "and CellGlobalIdentityCi<>0 and LocationAreaCodes<>0 and (bssid<>$bssid OR " .
                 "(bssid=$bssid AND ObjectId NOT LIKE 'AdjHand.$btsmgrid.0.%')) ORDER BY CellGlobalIdentityCi";
*/
    //echo $sqlstring . "<BR>";
    $result = mysql_query($sqlstring,$db) or
              die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
    $no_of_fields=mysql_num_fields($result);
    echo "<TABLE border=1 cellspacing=0 cellpadding=0" .
         "bordercolor=#000000 bordercolordark=#FFFFFF bordercolorlight=#000000 width=100%>\n";
    
    echo "<TR>\n";
    echo "<TD width=15%>{${$parameter}[cellname]}</TD>\n";
    echo "<TD width=12%>{${$parameter}[mcc]}</TD>\n";
    echo "<TD width=11%>{${$parameter}[mnc]}</TD>\n";
    echo "<TD width=11%>{${$parameter}[lac]}</TD>\n";
    echo "<TD width=11%>{${$parameter}[cellid]}</TD>\n";
    echo "<TD width=11%>{${$parameter}[bcch]}</TD>\n";
    echo "<TD width=10%>{${$parameter}[bsic]}</TD>\n";
    echo "<TD width=19%>{${$parameter}[control]}</TD>\n";
    echo "</TR>\n";
    $num=0;
    while ( $row = mysqli_fetch_row($result) )
    {
	$num++;
	echo "<TR>\n";
	echo "<FORM action='modify.php'>\n";
	//echo "<TD>$num</TD>\n";
	echo "<INPUT type=hidden name=language value=$language>\n";
	echo "<INPUT type=hidden name=bssid value=$bssid>\n";
	echo "<INPUT type=hidden name=objectid value=$new_neighbor_id>\n";
	echo "<INPUT type=hidden name=Label value=\"" . rawurldecode($row[0]) . "\">\n";
	echo "<INPUT type=hidden name=CellGlobalIdentityLaiMcc value=$row[1]>\n";
	echo "<INPUT type=hidden name=CellGlobalIdentityLaiMnc value=$row[2]>\n";
	echo "<INPUT type=hidden name=LocationAreaCodes value=$row[3]>\n";
	echo "<INPUT type=hidden name=CellGlobalIdentityCi value=$row[4]>\n";
	echo "<INPUT type=hidden name=BcchFrequency value=$row[5]>\n";
	echo "<INPUT type=hidden name=BsIdentityCode value=$row[6]>\n";

	echo "<INPUT type=hidden name=url value=adjhand.php>";
	echo "<INPUT type=hidden name=modified_data value=" .
             "Label,CellGlobalIdentityLaiMcc,CellGlobalIdentityLaiMnc," .
             "CellGlobalIdentityCi,LocationAreaCodes,BcchFrequency,BsIdentityCode" . ">\n";
	echo "<INPUT type=hidden name=tLabel value=" . STRINGS . ">\n";
	echo "<INPUT type=hidden name=tCellGlobalIdentityLaiMcc value=" . NUMBERS . ">\n";
	echo "<INPUT type=hidden name=tCellGlobalIdentityLaiMnc value=" . NUMBERS . ">\n";
	echo "<INPUT type=hidden name=tCellGlobalIdentityCi value=" . NUMBERS . ">\n";
	echo "<INPUT type=hidden name=tLocationAreaCodes value=" . NUMBERS . ">\n";
	echo "<INPUT type=hidden name=tBcchFrequency value=" . NUMBERS . ">\n";
	echo "<INPUT type=hidden name=tBsIdentityCode value=" . NUMBERS . ">\n";

	echo "<TD>" . ($row[0]==''?"-":rawurldecode($row[0])) . "</TD>\n";
	echo "<TD>$row[1]</TD>\n";
	echo "<TD>$row[2]</TD>\n";
	echo "<TD>$row[3]</TD>\n";
	echo "<TD>$row[4]</TD>\n";
	echo "<TD>$row[5]</TD>\n";
	echo "<TD>$row[6]</TD>\n";
	echo "<TD><INPUT type=submit value={${$parameter}[addneighbor]}></TD>\n";
	echo "</FORM>\n";
	echo "</TR>\n";
    }
    if (mysqli_num_rows($result) == 0)
    {
        $no_of_fields += 1;
	echo "<TR><TD align=center colspan=$no_of_fields><FONT color=cyan>" .
             ${$parameter}[noneighbor] . "</FONT></TD></TR>";
    }
    echo "</TABLE>";
    echo "<HR SIZE=1>\n";
    echo "<INPUT type=button name=back value='{${$parameter}[back]}'" .
         " onClick='javascript:location.href=\"adjhand.php?" .
         "language=$language" .
         "&bssid=$bssid" .
         "&objectid=AdjHand.$btsmgrid.0.0" .
         (isset($x)?"&x=$x":"") .
         (isset($y)?"&y=$y":"") .
         "\"'>\n";
    $sqlstring = "DROP TABLE tmp";
    //echo $sqlstring . "<BR>";
    $result = mysql_query($sqlstring,$db) or
              die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));

?>
</BODY>
</HTML>
