#!/perl/bin/perl.exe
$|=1;
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# Information Software SA
# Date In: 2009-08-25
# Updates: 2009-08-25
# Author:Rodolfo Machon
# File Name: users_clone_save.pl
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
#PERL MODULES
use Win32::ODBC;
use CGI;
#------------------------------------------------------------------------- 
#-------------------------------------------------------------------------
#HTML printing activation
print "Content-type: text/html\n\n";
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#GET PARAMETERS
$form           = new CGI;
$lan            = $form->param('lan');
$appid          = $form->param('appid');
$id_source      = $form->param('id_source');
$id_destine     = $form->param('i8_user_id');
$delete         = $form->param('borrar');
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
# CHECK PARAMETERS
if ($lan eq ""){print "No esta pasando LAN parameter<br>";}
if ($appid eq ""){print "No esta pasando APPID parameter<br>";}
if ($id_source eq ""){print "No esta pasando SOURCE ID parameter<br>";}
if ($id_destine eq ""){print "No esta pasando DESTINE ID parameter<br>";}
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#MAIN PROGRAM
if ( !( $db = new Win32::ODBC($appid) ) ) {print "Error connecting to $appid <br>";print "Error: " . Win32::ODBC::Error() . "<br>";exit;}
if ( !( $db1 = new Win32::ODBC($appid) ) ) {print "Error connecting to $appid <br>";print "Error: " . Win32::ODBC::Error() . "<br>";exit;}

if($id_source>0 && $id_destine>0){
    
    if($delete eq "on"){
        delete_user_menulayers();
        delete_user_menulayer_options();
    }
    %uml = get_user_menulayers();
    insert_user_menulayers();
    
    %umlo = get_user_menulayer_options();
    insert_user_menulayer_options();
    print "<center>EL USUARIO FUE CLONADO EXITOSAMENTE</center>";
    print "<center><A class=link href=javascript:window.close()>CERRAR</a></center>";
}
$db->Close();
$db1->Close();
exit;
#-------------------------------------------------------------------
# SUB-ROUTINES
sub delete_user_menulayers{
    my $sql = "delete from sdd_user_menulayers where i8_user_id='$id_destine'";
    if($db->Sql($sql)){print "Error:$sql".$db->Error();exit;}
}
sub get_user_menulayers{
    my %layers = ();
    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'";
    if($db->Sql($sql)){print "Error:$sql".$db->Error();exit;}
    while($db->FetchRow()){
        undef my @row;
        @row = $db->Data();
        $layers{$row[0]} = [@row];
    }    
    return %layers;
}
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())";
        if($db1->Sql($sql)){print "Error:$sql".$db1->Error();exit;}    
    }
}
sub delete_user_menulayer_options{
    my $sql = "delete from sdd_user_menulayer_options where i8_user_id='$id_destine'";
    if($db->Sql($sql)){print "Error:$sql".$db->Error();exit;}
}
sub get_user_menulayer_options{
    my $count = 0;
    my %layer_options = ();
    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 from sdd_user_menulayer_options where i8_user_id='$id_source'";
    if($db->Sql($sql)){print "Error:$sql".$db->Error();exit;}
    while($db->FetchRow()){
        undef my @row;
        @row = $db->Data();
        $layer_options{$count} = [@row];
        $count++;
    }    
    return %layer_options;
}
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,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]\",now(),now())";
        if($db1->Sql($sql)){print "Error:$sql".$db1->Error();exit;}    
    }
}