#!/usr/bin/perl
# cache_filter.cgi
# filter Users to Services with Tarantella
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL 

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

$postform=$in{form};
$filter = "*";
if ( "$postform" eq "winldapusers" ) {
#     &headerstta($text{'index_winldapusers'}, "", $postform ,undef,1,1,$text{headpage_rigth} ); 
      &headerstta("", "", $postform,undef,1,1,$text{headpage_rigth} ); 
 } else {
      &headerstta($text{'index_filter'}, "", "filter" ,undef,1,1,$text{headpage_rigth} ); 
};

 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 "/******************************************************************\n"; 
 print "   Chek_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Chek_fields(obj) {\n"; 
 print "     ObjClass=get_value(\"select\",obj.objclass);\n";
 print "     if ( emptyField(obj.filter) == true ) {\n"; 
 print "        if ( ObjClass == \"none\" ) {\n";
 print "          alert(obj.filter.name+\":\"+msg1+\"->\"+obj.filter.value+\"<-\");\n"; 
 print "		    obj.filter.focus();\n"; 
 print "          return false;\n"; 
 print "        };\n";
 print "     }; \n"; 
# print "     if ( get_value(\"select\",obj.usergrphosts) == \"none\" ) {\n";
# print "        if ( get_value(\"select\",obj.hostname) == \"none\" ) {\n";
# print "           if ( get_value(\"select\",obj.grpappsname) == \"none\" ) {\n";
# print "              if ( get_value(\"select\",obj.appname) == \"none\" ) {\n";
# print "                 if ( get_value(\"select\",obj.domain) == \"none\" ) {\n";
# print "                      alert(\"error $text{'obj_target'}\"+\":\"+msg1);\n";
# print "                      obj.usergrphosts.focus();\n";
# print "                      return false;\n";
# print "                 }   \n";
# print "              }    \n";
# print "            }   \n";
# print "         }   \n";
# print "     }  \n";
 print "     onlyOne=false;  \n";
 print "     ObjValue=get_value(\"select\",obj.usergrphosts);\n";
 print "     if ( ObjValue != \"none\" ) {\n";
 print "          obj_value= ObjValue;\n";
 print "          obj.objectype.value= \"grphosts\";\n";
 print "          onlyOne=true;  \n";
 print "     }  \n";
 print "     ObjValue=get_value(\"select\",obj.hostname);\n";
 print "     if ( ObjValue != \"none\" ) {\n";
 print "          if ( onlyOne == true ) {  \n";
 print "               alert(\"error $text{'obj_target'}\"+\":\"+msg1);\n";
 print "               return false;\n";
 print "          } else {  \n";
 print "               obj_value= ObjValue;\n";
 print "               obj.objectype.value= \"hosts\";\n";
 print "               onlyOne=true;  \n";
 print "          }  \n";
 print "     }  \n";
 print "     ObjValue=get_value(\"select\",obj.appname);\n";
 print "     if ( ObjValue != \"none\" ) {\n";
 print "          if ( onlyOne == true ) {  \n";
 print "               alert(\"error $text{'obj_target'}\"+\":\"+msg1);\n";
 print "               return false;\n";
 print "          } else {  \n";
 print "               obj_value= ObjValue;\n";
 print "               obj.objectype.value= \"apps\";\n";
 print "               onlyOne=true;  \n";
 print "          }  \n";
 print "     }  \n";
 print "     ObjValue=get_value(\"select\",obj.grpappsname);\n";
 print "     if ( ObjValue != \"none\" ) {\n";
 print "          if ( onlyOne == true ) {  \n";
 print "               alert(\"error $text{'obj_target'}\"+\":\"+msg1);\n";
 print "               return false;\n";
 print "          } else {  \n";
 print "               obj_value= ObjValue;\n";
 print "               obj.objectype.value= \"grpapps\";\n";
 print "               onlyOne=true;  \n";
 print "          }  \n";
 print "     }  \n";
 print "     ObjValue=get_value(\"select\",obj.domain);\n";
 print "     if ( ObjValue != \"none\" ) {\n";
 print "          if ( onlyOne == true ) {  \n";
 print "               alert(\"error $text{'obj_target'}\"+\":\"+msg1);\n";
 print "               return false;\n";
 print "          } else {  \n";
 print "               obj_value= ObjValue;\n";
 print "               obj.objectype.value= \"domains\";\n";
 print "               onlyOne=true;  \n";
 print "          }  \n";
 print "     }  \n";
 print "     if ( onlyOne == false ) {  \n";
 print "               obj.base.value= \"".$config{ldapcache_base}."\";\n";
 print "               obj.objectype.value= \"all\";\n";
 print "     } else {  \n";
 print "               ObjOUValue=get_value(\"select\",obj.userou);\n";
if ( $debug eq 2 ) { print "               alert(obj_value);  \n"; };
if ( $debug eq 2 ) { print "               alert(ObjOUValue);  \n"; };
 print "               if ( ObjOUValue != \"none\" ) {\n";
 print "                    obj.base.value= ObjOUValue+\",\"+obj_value+\",".$config{ldapcache_base}."\";\n";
 print "               } else {  \n";
 print "                    obj.base.value= obj_value+\",".$config{ldapcache_base}."\";\n";
 print "               }  \n";
 print "     }  \n";
 if ( $debug eq 1 ) { print "     alert(obj.filter.value);  \n"; };
 if ( $debug eq 1 ) { print "     alert(obj.base.value);  \n"; };
 print "     return true;\n";
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
 print "<p>\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_cachefilter'}  </b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<form METHOD=POST name=tta_filter ACTION=\"cache_selobj.cgi?form=$postform\" onSubmit=\"return Chek_fields(this)\">\n"; 
 print "  <input TYPE=\"hidden\" NAME=\"base\" VALUE=\"\">\n"; 
 print "  <input TYPE=\"hidden\" NAME=\"objectype\" VALUE=\"\">\n"; 
 
 print "<tr><td valign=top><b>$text{'obj_origin'}</b>&nbsp;</td></tr>\n";

 print "	<td valign=top>",&hlink("<b>$text{'f_filter'}</b>","ffilter"),"&nbsp;</td>";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"filter\" VALUE=\"$filter\" cols=\"22\" size=\"12\" ALIGN=left >\n"; 
 print "   &nbsp;<select NAME=\"objclass\">\n"; 
 print " 		<option VALUE=\"none\">",&hlink("<b>$text{'f_selecobjclass'}</b>","fselecobjclass"),"\n"; 
@l_objclass = split(/,/,$text{'dn_objclass'}, -1);
@l_tit_objclass = split(/,/,$text{'dn_tit_objclass'}, -1);
$nItems=0;
foreach $item (
@l_objclass) {
     print "         <option VALUE=\"$item\" ";
     print ">$l_tit_objclass[$nItems]</b>\n";
     $nItems++;
}
print "        </select></td></tr>\n";
 print "   <tr><td valign=top>",&hlink("<b>$text{'f_userou'}</b>","fuserou"),"&nbsp;</td><td valign=top><select NAME=\"userou\">\n"; 
 print " 		<option VALUE=\"none\">",&hlink("<b>$text{'f_selecou'}</b>","fselecou"),"\n"; 
$nItems=0;
%h_ou=&list_ous();
foreach $item (keys (%h_ou)) {
    print " <option VALUE=\"".$h_ou{$item}->{'obj'}."\">". $h_ou{$item}->{'name'}."\n";
    $nItems++;
}
 print "	</select></td></tr>\n"; 
 print "   <tr><td valign=top>",&hlink("<b>$text{'f_method'}</b>","fmethod"),"&nbsp;</td><td valign=top><select NAME=\"method\">\n"; 
 print " 		<option VALUE=\"none\">",&hlink("<b>$text{'f_selecmethod'}</b>","fselecmethod"),"\n"; 

@l_methodby = split(/,/,$text{'dn_method'}, -1);
@l_methodtit = split(/,/,$text{'dn_tit_method'}, -1);
$nItems=0;
foreach $item (
@l_methodby) {
     print "  	     <option VALUE=\"$item\" ";
     print ">$l_methodtit[$nItems]</b>\n";
     $nItems++;
}
 print "	</select></td></tr>\n"; 
 print "<tr><td valign=top><b>$text{'obj_target'}</b>&nbsp;</td></tr>\n";

 print "   <tr><td valign=top>",&hlink("<b>$text{'f_appobjname'}</b>","fappobjname"),"&nbsp;</td>\n";
 print "   <td valign=top><select NAME=\"appname\">\n";
 print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecapp'}</b>","fselecapps"),"\n";
 $nItems=0;
 %h_app=&list_apps();
 foreach $item (keys (%h_app)) {
     print " <option VALUE=\"".$h_app{$item}->{'obj'}."\">". $h_app{$item}->{'name'}."\n";
     $nItems++;
 }
  print "        </select></td></tr>\n";
  print "   <tr><td valign=top>",&hlink("<b>$text{'f_grpappsobjname'}</b>","fgrpappsobjname"),"&nbsp;</td>\n";
  print "   <td valign=top><select NAME=\"grpappsname\">\n";
  print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecgrpapps'}</b>","fselecgrpapps"),"\n";
 $nItems=0;
 %h_grpapp=&list_grpapps();
 foreach $item (keys (%h_grpapp)) {
     print " <option VALUE=\"".$h_grpapp{$item}->{'obj'}."\">". $h_grpapp{$item}->{'name'}."\n";
     $nItems++;
 }
 print "        </select></td></tr>\n";
  print "   <tr><td valign=top>",&hlink("<b>$text{'f_hostname'}</b>","fhostname"),"&nbsp;</td>\n";
  print "   <td valign=top><select NAME=\"hostname\">\n";
  print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selechosts'}</b>","fselechosts"),"\n";
 $nItems=0;
 %h_host=&list_hosts();
 foreach $item (keys (%h_host)) {
     print " <option VALUE=\"".$h_host{$item}->{'obj'}."\">". $h_host{$item}->{'name'}."\n";
     $nItems++;
 }
 print "   </select></td></tr>\n";
 print "   <tr><td valign=top>",&hlink("<b>$text{'f_usergrphosts'}</b>","fusergrphosts"),"&nbsp;</td>\n"
   ;
  print "   <td valign=top><select NAME=\"usergrphosts\">\n";
  print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecgrphosts'}</b>","fselecgrphosts"),"\n";
 $nItems=0;
 %h_grphost=&list_grphosts();
 foreach $item (keys (%h_grphost)) {
     print " <option VALUE=\"".$h_grphost{$item}->{'obj'}."\">". $h_grphost{$item}->{'name'}."\n";
     $nItems++;
 }
  print "        </select></td></tr>\n";
  print "        <tr><td valign=top>",&hlink("<b>$text{'f_domain'}</b>","fdomain"),"</td>\n";
 print "   <td valign=top><select NAME=\"domain\">\n";
 print "                <option VALUE=\"none\">",&hlink("<b>$text{'f_selecdomain'}</b>","fselecapps"),"\n";
 $nItems=0;
 %h_domain=&list_domains();
 foreach $item (keys (%h_domain)) {
     print " <option VALUE=\"".$h_domain{$item}->{'obj'}."\">". $h_domain{$item}->{'name'}."\n";
     $nItems++;
 }
  print "        </select></td></tr>\n";

 print "<tr><td valign=top><b>-----------</b>&nbsp;</td></tr>\n";
 print "   <tr><td valign=top>",&hlink("<b>$text{'f_orderby'}</b>","forderby"),"&nbsp;</td><td valign=top><select NAME=\"sort_on\">\n"; 
 print " 		<option VALUE=\"none\">",&hlink("<b>$text{'f_selecord'}</b>","fselecou"),"\n"; 

@l_orderby = split(/,/,$text{'dncache_orderby'}, -1);
@l_tit_orderby = split(/,/,$text{'dncache_tit_orderby'}, -1);
$nItems=0;
foreach $item (
@l_orderby) {
     print "         <option VALUE=\"$item\" ";
     print ">$l_tit_orderby[$nItems]</b>\n";
     $nItems++;
}
 print "	</select></td></tr>\n"; 

 print "   <tr><td valign=top>",&hlink("<b>$text{'f_ldapcacheserver'}</b>","fldapcacheserver"),"&nbsp;</td><td valign=top><select NAME=\"ldapcacheserver\">\n";
 print " <option VALUE=\"1\" >".$config{ldapcache_servertitle}."\n";
if ( $config{ldapcache2_server}.length gt 0 ) {
 print " <option VALUE=\"2\" >".$config{ldapcache2_servertitle}."\n";
}
 print "        </select></td></tr>\n";

 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></td></tr>\n"; 
 print "<tr><tr></table></table><hr><p>\n";
if ( "$postform" ne "winldapusers") {  
   &footer($config{'back_return'},$text{'index'}); 
}
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
