#!/perl/bin/perl.exe
$|=1;
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# Information Software SA
# Date In: 01-28-2005
# Updates: 03-22-2005,2016-03-13,2022-07-28
# Author:Rodolfo Machon
# File Name: menul.pl
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# PERL MODULES
use CGI;
use XML::Simple;
use Data::Dumper;
use DBI;
use CGI::Carp qw(fatalsToBrowser);
binmode STDOUT, ":encoding(utf8)";
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
# HTML PRINTING ACTIVATION
print "Content-type: text/html; charset=utf-8\n\n";
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
# GET PARAMETERS
$cgiparms 	= new CGI;
$clase      = $cgiparms->param('clase');
$id			= $cgiparms->param('id');
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# CHECK PARAMETERS
if ($clase eq ""){print "Your are not passing CLASE parameter<br>";}
if ($id eq ""){print "Your are not passing ID parameter<br>";}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# GET CLASE SCHEMA AND VARIABLES
get_app_data();
get_component_schema($clase);
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# OPEN DATABASE CONNECTIONS - ODBCs
get_database_handlers();
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# MAIN PROGRAM
$options = "";
get_user_profile();
construct_page();
exit;
#--------------------------------------------------------------------d-----
#-------------------------------------------------------------------------
sub get_user_profile{
	my $j			= 0;
	my $temp0		= "";
	my $layer_div	= "{layer_open}{table_open}{trtd}{table_close}{layer_close}";	
	my $fields  	= "a.i4_layer_id,a.i2_layer_type_id,a.v8_position,a.i4_left,a.i4_top,a.i4_height,a.i4_width,a.i2_index,a.v8_visibility,b.v128_$lan,a.i4_layer2open_id";
	my $tables  	= "sdd_user_menulayers a LEFT JOIN sdm_labels b on a.i8_label_id=b.i8_label_id";
	my $decision	= "a.i8_user_id='$id' ORDER BY 1";
	my $sql			= "SELECT $fields FROM $tables WHERE $decision";
	my $sth 	   = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_new_id');
	while (my @row = $sth->fetchrow_array) {
		my $table_open  ="";
		my $table_close ="";
		my $trtd        ="";
		my $div_caption ="<A HREF=\"#\" class=\"MenuLink\" onClick=\"Show('$row[10]')\">$row[9]</A>";		
		if($row[1]!=1){ 
			my $i			= 0;
			my $temp_trtd	= "";
			my $fields  	= "a.i4_layer_id,a.v128_url,a.i4_left,a.i4_top,a.i4_height,a.i4_width,a.v4_resizable,a.v4_toolbar,a.v4_location,a.v4_status,a.v4_scrollbar,a.v4_menubar,c.v128_$lan,a.i4_option_id,a.i4_layer2open_id,a.i2_link_type_id,a.txt_comment";
			my $tables  	= "sdd_user_menulayer_options a,sdm_labels c";
			my $desition	= "a.i4_layer_id=$row[0] and a.i8_user_id=$id and a.i8_label_id=c.i8_label_id order by a.i2_index";
			my $sql1 		= "select $fields from $tables where $desition";
			my $sth1 		= $dbh1->prepare($sql1);
			$sth1->execute or die SQL_fail($sql1,$dbh1,'sub get_new_id');
			while (my @row1 = $sth1->fetchrow_array) {
				if($row1[15]==1){
					my $option_id 	= $row1[13];
					my $left	  	= $row1[2];
					my $top	   		= $row1[3];
					my $height	   	= $row1[4];
					my $width	   	= $row1[5];
					my $resizable 	= $row1[6];
					my $toolbar   	= $row1[7];
					my $location  	= $row1[8];
					my $status	   	= $row1[9];
					my $scrollbars	= $row1[10];
					my $menubar   	= $row1[11];
					my $params    	= $row1[16];
					$params		=~ s/;/|/g;					
					my $id_field  	= "";
					my $fields1  	="a.c4_component_id,b.v64_name";
					my $tables1  	= "sdm_options a,sdm_classes b";
					my $desition1	= "a.i4_option_id=$option_id and a.i4_class_id=b.i4_class_id order by 1";
					my $sql2   		= "select $fields1 from $tables1 where $desition1";
					$sth2 		= $dbh2->prepare($sql2);
					$sth2->execute or die SQL_fail($sql2,$dbh2,'sub get_new_id');
					while (my @row2 = $sth2->fetchrow_array) {
						$component = $row2[0];
						$clase	   = $row2[1];
					}
					$temp_trtd = "<tr><td class=tdheader><A HREF=\"#\" class=\"letras\" onClick=\"HideOption('$row[10]');MenuOpenOption('$component','$clase','$left','$top','$id_field','$height','$width','$resizable','$toolbar','$location','$status','$scrollbars','$menubar','$params')\">$row1[12]</A></td></tr>";
				}elsif($row1[15]==2){
					$temp_trtd = "<tr><td class=tdheader><A HREF=\"#\" class=\"letras\" onClick=\"HideLayer('$row[10]');MenuOpenURL('$row1[1]','$appid$row1[0]','left=$row1[2],top=$row1[3],height=$row1[4],width=$row1[5],resizable=$row1[6],toolbar=$row1[7],location=$row1[8],status=$row1[9],scrollbars=$row1[10],menubar=$row1[11]')\">$row1[12]</A></td></tr>";
				}elsif($row1[15]==3){
					$temp_trtd = "<tr><td class=tdheader><A HREF=\"#\" class=\"letras\" onClick=\"ShowLayer('$row1[14]')\">$row1[12]</A></td></tr>";
				}
				$trtd      = $trtd."\n".$temp_trtd;
				$i++;
			}
			if($i>0){
				$table_open  ="<TABLE bordercolor=2F537A width=$row[6] border=1 cellpading=0 cellspacing=0>";
				$table_close ="</TABLE>";
			}
			$lay = "\"".$row[0]."\"";
			if ( $j!=0 ){
				$temp0 = $temp0.",".$lay;
			}else{
				$temp0 =$lay;
			}		
			$show_layers =$temp0; 
			$div_caption  ="";
			$j++;
		}
		$layer_open	= "<DIV ID=$row[0] STYLE=position:$row[2];left:$row[3]px;top:$row[4]px;height:$row[5]px;width:$row[6]px;z-index:$row[7];visibility:$row[8]>$div_caption";
		$layer_close	= "</DIV>";
		$line 	= $layer_div;
		$line	=~ s/{layer_open}/$layer_open/g;
		$line	=~ s/{layer_close}/$layer_close/g;
		$line	=~ s/{table_open}/$table_open/g;
		$line	=~ s/{table_close}/$table_close/g;		
		$line	=~ s/{trtd}/$trtd/g;
		$line 	=~ s/\{lan}/$lan/g;
		$line 	=~ s/\{appid}/$appid/g;		
		$options="$options"."\n"."$line";		
	}
}
sub construct_page{
	$user 	= $ENV{'REMOTE_USER'};
	open (DATA, "$path$file");
	while ($line = <DATA>){
		$line =~ s/\{lan}/$lan/g;
		$line =~ s/\{appid}/$appid/g;
		$line =~ s/\{path}/$path/g;
		$line =~ s/\{clase}/$clase/g;
		$line =~ s/\{id}/$id/g;		
		$line =~ s/\{layers}/$options/g;
		$line =~ s/\{show_layers}/$show_layers/g;
		if($user eq "admin"){$line =~ s/\{visibility}/visible/g}else{$line =~ s/\{visibility}/hidden/g}
		print "$line";
	}
}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#AUXILIAR-SUBROUTINES
sub get_app_data {
    my $schema  = "app.data.xml";
    my $xml     = new XML::Simple;
    my $data    = $xml->XMLin("../xml/".$schema);
    $appid      = $data->{portal}->{app};
    $lan        = $data->{portal}->{lang};
}
sub get_component_schema {
	my ($cmp)=@_;
	my $schema		= $cmp."_menu.xml";
	my $xml		= new XML::Simple;
	$data		= $xml->XMLin("../xml/".$schema);
	$path		= "../libraries/html/";
	$file		= "menul.html";
}
sub get_database_handlers {
	$dbh 	  = DBI->connect("dbi:ODBC:$appid",'','',{PrintError => 1, RaiseError => 1});
	if (!$dbh) {print "error: connection: $DBI::err\n$DBI::errstr\n$DBI::state\n";}
	$dbh->{LongReadLen} = 512 * 1024;
	$dbh1 	  = DBI->connect("dbi:ODBC:$appid",'','',{PrintError => 1, RaiseError => 1});
	if (!$dbh1) {print "error: connection: $DBI::err\n$DBI::errstr\n$DBI::state\n";}
	$dbh1->{LongReadLen} = 512 * 1024;
	$dbh2 	  = DBI->connect("dbi:ODBC:$appid",'','',{PrintError => 1, RaiseError => 1});
	if (!$dbh2) {print "error: connection: $DBI::err\n$DBI::errstr\n$DBI::state\n";}
	$dbh2->{LongReadLen} = 512 * 1024;	
}
sub SQL_fail {
    my ($query,$dbh,$section)= @_;
    print "SQL failed in Section $section<br>";
    print "Query: ".$query."<br>";    
    exit;
}
#-------------------------------------------------------------------------
