[LON-CAPA-cvs] cvs: loncom /interface lonsearchcat.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 08 Mar 2002 18:36:00 -0000


This is a MIME encoded message

--matthew1015612560
Content-Type: text/plain

matthew		Fri Mar  8 13:36:00 2002 EDT

  Modified files:              
    /loncom/interface	lonsearchcat.pm 
  Log:
  Changes to support calling from construction space (see edit::search and
  lonxml for javascript hooks).  Many code cleanups, including the utility
  functions popwin_js and popwin_imgupdate, for communicating with the
  search status window.
  
  
--matthew1015612560
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020308133600.txt"

Index: loncom/interface/lonsearchcat.pm
diff -u loncom/interface/lonsearchcat.pm:1.116 loncom/interface/lonsearchcat.pm:1.117
--- loncom/interface/lonsearchcat.pm:1.116	Thu Mar  7 15:12:36 2002
+++ loncom/interface/lonsearchcat.pm	Fri Mar  8 13:36:00 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.116 2002/03/07 20:12:36 matthew Exp $
+# $Id: lonsearchcat.pm,v 1.117 2002/03/08 18:36:00 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -111,6 +111,7 @@
     }
 }
 
+#------------------------------------------------------------- global variables
 my $diropendb = "";
 my $domain = "";
 
@@ -127,7 +128,7 @@
     $diropendb= "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
 
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-             ['catalogmode','launch','acts']);
+             ['catalogmode','launch','acts','mode','form','element']);
 
     if ($ENV{'form.launch'} eq '1') {
 	if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
@@ -161,7 +162,12 @@
 onClick='javascript:select_group()'>
 END
     }
-
+    $hidden .= <<END;
+<input type='hidden' name='mode'    value='$ENV{'form.mode'}'>
+<input type='hidden' name='form'    value='$ENV{'form.form'}'>
+<input type='hidden' name='element' value='$ENV{'form.element'}'>
+<input type='hidden' name='date' value='2'>
+END
 # ------------------------------------------------------ Determine current user
     $yourself=$ENV{'user.name'}.'@'.$ENV{'user.domain'};
 
@@ -405,142 +411,42 @@
     my ($name,$defaultmonth,$defaultday,$defaultyear,
 	$currentmonth,$currentday,$currentyear)=@_;
     ($defaultmonth,$defaultday,$defaultyear)=('','','');
-    my $month=<<END;
-<select name="${name}_month">
-<option value='$defaultmonth'> </option>
-<option value="1">January</option>
-<option value="2">February</option>
-<option value="3">March</option>
-<option value="4">April</option>
-<option value="5">May</option>
-<option value="6">June</option>
-<option value="7">July</option>
-<option value="8">August</option>
-<option value="9">September</option>
-<option value="10">October</option>
-<option value="11">November</option>
-<option value="12">December</option>
-</select>
-END
-    $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
+    #
+    # Day
     my $day=<<END;
 <select name="${name}_day">
 <option value='$defaultday'> </option>
-<option value="1">1</option>
-<option value="2">2</option>
-<option value="3">3</option>
-<option value="4">4</option>
-<option value="5">5</option>
-<option value="6">6</option>
-<option value="7">7</option>
-<option value="8">8</option>
-<option value="9">9</option>
-<option value="10">10</option>
-<option value="11">11</option>
-<option value="12">12</option>
-<option value="13">13</option>
-<option value="14">14</option>
-<option value="15">15</option>
-<option value="16">16</option>
-<option value="17">17</option>
-<option value="18">18</option>
-<option value="19">19</option>
-<option value="20">20</option>
-<option value="21">21</option>
-<option value="22">22</option>
-<option value="23">23</option>
-<option value="24">24</option>
-<option value="25">25</option>
-<option value="26">26</option>
-<option value="27">27</option>
-<option value="28">28</option>
-<option value="29">29</option>
-<option value="30">30</option>
-<option value="31">31</option>
-</select>
 END
+    for (my $i = 1; $i<=31; $i++) {
+	$day.="<option value=\"$i\">$i</option>\n";
+    }
+    $day.="</select>\n";
     $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
+    #
+    # Month
+    my $month=<<END;
+<select name="${name}_month">
+<option value='$defaultmonth'> </option>
+END
+    my $i = 1;
+    foreach (qw/January February March April May June 
+	     July August September October November December /){
+	$month .="<option value=\"$i\">$_</option>\n";
+	$i++;
+    }
+    $month.="</select>\n";
+    $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
+    #
+    # Year (obviously)
     my $year=<<END;
 <select name="${name}_year">
 <option value='$defaultyear'> </option>
-<option value="1976">1976</option>
-<option value="1977">1977</option>
-<option value="1978">1978</option>
-<option value="1979">1979</option>
-<option value="1980">1980</option>
-<option value="1981">1981</option>
-<option value="1982">1982</option>
-<option value="1983">1983</option>
-<option value="1984">1984</option>
-<option value="1985">1985</option>
-<option value="1986">1986</option>
-<option value="1987">1987</option>
-<option value="1988">1988</option>
-<option value="1989">1989</option>
-<option value="1990">1990</option>
-<option value="1991">1991</option>
-<option value="1992">1992</option>
-<option value="1993">1993</option>
-<option value="1994">1994</option>
-<option value="1995">1995</option>
-<option value="1996">1996</option>
-<option value="1997">1997</option>
-<option value="1998">1998</option>
-<option value="1999">1999</option>
-<option value="2000">2000</option>
-<option value="2001">2001</option>
-<option value="2002">2002</option>
-<option value="2003">2003</option>
-<option value="2004">2004</option>
-<option value="2005">2005</option>
-<option value="2006">2006</option>
-<option value="2007">2007</option>
-<option value="2008">2008</option>
-<option value="2009">2009</option>
-<option value="2010">2010</option>
-<option value="2011">2011</option>
-<option value="2012">2012</option>
-<option value="2013">2013</option>
-<option value="2014">2014</option>
-<option value="2015">2015</option>
-<option value="2016">2016</option>
-<option value="2017">2017</option>
-<option value="2018">2018</option>
-<option value="2019">2019</option>
-<option value="2020">2020</option>
-<option value="2021">2021</option>
-<option value="2022">2022</option>
-<option value="2023">2023</option>
-<option value="2024">2024</option>
-<option value="2025">2025</option>
-<option value="2026">2026</option>
-<option value="2027">2027</option>
-<option value="2028">2028</option>
-<option value="2029">2029</option>
-<option value="2030">2030</option>
-<option value="2031">2031</option>
-<option value="2032">2032</option>
-<option value="2033">2033</option>
-<option value="2034">2034</option>
-<option value="2035">2035</option>
-<option value="2036">2036</option>
-<option value="2037">2037</option>
-<option value="2038">2038</option>
-<option value="2039">2039</option>
-<option value="2040">2040</option>
-<option value="2041">2041</option>
-<option value="2042">2042</option>
-<option value="2043">2043</option>
-<option value="2044">2044</option>
-<option value="2045">2045</option>
-<option value="2046">2046</option>
-<option value="2047">2047</option>
-<option value="2048">2048</option>
-<option value="2049">2049</option>
-<option value="2050">2050</option>
-<option value="2051">2051</option>
-</select>
 END
+    my $maxyear = 2051; 
+    for (my $i = 1976; $i<=$maxyear; $i++) {
+	$year.="<option value=\"$i\">$i</option>\n";
+    }
+    $year.="</select>\n";
     $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
     return "$month$day$year";
 }
@@ -563,10 +469,6 @@
     return $selout.'</select>';
 }
 
-sub testf {
-    return @_[0];
-}
-
 # ----------------------------------------------- Performing an advanced search
 sub advancedsearch {
     my ($r,$envhash)=@_;
@@ -585,7 +487,12 @@
 		   'custommetadata','customshow') {
 	$ENV{"form.$field"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
     }
-
+    foreach ('mode','form','element') {
+	# is this required?  Hmmm.
+	next unless (exists($ENV{"form.$_"}));
+	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
+	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
+    }
     # Check to see if enough information was filled in
     for my $field ('title','author','subject','keywords','url','version',
 		   'notes','abstract','mime','language','owner',
@@ -692,6 +599,12 @@
     for my $field ('basicexp') {
 	$ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
     }
+    foreach ('mode','form','element') {
+	# is this required?  Hmmm.
+	next unless (exists($ENV{"form.$_"}));
+	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
+	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
+    }
 
     # Check to see if enough is filled in
     unless (&filled($ENV{'form.basicexp'})) {
@@ -855,7 +768,9 @@
 
     # conditional output of script functions dependent on the mode in
     # which the search was invoked
-        $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
+    if ($ENV{'form.catalogmode'} eq 'interactive'){
+	if (! exists($ENV{'form.mode'}) || $ENV{'form.mode'} ne 'edit') {
+            $r->print(<<SCRIPT) 
 <script type="text/javascript">
     function select_data(title,url) {
 	changeTitle(title);
@@ -874,7 +789,30 @@
     }
 </script>
 SCRIPT
-        $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'groupsearch';
+        } elsif ($ENV{'form.mode'} eq 'edit') {
+            my $form = $ENV{'form.form'};
+            my $element = $ENV{'form.element'};
+            $r->print(<<SCRIPT) 
+<script type="text/javascript">
+function select_data(title,url) {
+    changeURL(url);
+    self.close();
+}
+function changeTitle(val) {
+}
+function changeURL(val) {
+    if (window.opener.document) {
+        window.opener.document.forms["$form"].elements["$element"].value=val;
+    } else {
+	var url = 'forms[\"$form\"].elements[\"$element\"].value';
+        alert("Unable to transfer data to "+url);
+    }
+}
+</script>
+SCRIPT
+        }
+    }
+    $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'groupsearch';
 <script type="text/javascript">
     function select_data(title,url) {
 //	alert('DEBUG: Should be storing '+title+' and '+url);
@@ -893,7 +831,7 @@
     }
 </script>
 SCRIPT
-        $r->print(<<SCRIPT);
+    $r->print(<<SCRIPT);
 <script type="text/javascript">
     function displayinfo(val) {
 	popwin.document.forms.popremain.sdetails.value=val;
@@ -904,9 +842,7 @@
 	openhelpwin.focus();
     }
     function abortsearch(val) {
-	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
-	     'scrollbars=1,width=400,height=300');
-	openhelpwin.focus();
+	popwin.close();
     }
 </script>
 SCRIPT
@@ -1015,7 +951,7 @@
 	' value="" />'+
 	'<'+'br />'+
 	' <'+'input type="button" name="button"'+
-	' value="abort search and view current results" '+
+	' value="close this window" '+
 	' onClick="javascript:opener.abortsearch()" />'+
 	' <'+'input type="button" name="button"'+
 	' value="help" onClick="javascript:opener.openhelp()" />'+
@@ -1048,11 +984,7 @@
 	my $replyfile='';
 
 	if ($reply eq 'con_lost') {
-	    $r->print('<script type="text/javascript">popwin.document.img'.
-		      $sn.'.'.
-		      'src="/adm/lonIcons/srvbad.gif";</script>'.
-		      "\n");
-	    $r->rflush();
+	    &popwin_imgupdate($r,$sn,"srvbad.gif");
 	    $bloop--;
 	    $orkey{$rkey}=1;
 	}
@@ -1064,58 +996,31 @@
 		my $temp=0;
 	      WLOOP: while (1) {
 		  if (-e $replyfile && $tflag) {
-		      $r->print('<script type="text/javascript">'.
-				'popwin.document.img'.$sn.'.'.
-				'src="/adm/lonIcons/srvhalf.gif";</script>'.
-				"\n");
-		      $r->rflush();
-		      $r->print('<script type="text/javascript">'.
-				'popwin.hc["'.$rkey.'"]='.
-				'"still transferring..."'.';</script>'.
-				"\n");
-		      $r->rflush();
+		      &popwin_imgupdate($r,$sn,"srvhalf.gif");
+		      &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
+				 '"still transferring..."'.';');
 		      $tflag=0;
 		  }
 		  if (-e "$replyfile.end") {
 		      $bloop--;
 		      $orkey{$rkey}=1;
 		      if (-s $replyfile) {
-			  $r->print('<script type="text/javascript">'.
-				    'popwin.document.img'.$sn.'.'.
-				    'src="/adm/lonIcons/srvgood.gif";'.
-				    '</script>'."\n");
-			  $r->rflush();
+			  &popwin_imgupdate($r,$sn,"srvgood.gif");
 			  my $fh=Apache::File->new($replyfile) or 
 			      ($r->print('ERROR: file '.
 					 $replyfile.' cannot be opened') and
 			       return OK);
 			  @results=<$fh> if $fh;
 			  $hitcount{$rkey}=@results+0;
-			  $r->print('<script type="text/javascript">'.
-				    'popwin.hc["'.$rkey.'"]='.
-				    $hitcount{$rkey}.';</script>'.
-				    "\n");
-			  $r->rflush();
+			  &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
+				    $hitcount{$rkey}.';');
 			  $hitcountsum+=$hitcount{$rkey};
-			  $r->print('<script type="text/javascript">'.
-				    'popwin.document.forms.popremain.'.
-				    'numhits.value='.$hitcountsum.
-				    ';</script>'.
-				    "\n");
-			  $r->rflush();
+			  &popwin_js($r,'popwin.document.forms.popremain.'.
+				    'numhits.value='.$hitcountsum.';');
 		      }
 		      else {
-			  $r->print('<script type="text/javascript">'.
-				    'popwin.document.img'.$sn.'.'.
-				    'src="/adm/lonIcons/srvempty.gif";'.
-				    '</script>'.
-				    "\n");
-			  $r->rflush();
-			  $r->print('<script type="text/javascript">'.
-				    'popwin.hc["'.$rkey.'"]=0'.
-				    ';</script>'.
-				    "\n");
-			  $r->rflush();
+			  &popwin_imgupdate($r,$sn,"srvempty.gif");
+			  &popwin_js($r,'popwin.hc["'.$rkey.'"]=0;');
 		      }
 		      last WLOOP;
 		  }
@@ -1129,18 +1034,13 @@
 		  sleep 1;
 		  $timeremain--;
 		  $elapsetime++;
-		  $r->print('<script type="text/javascript">'.
-			    'popwin.document.popremain.elapsetime.'.
-			    'value="'.$elapsetime.'";</script>'."\n");
-		  $r->rflush();
+		  &popwin_js($r,"popwin.document.popremain.".
+			       "elapsetime.value=$elapsetime;");
 		  $temp++;
 	      }
 	    }
-	    $r->print('<script type="text/javascript">'.
-		      'popwin.document.whirly.'.
-		      'src="'.'/adm/lonIcons/lonanimend.gif'.
-		      '";</script>'."\n");
-	    $r->rflush();
+	    &popwin_js($r,'popwin.document.whirly.'.
+		       'src="/adm/lonIcons/lonanimend.gif";');
 	}
 	my $customshow='';
 	my $extrashow='';
@@ -1518,6 +1418,27 @@
 	}
     }
 }
+
+# ----------------------------------------------- send javascript to popwin
+sub popwin_js {
+    # Print javascript out to popwin, but make sure we dont generate
+    # any javascript errors in doing so.
+    my ($r,$text) = @_;
+    $r->print(<<"END");
+<script type="text/javascript">
+    if (! popwin.closed) {
+	$text
+    }
+</script>
+END
+    $r->rflush();
+}
+
+sub popwin_imgupdate {
+    my ($r,$imgnum,$icon) = @_;
+    &popwin_js($r,'popwin.document.img'.$imgnum.'.'.
+	       'src="/adm/lonIcons/'.$icon.'";');
+}    
 
 1;
 

--matthew1015612560--