#!/perl/bin/perl.exe
$|=1;
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# Author:Rodolfo Machon
# Company:WebSoft, SA de CV 
# Nombre del archivo: user.clone.save.pl
# Created: 2016-08-05
# Updates: 2016-08-05,2022-08-12
# Copyright - All Rights Reserved
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# PERL MODULES
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use XML::Simple;
use Data::Dumper;
binmode STDOUT, ":encoding(utf8)";
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# HTML PRINTING ACTIVATION
print "Content-type: text/html; charset=utf-8\n\n";
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#GET PARAMETERS
$form           = new CGI;
$id_source      = $form->param('id_source');
$id_destine     = $form->param('i8_user_id');
$delete         = $form->param('borrar');
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# CHECK PARAMETERS
if ($id_source eq ""){print "No esta pasando SOURCE ID parameter<br>";}
if ($id_destine eq ""){print "No esta pasando DESTINE ID parameter<br>";}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# GET VARIABLES
get_app_data();
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
# OPEN DATABASE CONNECTIONS - ODBCs
$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;
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
#MAIN PROGRAM
if($id_source>0 && $id_destine>0){
    if($delete eq "on"){
        delete_user_menulayers();
        delete_user_menulayer_options();
        delete_user_options();
        delete_user_option_functions();
        delete_user_option_views();
        #delete_user_level();
        #delete_user_permissions();        
    }
    %uml = get_user_menulayers();
    insert_user_menulayers();
    
    %umlo = get_user_menulayer_options();
    insert_user_menulayer_options();
    
    %uo = get_user_options();
    insert_user_options();

    %uof = get_user_option_functions();
    insert_user_option_functions();
    
    %uov = get_user_option_views();
    insert_user_option_views();

    #%uol = get_user_option_level();
    #insert_user_option_level();    

    #%uop = get_user_option_permissions();
    #insert_user_option_permissions();
    
    print "<center>EL USUARIO FUE CLONADO EXITOSAMENTE</center>";
    print "<center><A class=link href=javascript:window.close()>CERRAR</a></center>";
}
exit;
#-------------------------------------------------------------------
# SUB-ROUTINES
sub delete_user_menulayers{
    my $sql = "delete from sdd_user_menulayers where i8_user_id='$id_destine'";
 	$dbh->do($sql);
}
sub get_user_menulayers{
    my %hash = ();
    my $sql="select i4_layer_id,i2_layer_type_id,i8_label_id,i4_layer2open_id,txt_comment,v8_position,i4_left,i4_top,i4_height,i4_width,i2_index,v8_visibility,c8_flag from sdd_user_menulayers where i8_user_id='$id_source'";
    #print $sql."<br>";
	my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_user_menulayers');
	while (my @row = $sth->fetchrow_array) {
        $hash{$row[0]} = [@row];
    }    
    return %hash;
}
sub insert_user_menulayers{
    foreach $k(sort keys %uml){
        my $sql = "insert into sdd_user_menulayers(i8_user_id,i4_layer_id,i2_layer_type_id,i8_label_id,i4_layer2open_id,txt_comment,v8_position,i4_left,i4_top,i4_height,i4_width,i2_index,v8_visibility,c8_flag,ts_update,ts_in)
                   values($id_destine,\"$uml{$k}[0]\",\"$uml{$k}[1]\",\"$uml{$k}[2]\",\"$uml{$k}[3]\",\"$uml{$k}[4]\",\"$uml{$k}[5]\",\"$uml{$k}[6]\",\"$uml{$k}[7]\",\"$uml{$k}[8]\",\"$uml{$k}[9]\",\"$uml{$k}[10]\",\"$uml{$k}[11]\",\"$uml{$k}[12]\",now(),now())";
        $dbh->do($sql);
    }
}
sub delete_user_menulayer_options{
    my $sql = "delete from sdd_user_menulayer_options where i8_user_id='$id_destine'";
 	$dbh->do($sql);
}
sub get_user_menulayer_options{
    my $count = 0;
    my %hash  = ();
    my $sql="select i4_layer_id,i2_link_type_id,i4_left,i4_top,i4_width,i4_height,v4_toolbar,v4_scrollbar,v4_menubar,v4_resizable,v4_location,v4_status,i8_label_id,i4_option_id,v128_url,i4_layer2open_id,txt_comment,i2_index from sdd_user_menulayer_options where i8_user_id='$id_source'";
	my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_user_menulayers_options');
	while (my @row = $sth->fetchrow_array) {
        $hash{$count} = [@row];
        $count++;
    }    
    return %hash;
}
sub insert_user_menulayer_options{
    foreach $k(sort keys %umlo){
        my $sql = "insert into sdd_user_menulayer_options(i8_user_id,i4_layer_id,i2_link_type_id,i4_left,i4_top,i4_width,i4_height,v4_toolbar,v4_scrollbar,v4_menubar,v4_resizable,v4_location,v4_status,i8_label_id,i4_option_id,v128_url,i4_layer2open_id,txt_comment,i2_index,ts_update,ts_in)
                   values($id_destine,\"$umlo{$k}[0]\",\"$umlo{$k}[1]\",\"$umlo{$k}[2]\",\"$umlo{$k}[3]\",\"$umlo{$k}[4]\",\"$umlo{$k}[5]\",\"$umlo{$k}[6]\",\"$umlo{$k}[7]\",\"$umlo{$k}[8]\",\"$umlo{$k}[9]\",\"$umlo{$k}[10]\",\"$umlo{$k}[11]\",\"$umlo{$k}[12]\",\"$umlo{$k}[13]\",\"$umlo{$k}[14]\",\"$umlo{$k}[15]\",\"$umlo{$k}[16]\",\"$umlo{$k}[17]\",now(),now())";
        $dbh->do($sql);
    }
}
sub delete_user_options{
    my $sql = "delete from sdd_user_options where i8_user_id='$id_destine'";
 	$dbh->do($sql);
}
sub get_user_options{
    my $count = 0;
    my %hash = ();
    my $sql="select i4_option_id,dt_option,txt_option,c8_estado,ts_update,ts_in from sdd_user_options where i8_user_id='$id_source'";
	my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_user_options');
	while (my @row = $sth->fetchrow_array) {
        $hash{$count} = [@row];
        $count++;
    }    
    return %hash;
}
sub insert_user_options{
    foreach $k(sort keys %uo){
        my $sql = "insert into sdd_user_options(i8_user_id,i4_option_id,dt_option,txt_option,c8_estado,ts_update,ts_in)
                   values($id_destine,\"$uo{$k}[0]\",\"$uo{$k}[1]\",\"$uo{$k}[2]\",\"$uo{$k}[3]\",now(),now())";
        $dbh->do($sql);
    }
}
sub delete_user_option_functions{
    my $sql = "delete from sdd_user_option_functions where i8_user_id='$id_destine'";
 	$dbh->do($sql);
}
sub get_user_option_functions{
    my $count = 0;
    my %hash = ();
    my $sql="select i4_option_id,i8_function_id,v128_function,dt_function,txt_function,c8_estado,v128_label,ts_update,ts_in from sdd_user_option_functions where i8_user_id='$id_source'";
	my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_user_option_functions');
	while (my @row = $sth->fetchrow_array) {
        $hash{$count} = [@row];
        $count++;
    }    
    return %hash;
}
sub insert_user_option_functions{
    foreach $k(sort keys %uof){
        my $sql = "insert into sdd_user_option_functions(i8_user_id,i4_option_id,i8_function_id,v128_function,dt_function,txt_function,c8_estado,v128_label,ts_update,ts_in)
                   values($id_destine,\"$uof{$k}[0]\",\"$uof{$k}[1]\",\"$uof{$k}[2]\",\"$uof{$k}[3]\",\"$uof{$k}[4]\",\"$uof{$k}[5]\",\"$uof{$k}[6]\",now(),now())";
        $dbh->do($sql);
    }
}
sub delete_user_option_views{
    my $sql = "delete from sdd_user_option_views where i8_user_id='$id_destine'";
 	$dbh->do($sql);
}
sub get_user_option_views{
    my $count = 0;
    my %hash = ();
    my $sql="select i4_option_id,i8_view_id,v128_view,dt_view,txt_view,c8_estado,v4_toolbar,v4_scrollbar,v4_menubar,v4_resizable,v4_location,v4_status,v64_folder,v64_opener,ts_update,ts_in from sdd_user_option_views where i8_user_id='$id_source'";
	my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_user_option_views');
	while (my @row = $sth->fetchrow_array) {
        $hash{$count} = [@row];
        $count++;
    }    
    return %hash;
}
sub insert_user_option_views{
    foreach $k(sort keys %uov){
        my $sql = "insert into sdd_user_option_views(i8_user_id,i4_option_id,i8_view_id,v128_view,dt_view,txt_view,c8_estado,v4_toolbar,v4_scrollbar,v4_menubar,v4_resizable,v4_location,v4_status,v64_folder,v64_opener,ts_update,ts_in)
                   values($id_destine,\"$uov{$k}[0]\",\"$uov{$k}[1]\",\"$uov{$k}[2]\",\"$uov{$k}[3]\",\"$uov{$k}[4]\",\"$uov{$k}[5]\",\"$uov{$k}[6]\",\"$uov{$k}[7]\",\"$uov{$k}[8]\",\"$uov{$k}[9]\",\"$uov{$k}[10]\",\"$uov{$k}[11]\",\"$uov{$k}[12]\",\"$uov{$k}[13]\",now(),now())";
        $dbh->do($sql);
    }
}
sub delete_user_option_levels{
    my $sql = "delete from sdd_user_option_levels where i8_user_id='$id_destine'";
 	$dbh->do($sql);
}
sub get_user_option_levels{
    my $count = 0;
    my %hash = ();
    my $sql="select i8_user_id,i4_option_id,c16_level,dt_level,txt_level,c8_estado,ts_update,ts_in from sdd_user_option_levels where i8_user_id='$id_source'";
	my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_user_option_levels');
	while (my @row = $sth->fetchrow_array) {
        $hash{$count} = [@row];
        $count++;
    }    
    return %hash;
}
sub insert_user_option_levels{
    foreach $k(sort keys %uol){
        my $sql = "insert into sdd_user_option_levels(i8_user_id,i4_option_id,c16_level,dt_level,txt_level,c8_estado,ts_update,ts_in)
                   values($id_destine,\"$uo{$k}[0]\",\"$uo{$k}[1]\",\"$uo{$k}[2]\",\"$uo{$k}[3]\",\"$uo{$k}[4]\",,now(),now())";
        $dbh->do($sql);
    }
}
sub delete_user_option_permissions{
    my $sql = "delete from sdd_user_option_permissions where i8_user_id='$id_destine'";
 	$dbh->do($sql);
}
sub get_user_option_permissions{
    my $count = 0;
    my %hash = ();
    my $sql="select i8_user_id,i4_option_id,c2_permissiontype_id,dt_permission,txt_permission,c8_estado,ts_update,ts_in from sdd_user_option_permissions where i8_user_id='$id_source'";
	my $sth = $dbh->prepare($sql);
	$sth->execute or die SQL_fail($sql,$dbh,'sub get_user_option_permissions');
	while (my @row = $sth->fetchrow_array) {
        $hash{$count} = [@row];
        $count++;
    }    
    return %hash;
}
sub insert_user_option_permissions{
    foreach $k(sort keys %uop){
        my $sql = "insert into sdd_user_option_permissions(i8_user_id,i4_option_id,c2_permissiontype_id,dt_permission,txt_permission,c8_estado,ts_update,ts_in)
                   values($id_destine,\"$uo{$k}[0]\",\"$uo{$k}[1]\",\"$uo{$k}[2]\",\"$uo{$k}[3]\",\"$uo{$k}[4]\",,now(),now())";
        $dbh->do($sql);
    }
}


#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#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};
}