#!/usr/bin/perl
# my_obj_chooser.cgi
# A modified version of chooser.cgi that uses the my_ functions to select objs
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]

require '../web-lib.pl';
do 'web-lib.pl';
do 'global-lib.pl';
do 'lists-lib.pl';
do 'html-lib.pl';

&init_config();
&load_dataconf();

&ReadParse(undef, undef, 1);
%access = &get_module_acl(undef, "");
$obj=$in{obj};
$objtype=$in{type};
$fieldType=$in{f};
$sortobj=$in{ord};
$defobj=$in{deft};
if ( $objtype eq "ou" ) { %h_objs=&list_ous(); }
if ( $objtype eq "domains" ) { %h_objs=&list_domains(); }
if ( $objtype eq "links" ) { %h_objs=&list_links(); }
if ( $objtype eq "hosts" ) { %h_objs=&list_hosts(); }
if ( $objtype eq "grphosts" ) { %h_objs=&list_grphosts(); }
if ( $objtype eq "apps" ) { %h_objs=&list_apps(); }
if ( $objtype eq "grpapps" ) { %h_objs=&list_grpapps(); }
$obj_text="choose_list".$objtype;
$obj_title=$text{$obj_text};
$obj_text="index_".$objtype;
$obj_index=$text{$obj_text};
$obj_text="choose_sel".$objtype;
$obj_seltit=$text{$obj_text};

if ($in{'multi'}) {
	# selecting multiple objs.
	if ($in{'frame'} == 0) {
		# base frame
		&PrintHeader();
		print "<script>\n";
                @hl = split(/\s+/, $obj);
	        $len = @hl;
		print "sel = new Array($len);\n";
		for($i=0; $i<$len; $i++) {
			print "sel[$i] = \"$hl[$i]\";\n";
                        $h=$hl[$i];
			}
		print "</script>\n";
		print "<title>$obj_index</title>\n";
		print "<frameset cols='75%,25%'>\n";
		print "<frame src=\"obj_chooser.cgi?frame=1&type=$objtype&multi=1&ord=$sortobj&deft=$defobj&f=$fieldType&obj=$obj\">\n";
		print "<frameset rows='*,50' frameborder=no>\n";
		print " <frame src=\"obj_chooser.cgi?frame=2&multi=1\">\n";
		print " <frame src=\"obj_chooser.cgi?frame=3&multi=1\" scrolling=no>\n";
		print "</frameset>\n";
		print "</frameset>\n";
		}
	elsif ($in{'frame'} == 1) {
		# list of all objs to choose from
                @hl = split(/\s+/, $obj);
	        $len = @hl;
		&header();
                print &out_css();
		print "<script>\n";
		print "function addobj(h)\n";
		print "{\n";
                print "   for (var i=0; i < top.sel.length; i++) {\n";
                print "      if ( top.sel[i] == h  ) { \n";
                print "            return false; \n";
                print "       }\n";
                print "   }\n";
		print "   top.sel[top.sel.length] = h\n";
                print "   top.opener.ifield.value = top.sel.join(' ');\n";
		print "   top.frames[1].location = top.frames[1].location;\n";
		print "   return false;\n";
		print "}\n";
		print "</script>\n";
		print "<table border=0><tr><td><font size=+1>$obj_title</font></td>\n";
		print "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>\n";
                print "<table border=1><tr $cb><td>$text{choose_msg_objtosel}</td></tr></table>\n";
		print "</td></tr></table><hr>\n";
	        print "<table width=100% border=1><tr><td $cb>\n";
                &out_htmlOMobj($objtype,\%h_objs,$sortobj,$defobj,$obj,
                       "onClick=\"return addobj('VALUE')\"");
        	print "</td></tr></table>\n";
                print "<br><br><table border=1><tr $cb><td>$text{choose_msg_objtosel}</td></tr></table><br>\n";
		}
	elsif ($in{'frame'} == 2) {
		# show chosen objs
		&header();
                print &out_css();
		print "<font size=+1>$obj_seltit</font><br><hr>\n";
                print "<table width=100% border=1 $cb>\n";
		print <<'EOF';
<script>
function sub(j)
{
sel2 = new Array();
for(k=0,l=0; k<top.sel.length; k++) {
	if (k != j) {
		sel2[l] = top.sel[k];
		l++;
		}
	}
top.sel = sel2; 
top.opener.ifield.value = top.sel.join(' ');
top.frames[1].location = top.frames[1].location;
return false;
}
for(i=0; i<top.sel.length; i++) {
	document.write("<tr>\n");
	document.write("<td><a class=\"menu1\" href=\"\" onClick=\"return sub("+i+")\">"+top.sel[i]+"</a></td></tr>\n");
	}
</script>
</table>
EOF
print "<br><br><table border=1><tr $cb><td>$text{choose_msg_tounsel}</td></tr></table><br>\n";
		}
	elsif ($in{'frame'} == 3) {
		# output OK and Cancel buttons
		&header();
		print "<form>\n";
		print "<input type=button value=\"$text{'choose_ok'}\" ",
		      "onClick=\" ",
		      "top.close()\">\n";
#		      "onClick=\"top.opener.ifield.value = top.sel.join(' '); ",
		print "<input type=button value=\"$text{'choose_cancel'}\" ",
		      "onClick='top.close()'>\n";
		print "&nbsp;&nbsp;<input type=button value=\"$text{'choose_clear'}\" onClick=\"top.sel = new Array(); top.frames[1].location = top.frames[1].location\">\n";
		print "</form>\n";
		}
	}
else {
	# selecting just one user .. display a list of all users to choose from
	&header();
	print "<script>\n";
	print "function select(f)\n";
	print "{\n";
        if ( $fieldType eq "select" ) {
   	     print "     var elem=top.opener.selfield;\n";
             print "     var j  = elem.length; \n";
             print "     var k  = -1; \n";
             print "     var opts = elem.options; \n";
             print "     for(i = 0; i < j; i++){ \n";
             print "         if (opts[i].selected ) { \n";
             print "              opts[i].selected=\"\"; \n";
             print "         }  \n";
             print "         if (opts[i].value == f ){ \n";
             print "              opts[i].selected=\"1\"; \n";
             print "         }  \n";
             print "     } \n";
        } else {
   	     print "   top.opener.ifield.value = f;\n";
        }
	print "   top.close();\n";
	print "   return false;\n";
	print "}\n";
	print "</script>\n";
	print "<table border=0><tr><td><font size=+1>$obj_title</font></td>\n";
	print "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>\n";
        print "<table border=><tr $cb><td>$text{choose_msg_objtosel}</td></tr></table>\n";
	print "</td></tr></table><hr>\n";
	print "<table width=100% border=1><tr><td $cb>\n";
        &out_htmlOMobj($objtype,\%h_objs,$sortobj,$defobj,$obj,
               "onClick=\"return select('VALUE')\"");
	print "</td></tr></table>\n";
}

sub get_obj_list
{
  local @listobjs=();
  foreach $item (keys (%h_objs)) { push (@listobjs, $h_objs{$item}); }
  return sort {$a->{'descrip'} cmp $b->{'descrip'}} @listobjs;
}

