#!/usr/bin/perl
# cache_addapp.cgi
# Add APP objects 
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]

require './stta-lib.pl';
do 'app-lib.pl';
&ReadParse();

&headerstta($text{'index_addapp'}, "","addapp" ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 

#if ( $config{'is_ldapcache'} ne "true" ) { &footCacheHere; exit }

$tta_AppType= $in{ttaapptype};
%h_tta_apps=&load_tta_apptypes();
if ( ! defined ($h_tta_apps{$tta_AppType}) ||  $h_tta_apps{$tta_AppType} eq "" ) {
    $tta_AppType="";
    @appdef=();
    $ttacmd="";
} else {
    if ( $tta_AppType =~ /windows/ ) { $tta3tier_domain=$config{cache_domain};  }
    @appdef=&get_tta_appdef($tta_AppType);
    $ttacmd="new_".$tta_AppType;
    $temp_f=$config{stta_defslist}."/".$tta_AppType."-defaults";
    if ( -r $temp_f ) {
         %h_ttaobj=&load_ttaobj();
         &read_file("$root_directory/$module_name/dataconf/map_vars", \%h_map);
         %h_hosts=&list_hosts(4);
         foreach $data (@appdef) {
            $i=$data->{att};
            next if ( ! defined($h_ttaobj{$i}{value}) || $h_ttaobj{$i}{value} eq "" ); 
            if ( $i eq "appserv" ) {
                $hosts_list ="";
                foreach $h (split(/,/, $h_ttaobj{$i}{value})) {
                        $h=~ s/$config{tta_base}\///g;
                        if ( $hosts_list ne "" ) { $hosts_list .= "\n" }
                        $hosts_list .=$h_hosts{$h}->{alias};
                }
                $data->{'value'} = $hosts_list;
            } else {
                $ens_map="ens_".$i."_".$h_ttaobj{$i}{value};
                if ( defined($h_map{$ens_map}) ) {
                      $data->{'value'} = $h_map{$ens_map};
                } else {
                      $data->{'value'} = $h_ttaobj{$i}{value};
                }
            }
         }
    }
}
 $grpapps=$in{grpapps};
 $ntdomain="";
 $maxsess=$config{default_maxsess}; 
 print "<hr><p>\n";
 print "<SCRIPT SRC=\"global.js\">\n"; 
 print "</SCRIPT>\n"; 
 print "<SCRIPT LANGUAGE=\"JavaScript\">\n"; 
 print "<!--\n"; 
 print "/*  Strings for locales  */\n"; 
 print "var msg0=\"".$text{'msg_0'},"\";\n";
 print "var msg1=\"".$text{'msg_1'},"\";\n";
 print "var msg2=\"".$text{'msg_2'},"\";\n";
 print "var msg3=\"".$text{'msg_3'},"\";\n";
 print "var msg4=\"".$text{'msg_4'},"\";\n";
 print "var msg5=\"".$text{'msg_5'},"\";\n";
 print "var msg6=\"".$text{'msg_6'},"\";\n";
 print "var msg7=\"".$text{'msg_7'},"\";\n";
 print "var ask_confadmin=true;\n"; 
 print "\n"; 
 if ( $tta_AppType ne "" ) {
    &htmlCheckAlias("apps");
    if ( $config{'is_ldapcache'} eq "true" ) { &htmlCacheaShowPassw }
 }
 print "/******************************************************************\n"; 
 print "   Check_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Check_fields(obj) {\n"; 
 if ( $tta_AppType ne "" ) {
      print "     if ( get_value(\"select\",obj.grpapps) == \"none\" ) {\n";
      print "          alert(obj.grpapps.name+\":\"+msg1);\n";
      print "                    obj.grpapps.focus();\n";
      print "          return false;\n";
      print "     }         \n";
      print "     if ( emptyField(obj.appname) == true ) {\n"; 
      print "          alert(obj.appname.name+\":\"+msg1+\"->\"+obj.appname.value+\"<-\");\n"; 
      print "		    obj.appname.focus();\n"; 
      print "          return false;\n"; 
      print "     }         \n"; 
      print "     if ( emptyField(obj.aliasapp) == true ) {\n"; 
      print "          alert(obj.aliasapp.name+\":\"+msg1+\"->\"+obj.aliasapp.value+\"<-\");\n"; 
      print "		    obj.aliasapp.focus();\n"; 
      print "          return false;\n"; 
      print "     }         \n"; 
      print "     if ( checkAlias_apps(obj.aliasapp) == true ) {\n";
      print "          return false;\n";
      print "     }         \n";
      print "     nro=parseInt(obj.maxsess.value);\n";
      print "     if ( nro+1 > 0) { \n";
      print "          obj.maxsess.value=nro;\n";
      print "     } else {;\n";
      print "          alert(obj.maxsess.name+\":\"+msg1+\"->\"+obj.maxsess.value+\"<-\");\n";
      print "          obj.maxsess.focus();\n";
      print "          return false;\n";
      print "     };\n";
      &out_tta_app_js(@appdef); 
      if ( $config{'is_ldapcache'} eq "true" ) { &script_cache_chek; };
      print "     if ( get_value(\"select\",obj.grpapps) != \"none\" ) {\n"; 
      print "          return confirm(obj.appname.value+msg7+get_value('select',obj.grpapps));\n"; 
      print "     }         \n"; 
      print "\n"; 
 } else { 
      print "     if ( get_value(\"select\",obj.ttaapptype) == \"none\" ) {\n";
      print "          alert(obj.ttaapptype.name+\":\"+msg1);\n";
      print "                    obj.ttaapptype.focus();\n";
      print "          return false;\n";
      print "     }         \n";
 }
 print "     return true;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
 print "\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_adapp'}</b> -- $h_tta_apps{$tta_AppType} --</td> </tr>\n";
 if ( $tta_AppType ne "" ) {
     print "<tr $cb> <td><table width=100%>\n";
     print "<form METHOD=POST name=newapp ACTION=\"addapps.cgi\" onSubmit=\"return Check_fields(this)\">\n"; 
 print "	<input TYPE=\"hidden\" NAME=\"ttacmd\" VALUE=\"",$ttacmd,"\">\n"; 
 print "	<input TYPE=\"hidden\" NAME=\"ttaapptype\" VALUE=\"",$tta_AppType,"\">\n"; 
     print "	<tr><td valign=top>",&hlink("<b>$text{'f_appobjname'}</b>","fappobjname"),"&nbsp;</td>\n";
    print "		<td valign=top><input TYPE=\"text\" NAME=\"appname\" VALUE=\"\" cols=\"35\" size=\"35\" ALIGN=left></td></tr>\n"; 
     print "	<tr><td valign=top>",&hlink("<b>$text{'f_descrip'}</b>","fdescrip"),"</td>\n"; 
     print "	<td valign=top><input TYPE=\"text\" NAME=\"descrip\" VALUE=\"$descrip\" cols=\"40\" size=\"40\" ALIGN=left ></td></tr>\n"; 
     print "   <tr><td valign=top>",&hlink("<b>$text{'f_grpapps'}</b>","fgrpapps"),"&nbsp;</td>\n";
     print "   <td valign=top><select NAME=\"grpapps\">\n";
     print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecgrpapps'}</b>","fselecgrpapps"),"\n";
    %h_grpapps=&list_grpapps();
    &out_htmlselobj(\%h_grpapps,"descrip","alias",$grpapps);
     print "        </select>\n";
      print "<input type=button onClick='selfield = document.forms[0].grpapps; ifield = document.forms[0].grpapps.options[document.forms[0].grpapps.selectedIndex]; if ( ifield.value == \"$grpapps\" ) { ifield.value = \"\" } ; chooser = window.open(\"/stta/obj_chooser.cgi?type=grpapps&multi=$multiobj&ord=ens&deft=alias&f=select&obj=\"+escape(ifield.value), \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,width=800,height=500\"); chooser.ifield = ifield; window.ifield = ifield' value=\"$text{browse}\"></td>\n";
     print "</tr>";
     print "	<tr><td valign=top>",&hlink("<b>$text{'f_aliasapp'}</b>","faliasapp"),"&nbsp;</td>\n";
    print "         <td valign=top> <input TYPE=\"text\" NAME=\"aliasapp\" VALUE=\"\" cols=\"12\" size=\"12\" ALIGN=left onChange=\"checkAlias_apps(this)\"></td></tr>\n"; 
     print "        <tr><td valign=top>",&hlink("<b>$text{'f_maxsess'}</b>","fmaxsess"),"&nbsp;</td>\n";
     print "        <td valign=top><input TYPE=\"text\" NAME=\"maxsess\" VALUE=\"$maxsess\" cols=\"5\" size=\"5\" ALIGN=left onChange=\"isNroThis(this)\"></td></tr>\n";
 print "        <tr><td>",&hlink("<b>$text{'f_norunttacmd'}</b>","fnorunttacmd"),"</td>\n";
 print "        <td><input type=\"checkbox\" name=\"ttanorun\" value=\"true\" ></td></tr>\n";
      
     print "</table></td></tr>\n"; 
     print "<tr $cb><td><b>$text{tta_obj}</b> -- $h_tta_apps{$tta_AppType} -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$text{msg_fieldsreq}</td></tr>\n"; 
     print "<tr $cb> <td><table width=100% border=1 >\n"; 
     &out_tta_appform(@appdef);
     print "</td></tr></table></td></tr>\n"; 
     print "<tr $cb> <td><table width=100%>\n"; 
     if ( $config{'is_ldapcache'} eq "true" ) { $formObj="newapp";$msgObj=$text{'default_obj'};&htmlCacheapp(msgObj) }
 } else {
    print "<tr $cb> <td><table align=left>\n";
    print "<form METHOD=POST name=newapp ACTION=\"cache_addapps.cgi\" onSubmit=\"return Check_fields(this)\">\n"; 
    &sel_tta_app();
 }
 print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\">\n";
 print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"reset\" NAME=\"",$text{'f_cancelreq'},"\" VALUE=\"",$text{'f_cancelreq'},"\"></td></tr>\n";
 print "	</form>\n"; 
 print "<tr><tr></table></table><hr><p>\n"; 
 &footer($config{'back_return'},$text{'index'}); 
 print "<p><p><p>\n";
 do "footer.pl";
 exit;



