<?php
//
// Created on: 
// Usage: alarmevent.php
//        alarmevent.php?language="eng|gb"
//                       [&bssid=0]
//                       [&perBssid=XXX]
//                       [&perSeverity=XXX]
//                       [&perObjectid=XXX]
//                       [&perStartPeriod=XXX]
//                       [&perEndPeriod=XXX]
//
//          The following is used for alarm clearing
//                       [&rmBssid=XXX]     
//                       [&rmObjectid=XXX]
//                       [&rmEventtime=XXX]
//
//          The following is used for page display
//                       [&offset=XXX]
//                       
//          The following is used for displaying a specific signaling element
//                       [&perSignal_Element=XXX]
//                       
// Function: Display alarm from offset to offset+stepsize
//           Stepsize for each page ($stepsize) is default to 13
//
// Filter  : Support filter per bssid, objectid, event time and severity
//                       
// MySQL table involved: OMCR_ALARMEVENT
//
?>
<?php
include("nocache.inc");
include("alarmevent.inc");
if (!isset($language))
	$language="eng";
$parameter = "${language}namearray";	// use of variable variables
include("login_parm.inc");
$stepsize=13;

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

if (isset($rmBssid) && isset($rmObjectid) && isset($rmEventtime))
{
    //
    // Clear this alarm in OMCR_ALARMEVENT and OMCR_BSSTREE
    //
    $sqlstring = "UPDATE OMCR_ALARMEVENT SET Severity=0, ClearTime=NOW()" .
                 " WHERE bssid=$rmBssid" .
                 " AND ObjectId='$rmObjectid'" .
                 " AND EventTime='$rmEventtime'";
    echo $sqlstring . "<BR><br>";
    $result = mysql_query($sqlstring,$db) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));

    $sqlstring = "SELECT min(Severity) FROM OMCR_ALARMEVENT" .
                 " WHERE bssid=$rmBssid" .
                 " AND ObjectId='$rmObjectid'" .
                 " AND Severity>0 AND Severity<=3";
    echo $sqlstring . "<BR><br>";
    $result = mysql_query($sqlstring,$db) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
    $row = mysqli_fetch_row($result);

    if ( $row[0] == null ) //  All alarm cleared
         $row[0] = 0;

    $sqlstring = "UPDATE OMCR_BSSTREE SET MaxSeverity=" . $row[0] .
                 " WHERE bssid=$rmBssid" .
                 " AND ObjectId='$rmObjectid'";
    echo $sqlstring . "<BR>";
    $result = mysql_query($sqlstring,$db) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));

    header("Location: " .
         "alarmevent.php?" .
         "language=$language" .
         (isset($bssid)? "&bssid=$bssid":"") .
         (isset($perBssid)? "&perBssid=$perBssid":"") .
         (isset($perSeverity)? "&perSeverity=$perSeverity":"") .
         (isset($perObjectid)? "&perObjectid=$perObjectid":"") .
         (isset($perStartPeriod)? "&perStartPeriod=$perStartPeriod":"") .
         (isset($perEndPeriod)? "&perEndPeriod=$perEndPeriod":"") .
         (isset($perSignal_Element)? "&perSignal_Element=$perSignal_Element":"") .
         "&offset=$offset"
         //"&offset=" . ($offset-$stepsize)
    );
}
?>
<HTML>
<HEAD>
<META HTTP-EQUIV="Refresh" content="60">
<?php include("header.inc"); ?>
<SCRIPT language="JavaScript" type="text/javascript">
<!--
    function show_status(status, alarm)
    {
        if (typeof(status) == "undefined" );
    }


    function go_to_webpage(myForm)
    {
        if (myForm.gotopage.value == '')
            myForm.gotopage.value = 1;
        else if (myForm.gotopage.value <= 0)
            myForm.gotopage.value = 1;

<?php
        echo "window.location.href=\"alarmevent.php?language=$language";
        echo (isset($bssid)? "&bssid=$bssid":"") .
        (isset($perBssid)? "&perBssid=$perBssid":"") .
        (isset($perSeverity)? "&perSeverity=$perSeverity":"") .
        (isset($perObjectid)? "&perObjectid=$perObjectid":"") .
        (isset($perStartPeriod)? "&perStartPeriod=$perStartPeriod":"") .
        (isset($perEndPeriod)? "&perEndPeriod=$perEndPeriod":"");
        echo "&offset=\" + $stepsize * (myForm.gotopage.value - 1);";
?>

    }
//-->
</SCRIPT>
<TITLE> ALARM EVENT </TITLE>
</HEAD>
<BODY onLoad='show_status("<?=$status?>","<?=$alarm?>");'>
<FORM name='alarmtable' method=post
 onSubmit='return submit_form(this.document.alarmtable)'>
<?php

$alarm_to_show=2;  // =SV_MAJOR
$status="";

if (!isset($offset))
{
    $offset = 0;
}

/*
 * Create string like "ObjectId=Trx.1.0.4 OR ObjectId=Bts.1.0" from Signal_Element
 */
if (isset($perSignal_Element))
{
    $sqlstring = "SELECT DISTINCT ObjectId FROM OMCR_BSSTREE WHERE Signal_Element=$perSignal_Element";
    $result = mysql_query($sqlstring,$db) or
              die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));

    $firstterm=0;
    while ( list($objects) = mysqli_fetch_row($result) )
    {
        if ($firstterm)
            $XXX .= " OR ";
        else
            $XXX = "";

        $XXX .= "t1.ObjectId='$objects'";
        $firstterm++;
    }

    mysql_free_result($result);
}

$sqlstring = "SELECT t1.bssid,t1.ObjectId," .
    "t2." . $language . "EventInfo," .
    "t1.Severity,t1.EventTime," .
    "t1.ClearTime," .
    "t1.Cause " .
    " FROM OMCR_ALARMEVENT AS t1, OMCR_ALARMCODE AS t2" .
    " WHERE t1.GlobalEventNumber>0" .
    (isset($perBssid)?" AND t1.bssid=$perBssid":"") .
    (isset($perObjectid)?" AND t1.ObjectId REGEXP '$perObjectid'":"") .
    (isset($perSeverity)?" AND t1.Severity$perSeverity":"") .
    (isset($perStartPeriod)?" AND t1.EventTime>='$perStartPeriod'":"") .
    (isset($perEndPeriod)? " AND t1.EventTime<='$perEndPeriod'":"") .
    (
       ( isset($perBssid) || isset($perObjectid) ||
         isset($perSeverity) ||
         isset($perStartPeriod) || isset($perEndPeriod)
       )? "":" AND (t1.Severity>0 OR t1.ClearTime >= NOW()-INTERVAL 60 MINUTE)"
    ) .
    (isset($XXX)? " AND ($XXX)":"") .
    " AND t1.Severity <= $alarm_to_show AND t1.Cause = t2.Cause ";


//echo $sqlstring . "<BR>";
$result = mysql_query($sqlstring,$db) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));

$no_of_records=mysqli_num_rows($result);

echo "<TABLE border=1 cellspacing=0 cellpadding=0 bordercolor=#000000 " .
     "bordercolordark=#FFFFFF bordercolorlight=#000000 width=100%>\n";
echo "<TR>\n";
echo "<TD width=20%>\n";
echo ${$parameter}[total] . ": " .$no_of_records . " " . ${$parameter}[record];
echo "</TD>\n";
echo "<TD width=20%>\n";
echo "$stepsize " . ${$parameter}[record] . "/" . ${$parameter}[pages];
echo "</TD>\n";
echo "<TD width=30%>\n";
if ($no_of_records)
     echo (floor($offset/$stepsize) + 1) . "/" .
          (ceil($no_of_records/$stepsize)) . " " . ${$parameter}[pages];
else
     echo "0/0 " . ${$parameter}[pages];
echo "</TD>\n";
echo "<TD width=30%>&nbsp";
echo "<INPUT type=button value={${$parameter}[go]} onClick='go_to_webpage(this.document.alarmtable);'>";
echo "<INPUT type=TEXT name=gotopage size=4> ";
echo "{${$parameter}[pages]}&nbsp&nbsp&nbsp";

if ($offset > $stepsize-1)
{
    echo "<A href=\"alarmevent.php?" .
         "language=$language" .
         (isset($bssid)? "&bssid=$bssid":"") .
         (isset($perBssid)? "&perBssid=$perBssid":"") .
         (isset($perSeverity)? "&perSeverity=$perSeverity":"") .
         (isset($perObjectid)? "&perObjectid=$perObjectid":"") .
         (isset($perStartPeriod)? "&perStartPeriod=$perStartPeriod":"") .
         (isset($perEndPeriod)? "&perEndPeriod=$perEndPeriod":"") .
         (isset($perSignal_Element)? "&perSignal_Element=$perSignal_Element":"") .
         "&offset=" . ($offset-$stepsize) . "\">";
    echo "<IMG align=absbottom src='images/st_minus.gif'>";
    echo "</A>\n";
}
else
{
    echo "<A href=\"alarmevent.php?" .
         "language=$language" .
         (isset($bssid)? "&bssid=$bssid":"") .
         (isset($perBssid)? "&perBssid=$perBssid":"") .
         (isset($perSeverity)? "&perSeverity=$perSeverity":"") .
         (isset($perObjectid)? "&perObjectid=$perObjectid":"") .
         (isset($perStartPeriod)? "&perStartPeriod=$perStartPeriod":"") .
         (isset($perEndPeriod)? "&perEndPeriod=$perEndPeriod":"") .
         (isset($perSignal_Element)? "&perSignal_Element=$perSignal_Element":"") .
         "&offset=" . $offset . "\">";
    echo "<IMG align=absbottom src='images/gt_minus.gif'>";
    echo "</A>\n";
}
if ($no_of_records > $offset + $stepsize)
{
    echo "<A href=\"alarmevent.php?" .
         "language=$language" .
         (isset($bssid)? "&bssid=$bssid":"") .
         (isset($perBssid)? "&perBssid=$perBssid":"") .
         (isset($perSeverity)? "&perSeverity=$perSeverity":"") .
         (isset($perObjectid)? "&perObjectid=$perObjectid":"") .
         (isset($perStartPeriod)? "&perStartPeriod=$perStartPeriod":"") .
         (isset($perEndPeriod)? "&perEndPeriod=$perEndPeriod":"") .
         (isset($perSignal_Element)? "&perSignal_Element=$perSignal_Element":"") .
         "&offset=" . ($offset+$stepsize) . "\">";
    echo "<IMG align=absbottom src='images/minus_gt.gif'>";
    echo "</A>\n";
}
else
{
    echo "<A href=\"alarmevent.php?" .
         "language=$language" .
         (isset($bssid)? "&bssid=$bssid":"") .
         (isset($perBssid)? "&perBssid=$perBssid":"") .
         (isset($perSeverity)? "&perSeverity=$perSeverity":"") .
         (isset($perObjectid)? "&perObjectid=$perObjectid":"") .
         (isset($perStartPeriod)? "&perStartPeriod=$perStartPeriod":"") .
         (isset($perEndPeriod)? "&perEndPeriod=$perEndPeriod":"") .
         (isset($perSignal_Element)? "&perSignal_Element=$perSignal_Element":"") .
         "&offset=" . $offset . "\">";
    echo "<IMG align=absbottom src='images/minus_st.gif'>";
    echo "</A>\n";
}
echo "</TD>\n";
echo "</TR>\n";
echo "<TR><TD colspan=4>\n";

//
// Only show alarm with Severity <= $alarm_to_show
if ($offset >= 0)
{
    $sqlstring .= "ORDER BY EventTime DESC,GlobalEventNumber DESC LIMIT $offset,$stepsize";
}
else
{
    $sqlstring .= "ORDER BY EventTime DESC,GlobalEventNumber DESC LIMIT 0," .
                 ($stepsize+$offset);
}

//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 nowrap height=30 bgcolor=#D0D0D0>{${$parameter}[bssid]}</TD>\n";
echo "<TD nowrap height=30 bgcolor=#D0D0D0>{${$parameter}[objectid]}</TD>\n";
echo "<TD nowrap height=30 bgcolor=#D0D0D0>{${$parameter}[description]}</TD>\n";
echo "<TD nowrap height=30 bgcolor=#D0D0D0>{${$parameter}[severity]}</TD>\n";
echo "<TD nowrap height=30 bgcolor=#D0D0D0>{${$parameter}[eventtime]}</TD>\n";
echo "<TD nowrap height=30 bgcolor=#D0D0D0>{${$parameter}[cleartime]}</TD>\n";
echo "<TD nowrap height=30 bgcolor=#D0D0D0>-  -</TD>\n";
echo "</TR>\n";

while ( $row = mysqli_fetch_row($result) )
{
    list($mybssid,$myobjectid,$myeventinfo,$myseverity,
         $myeventtime,$mycleartime, $cause)=$row;
    echo "<TR>";
    for ($i=0;$i<$no_of_fields;$i++)
    {
        if (!strcmp(mysql_field_name($result, $i),"Severity"))
        {
            $severity_field_no = $i;
            echo "<TD height=30>";
            switch ($row[$i])
	    {
               case 0:  echo ${$parameter}[clear] . "</TD>\n";
                        break;
               case 1:  echo "<FONT color=#FF0000>" .
                             ${$parameter}[critical] . "</FONT></TD>\n";
                        break;
               case 2:  echo "<FONT color=#FFA500>" .
                             ${$parameter}[major] . "</FONT></TD>\n";
                        break;
               case 3:  echo ${$parameter}[minor] . "</TD>\n";
                        break;
               case 4:  echo ${$parameter}[minor]  . "</TD>\n";
                        break;
               default: echo $row[$i] . "</TD>\n";
                        break;
            }
        }
        else if (!strcmp(mysql_field_name($result, $i),"Cause"))
        {
              // Not need to display
        }
        else  // Is not Severity or Cause
        {
            printf("<TD height=30>%s</TD>\n",(isset($row[$i])? $row[$i]:"-"));
        }
    } // end_of_for ()

    // Manual clear button for non-clear alarm
    if ($row[$severity_field_no] > 0)
        echo "<TD title=$cause><INPUT type=button value='{${$parameter}[remove]}'" .
             " onClick='javascript:window.location.href=\"alarmevent.php?" .
             "language=$language" .
             (isset($bssid)? "&bssid=$bssid":"") .
             (isset($perBssid)? "&perBssid=$perBssid":"") .
             (isset($perSeverity)? "&perSeverity=$perSeverity":"") .
             (isset($perObjectid)? "&perObjectid=$perObjectid":"") .
             (isset($perStartPeriod)? "&perStartPeriod=$perStartPeriod":"") .
             (isset($perEndPeriod)? "&perEndPeriod=$perEndPeriod":"") .
             (isset($perSignal_Element)? "&perSignal_Element=$perSignal_Element":"") .
             "&offset=" . $offset . 
             "&rmBssid=" . $mybssid . 
             "&rmObjectid=" . $myobjectid . 
             "&rmEventtime=" . $myeventtime . 
             "\"'></TD>";
    else
        echo "<TD title=$cause>-</TD>";
    echo "</TR>\n";
}
echo "</TABLE>\n";
if (mysqli_num_rows($result) == 0)
   echo "<BR><CENTER><FONT color=cyan>{${$parameter}[norecord]}</FONT></CENTER><BR>";
echo "</TD>\n";
echo "</TR>\n";
echo "</TABLE>\n";
echo "<FONT color=cyan>{${$parameter}[note]}</FONT>\n";
mysql_free_result($result);
?>
</BODY>
</HTML>
