<?php
//
// Created on: 29 Dec 2009
// Usage     : create_objects.php?language="eng|gb"&bssid=0&objectid=ALINKSET.0.0
//						 &tablename=OMCR_ALinkSet
//             [&status="Save is Completed][&alarm=attr value rejected]
// Function  : Create a Object based on OMCR_Meta_Table
//             and displaytype.inc.
// Remarks   : If objectid=BssFunc, it will redirect to another php file.
//             If $status is defined, the status will show in the status bar of browser.
//             If $alarm is defined, the text in $alarm will show in popup box.
//             Attributes has HTML object name as the <attribute name>
//             Attributes has type as the t<attribute name>
//
// MySQL table: OMCR_Meta_Table and table specified by $tablename
//
// modifyBssParam.php ___ bsstree.php (contents)
//                    \__ objects.php (main1)
//																					\create_objects.php
//
?>
<?php
if (!isset($language))
{
    $language = "eng";
}

$parameter="${language}namearray";
include("nocache.inc");
include("objects.inc");
?>
<HTML>
<HEAD>
<?php include("header.inc"); ?>
<?php
function CheckObjExist($objectid,$Object_Name,$bssid)
{
	$sp1=explode(".",$objectid);
	$replaced=$sp1[0];
	$sp2=explode("_",$Object_Name);
	$replacing=$sp2[1];
	$new_objectid=str_replace($replaced,$replacing,$objectid);
	
	unset($objExistFlagSet);
	
	$sql="select * from BssOmcDb.OMCR_BSSTREE where ObjectId like '{$new_objectid}%' and bssid='{$bssid}' ";
	$res=@mysqli_query($pubConn,$sql);
	$num=@mysqli_num_rows($res);
	for($i=0;$i<$num;$i++)
	{
		$row=@mysqli_fetch_array($res);
		$tmp_objectid=$row[ObjectId];
		$sp3=explode(".",$tmp_objectid);
		$no=$sp3[count($sp3)-1]-0;
		$objExistFlagSet[$no]=$row[Presence]-0;
	}
	return $objExistFlagSet;
}
?>
<STYLE type="text/ccs">
td    {height: 24} 
</STYLE>
<SCRIPT language="JavaScript" type="text/javascript">
<!--
    var alerting=false;
    var oldvalue=0;
    var no_of_refresh=0;
    var timeout_handle = null;
    var refresh_bsstree_string="<?=${$parameter}[actioniscompleted]?>";

    function actions(obj,nmicmd,need_confirm)
    {
        if (need_confirm)
        {
            window.status = "<?=${$parameter}[confirmquestionbegin]?>"
                          + obj.value + "<?=${$parameter}[confirmquestionend]?>";
            answer = confirm("<?=${$parameter}[confirmquestionbegin]?>"
                          + obj.value + "<?=${$parameter}[confirmquestionend]?>");
            window.status = "";
            if (answer != true)
            {
                return;
            }
        }
		
        if (nmicmd != '')
        {
            //this.document.object_form.modified_data.value = '';
            this.document.object_form.nmicommand.value = nmicmd;
        }
        this.document.object_form.submit();
    }
    function show_status(status,alarm)
    {
        //
        // alarm and status display
        if ((typeof(alarm) != "undefined") && (alarm != ""))
        {
            window.status=alarm;
            alert(alarm);
            window.status="";
        }
        if ((typeof(status) != "undefined") && (status != ""))
        {
            window.status=status;
            if ((no_of_refresh < 5) && (status == refresh_bsstree_string))
            {
                if (no_of_refresh++ < 5)
                {
                    //refresh every 2 seconds and repeat for 5 times
                    timeout_handle = setTimeout(show_status,2);
                    parent.contents.location.href=parent.contents.location;
                }
                else
                {
                    clearTimeout(timeout_handle); 
                    timeout_handle = null;
                }
            }
        }
    }
    function submit_form(data)
    {
        if (( data.modified_data.value == '') && ( data.nmicommand.value == ''))
        {
            window.status="<?=${$parameter}[savenotneed]?>";
            return(false);
        }
        else
            return(true);
    }
    function b4_change(data)
    {
        if (alerting)
            return;

        if ( data.value != '' )
        {
            oldvalue=data.value;
        }
        else
        {
            if ( oldvalue!=0 )
                data.value=oldvalue;
        }
    }
    function change_made(data)
    {
        var parm_string=this.document.object_form.modified_data.value;

        if ( data.value == '')
        {
            if ( oldvalue!=0 )
            {
                alerting = true;		// True if alert() dialogue box is displayed.
                alert("<?=${$parameter}[entervalidvalue]?>");
                data.value = oldvalue;
                data.focus();
                alerting = false;
            }
            return;
        }
        else if (data.value == oldvalue) //no Change
        {
            return;
        }

        if ( parm_string == '' )
        {
            this.document.object_form.modified_data.value = data.name;
        }
        else
        {
            if (parm_string.search(data.name) == -1)
                this.document.object_form.modified_data.value += ","+data.name;
        }
    }
//-->
</SCRIPT>
<TITLE>

BSS OMC -> <?=$objectid;?>
</TITLE>
</HEAD>
<BODY onLoad='show_status("<?=$status?>","<?=$alarm?>");'>
<?php
//echo "<br>saved=$saved";
echo "<font color=red size=+1>$alarm</font>";
echo "<HR SIZE=1>";
$status="";

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

include("meta_table_type.inc");
include("login_parm.inc");

//
// Translate the value to string
// e.g. AdminState=2 --> AdminState=Unlocked
//
function translate($input,$data)
{
    $found=-1;
    if ($data)
    {
        $items=split("[;=]",$data);
        $no_of_items=count($items);

        for ($i=0;$i<$no_of_items;$i+=2) {
            if ( $input == $items[$i+1] ) {
                $found=$i;
                break;
            }
        }
        if ($found != -1)
            return($items[$found]);
    }
    return($input);
}

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

// LOOK UP OMCR_META_TABLE for display parameters and buttons
//
// Display different xxxDisplay_Name base on the language selected
// 
//


$sqlstring = "select Object_Name,Attribute_Name,Attribute_Id,Attribute_Type,Display_Type,Default_Value," .
             "${language}Display_Name,${language}Lookup,${language}Help_Text" .
             " from OMCR_Meta_Table " .
             "where Display_Type<> 0 AND Object_Name='" . substr($tablename,5) . "'";

//echo $sqlstring . "<BR>";
$objExistFlagSet=CheckObjExist($objectid,substr($tablename,5),$bssid);
ksort($objExistFlagSet);
$haveElementFlag=0;
if(0)
{
	echo "<pre>";
	print_r($objExistFlagSet);
	echo "</pre>";
}
$result = mysql_query($sqlstring,$db) or
          die("Invalid query: $sqlstring\n" . mysqli_error($pubConn));
while ( $row = mysqli_fetch_row($result) )
{
    if (($row[4] == PUSH_BUTTON) || ($row[4] == CONFIRM_PUSH_BUTTON)) //For action item in NMI
        list($act_object_name[], $act_attribute_name[], $act_attribute_id[], $act_attribute_type[],
            $act_display_type[], $act_default_value[],$act_display_name[], $act_lookup[],
            $act_help_text[]) = $row;
    else
        list($object_name[],$attribute_name[],$attribute_id[],$attribute_type[],$display_type[],$default_value[],
            $display_name[],$lookup[],$help_text[]) = $row;
}
$no_of_parms = count($attribute_name);

echo "<U><FONT color=#00FF0F size=+3pt>=) " . strtoupper($objectid) . "</FONT></U><BR>\n";
echo "<FORM action=modify.php name='object_form' method=post\n" .
     "onSubmit='return submit_form(this.document.object_form);'>";

if ($no_of_parms == 0)
{
    echo "<BR>";
}
else
{
     echo "<TABLE border=0 cellspacing=0 cellpadding=0 width=400>\n";
     echo "<INPUT type=hidden name='modified_data' value=''>\n";
     for ($i=0; $i<$no_of_parms; $i++)
     {
         //printf("%s%s %s\n",$display_type[$i],$attribute_name[$i],$lookup[$i]);
         switch($display_type[$i])
         {
             case DO_NOT_DISPLAY:    //do not display
                 $widget="";
                 continue;
             case PULLDOWN_MENU:    //pulldown menu
                 $widget="<SELECT size=1 name='" . $attribute_name[$i] .
                     "' onFocus='b4_change(this.document.object_form." .
                     $attribute_name[$i] .
                     ")' onBlur='change_made(this.document.object_form." .
                     $attribute_name[$i] .")' style='font-size: 10pt; border: 1 solid #646464'>";
                 $existed=0;
    
                 $menu_items=split("[;=]", $lookup[$i]);
                 $no_of_menu_items=count($menu_items);			 
								 /*
                 for($j=0;$j<$no_of_menu_items;$j+=2)
                 {
                 		 if($objExistFlagSet[$menu_items[$j]] == 1)
                 		 	continue;
                     $widget.="<option value=" . $menu_items[$j+1];
                     if ($default_value[$i] == $menu_items[$j+1])
                     {
                         $widget.=" selected>";
                         $existed=1;
                     }
                     else
                     {
                         $widget.=">";
                     }
                     $widget.=$menu_items[$j] . "</option>\n";
                 }
                 */
                 					$isSelected=0;
									foreach($objExistFlagSet as $key => $value)
									{
										if($value == 0)
											$haveElementFlag=1;
										if($value != 1)
										{
											$widget.="<option value=\"{$key}\" ";
											if($isSelected == 0)
											{
												$isSelected=1;
												$widget.=" selected>";
                        $existed=1;
                      }
                      else
                      {
                      	$widget.=">";
                      }
											$widget.=$key . "</option>\n";
										}
									}
									
                 $widget.="</SELECT>";
                 ?>
                 <SCRIPT language="JavaScript" type="text/javascript">
                 	var parm_string=this.document.object_form.modified_data.value;
                 	if ( parm_string == '' )
        			{
            			this.document.object_form.modified_data.value = <?php echo "'".$attribute_name[$i]."'" ?>;
        			}
        			else
        			{
            			if (parm_string.search(<?php echo "'".$attribute_name[$i]."'" ?>) == -1)
                			this.document.object_form.modified_data.value += ","+<?php echo "'".$attribute_name[$i]."'" ?>;
        			}
                 </SCRIPT>
                 <?php
                // if (!$existed)
                //   $widget="* Unknown value ($default_value[$i]) *";
                 break;
             case TEXTBOX:    //text box
                 $widget="<INPUT type=text name=" . $attribute_name[$i] .
                     " size=20 value='" . rawurldecode($default_value[$i]) .
                     "' onFocus='b4_change(this.document.object_form." .
                     $attribute_name[$i] .
                     ")' onBlur='change_made(this.document.object_form." .
                     $attribute_name[$i] .")' style='font-size: 10pt; border: 1 solid #646464'>";
                 break;
             case LABEL:    //label
                 if (!strcasecmp($attribute_name[$i], "adminstate"))
                     $adminstate = $default_value[$i];
                 $widget=translate($default_value[$i],$lookup[$i]);
                 $widget.="<INPUT type=hidden name='$attribute_name[$i]' value='$default_value[$i]'>\n";
                 break;
             case RADIO_BUTTON:    //radio button
                 $widget="<INPUT type=radio name=" . $attribute_name .
                     " value='" . translate($default_value[$i],$lookup[$i]) . 
                     "' onBlur='change_made(this.document.object_form." .
                     $attribute_name[$i] . ")'>" .
                     $default_value[$i] .  "\n";
                 break;
             case CHECKBOX:    //check box
                 $widget="<INPUT type=checkbox name=" . $attribute_name[$i] .
                     " value=1" .  ($default_value[$i]? " CHECKED ":" ") .
                     "onBlur='change_made(this.document.object_form." .
                     $attribute_name[$i] . ")'>";
                 break;
             case TEXTAREA:    //text area
                 $widget="<TEXTAREA name=" . $attribute_name[$i] .
                     " rows=4 cols=45" .
                     "onBlur='change_made(this.document.object_form." .
                     $attribute_name[$i] . ")'>" . rawurldecode($default_value[$i]) . "</TEXTAREA>";
                 break;
         }

         if (strstr($saved,$attribute_name[$i]) != false)
             $color="#00F0FF";
         else
             $color="#FFFFFF";

         $widget_type = "<INPUT type=hidden name=t$attribute_name[$i] value=$attribute_type[$i]>";

         printf("  <TR><TD bgcolor=%s title='%s'>%s%s</TD><TD>%s</TD></TR>\n",
             $color, $help_text[$i], $display_name[$i], $widget_type, $widget);
     }
     echo "</TABLE>\n";
}

//
// Display the action buttons & confirmed action buttons
//
//
$no_of_actions = count($act_attribute_name);
for ($i=0;$i<$no_of_actions;$i++)
{
    if (!strncasecmp($act_lookup[$i],"javascript:",11))	// Direct to another webpage
    {
        echo "<INPUT type=submit value='" . $act_display_name[$i] .
            "' name='" . $act_attribute_name[$i] .
            "' onClick='" . $act_lookup[$i] . "'>\n";
    }
    else if ($act_lookup[$i] != '')	// NMI Actions
    {
        if ($act_display_type[$i] == CONFIRM_PUSH_BUTTON)
            $need_confirm = "true";
        else
            $need_confirm = "false";

        echo "<INPUT type=button value='" . $act_display_name[$i] .
            "' name='" . $act_attribute_name[$i] .
            "' onClick='actions(this.document.object_form." .
            $act_attribute_name[$i] . ",\"" . $act_lookup[$i] . "\",$need_confirm)' ";
        
        if(stristr($act_display_name[$i],"create"))
        {
        	if($haveElementFlag == 0)
        		echo "disabled";
        }  
            
          
        echo ">\n";		
    }
    else if ($act_attribute_name[$i] == "Revert")	// Reset Webpage input the original value
    {
        echo "<INPUT type=reset value='" . $act_display_name[$i] .
            "' name='Revert'>\n";
    }
    else						// Simply Save the altered values
    {
        echo "<INPUT type=submit value='" . $act_display_name[$i] .
            "' name='" . $act_attribute_name[$i] . "'>\n";
    }
}


echo "<INPUT type=hidden name='nmicommand'    value=''>\n";
echo "<INPUT type=hidden name='bssid'         value=$bssid>\n";
echo "<INPUT type=hidden name='objectid'      value=$objectid>\n";
echo "<INPUT type=hidden name='tablename'     value=$tablename>\n";
echo "<INPUT type=hidden name='language'      value=$language>\n";
echo "<INPUT type=hidden name='x'             value=$x>\n";
echo "<INPUT type=hidden name='y'             value=$y>\n";
echo "<HR size=1>\n";
?>

</FORM>
</BODY>
</HTML>
