<?php
//
// Created on: 22 Feb 2001
// Usage     : bsstree.php?language="eng|gb"&bssid=0
//
// Function  : Show the BSS Object in Tree form
//
// MySQL table: OMCR_BSSTREE
//
// modifyBssParam.php ___ bsstree.php (contents)
//                    \__ objects.php (main1)
//
//
?>
<?php
include("nocache.inc");
include("login_parm.inc");
include("bsstree.inc");
define("NO_IMAGE",0);
define("I_IMAGE",1);
define("LPLUS_IMAGE",2);
define("TPLUS_IMAGE",3);
define("N_IMAGE",4);
define("L_IMAGE",5);
define("T_IMAGE",6);
define("LMINUS_IMAGE",7);
define("TMINUS_IMAGE",8);

define("MAX_LAYER",6);
define("BASE",16);
// 0 - white, 1 - I.gif, 2 - L.gif, 3 - T.gif
$image = Array (
            "",
            "<IMG alt='' height=16 src=images/line.gif width=16 align=absBottom border=0>",
            "<IMG alt='' height=16 src=images/lastplus.gif width=16 align=absBottom border=0>",
            "<IMG alt='' height=16 src=images/midplus.gif width=16 align=absBottom border=0>",
            "<IMG alt='' height=16 src=images/none.gif width=16 align=absBottom border=0>",
            "<IMG alt='' height=16 src=images/lastblk.gif width=16 align=absBottom border=0>",
            "<IMG alt='' height=16 src=images/midblk.gif width=16 align=absBottom border=0>",
            "<IMG alt='' height=16 src=images/lastminus.gif width=16 align=absBottom border=0>",
            "<IMG alt='' height=16 src=images/midminus.gif width=16 align=absBottom border=0>"
         );

$db = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname,$db);

if (!isset($language))
    $language = "eng";
$parameter = "${language}namearray";

$sqlstring = "SELECT connected from OMCR_BSSTABLE where bssid=$bssid";
//echo $sqlstring;
$result = mysql_query($sqlstring,$db) or die("Invalid query:" . mysqli_error($pubConn));
if (mysqli_num_rows($result) == 1)
{
    list($connected) = mysqli_fetch_row($result);
}

/*if(isset($sleep_seconds))
{
	echo "sleep_seconds=$sleep_seconds<br>";
	sleep($sleep_seconds);
}*/

//READ FROM BSSTREE
$sqlstring = "select ObjectId, ${language}DisplayName,MaxSeverity,Url,Layer from OMCR_BSSTREE " .
             "where Url<>'Notshown' and bssid=$bssid and Presence=1";

//echo "sqlstring=$sqlstring";
$result = mysql_query($sqlstring,$db) or die("Invalid query" . mysqli_error($pubConn));
$no_of_objects=mysqli_num_rows($result);
if ($no_of_objects < 1)
{
    if (!isset($mode) || !($mode=="newbss"))
    {
        include("header.inc");
        printf("<CENTER>{${$parameter}[noobj]}</CENTER><BR>\n");
        echo "</BODY></HTML>";
        return;
    }
}

if ($connected == 1)
    echo "<META HTTP-EQUIV=Refresh content=6>";
else if ($connected == 0)
    echo "<META HTTP-EQUIV=Refresh content=60>";  // 1 min
else if ($connected == 254)
    echo "<META HTTP-EQUIV=Refresh content=600>"; // 10 mins

include("header.inc");
echo <<< _EOT_
<HTML>
<HEAD>
<SCRIPT Language="javascript1.2" type="text/javascript">
<!--
    var x=0, y=0;
    var isExpanded;

    image1 = new Image();
    image2 = new Image();
    image3 = new Image();
    image4 = new Image();
    image5 = new Image();
    image6 = new Image();
    image1.src = "images/line.gif";
    image2.src = "images/midplus.gif";
    image3.src = "images/none.gif";
    image4.src = "images/lastblk.gif";
    image5.src = "images/midblk.gif";
    image6.src = "images/midminus.gif";

    //
    // send scroll bar (x,y) to objects.php so that it can refresh
    // bsstree.php without moving the scroll bar
    function object_onclick(data)
    {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
        parent.main1.location.href = data + "&x=" + x + "&y=" + y;
    }

    //
    // Show or hide a <DIV> tags
    // isExpanded[] is a global variable which keep trace of the state
    // of a branch in the folder tree
    //
    // start_id is here for recusion in later implementation
    //
    function show_or_hide(id,layers,start_id)
    {
        var imgsrc;
        var disptype="", origimg="", newimg="";
        var expand_flag=false;
        var re;
        var temp = document.all.tags("DIV");
        var str_id = "obj" + id;
        var div_id = eval(str_id);

        //alert(str_id.substr(0,layers+str_id.length-5));
        for (i=start_id;i<temp.length;i++)
        {
            if (temp(i).id == str_id)
            {
                expand_flag = !isExpanded[i];
                //isExpanded[i] = !isExpanded[i];
                //if ((imgsrc=document.images["img" + str_id.substr(3,6)]) != "undefined")
                //{
                //    if (imgsrc.src.indexOf("mid" + origimg) != -1)
                //        imgsrc.src = "images/mid" + newimg;
                //    else if (imgsrc.src.indexOf("last" + origimg) != -1)
                //        imgsrc.src = "images/last" + newimg;
                //}
                break;
            }
        }

        id_prefix = str_id.substr(0,layers+str_id.length-5);
        re = new RegExp(id_prefix+"[0-9a-f]"+str_id.substr(4+layers,str_id.length-layers-5));
        for (i=start_id;i<temp.length;i++)
        {
            if (temp(i).id.indexOf(id_prefix) != -1)	// if it is affected row, then
            {
                if (temp(i).id.search(re) != -1)	//     if it is the affected 1st/2nd layer
                {
                    if (temp(i).id == str_id)		//         if it is the clicked <DIV>
                    {
                        if (expand_flag)		//             if expand leaves
                        {
                            disptype="block";
                            isExpanded[i] = true;
                            origimg="plus";
                            newimg="minus.gif";
                        }
                        else				//             if hide leaves
                        {
                            disptype="block";
                            isExpanded[i] = false;
                            origimg="minus";
                            newimg="plus.gif";
                        }
                    }
                    else				//         else if it is the 2nd layer
                    {
                        if (expand_flag)		//             if expand it
                        {
                            disptype="block";
                            isExpanded[i] = false;
                            origimg="minus";		// don't care
                            newimg="plus.gif";		// don't care
                        }
                        else				//             if hide it
                        {
                            disptype="none";
                            isExpanded[i] = false;	// don't care
                            origimg="minus";		// don't care
                            newimg="plus.gif";		// don't care
                        }
                    }
                }
                else					//     else if if it is not the affected 1st/2nd layer
                {
                    if (expand_flag)			//             if expand it
                    {
                        disptype="none";
                        isExpanded[i] = false;	// don't care
                        origimg="minus";
                        newimg="plus.gif";
                    }
                }

                //if (temp(i).id != str_id)
                //{
            	    temp(i).style.display = disptype;
                //}

                //imgsrc=document.images["img" + temp(i).id.substr(3,6)];
                //if (typeof(imgsrc) != "undefined")
                if (typeof(imgsrc=document.images["img" + temp(i).id.substr(3,6)]) != "undefined")
                {
                    if (imgsrc.src.indexOf("mid" + origimg) != -1)
                        imgsrc.src = "images/mid" + newimg;
                    else if (imgsrc.src.indexOf("last" + origimg) != -1)
                        imgsrc.src = "images/last" + newimg;
                }
                //}   /* End of if (temp(i).id */
            }
        }   /* End of for (i=0;i<temp.length;i++) */
    }

    function initIt()
    {
        var i;
        var div_obj = document.all.tags("DIV");

        isExpanded = new Array(div_obj.length);
        for (i=0;i<div_obj.length;i++)
        {
       	    isExpanded[i] = false;
            if (div_obj(i).id.substr(5,4) != "0000")
            {
                div_obj(i).style.display = "none";
            }
        }
    }

    //
    // scroll when the page is loaded.
    function compensate_scroll(scroll_x, scroll_y)
    {
        initIt();
        if ((typeof(scroll_x) != "undefined") && (typeof(scroll_y) != "undefined")
            && (scroll_y != "") && (scroll_y != ""))
        {
            self.scrollBy(scroll_x, scroll_y);
        }
        return;
    }
//-->
</SCRIPT>
</HEAD>
<BODY onLoad='compensate_scroll("$x","$y");'>
<CENTER>
{${$parameter}[bssid]} $bssid <BR>
<INPUT type=button value='{${$parameter}[showalarm]}' name=showalarm
 class=chinese style='width: 100'
 onClick='javascript:window.parent.location.href=
 "alarmframe.php?language=$language&bssid=$bssid"'
//<INPUT type=button value='{${$parameter}[statistics]}' name=statistics
// class=chinese style='width: 100'
// onClick='javascript:window.parent.location.href=
// "btsmeasframe.php?language=$language&bssid=$bssid"'>
<BR>
</CENTER>
_EOT_;
echo "<CENTER>\n";
if ( $connected == 0 )
    echo "<font color=red>{${$parameter}[broken]}</font>";
else if ($connected == 1)
    echo "<font color=orange>{${$parameter}[contacted]}</font>";
else
    echo "<font color=blue>{${$parameter}[connected]}</font>";
echo "</CENTER>\n";
echo "<HR SIZE=1>\n";


$max_layer=0;
while (list($objectid[], $displayname[], $maxseverity[], $url[], $this_layer)
     = mysqli_fetch_row($result))
{
    $layer[] = $this_layer;
    if ( $this_layer > $max_layer ) { $max_layer=$this_layer; }
}

//
// Initialize rowarray and make it contains $max_layer elements
//
for ($x=0;$x<=$max_layer;$x++)
{
    $rowarray[]=NO_IMAGE;
}

for ($y=0;$y<$no_of_objects;$y++)
{
    if ($y != 0)
    {
        if ($layer[$y] >= $layer[$y-1])
            $this_value[$y] = $last_value + pow(BASE,MAX_LAYER-$layer[$y]-1);
        else if ($layer[$y] < $layer[$y-1])
            $this_value[$y] = (floor($last_value/pow(BASE,MAX_LAYER-$layer[$y]-1))+1) * pow(BASE,MAX_LAYER-$layer[$y]-1);
    }
    else
    {
        $this_value[$y]=pow(BASE,MAX_LAYER-1);
    }
    $last_value=$this_value[$y];
}

for ($y=0;$y<$no_of_objects;$y++)
{
	if($layer[$y]==0)
		continue;
	if($layer[$y]==1)
		$parent_objectid[$y]='BssFunc';
	else{
		for($yy=$y-1;$yy>=0;$yy--){
			if($layer[$y]-1==$layer[$yy]){
				$parent_objectid[$y]=$objectid[$yy];
				break;
			}
		}
	}
}

for ($y=$no_of_objects-1;$y>=0;$y--)
{
    $tablearray[$y] = "<DIV ID=" . sprintf("obj%x",$this_value[$y]) . "><NOBR>";

    //$tablearray[$y] = "<DIV><NOBR>";
    for ($x=0;$x<=$max_layer;$x++)
    {
        if ( $x > $layer[$y] )
        {
            $rowarray[$x] = NO_IMAGE;
        }
        else if ( $x == $layer[$y] - 1 )
        {
            if (($rowarray[$x] == NO_IMAGE ) || ($rowarray[$x] == N_IMAGE ))
            {
                if (($rowarray[$x+1] == N_IMAGE) || ($rowarray[$x+1] == NO_IMAGE))	// is description
                    $rowarray[$x] = L_IMAGE;
                else
                    $rowarray[$x] = LPLUS_IMAGE;
            }
            else
            {
                if (($rowarray[$x+1] == N_IMAGE) || ($rowarray[$x+1] == NO_IMAGE))	// is description
                    $rowarray[$x] = T_IMAGE;
                else
                    $rowarray[$x] = TPLUS_IMAGE;
            }
        }
        else
        {
            if (($rowarray[$x] == NO_IMAGE ) || ($rowarray[$x] == N_IMAGE ))
                $rowarray[$x] = N_IMAGE;
            else
                $rowarray[$x] = I_IMAGE;
        }

        if ( $x == $layer[$y] )
        {
            //$tablearray[$y] .= "<FONT color=red>";
            $rowarray[$x] = N_IMAGE;
            if ( !strcmp($url[$y],"Nolink") )
            {
                $tablearray[$y] .= "$displayname[$y]";
            }
            elseif ( !strcmp($url[$y],"null") )
            {
                $tablename=strtok($objectid[$y],".");
                $tablearray[$y] .=
                    "<A HREF='javascript:void(0)' onClick=\"javascript:object_onclick('" .
                    "objects.php?language=$language&bssid=$bssid&objectid=" .
                    $objectid[$y] . "&parent_objectid=" . $parent_objectid[$y] . "&tablename=OMCR_" . $tablename . "');\">" .
                    $displayname[$y] . "</A>";
            }
            elseif ( !strncmp($url[$y],"OMCR_",5) )
            {
                $tablearray[$y] .=
                    "<A HREF='javascript:void(0)' onClick=\"javascript:object_onclick('" .
                    "objects.php?language=$language&bssid=$bssid&objectid=" .
                    $objectid[$y] . "&parent_objectid=" . $parent_objectid[$y] . "&tablename=" . $url[$y] . "');\">" .
                    $displayname[$y] . "</A>";
            }
            else    // URL is specified
            {
                $tablearray[$y] .=
                    "<A HREF='javascript:void(0)' onClick=\"javascript:object_onclick('" .
                    "$url[$y]?language=$language&bssid=$bssid&objectid=" .
                    $objectid[$y] . "&parent_objectid=" . $parent_objectid[$y] . "');\">" .
                    $displayname[$y] . "</A>";
            }
            //$tablearray[$y] .= "</FONT>";
        }
        else
        {
            if ($rowarray[$x] == LPLUS_IMAGE)
                $tablearray[$y] .= "<A href='javascript:show_or_hide(" . sprintf("\"%x\",",$this_value[$y])
                                . $layer[$y] . ",0);'>" . "<IMG name=" . sprintf("\"img%x\",",$this_value[$y])
                                . " alt='' height=16 src=images/midplus.gif width=16 align=absBottom border=0>" . "</A>";
            else if ($rowarray[$x] == TPLUS_IMAGE)
                $tablearray[$y] .= "<A href='javascript:show_or_hide(" . sprintf("\"%x\",",$this_value[$y])
                                . $layer[$y] . ",0);'>" . "<IMG name=" . sprintf("\"img%x\",",$this_value[$y])
                                . " alt='' height=16 src=images/midplus.gif width=16 align=absBottom border=0>" . "</A>";
            else
                $tablearray[$y] .= $image[$rowarray[$x]];
        }
    }
    $tablearray[$y] .= "</NOBR></DIV>\n";
}

//
// Draw the table now after determining the hierachy
//
for ($i=0;$i<$no_of_objects;$i++)
{
    echo $tablearray[$i];
}
?>

</BODY>
</HTML>
