Annotation of loncom/interface/lonsearchcat.pm, revision 1.133

1.98      harris41    1: # The LearningOnline Network with CAPA
1.108     harris41    2: # Search Catalog
                      3: #
1.133   ! matthew     4: # $Id: lonsearchcat.pm,v 1.132 2002/06/27 14:46:00 matthew Exp $
1.108     harris41    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
1.98      harris41   14: #
1.108     harris41   15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
1.1       www        27: #
1.97      harris41   28: # YEAR=2001
1.104     harris41   29: # 3/8, 3/12, 3/13, 3/14, 3/15, 3/19 Scott Harrison
                     30: # 3/20, 3/21, 3/22, 3/26, 3/27, 4/2, 8/15, 8/24, 8/25 Scott Harrison
1.113     harris41   31: # 10/12,10/14,10/15,10/16,11/28,11/29,12/10,12/12,12/16 Scott Harrison
1.115     harris41   32: # YEAR=2002
                     33: # 1/17 Scott Harrison
1.121     matthew    34: # 6/17 Matthew Hall
1.104     harris41   35: #
1.121     matthew    36: ###############################################################################
                     37: ###############################################################################
                     38: 
                     39: =pod 
                     40: 
                     41: =head1 NAME
                     42: 
                     43: lonsearchcat
                     44: 
                     45: =head1 SYNOPSIS
                     46: 
                     47: Search interface to LON-CAPAs digital library
                     48: 
                     49: =head1 DESCRIPTION
                     50: 
                     51: This module enables searching for a distributed browseable catalog.
1.104     harris41   52: 
1.121     matthew    53: This is part of the LearningOnline Network with CAPA project
                     54: described at http://www.lon-capa.org.
                     55: 
                     56: lonsearchcat presents the user with an interface to search the LON-CAPA
                     57: digital library.  lonsearchcat also initiates the execution of a search
                     58: by sending the search parameters to LON-CAPA servers.  The progress of 
                     59: search (on a server basis) is displayed to the user in a seperate window.
                     60: 
                     61: =head1 Internals
                     62: 
                     63: =over 4
                     64: 
                     65: =cut
                     66: 
                     67: ###############################################################################
1.98      harris41   68: ###############################################################################
1.121     matthew    69: 
1.128     harris41   70: ###############################################################################
1.98      harris41   71: ##                                                                           ##
                     72: ## ORGANIZATION OF THIS PERL MODULE                                          ##
                     73: ##                                                                           ##
1.105     harris41   74: ## 1. Modules used by this module                                            ##
1.128     harris41   75: ## 2. Variables used throughout the module                                   ##
                     76: ## 3. handler subroutine called via Apache and mod_perl                      ##
                     77: ## 4. Other subroutines                                                      ##
1.98      harris41   78: ##                                                                           ##
                     79: ###############################################################################
                     80: 
1.1       www        81: package Apache::lonsearchcat;
                     82: 
1.98      harris41   83: # ------------------------------------------------- modules used by this module
1.1       www        84: use strict;
                     85: use Apache::Constants qw(:common);
1.6       harris41   86: use Apache::lonnet();
                     87: use Apache::File();
1.7       harris41   88: use CGI qw(:standard);
1.41      harris41   89: use Text::Query;
1.101     harris41   90: use GDBM_File;
1.112     harris41   91: use Apache::loncommon();
1.1       www        92: 
1.90      harris41   93: # ---------------------------------------- variables used throughout the module
                     94: 
1.121     matthew    95: ######################################################################
                     96: ######################################################################
                     97: 
                     98: =pod 
                     99: 
                    100: =item Global variables
                    101: 
                    102: =over 4
                    103: 
                    104: =item $closebutton
                    105: 
                    106: button that closes the search window
                    107: 
                    108: =item $importbutton
                    109: 
                    110: button to take the selecte results and go to group sorting
                    111: 
                    112: =item %hash   
                    113: 
                    114: The ubiquitous database hash
                    115: 
                    116: =item $diropendb 
                    117: 
                    118: The full path to the (temporary) search database file.  This is set and
                    119: used in &handler() and is also used in &output_results().
                    120: 
                    121: =back 
                    122: 
                    123: =cut
                    124: 
                    125: ######################################################################
                    126: ######################################################################
                    127: 
1.98      harris41  128: # -- dynamically rendered interface components
1.121     matthew   129: my $closebutton;  # button that closes the search window
1.98      harris41  130: my $importbutton; # button to take the selected results and go to group sorting
                    131: 
                    132: # -- miscellaneous variables
1.121     matthew   133: my %hash;     # database hash
1.127     matthew   134: my $diropendb = "";    # db file
1.98      harris41  135: 
1.121     matthew   136: ######################################################################
                    137: ######################################################################
                    138: 
                    139: =pod 
                    140: 
                    141: =item &handler() - main handler invoked by httpd child
                    142: 
1.124     matthew   143: =item Variables
                    144: 
                    145: =over 4
                    146: 
                    147: =item $hidden
                    148: 
                    149: holds 'hidden' html forms
                    150: 
                    151: =item $scrout
                    152: 
                    153: string that holds portions of the screen output
                    154: 
                    155: =back 
                    156: 
1.121     matthew   157: =cut
1.101     harris41  158: 
1.121     matthew   159: ######################################################################
                    160: ######################################################################
1.98      harris41  161: sub handler {
                    162:     my $r = shift;
1.103     harris41  163:     untie %hash;
1.98      harris41  164: 
                    165:     $r->content_type('text/html');
                    166:     $r->send_http_header;
                    167:     return OK if $r->header_only;
                    168: 
1.121     matthew   169:     my $domain  = $r->dir_config('lonDefDomain');
1.122     matthew   170:     $diropendb= "/home/httpd/perl/tmp/".&Apache::lonnet::escape($domain).
                    171:             "\_".&Apache::lonnet::escape($ENV{'user.name'})."_searchcat.db";
1.101     harris41  172: 
1.116     matthew   173:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.124     matthew   174:              ['catalogmode','launch','acts','mode','form','element',
                    175:               'reqinterface']);
                    176:     ##
                    177:     ## Clear out old values from database
                    178:     ##
1.101     harris41  179:     if ($ENV{'form.launch'} eq '1') {
                    180: 	if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
                    181: 	    &start_fresh_session();
                    182: 	    untie %hash;
1.122     matthew   183: 	} else {
1.101     harris41  184: 	    $r->print('<html><head></head><body>Unable to tie hash to db '.
                    185: 		      'file</body></html>');
                    186: 	    return OK;
                    187: 	}
                    188:     }
1.124     matthew   189:     ##
                    190:     ## Produce some output, so people know it is working
                    191:     ##
1.119     www       192:     $r->print("\n");
                    193:     $r->rflush;
1.124     matthew   194:     ##
                    195:     ## Configure dynamic components of interface
                    196:     ##
                    197:     my $hidden;       # Holds 'hidden' html forms
1.98      harris41  198:     if ($ENV{'form.catalogmode'} eq 'interactive') {
                    199: 	$hidden="<input type='hidden' name='catalogmode' value='interactive'>".
                    200: 	    "\n";
                    201:         $closebutton="<input type='button' name='close' value='CLOSE' ".
                    202: 	    "onClick='self.close()'>"."\n";
1.124     matthew   203:     } elsif ($ENV{'form.catalogmode'} eq 'groupsearch') {
1.98      harris41  204: 	$hidden=<<END;
                    205: <input type='hidden' name='catalogmode' value='groupsearch'>
                    206: END
                    207:         $closebutton=<<END;
                    208: <input type='button' name='close' value='CLOSE' onClick='self.close()'>
                    209: END
                    210:         $importbutton=<<END;
                    211: <input type='button' name='import' value='IMPORT'
                    212: onClick='javascript:select_group()'>
                    213: END
                    214:     }
1.133   ! matthew   215:     $hidden .= &make_persistent({ "form.mode"    => $ENV{'form.mode'},
        !           216:                                   "form.form"    => $ENV{'form.form'},
        !           217:                                   "form.element" => $ENV{'form.element'},
        !           218:                                   "form.date"    => 2 });
1.124     matthew   219:     ##
                    220:     ##  What are we doing?
                    221:     ##
                    222:     if ($ENV{'form.basicsubmit'} eq 'SEARCH') {
                    223:         # Perform basic search and give results
1.130     matthew   224: 	return &basicsearch($r,$hidden);
1.124     matthew   225:     } elsif ($ENV{'form.advancedsubmit'} eq 'SEARCH') {
                    226:         # Perform advanced search and give results
1.130     matthew   227: 	return &advancedsearch($r,$hidden);
1.124     matthew   228:     } elsif ($ENV{'form.reqinterface'} eq 'advanced') {
                    229:         # Output the advanced interface
                    230:         $r->print(&advanced_search_form($closebutton,$hidden));
                    231:         return OK;
                    232:     } else { 
                    233:         # Output normal search interface
                    234:         $r->print(&basic_search_form($closebutton,$hidden));
                    235:     }
                    236:     return OK;
                    237: } 
1.98      harris41  238: 
1.124     matthew   239: ######################################################################
                    240: ######################################################################
                    241: 
                    242: =pod 
                    243: 
                    244: =item &basic_search_form() 
                    245: 
                    246: Returns a scalar which holds html for the basic search form.
                    247: 
                    248: =cut
                    249: 
                    250: ######################################################################
                    251: ######################################################################
1.3       harris41  252: 
1.124     matthew   253: sub basic_search_form{
                    254:     my ($closebutton,$hidden) = @_;
                    255:     my $scrout=<<"ENDDOCUMENT";
                    256: <html>
                    257: <head>
                    258: <title>The LearningOnline Network with CAPA</title>
                    259: <script type="text/javascript">
                    260:     function openhelp(val) {
                    261: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                    262: 	     'scrollbars=1,width=600,height=300');
                    263: 	openhelpwin.focus();
1.6       harris41  264:     }
1.124     matthew   265: </script>
                    266: </head>
                    267: <body bgcolor="#FFFFFF">
                    268: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
                    269: <h1>Search Catalog</h1>
                    270: <form method="post" action="/adm/searchcat">
                    271: $hidden
                    272: <h3>Basic Search</h3>
                    273: <p>
1.131     matthew   274: Enter terms or phrases separated by AND, OR, or NOT 
1.129     matthew   275: then press SEARCH below.
1.124     matthew   276: </p>
                    277: <p>
                    278: <table>
                    279: <tr><td>
                    280: ENDDOCUMENT
                    281:     $scrout.='&nbsp;'.&simpletextfield('basicexp',$ENV{'form.basicexp'},40).
                    282:         '&nbsp;';
                    283: #    $scrout.=&simplecheckbox('allversions',$ENV{'form.allversions'});
                    284: #    $scrout.='<font color="#800000">Search historic archives</font>';
                    285:     $scrout.=<<ENDDOCUMENT;
                    286: </td><td><a href="/adm/searchcat?reqinterface=advanced">Advanced Search</a></td></tr></table>
                    287: </p>
                    288: <p>
                    289: &nbsp;<input type="submit" name="basicsubmit" value='SEARCH' />&nbsp;
                    290: $closebutton
1.130     matthew   291: <!-- view selection -->
                    292: <select name="viewselect">
                    293: <option value="Detailed Citation View" selected="true" >Detailed Citation View</option>
                    294: <option value="Summary View"  >Summary View</option>
                    295: <option value="Fielded Format">Fielded Format</option>
                    296: <option value="XML/SGML"      >XML/SGML</option>
1.124     matthew   297: </select>
1.130     matthew   298: <!-- end of view selection -->
1.124     matthew   299: <input type="button" value="HELP" onClick="openhelp()" />
                    300: </p>
                    301: </form>
                    302: </body>
                    303: </html>
                    304: ENDDOCUMENT
                    305:     return $scrout;
                    306: }
                    307: ######################################################################
                    308: ######################################################################
                    309: 
                    310: =pod 
                    311: 
                    312: =item &advanced_search_form() 
                    313: 
                    314: Returns a scalar which holds html for the advanced search form.
                    315: 
                    316: =cut
                    317: 
                    318: ######################################################################
                    319: ######################################################################
                    320: 
                    321: sub advanced_search_form{
                    322:     my ($closebutton,$hidden) = @_;
1.129     matthew   323:     my $advanced_buttons = <<"END";
                    324: <p>
                    325: <input type="submit" name="advancedsubmit" value='SEARCH' />
                    326: <input type="reset" name="reset" value='RESET' />
                    327: $closebutton
                    328: <input type="button" value="HELP" onClick="openhelp()" />
                    329: </p>
                    330: END
1.124     matthew   331:     my $scrout=<<"ENDHEADER";
                    332: <html>
                    333: <head>
                    334: <title>The LearningOnline Network with CAPA</title>
                    335: <script type="text/javascript">
                    336:     function openhelp(val) {
                    337: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                    338: 	     'scrollbars=1,width=600,height=300');
                    339: 	openhelpwin.focus();
1.18      harris41  340:     }
1.124     matthew   341: </script>
                    342: </head>
                    343: <body bgcolor="#FFFFFF">
                    344: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
1.129     matthew   345: <h1>Advanced Catalog Search</h1>
                    346: <hr />
1.131     matthew   347: Enter terms or phrases separated by search operators 
1.129     matthew   348: such as AND, OR, or NOT.<br />
1.130     matthew   349: <form method="post" action="/adm/searchcat">
1.129     matthew   350: $advanced_buttons
1.124     matthew   351: $hidden
1.129     matthew   352: <table>
1.130     matthew   353: <tr><td><font color="#800000" face="helvetica"><b>VIEW:</b></font></td>
                    354: <td>
                    355: <!-- view selection -->
                    356: <select name="viewselect" size ="1">
                    357:     <option value="Detailed Citation View" selected="true">Detailed Citation View</option>
                    358:     <option value="Summary View">Summary View</option>
                    359:     <option value="Fielded Format">Fielded Format</option>
                    360:     <option value="XML/SGML">XML/SGML</option>
                    361: </select>
                    362: <!-- end of view selection -->
                    363: </td></tr>
1.124     matthew   364: ENDHEADER
1.129     matthew   365:     $scrout.=&searchphrasefield('title','title',
1.11      harris41  366: 			$ENV{'form.title'});
1.129     matthew   367:     $scrout.=&searchphrasefield('author','author',
1.11      harris41  368: 			$ENV{'form.author'});
1.129     matthew   369:     $scrout.=&searchphrasefield('subject','subject',
1.11      harris41  370: 			$ENV{'form.subject'});
1.129     matthew   371:     $scrout.=&searchphrasefield('keywords','keywords',
1.11      harris41  372: 			$ENV{'form.keywords'});
1.129     matthew   373:     $scrout.=&searchphrasefield('URL','url',
1.11      harris41  374: 			$ENV{'form.url'});
1.96      harris41  375: #    $scrout.=&searchphrasefield('Limit by version','version',
                    376: #			$ENV{'form.version'});
1.129     matthew   377:     $scrout.=&searchphrasefield('notes','notes',
1.11      harris41  378: 			$ENV{'form.notes'});
1.129     matthew   379:     $scrout.=&searchphrasefield('abstract','abstract',
1.11      harris41  380: 			$ENV{'form.abstract'});
1.129     matthew   381:     # Hack - an empty table row.
                    382:     $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
                    383:     $scrout.=&searchphrasefield('file<br />extension','mime',
                    384:                         $ENV{'form.mime'});
                    385:     $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
                    386:     $scrout.=&searchphrasefield('publisher<br />owner','owner',
                    387: 				$ENV{'form.owner'});
                    388:     $scrout.="</table>\n";
1.131     matthew   389:     $ENV{'form.category'}='any' unless length($ENV{'form.category'});
1.132     matthew   390:     $scrout.=&selectbox('File Category','category',
1.131     matthew   391: 			$ENV{'form.category'},
                    392: 			'any','Any category',
                    393: 			undef,
                    394: 			(&Apache::loncommon::filecategories()));
1.11      harris41  395:     $ENV{'form.language'}='any' unless length($ENV{'form.language'});
1.133   ! matthew   396:     #----------------------------------------------------------------
1.132     matthew   397:     # Allow restriction to multiple domains.
                    398:     #   I make the crazy assumption that there will never be a domain 'any'.
                    399:     #
1.133   ! matthew   400:     $ENV{'form.domains'} = 'any' if (! exists($ENV{'form.domains'}));
        !           401:     my @allowed_domains = (ref($ENV{'form.domains'}) ? @{$ENV{'form.domains'}} 
        !           402:                            :  ($ENV{'form.domains'}) );
        !           403:     my %domain_hash = ();
        !           404:     foreach (@allowed_domains) {
        !           405:         $domain_hash{$_}++;
        !           406:     }
1.132     matthew   407:     my @domains =&Apache::loncommon::get_domains();
                    408:     # adjust the size of the select box
                    409:     my $size = 4;
                    410:     my $size = (scalar @domains < ($size - 1) ? scalar @domains + 1 : $size);
                    411:     # standalone machines do not get to choose a domain to search.
                    412:     if ((scalar @domains) == 1) {
                    413:         $scrout .='<input type="hidden" name="domains" value="any" />'."\n";
                    414:     } else {
1.133   ! matthew   415:         $scrout.="\n".'<font color="#800000" face="helvetica"><b>'.
1.132     matthew   416:             'DOMAINS</b></font><br />'.
                    417:                 '<select name="domains" size="'.$size.'" multiple>'."\n".
1.133   ! matthew   418:                     '<option name="any" value="any" '.
        !           419:                         ($domain_hash{'any'}? 'selected ' :'').
        !           420:                         '>all domains</option>'."\n";
1.132     matthew   421:         foreach my $dom (sort @domains) {
1.133   ! matthew   422:             $scrout.="<option name=\"$dom\" ".
        !           423:                 ($domain_hash{$dom} ? 'selected ' :'').">$dom</option>\n";
1.132     matthew   424:         }
                    425:         $scrout.="</select>\n";
                    426:     }
1.133   ! matthew   427:     #----------------------------------------------------------------
1.132     matthew   428:     # 
                    429:     #
1.3       harris41  430:     $scrout.=&selectbox('Limit by language','language',
1.111     harris41  431: 			$ENV{'form.language'},'any','Any Language',
                    432: 			\&{Apache::loncommon::languagedescription},
                    433: 			(&Apache::loncommon::languageids),
                    434: 			);
1.8       harris41  435: # ------------------------------------------------ Compute date selection boxes
                    436:     $scrout.=<<CREATIONDATESTART;
1.3       harris41  437: <p>
                    438: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
                    439: </font>
1.98      harris41  440: <br />
1.8       harris41  441: between:
                    442: CREATIONDATESTART
1.11      harris41  443:     $scrout.=&dateboxes('creationdatestart',1,1,1976,
                    444: 			$ENV{'form.creationdatestart_month'},
                    445: 			$ENV{'form.creationdatestart_day'},
                    446: 			$ENV{'form.creationdatestart_year'},
                    447: 			);
1.124     matthew   448:     $scrout.="and:\n";
1.11      harris41  449:     $scrout.=&dateboxes('creationdateend',12,31,2051,
                    450: 			$ENV{'form.creationdateend_month'},
                    451: 			$ENV{'form.creationdateend_day'},
                    452: 			$ENV{'form.creationdateend_year'},
                    453: 			);
1.8       harris41  454:     $scrout.="</p>";
                    455:     $scrout.=<<LASTREVISIONDATESTART;
                    456: <p>
                    457: <font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
                    458: </b></font>
1.98      harris41  459: <br />between:
1.8       harris41  460: LASTREVISIONDATESTART
1.11      harris41  461:     $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
                    462: 			$ENV{'form.lastrevisiondatestart_month'},
                    463: 			$ENV{'form.lastrevisiondatestart_day'},
                    464: 			$ENV{'form.lastrevisiondatestart_year'},
                    465: 			);
1.8       harris41  466:     $scrout.=<<LASTREVISIONDATEEND;
                    467: and:
                    468: LASTREVISIONDATEEND
1.11      harris41  469:     $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
                    470: 			$ENV{'form.lastrevisiondateend_month'},
                    471: 			$ENV{'form.lastrevisiondateend_day'},
                    472: 			$ENV{'form.lastrevisiondateend_year'},
                    473: 			);
1.8       harris41  474:     $scrout.='</p>';
1.11      harris41  475:     $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
1.8       harris41  476:     $scrout.=&selectbox('Limit by copyright/distribution','copyright',
1.111     harris41  477: 			 $ENV{'form.copyright'},
                    478: 			 'any','Any copyright/distribution',
                    479: 			 \&{Apache::loncommon::copyrightdescription},
                    480: 			 (&Apache::loncommon::copyrightids),
                    481: 			 );
1.14      harris41  482: # ------------------------------------------- Compute customized metadata field
                    483:     $scrout.=<<CUSTOMMETADATA;
                    484: <p>
1.77      harris41  485: <font color="#800000" face="helvetica"><b>LIMIT BY SPECIAL METADATA FIELDS:</b>
1.14      harris41  486: </font>
1.77      harris41  487: For resource-specific metadata, enter in an expression in the form of 
1.100     harris41  488: <i>key</i>=<i>value</i> separated by operators such as AND, OR or NOT.<br />
1.14      harris41  489: <b>Example:</b> grandmother=75 OR grandfather=85
1.98      harris41  490: <br />
1.14      harris41  491: CUSTOMMETADATA
1.124     matthew   492:     $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'});
1.77      harris41  493:     $scrout.=<<CUSTOMSHOW;
                    494: <p>
                    495: <font color="#800000" face="helvetica"><b>SHOW SPECIAL METADATA FIELDS:</b>
                    496: </font>
                    497: Enter in a space-separated list of special metadata fields to show
                    498: in a fielded listing for each record result.
1.98      harris41  499: <br />
1.77      harris41  500: CUSTOMSHOW
1.124     matthew   501:     $scrout.=&simpletextfield('customshow',$ENV{'form.customshow'});
                    502:     $scrout.=<<ENDDOCUMENT;
1.129     matthew   503: $advanced_buttons
1.8       harris41  504: </form>
                    505: </body>
                    506: </html>
                    507: ENDDOCUMENT
1.124     matthew   508:     return $scrout;
                    509: }
1.8       harris41  510: 
1.121     matthew   511: ######################################################################
                    512: ######################################################################
                    513: 
                    514: =pod 
                    515: 
                    516: =item &make_persistent() 
                    517: 
                    518: Returns a scalar which holds the current ENV{'form.*'} values in
1.126     matthew   519: a 'hidden' html input tag.  This allows search interface information
                    520: to be somewhat persistent.
1.122     matthew   521: 
1.121     matthew   522: =cut
                    523: 
                    524: ######################################################################
                    525: ######################################################################
                    526: 
1.98      harris41  527: sub make_persistent {
1.133   ! matthew   528:     my %save = %{shift()};
1.98      harris41  529:     my $persistent='';
1.133   ! matthew   530:     foreach (keys %save) {
1.98      harris41  531: 	if (/^form\./ && !/submit/) {
                    532: 	    my $name=$_;
1.133   ! matthew   533:             my @values = (ref($save{$name}) ? @{$save{$name}} : ($save{$name}));
1.98      harris41  534: 	    $name=~s/^form\.//;
1.133   ! matthew   535:             foreach (@values) {
        !           536:                 s/\"/\'/g; # do not mess with html field syntax
        !           537:                 $persistent.=<<END;
        !           538: <input type="hidden" name="$name" value="$_" />
1.98      harris41  539: END
1.133   ! matthew   540:             }
1.98      harris41  541:         }
1.109     harris41  542:     }
1.98      harris41  543:     return $persistent;
                    544: }
                    545: 
1.122     matthew   546: 
                    547: ######################################################################
                    548: ######################################################################
                    549: 
                    550: =pod 
                    551: 
                    552: =item HTML form building functions
                    553: 
                    554: =over 4
                    555: 
                    556: =item &simpletextfield() 
                    557: 
                    558: Inputs: $name,$value,$size
                    559: 
                    560: Returns a text input field with the given name, value, and size.  
                    561: If size is not specified, a value of 20 is used.
                    562: 
                    563: =item &simplecheckbox()
                    564: 
                    565: Inputs: $name,$value
                    566: 
                    567: Returns a simple check box with the given $name.
                    568: If $value eq 'on' the box is checked.
                    569: 
                    570: =item &searchphrasefield()
                    571: 
                    572: Inputs: $title,$name,$value
                    573: 
                    574: Returns html for a title line and an input field for entering search terms.
                    575: the instructions "Enter terms or phrases separated by search operators such 
                    576: as AND, OR, or NOT." are given following the title.  The entry field (which
                    577: is where the $name and $value are used) is an 80 column simpletextfield.
                    578: 
                    579: =item &dateboxes()
                    580: 
1.126     matthew   581: Returns html selection form elements for the specification of 
                    582: the day, month, and year.
                    583: 
1.122     matthew   584: =item &selectbox()
                    585: 
1.129     matthew   586: Returns a scalar containing an html <select> form.  
                    587: 
                    588: Inputs: 
                    589: 
                    590: =over 4
                    591: 
                    592: =item $title 
                    593: 
                    594: Printed above the select box, in uppercase.
                    595: 
                    596: =item $name 
                    597: 
                    598: The name element of the <select> tag.
                    599: 
                    600: =item $default 
                    601: 
                    602: The default value of the form.  Can be $anyvalue or in @idlist.
                    603: 
                    604: =item $anyvalue 
                    605: 
                    606: The <option value="..."> used to indicate a default of 
                    607: none of the values.
                    608: 
                    609: =item $anytag 
                    610: 
                    611: The text associate with $anyvalue above.
                    612: 
                    613: =item $functionref 
                    614: 
                    615: Each element in @idlist will be passed as a parameter 
                    616: to the function referenced here.  The return value of the function should
                    617: be a scalar description of the items.  If this value is undefined the 
                    618: description of each item in @idlist will be the item name.
                    619: 
                    620: =item @idlist 
                    621: 
                    622: The items to be selected from.  One of these or $anyvalue will be the 
                    623: value returned by the form element, $ENV{form.$name}.
                    624: 
                    625: =back
1.126     matthew   626: 
1.122     matthew   627: =back 
                    628: 
                    629: =cut
                    630: 
                    631: ######################################################################
                    632: ######################################################################
1.8       harris41  633: 
1.11      harris41  634: sub simpletextfield {
1.122     matthew   635:     my ($name,$value,$size)=@_;
                    636:     $size = 20 if (! defined($size));
                    637:     return '<input type="text" name="'.$name.
                    638:         '" size="'.$size.'" value="'.$value.'" />';
1.11      harris41  639: }
                    640: 
                    641: sub simplecheckbox {
                    642:     my ($name,$value)=@_;
                    643:     my $checked='';
1.129     matthew   644:     $checked="checked" if $value eq 'on';
1.122     matthew   645:     return '<input type="checkbox" name="'.$name.'" '. $checked . ' />';
1.11      harris41  646: }
                    647: 
1.8       harris41  648: sub searchphrasefield {
                    649:     my ($title,$name,$value)=@_;
                    650:     my $uctitle=uc($title);
1.129     matthew   651:     return '<tr><td><font color="#800000" face="helvetica">'.
                    652:         '<b>'.$uctitle.':&nbsp;</b></font></td><td>'.
                    653:                 &simpletextfield($name,$value,50)."</td></tr>\n";
1.8       harris41  654: }
1.3       harris41  655: 
1.8       harris41  656: sub dateboxes {
1.11      harris41  657:     my ($name,$defaultmonth,$defaultday,$defaultyear,
                    658: 	$currentmonth,$currentday,$currentyear)=@_;
                    659:     ($defaultmonth,$defaultday,$defaultyear)=('','','');
1.117     matthew   660:     #
                    661:     # Day
                    662:     my $day=<<END;
                    663: <select name="${name}_day">
                    664: <option value='$defaultday'> </option>
                    665: END
                    666:     for (my $i = 1; $i<=31; $i++) {
                    667: 	$day.="<option value=\"$i\">$i</option>\n";
                    668:     }
                    669:     $day.="</select>\n";
                    670:     $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
                    671:     #
                    672:     # Month
1.11      harris41  673:     my $month=<<END;
1.8       harris41  674: <select name="${name}_month">
1.11      harris41  675: <option value='$defaultmonth'> </option>
                    676: END
1.117     matthew   677:     my $i = 1;
                    678:     foreach (qw/January February March April May June 
                    679: 	     July August September October November December /){
                    680: 	$month .="<option value=\"$i\">$_</option>\n";
                    681: 	$i++;
                    682:     }
                    683:     $month.="</select>\n";
1.11      harris41  684:     $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
1.117     matthew   685:     #
                    686:     # Year (obviously)
1.11      harris41  687:     my $year=<<END;
1.8       harris41  688: <select name="${name}_year">
1.11      harris41  689: <option value='$defaultyear'> </option>
1.3       harris41  690: END
1.117     matthew   691:     my $maxyear = 2051; 
                    692:     for (my $i = 1976; $i<=$maxyear; $i++) {
                    693: 	$year.="<option value=\"$i\">$i</option>\n";
                    694:     }
                    695:     $year.="</select>\n";
1.11      harris41  696:     $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
                    697:     return "$month$day$year";
1.3       harris41  698: }
                    699: 
                    700: sub selectbox {
1.129     matthew   701:     my ($title,$name,$default,$anyvalue,$anytag,$functionref,@idlist)=@_;
                    702:     if (! defined($functionref)) { $functionref = sub { $_[0]}; }
1.3       harris41  703:     my $uctitle=uc($title);
1.122     matthew   704:     my $selout="\n".'<p><font color="#800000" face="helvetica">'.
1.131     matthew   705:         '<b>'.$uctitle.': </b></font><select name="'.$name.'">';
1.111     harris41  706:     foreach ($anyvalue,@idlist) {
1.122     matthew   707:         $selout.='<option value="'.$_.'"';
1.129     matthew   708:         if ($_ eq $default and !/^any$/) {
1.122     matthew   709: 	    $selout.=' selected >'.&{$functionref}($_).'</option>';
1.111     harris41  710: 	}
1.129     matthew   711: 	elsif ($_ eq $default and /^$anyvalue$/) {
1.122     matthew   712: 	    $selout.=' selected >'.$anytag.'</option>';
1.111     harris41  713: 	}
                    714:         else {$selout.='>'.&{$functionref}($_).'</option>';}
1.109     harris41  715:     }
1.129     matthew   716:     return $selout.'</select></p>';
1.6       harris41  717: }
                    718: 
1.122     matthew   719: ######################################################################
                    720: ######################################################################
                    721: 
                    722: =pod 
                    723: 
1.126     matthew   724: =item &advancedsearch()
                    725: 
                    726: Parse advanced search results.
1.122     matthew   727: 
                    728: =cut
                    729: 
                    730: ######################################################################
                    731: ######################################################################
1.18      harris41  732: sub advancedsearch {
1.130     matthew   733:     my ($r,$hidden)=@_;
1.32      harris41  734:     my $fillflag=0;
1.64      harris41  735:     # Clean up fields for safety
                    736:     for my $field ('title','author','subject','keywords','url','version',
                    737: 		   'creationdatestart_month','creationdatestart_day',
                    738: 		   'creationdatestart_year','creationdateend_month',
                    739: 		   'creationdateend_day','creationdateend_year',
                    740: 		   'lastrevisiondatestart_month','lastrevisiondatestart_day',
                    741: 		   'lastrevisiondatestart_year','lastrevisiondateend_month',
                    742: 		   'lastrevisiondateend_day','lastrevisiondateend_year',
                    743: 		   'notes','abstract','mime','language','owner',
1.131     matthew   744: 		   'custommetadata','customshow','category') {
1.101     harris41  745: 	$ENV{"form.$field"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
1.64      harris41  746:     }
1.117     matthew   747:     foreach ('mode','form','element') {
                    748: 	# is this required?  Hmmm.
                    749: 	next unless (exists($ENV{"form.$_"}));
                    750: 	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
                    751: 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
                    752:     }
1.131     matthew   753:     # Preprocess the category form element.
                    754:     if ($ENV{'form.category'} ne 'any') {
                    755:         my @extensions = &Apache::loncommon::filecategorytypes
                    756:             ($ENV{'form.category'});
                    757:         $ENV{'form.mime'} = join ' OR ',@extensions;
                    758:     }
1.90      harris41  759:     # Check to see if enough information was filled in
1.32      harris41  760:     for my $field ('title','author','subject','keywords','url','version',
                    761: 		   'notes','abstract','mime','language','owner',
                    762: 		   'custommetadata') {
1.40      harris41  763: 	if (&filled($ENV{"form.$field"})) {
1.32      harris41  764: 	    $fillflag++;
                    765: 	}
                    766:     }
                    767:     unless ($fillflag) {
                    768: 	&output_blank_field_error($r);
                    769: 	return OK;
                    770:     }
1.90      harris41  771:     # Turn the form input into a SQL-based query
1.39      harris41  772:     my $query='';
1.45      harris41  773:     my @queries;
1.90      harris41  774:     # Evaluate logical expression AND/OR/NOT phrase fields.
1.58      harris41  775:     foreach my $field ('title','author','subject','notes','abstract','url',
1.129     matthew   776: 		       'keywords','version','owner','mime') {
1.44      harris41  777: 	if ($ENV{'form.'.$field}) {
1.45      harris41  778: 	    push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
1.131     matthew   779:         }
1.44      harris41  780:     }
1.90      harris41  781:     # Evaluate option lists
1.58      harris41  782:     if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
1.90      harris41  783: 	push @queries,"(language like \"$ENV{'form.language'}\")";
1.58      harris41  784:     }
                    785:     if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
1.90      harris41  786: 	push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
1.58      harris41  787:     }
1.90      harris41  788:     # Evaluate date windows
1.60      harris41  789:     my $datequery=&build_date_queries(
                    790: 			$ENV{'form.creationdatestart_month'},
                    791: 			$ENV{'form.creationdatestart_day'},
                    792: 			$ENV{'form.creationdatestart_year'},
                    793: 			$ENV{'form.creationdateend_month'},
                    794: 			$ENV{'form.creationdateend_day'},
                    795: 			$ENV{'form.creationdateend_year'},
                    796: 			$ENV{'form.lastrevisiondatestart_month'},
                    797: 			$ENV{'form.lastrevisiondatestart_day'},
                    798: 			$ENV{'form.lastrevisiondatestart_year'},
                    799: 			$ENV{'form.lastrevisiondateend_month'},
                    800: 			$ENV{'form.lastrevisiondateend_day'},
                    801: 			$ENV{'form.lastrevisiondateend_year'},
                    802: 			);
1.90      harris41  803:     # Test to see if date windows are legitimate
1.61      harris41  804:     if ($datequery=~/^Incorrect/) {
                    805: 	&output_date_error($r,$datequery);
                    806: 	return OK;
                    807:     }
                    808:     elsif ($datequery) {
1.60      harris41  809: 	push @queries,$datequery;
                    810:     }
1.90      harris41  811:     # Process form information for custom metadata querying
1.76      harris41  812:     my $customquery='';
1.64      harris41  813:     if ($ENV{'form.custommetadata'}) {
                    814: 	$customquery=&build_custommetadata_query('custommetadata',
                    815: 				      $ENV{'form.custommetadata'});
                    816:     }
1.83      harris41  817:     my $customshow='';
                    818:     if ($ENV{'form.customshow'}) {
                    819: 	$customshow=$ENV{'form.customshow'};
                    820: 	$customshow=~s/[^\w\s]//g;
                    821: 	my @fields=split(/\s+/,$customshow);
                    822: 	$customshow=join(" ",@fields);
                    823:     }
1.133   ! matthew   824:     ## ---------------------------------------------------------------
1.132     matthew   825:     ## Deal with restrictions to given domains
                    826:     ## 
                    827:     my $libraries_to_query = undef;
                    828:     # $ENV{'form.domains'} can be either a scalar or an array reference.
                    829:     # We need an array.
                    830:     my @allowed_domains = (ref($ENV{'form.domains'}) ? @{$ENV{'form.domains'}} 
                    831:                            :  ($ENV{'form.domains'}) );
                    832:     my %domain_hash = ();
                    833:     foreach (@allowed_domains) {
                    834:         $domain_hash{$_}++;
                    835:     }
                    836:     foreach (keys(%Apache::lonnet::libserv)) {
                    837:         if ($_ eq 'any') {
                    838:             $libraries_to_query = undef;
                    839:             last;
                    840:         }
                    841:         if (exists($domain_hash{$Apache::lonnet::hostdom{$_}})) {
                    842:             push @$libraries_to_query,$_;
                    843:         }
                    844:     }
1.133   ! matthew   845:     ## ---------------------------------------------------------------
1.132     matthew   846:     #
1.90      harris41  847:     # Send query statements over the network to be processed by either the SQL
                    848:     # database or a recursive scheme of 'grep'-like actions (for custom
                    849:     # metadata).
1.45      harris41  850:     if (@queries) {
1.58      harris41  851: 	$query=join(" AND ",@queries);
1.46      harris41  852: 	$query="select * from metadata where $query";
1.90      harris41  853: 	my $reply; # reply hash reference
1.83      harris41  854: 	unless ($customquery or $customshow) {
1.132     matthew   855:             $reply=&Apache::lonnet::metadata_query($query,undef,undef,
                    856:                                                    $libraries_to_query);
1.76      harris41  857: 	}
                    858: 	else {
1.83      harris41  859: 	    $reply=&Apache::lonnet::metadata_query($query,
1.132     matthew   860: 						   $customquery,$customshow,
                    861:                                                    $libraries_to_query);
1.76      harris41  862: 	}
1.130     matthew   863: 	&output_results('Advanced',$r,$customquery,$reply,$hidden);
1.126     matthew   864:         return OK;
                    865:     } elsif ($customquery) {
1.90      harris41  866: 	my $reply; # reply hash reference
1.86      harris41  867: 	$reply=&Apache::lonnet::metadata_query('',
1.132     matthew   868: 					       $customquery,$customshow,
                    869:                                                $libraries_to_query);
1.130     matthew   870: 	&output_results('Advanced',$r,$customquery,$reply,$hidden);
1.126     matthew   871:         return OK;
1.45      harris41  872:     }
1.92      harris41  873:     # should not get to this point
                    874:     return 'Error.  Should not have gone to this point.';
1.18      harris41  875: }
                    876: 
1.122     matthew   877: ######################################################################
                    878: ######################################################################
                    879: 
                    880: =pod 
                    881: 
                    882: =item &basicsearch() 
                    883: 
1.126     matthew   884: Parse basic search form.
                    885: 
1.122     matthew   886: =cut
                    887: 
                    888: ######################################################################
                    889: ######################################################################
1.6       harris41  890: sub basicsearch {
1.130     matthew   891:     my ($r,$hidden)=@_;
1.64      harris41  892:     # Clean up fields for safety
                    893:     for my $field ('basicexp') {
                    894: 	$ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
                    895:     }
1.117     matthew   896:     foreach ('mode','form','element') {
                    897: 	# is this required?  Hmmm.
                    898: 	next unless (exists($ENV{"form.$_"}));
                    899: 	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
                    900: 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
                    901:     }
1.64      harris41  902: 
1.90      harris41  903:     # Check to see if enough is filled in
1.26      harris41  904:     unless (&filled($ENV{'form.basicexp'})) {
1.24      harris41  905: 	&output_blank_field_error($r);
                    906: 	return OK;
                    907:     }
1.22      harris41  908: 
1.90      harris41  909:     # Build SQL query string based on form page
1.39      harris41  910:     my $query='';
1.33      harris41  911:     my $concatarg=join(',"    ",',
1.124     matthew   912: 		       ('title', 'author', 'subject', 'notes', 'abstract',
                    913:                         'keywords'));
1.95      harris41  914:     $concatarg='title' if $ENV{'form.titleonly'};
1.94      harris41  915: 
                    916:     $query=&build_SQL_query('concat('.$concatarg.')',$ENV{'form.'.'basicexp'});
                    917: 
1.90      harris41  918:     # Get reply (either a hash reference to filehandles or bad connection)
1.126     matthew   919: #    &Apache::lonnet::logthis("metadata query started:".time);
1.94      harris41  920:     my $reply=&Apache::lonnet::metadata_query('select * from metadata where '.$query);
1.126     matthew   921: #    &Apache::lonnet::logthis("metadata query finished:".time);
1.90      harris41  922:     # Output search results
1.98      harris41  923: 
1.130     matthew   924:     &output_results('Basic',$r,$query,$reply,$hidden);
1.90      harris41  925: 
1.18      harris41  926:     return OK;
1.22      harris41  927: }
                    928: 
1.122     matthew   929: 
                    930: ######################################################################
                    931: ######################################################################
                    932: 
                    933: =pod 
                    934: 
                    935: =item &build_SQL_query() 
                    936: 
1.126     matthew   937: Builds a SQL query string from a logical expression with AND/OR keywords
                    938: using Text::Query and &recursive_SQL_query_builder()
                    939: 
1.122     matthew   940: =cut
                    941: 
                    942: ######################################################################
                    943: ######################################################################
1.98      harris41  944: sub build_SQL_query {
                    945:     my ($field_name,$logic_statement)=@_;
                    946:     my $q=new Text::Query('abc',
                    947: 			  -parse => 'Text::Query::ParseAdvanced',
                    948: 			  -build => 'Text::Query::Build');
                    949:     $q->prepare($logic_statement);
                    950:     my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
                    951:     my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
                    952:     return $sql_query;
                    953: }
                    954: 
1.122     matthew   955: ######################################################################
                    956: ######################################################################
                    957: 
                    958: =pod 
                    959: 
                    960: =item &build_custommetadata_query() 
                    961: 
1.126     matthew   962: Constructs a custom metadata query using a rather heinous regular
                    963: expression.
                    964: 
1.122     matthew   965: =cut
                    966: 
                    967: ######################################################################
                    968: ######################################################################
1.98      harris41  969: sub build_custommetadata_query {
                    970:     my ($field_name,$logic_statement)=@_;
                    971:     my $q=new Text::Query('abc',
                    972: 			  -parse => 'Text::Query::ParseAdvanced',
                    973: 			  -build => 'Text::Query::BuildAdvancedString');
                    974:     $q->prepare($logic_statement);
                    975:     my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
                    976:     # quick fix to change literal into xml tag-matching
                    977:     # will eventually have to write a separate builder module
1.122     matthew   978:     # wordone=wordtwo becomes\<wordone\>[^\<] *wordtwo[^\<]*\<\/wordone\>
                    979:     $matchexp =~ s/(\w+)\\=([\w\\\+]+)?# wordone=wordtwo is changed to 
                    980:                  /\\<$1\\>?#           \<wordone\>
                    981:                    \[\^\\<\]?#        [^\<]         
                    982:                    \*$2\[\^\\<\]?#           *wordtwo[^\<]
                    983:                    \*\\<\\\/$1\\>?#                        *\<\/wordone\>
                    984:                    /g;
1.98      harris41  985:     return $matchexp;
                    986: }
                    987: 
1.122     matthew   988: ######################################################################
                    989: ######################################################################
                    990: 
                    991: =pod 
                    992: 
                    993: =item &recursive_SQL_query_build() 
                    994: 
1.126     matthew   995: Recursively constructs an SQL query.  Takes as input $dkey and $pattern.
                    996: 
1.122     matthew   997: =cut
                    998: 
                    999: ######################################################################
                   1000: ######################################################################
1.98      harris41 1001: sub recursive_SQL_query_build {
                   1002:     my ($dkey,$pattern)=@_;
                   1003:     my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
                   1004:     return $pattern unless @matches;
                   1005:     foreach my $match (@matches) {
                   1006: 	$match=~/\[ (\w+)\s(.*) \]/;
                   1007: 	my ($key,$value)=($1,$2);
                   1008: 	my $replacement='';
                   1009: 	if ($key eq 'literal') {
                   1010: 	    $replacement="($dkey like \"\%$value\%\")";
                   1011: 	}
                   1012: 	elsif ($key eq 'not') {
                   1013: 	    $value=~s/like/not like/;
                   1014: #	    $replacement="($dkey not like $value)";
                   1015: 	    $replacement="$value";
                   1016: 	}
                   1017: 	elsif ($key eq 'and') {
                   1018: 	    $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
                   1019: 	    $replacement="($1 AND $2)";
                   1020: 	}
                   1021: 	elsif ($key eq 'or') {
                   1022: 	    $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
                   1023: 	    $replacement="($1 OR $2)";
                   1024: 	}
                   1025: 	substr($pattern,
                   1026: 	       index($pattern,$match),
                   1027: 	       length($match),
                   1028: 	       $replacement
                   1029: 	       );
                   1030:     }
                   1031:     &recursive_SQL_query_build($dkey,$pattern);
                   1032: }
1.22      harris41 1033: 
1.122     matthew  1034: ######################################################################
                   1035: ######################################################################
                   1036: 
                   1037: =pod 
                   1038: 
                   1039: =item &build_date_queries() 
                   1040: 
1.126     matthew  1041: Builds a SQL logic query to check time/date entries.
                   1042: Also reports errors (check for /^Incorrect/).
                   1043: 
1.122     matthew  1044: =cut
                   1045: 
                   1046: ######################################################################
                   1047: ######################################################################
1.98      harris41 1048: sub build_date_queries {
                   1049:     my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
                   1050: 	$lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
                   1051:     my @queries;
                   1052:     if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
                   1053: 	unless ($cmonth1 and $cday1 and $cyear1 and
                   1054: 		$cmonth2 and $cday2 and $cyear2) {
                   1055: 	    return "Incorrect entry for the creation date.  You must specify ".
                   1056: 		   "a starting month, day, and year and an ending month, ".
                   1057: 		   "day, and year.";
                   1058: 	}
                   1059: 	my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
                   1060: 	$cnumeric1+=0;
                   1061: 	my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
                   1062: 	$cnumeric2+=0;
                   1063: 	if ($cnumeric1>$cnumeric2) {
                   1064: 	    return "Incorrect entry for the creation date.  The starting ".
                   1065: 		   "date must occur before the ending date.";
                   1066: 	}
                   1067: 	my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
                   1068: 	           "$cyear2-$cmonth2-$cday2 23:59:59')";
                   1069: 	push @queries,$cquery;
                   1070:     }
                   1071:     if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
                   1072: 	unless ($lmonth1 and $lday1 and $lyear1 and
                   1073: 		$lmonth2 and $lday2 and $lyear2) {
                   1074: 	    return "Incorrect entry for the last revision date.  You must ".
                   1075: 		   "specify a starting month, day, and year and an ending ".
                   1076: 		   "month, day, and year.";
                   1077: 	}
                   1078: 	my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
                   1079: 	$lnumeric1+=0;
                   1080: 	my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
                   1081: 	$lnumeric2+=0;
                   1082: 	if ($lnumeric1>$lnumeric2) {
                   1083: 	    return "Incorrect entry for the last revision date.  The ".
                   1084: 		   "starting date must occur before the ending date.";
                   1085: 	}
                   1086: 	my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
                   1087: 	           "$lyear2-$lmonth2-$lday2 23:59:59')";
                   1088: 	push @queries,$lquery;
                   1089:     }
                   1090:     if (@queries) {
                   1091: 	return join(" AND ",@queries);
                   1092:     }
                   1093:     return '';
1.18      harris41 1094: }
1.6       harris41 1095: 
1.122     matthew  1096: ######################################################################
                   1097: ######################################################################
                   1098: 
                   1099: =pod 
                   1100: 
                   1101: =item &output_results() 
                   1102: 
                   1103: Format and output results based on a reply list.
                   1104: There are two windows that this function writes to.  The main search
                   1105: window ("srch") has a listing of the results.  A secondary window ("popwin")
                   1106: gives the status of the network search (time elapsed, number of machines
                   1107: contacted, etc.)
                   1108: 
                   1109: =cut
                   1110: 
                   1111: ######################################################################
                   1112: ######################################################################
1.18      harris41 1113: sub output_results {
1.126     matthew  1114: #    &Apache::lonnet::logthis("output_results:".time);
1.101     harris41 1115:     my $fnum; # search result counter
1.130     matthew  1116:     my ($mode,$r,$query,$replyref,$hidden)=@_;
1.92      harris41 1117:     my %rhash=%{$replyref};
1.44      harris41 1118:     my $compiledresult='';
1.125     matthew  1119:     my $timeremain=300; # (seconds)
1.98      harris41 1120:     my $elapsetime=0;
1.93      harris41 1121:     my $resultflag=0;
                   1122:     my $tflag=1;
1.130     matthew  1123:     my $viewselect=$ENV{'form.viewselect'};
1.125     matthew  1124:     #
1.93      harris41 1125:     # make query information persistent to allow for subsequent revision
1.133   ! matthew  1126:     my $persistent=&make_persistent(\%ENV);
1.130     matthew  1127:     # spit out the results header
                   1128:     $r->print(&search_results_header($mode));
1.98      harris41 1129:     $r->rflush();
                   1130:     # begin showing the cataloged results
1.129     matthew  1131:     my $action = "/adm/searchcat";
                   1132:     if ($mode eq 'Basic') { 
                   1133:         $action .= "?reqinterface=basic";
                   1134:     } elsif ($mode eq 'Advanced') {
                   1135:         $action .= "?reqinterface=advanced";
                   1136:     }
                   1137:     $r->print(<<CATALOGCONTROLS);
                   1138: <form name='results' method="post" action="$action">
1.118     www      1139: $hidden
1.98      harris41 1140: <input type='hidden' name='acts' value='' />
1.93      harris41 1141: <input type='button' value='Revise search request'
1.98      harris41 1142: onClick='this.form.submit();' />
                   1143: $importbutton
1.93      harris41 1144: $closebutton
                   1145: $persistent
1.98      harris41 1146: <hr />
                   1147: CATALOGCONTROLS
1.125     matthew  1148:     #
                   1149:     # make the pop-up window for status
                   1150:     #
                   1151:     $r->print(&make_popwin(%rhash));
1.92      harris41 1152:     $r->rflush();
1.125     matthew  1153:     ##
                   1154:     ## Prepare for the main loop below
                   1155:     ##
1.93      harris41 1156:     my $servercount=0;
1.98      harris41 1157:     my $hitcountsum=0;
1.125     matthew  1158:     my $servernum=(keys %rhash);
                   1159:     my $serversleft=$servernum;
                   1160:     ##
                   1161:     ## Run until we run out of time or we run out of servers
                   1162:     ##
                   1163:     while($serversleft && $timeremain) {
                   1164:       ##
                   1165:       ## %rhash has servers deleted from it as results come in 
                   1166:       ## (within the foreach loop below).
                   1167:       ##
                   1168:       foreach my $rkey (sort keys %rhash) {
1.126     matthew  1169: #        &Apache::lonnet::logthis("Server $rkey:".time);
1.93      harris41 1170: 	$servercount++;
                   1171: 	$compiledresult='';
1.92      harris41 1172: 	my $reply=$rhash{$rkey};
1.18      harris41 1173: 	my @results;
1.93      harris41 1174: 	if ($reply eq 'con_lost') {
1.125     matthew  1175: 	    &popwin_imgupdate($r,$rkey,"srvbad.gif");
                   1176: 	    $serversleft--;
                   1177:             delete $rhash{$rkey};
                   1178: 	} else {
                   1179:             # must do since 'use strict' checks for tainting
                   1180: 	    $reply=~/^([\.\w]+)$/; 
                   1181: 	    my $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
1.93      harris41 1182: 	    $reply=~/(.*?)\_/;
1.126     matthew  1183:             for (my $counter=0;$counter<2;$counter++) {
                   1184:                 if (-e $replyfile && ! -e "$replyfile.end") {
                   1185:                     &popwin_imgupdate($r,$rkey,"srvhalf.gif");
                   1186:                     &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
                   1187:                                '"still transferring..."'.';');
                   1188:                 }
                   1189:                 # Are we finished transferring data?
                   1190:                 if (-e "$replyfile.end") {
                   1191:                     $serversleft--;
                   1192:                     delete $rhash{$rkey};
                   1193:                     if (-s $replyfile) {
                   1194:                         &popwin_imgupdate($r,$rkey,"srvgood.gif");
                   1195:                         my $fh;
                   1196:                         unless ($fh=Apache::File->new($replyfile)){ 
                   1197:                             # Is it really appropriate to die on this error?
                   1198:                             $r->print('ERROR: file '.
                   1199:                                       $replyfile.' cannot be opened');
                   1200:                             return OK;
                   1201:                         }
                   1202:                         @results=<$fh> if $fh;
1.127     matthew  1203:                         my $hits =@results;
1.126     matthew  1204:                         &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
1.127     matthew  1205:                                    $hits.';');
                   1206:                         $hitcountsum+=$hits;
1.126     matthew  1207:                         &popwin_js($r,'popwin.document.forms.popremain.'.
                   1208:                                    'numhits.value='.$hitcountsum.';');
                   1209:                     } else {
                   1210:                         &popwin_imgupdate($r,$rkey,"srvempty.gif");
                   1211:                         &popwin_js($r,'popwin.hc["'.$rkey.'"]=0;');
                   1212:                     }
                   1213:                     last;
                   1214:                 } # end of if ( -e "$replyfile.end")
                   1215:                 last unless $timeremain;
                   1216:                 sleep 1;    # wait for daemons to write files?
                   1217:                 $timeremain--;
                   1218:                 $elapsetime++;
                   1219:                 &popwin_js($r,"popwin.document.popremain.".
                   1220:                            "elapsetime.value=$elapsetime;");
1.93      harris41 1221: 	    }
1.117     matthew  1222: 	    &popwin_js($r,'popwin.document.whirly.'.
                   1223: 		       'src="/adm/lonIcons/lonanimend.gif";');
1.125     matthew  1224: 	} # end of if ($reply eq 'con_lost') else statement
1.126     matthew  1225:         my %Fields = undef;     # Holds the data to be sent to the various 
                   1226:                                 # *_view routines.
                   1227:         my ($extrashow,$customfields,$customhash) = &handle_custom_fields(\@results);
                   1228:         my @customfields = @$customfields;
                   1229:         my %customhash   = %$customhash;
                   1230: 	untie %hash if (keys %hash);
                   1231:         #
                   1232: 	if (! tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
                   1233: 	    $r->print('<html><head></head><body>Unable to tie hash to db '.
                   1234:                       'file</body></html>');
                   1235:         } else {
1.101     harris41 1236: 	    if ($ENV{'form.launch'} eq '1') {
                   1237: 		&start_fresh_session();
                   1238: 	    }
                   1239: 	    foreach my $result (@results) {
                   1240: 		next if $result=~/^custom\=/;
                   1241: 		chomp $result;
                   1242: 		next unless $result;
1.126     matthew  1243:                 %Fields = &parse_raw_result($result,$rkey);
                   1244: 		$Fields{'extrashow'}=$extrashow;
1.101     harris41 1245: 		if ($extrashow) {
                   1246: 		    foreach my $field (@customfields) {
                   1247: 			my $value='';
1.126     matthew  1248: 			$value = $1 if ($customhash{$Fields{'url'}}=~/\<{$field}[^\>]*\>(.*?)\<\/{$field}[^\>]*\>/s);
                   1249:                         $Fields{'extrashow'}=~s/\<\!\-\- $field \-\-\>/ $value/g;
                   1250:                     }
                   1251:                 }
                   1252:                 $compiledresult.="\n<p>\n";
                   1253:                 if ($ENV{'form.catalogmode'} eq 'interactive') {
                   1254:                     my $titleesc=$Fields{'title'};
                   1255:                     $titleesc=~s/\'/\\'/; # '
                   1256:                     $compiledresult.=<<END if ($ENV{'form.catalogmode'} eq 'interactive');
1.8       harris41 1257: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
1.126     matthew  1258: onClick="javascript:select_data('$titleesc','$Fields{'url'}')">
1.8       harris41 1259: </font>
1.98      harris41 1260: <br />
                   1261: END
1.115     harris41 1262:                 }
1.101     harris41 1263:                 if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                   1264: 		    $fnum+=0;
1.126     matthew  1265: 		    $hash{"pre_${fnum}_link"}=$Fields{'url'};
                   1266: 		    $hash{"pre_${fnum}_title"}=$Fields{'title'};
1.101     harris41 1267: 		    $compiledresult.=<<END;
1.118     www      1268: <font size='-1'>
                   1269: <input type="checkbox" name="returnvalues" value="SELECT"
1.98      harris41 1270: onClick="javascript:queue($fnum)" />
                   1271: </font>
                   1272: <br />
1.8       harris41 1273: END
1.101     harris41 1274: # <input type="hidden" name="title$fnum" value="$title" />
                   1275: # <input type="hidden" name="url$fnum" value="$url" />
                   1276:                     $fnum++;
                   1277: 		}
                   1278: 	        if ($viewselect eq 'Detailed Citation View') {
1.126     matthew  1279: 		    $compiledresult.=&detailed_citation_view
                   1280:                         (%Fields, hostname => $rkey );
1.101     harris41 1281: 		}
                   1282:                 elsif ($viewselect eq 'Summary View') {
1.126     matthew  1283: 		    $compiledresult.=&summary_view
                   1284:                         (%Fields, hostname => $rkey );
1.101     harris41 1285: 	        }
                   1286:                 elsif ($viewselect eq 'Fielded Format') {
1.126     matthew  1287: 		    $compiledresult.=&fielded_format_view
                   1288:                         (%Fields, hostname => $rkey );
1.101     harris41 1289: 	        }
                   1290:                 elsif ($viewselect eq 'XML/SGML') {
1.126     matthew  1291: 		    $compiledresult.=&xml_sgml_view
                   1292:                         (%Fields, hostname => $rkey );
1.101     harris41 1293: 		}
1.130     matthew  1294:                 if ($compiledresult or $servercount!=$servernum) {
                   1295:                     $compiledresult.="<hr align='left' width='200' noshade />";
                   1296:                 }
1.101     harris41 1297:             }
                   1298:             untie %hash;
1.18      harris41 1299:         }
1.93      harris41 1300: 	if ($compiledresult) {
                   1301: 	    $resultflag=1;
1.126     matthew  1302:             $r->print($compiledresult);
1.18      harris41 1303: 	}
1.93      harris41 1304:         my $percent=sprintf('%3.0f',($servercount/$servernum*100));
1.126     matthew  1305:       } # End of foreach loop over servers remaining
                   1306:     }   # End of big loop - while($serversleft && $timeremain)
1.93      harris41 1307:     unless ($resultflag) {
                   1308:         $r->print("\nThere were no results that matched your query\n");
1.43      harris41 1309:     }
1.100     harris41 1310: #    $r->print('<script type="text/javascript">'.'popwin.close()</script>'."\n"); $r->rflush(); 
1.126     matthew  1311:     $r->print("</body>\n</html>\n");
                   1312:     return;
                   1313: }
                   1314: 
                   1315: ###########################################################
                   1316: ###########################################################
                   1317: 
                   1318: =pod
                   1319: 
                   1320: =item &parse_raw_result()
                   1321: 
                   1322: Takes a line from the file of results and parse it.  Returns a hash 
                   1323: with keys for the following fields:
                   1324: 'title', 'author', 'subject', 'url', 'keywords', 'version', 'notes', 
                   1325: 'abstract', 'mime', 'lang', 'owner', 'copyright', 'creationdate', 
                   1326: 'lastrevisiondate'.
                   1327: 
                   1328: In addition, the following tags are set by calling the appropriate 
                   1329: lonnet function: 'language', 'cprtag', 'mimetag'.
                   1330: 
                   1331: The 'title' field is set to "Untitled" if the title field is blank.
                   1332: 
                   1333: 'abstract' and 'keywords' are truncated to 200 characters.
                   1334: 
                   1335: =cut
                   1336: 
                   1337: ###########################################################
                   1338: ###########################################################
                   1339: sub parse_raw_result {
                   1340:     my ($result,$hostname) = @_;
                   1341:     # Check for a comma - if it is there then we do not need to unescape the
                   1342:     # string.  There seems to be some kind of problem with some items in
                   1343:     # the database - the entire string gets sent out unescaped...?
                   1344:     unless ($result =~ /,/) {
                   1345:         $result = &Apache::lonnet::unescape($result);
                   1346:     }
                   1347:     my @fields=map {
                   1348:         &Apache::lonnet::unescape($_);
                   1349:     } (split(/\,/,$result));
                   1350:     my ($title,$author,$subject,$url,$keywords,$version,
                   1351:         $notes,$abstract,$mime,$lang,
                   1352:         $creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
                   1353:     my %Fields = 
                   1354:         ( title     => &Apache::lonnet::unescape($title),
                   1355:           author    => &Apache::lonnet::unescape($author),
                   1356:           subject   => &Apache::lonnet::unescape($subject),
                   1357:           url       => &Apache::lonnet::unescape($url),
                   1358:           keywords  => &Apache::lonnet::unescape($keywords),
                   1359:           version   => &Apache::lonnet::unescape($version),
                   1360:           notes     => &Apache::lonnet::unescape($notes),
                   1361:           abstract  => &Apache::lonnet::unescape($abstract),
                   1362:           mime      => &Apache::lonnet::unescape($mime),
                   1363:           lang      => &Apache::lonnet::unescape($lang),
                   1364:           owner     => &Apache::lonnet::unescape($owner),
                   1365:           copyright => &Apache::lonnet::unescape($copyright),
                   1366:           creationdate     => &Apache::lonnet::unescape($creationdate),
                   1367:           lastrevisiondate => &Apache::lonnet::unescape($lastrevisiondate)
                   1368:         );
                   1369:     $Fields{'language'} = 
                   1370:         &Apache::loncommon::languagedescription($Fields{'lang'});
                   1371:     $Fields{'copyrighttag'} =
                   1372:         &Apache::loncommon::copyrightdescription($Fields{'copyright'});
                   1373:     $Fields{'mimetag'} =
                   1374:         &Apache::loncommon::filedescription($Fields{'mime'});
                   1375:     # Put spaces in the keyword list, if needed.
                   1376:     $Fields{'keywords'}=~ s/,([A-z])/, $1/g; 
                   1377:     if ($Fields{'title'}=~ /^\s*$/ ) { 
                   1378:         $Fields{'title'}='Untitled'; 
                   1379:     }
                   1380:     unless ($ENV{'user.adv'}) {
                   1381:         $Fields{'keywords'} = '- not displayed -';
                   1382:         $Fields{'notes'}    = '- not displayed -';
                   1383:         $Fields{'abstract'} = '- not displayed -';
                   1384:         $Fields{'subject'}  = '- not displayed -';
                   1385:     }
                   1386:     if (length($Fields{'abstract'})>200) {
                   1387:         $Fields{'abstract'} = 
                   1388:             substr($Fields{'abstract'},0,200).'...';
                   1389:     }
                   1390:     if (length($Fields{'keywords'})>200) {
                   1391:         $Fields{'keywords'} =
                   1392:             substr($Fields{'keywords'},0,200).'...';
                   1393:     }
                   1394:     return %Fields;
                   1395: }
                   1396: 
                   1397: ###########################################################
                   1398: ###########################################################
                   1399: 
                   1400: =pod
                   1401: 
                   1402: =item &handle_custom_fields()
                   1403: 
                   1404: =cut
                   1405: 
                   1406: ###########################################################
                   1407: ###########################################################
                   1408: sub handle_custom_fields {
                   1409:     my @results = @{shift()};
                   1410:     my $customshow='';
                   1411:     my $extrashow='';
                   1412:     my @customfields;
                   1413:     if ($ENV{'form.customshow'}) {
                   1414:         $customshow=$ENV{'form.customshow'};
                   1415:         $customshow=~s/[^\w\s]//g;
                   1416:         my @fields=map {
                   1417:             "<font color=\"#008000\">$_:</font><!-- $_ -->";
                   1418:         } split(/\s+/,$customshow);
                   1419:         @customfields=split(/\s+/,$customshow);
                   1420:         if ($customshow) {
                   1421:             $extrashow="<ul><li>".join("</li><li>",@fields)."</li></ul>\n";
                   1422:         }
                   1423:     }
                   1424:     my $customdata='';
                   1425:     my %customhash;
                   1426:     foreach my $result (@results) {
                   1427:         if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
                   1428:             my $tmp=$result;
                   1429:             $tmp=~s/^custom\=//;
                   1430:             my ($k,$v)=map {&Apache::lonnet::unescape($_);
                   1431:                         } split(/\,/,$tmp);
                   1432:             $customhash{$k}=$v;
                   1433:         }
                   1434:     }
                   1435:     return ($extrashow,\@customfields,\%customhash);
1.41      harris41 1436: }
                   1437: 
1.122     matthew  1438: ######################################################################
                   1439: ######################################################################
                   1440: 
1.125     matthew  1441: =pod
                   1442: 
                   1443: =item &search_results_header
                   1444: 
1.130     matthew  1445: Output the proper html headers and javascript code to deal with different 
                   1446: calling modes.
                   1447: 
                   1448: Takes most inputs directly from %ENV, except $mode.  
                   1449: 
                   1450: =over 4
                   1451: 
                   1452: =item $mode is either (at this writing) 'Basic' or 'Advanced'
                   1453: 
                   1454: =back
1.126     matthew  1455: 
1.130     matthew  1456: The following environment variables are checked:
1.126     matthew  1457: 
                   1458: =over 4
                   1459: 
                   1460: =item 'form.catalogmode' 
                   1461: 
                   1462: Checked for 'interactive' and 'groupsearch'.
                   1463: 
                   1464: =item 'form.mode'
                   1465: 
                   1466: Checked for existance & 'edit' mode.
                   1467: 
                   1468: =item 'form.form'
                   1469: 
                   1470: =item 'form.element'
                   1471: 
                   1472: =back
                   1473: 
1.125     matthew  1474: =cut
                   1475: 
                   1476: ######################################################################
                   1477: ######################################################################
                   1478: sub search_results_header {
1.130     matthew  1479:     my ($mode) = @_;
                   1480:     $mode = lc($mode);
                   1481:     my $title;
                   1482:     if ($mode eq 'advanced') {
                   1483:         $title = "Advanced Search Results";
                   1484:     } elsif ($mode eq 'basic') {
                   1485:         $title = "Basic Search Results";
                   1486:     }
1.125     matthew  1487:     my $result = '';
                   1488:     # output beginning of search page
                   1489:     $result.=<<BEGINNING;
                   1490: <html>
                   1491: <head>
1.130     matthew  1492: <title>$title</title>
1.125     matthew  1493: BEGINNING
                   1494:     # conditional output of script functions dependent on the mode in
                   1495:     # which the search was invoked
                   1496:     if ($ENV{'form.catalogmode'} eq 'interactive'){
                   1497: 	if (! exists($ENV{'form.mode'}) || $ENV{'form.mode'} ne 'edit') {
                   1498:             $result.=<<SCRIPT;
                   1499: <script type="text/javascript">
                   1500:     function select_data(title,url) {
                   1501: 	changeTitle(title);
                   1502: 	changeURL(url);
                   1503: 	self.close();
                   1504:     }
                   1505:     function changeTitle(val) {
                   1506: 	if (opener.inf.document.forms.resinfo.elements.t) {
                   1507: 	    opener.inf.document.forms.resinfo.elements.t.value=val;
                   1508: 	}
                   1509:     }
                   1510:     function changeURL(val) {
                   1511: 	if (opener.inf.document.forms.resinfo.elements.u) {
                   1512: 	    opener.inf.document.forms.resinfo.elements.u.value=val;
                   1513: 	}
                   1514:     }
                   1515: </script>
                   1516: SCRIPT
                   1517:         } elsif ($ENV{'form.mode'} eq 'edit') {
                   1518:             my $form = $ENV{'form.form'};
                   1519:             my $element = $ENV{'form.element'};
                   1520:             $result.=<<SCRIPT;
                   1521: <script type="text/javascript">
                   1522: function select_data(title,url) {
                   1523:     changeURL(url);
                   1524:     self.close();
                   1525: }
                   1526: function changeTitle(val) {
                   1527: }
                   1528: function changeURL(val) {
                   1529:     if (window.opener.document) {
                   1530:         window.opener.document.forms["$form"].elements["$element"].value=val;
                   1531:     } else {
                   1532: 	var url = 'forms[\"$form\"].elements[\"$element\"].value';
                   1533:         alert("Unable to transfer data to "+url);
                   1534:     }
                   1535: }
                   1536: </script>
                   1537: SCRIPT
                   1538:         }
                   1539:     }
                   1540:     $result.=<<SCRIPT if $ENV{'form.catalogmode'} eq 'groupsearch';
                   1541: <script type="text/javascript">
                   1542:     function select_data(title,url) {
                   1543: //	alert('DEBUG: Should be storing '+title+' and '+url);
                   1544:     }
                   1545:     function queue(val) {
                   1546: 	if (eval("document.forms.results.returnvalues["+val+"].checked")) {
                   1547: 	    document.forms.results.acts.value+='1a'+val+'b';
                   1548: 	}
                   1549: 	else {
                   1550: 	    document.forms.results.acts.value+='0a'+val+'b';
                   1551: 	}
                   1552:     }
                   1553:     function select_group() {
                   1554: 	window.location=
                   1555:     "/adm/groupsort?mode=$ENV{'form.mode'}&catalogmode=groupsearch&acts="+
                   1556: 	    document.forms.results.acts.value;
                   1557:     }
                   1558: </script>
                   1559: SCRIPT
                   1560:     $result.=<<SCRIPT;
                   1561: <script type="text/javascript">
                   1562:     function displayinfo(val) {
                   1563: 	popwin.document.forms.popremain.sdetails.value=val;
                   1564:     }
                   1565:     function openhelp(val) {
                   1566: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                   1567: 	     'scrollbars=1,width=400,height=300');
                   1568: 	openhelpwin.focus();
                   1569:     }
                   1570:     function abortsearch(val) {
                   1571: 	popwin.close();
                   1572:     }
                   1573: </script>
                   1574: SCRIPT
1.130     matthew  1575:     $result.=<<END;
                   1576: </head>
                   1577: <body bgcolor="#ffffff">
                   1578: <img align=right src=/adm/lonIcons/lonlogos.gif>
                   1579: <h1>$title</h1>
                   1580: END
1.125     matthew  1581:     return $result;
                   1582: }
                   1583: 
                   1584: ######################################################################
                   1585: ######################################################################
                   1586: 
                   1587: =pod
                   1588: 
                   1589: =item &make_popwin()
                   1590: 
                   1591: Returns html with javascript in it to open up the status window.
                   1592: 
                   1593: =cut
                   1594: 
                   1595: ######################################################################
                   1596: ######################################################################
                   1597: sub make_popwin {
                   1598:     my %rhash = @_;
                   1599:     my $servernum=(keys %rhash);
                   1600:     my $hcinit;
                   1601:     my $grid="'<br />'+\n";
                   1602:     # $sn is the server number, used ONLY to make sure we have
                   1603:     # rows of 10 each.  No longer used to index images.
                   1604:     my $sn=1;
                   1605:     foreach my $sk (sort keys %rhash) {
                   1606: 	# '<a href="
                   1607: 	$grid.="'<a href=\"";
                   1608: 	# javascript:displayinfo('+
                   1609: 	$grid.="javascript:opener.displayinfo('+";
                   1610: 	# "'"+'key
                   1611: 	$grid.="\"'\"+'";
                   1612: 	$grid.=$sk;
                   1613: 	my $hc;
                   1614: 	if ($rhash{$sk} eq 'con_lost') {
                   1615: 	    $hc="BAD CONNECTION, CONTACT SYSTEM ADMINISTRATOR ";
                   1616: 	}
                   1617: 	else {
                   1618: 	    $hc="'+\"'\"+\"+hc['$sk']+\"+\"'\"+'";
                   1619: 	    $hcinit.="hc[\"$sk\"]=\"not yet connected...\";";
                   1620: 	}
                   1621: 	$grid.=" hitcount=".$hc;
1.127     matthew  1622: 	$grid.=" domain=".$Apache::lonnet::hostdom{$sk};
                   1623: 	$grid.=" IP=".$Apache::lonnet::hostip{$sk};
1.125     matthew  1624: 	# '+"'"+'">'+
                   1625: 	$grid.="'+\"'\"+')\">'+";
                   1626: 	$grid.="\n";
1.127     matthew  1627: 	$grid.="'<img border=\"0\" name=\"img_".$Apache::lonnet::hostdom{$sk}.
                   1628:             '_'.$sk."\" src=\"/adm/lonIcons/srvnull.gif\" alt=\"".$sk.
                   1629:                 "\" /></a>'+\n";
1.125     matthew  1630: 	$grid.="'<br />'+\n" unless $sn%10;
                   1631:         $sn++;
                   1632:     }
                   1633:     my $result.=<<ENDPOP;
                   1634: <script type="text/javascript">
                   1635:     popwin=open('','popwin','scrollbars=1,width=400,height=220');
                   1636:     popwin.focus();
                   1637:     popwin.document.writeln('<'+'html>');
                   1638:     popwin.document.writeln('<'+'head>');
                   1639:     popwin.document.writeln('<'+'script>');
                   1640:     popwin.document.writeln('hc=new Array();$hcinit');
                   1641:     popwin.document.writeln('<'+'/script>');
                   1642:     popwin.document.writeln('<'+'/head>'+
                   1643:         '<'+'body bgcolor="#FFFFFF">'+
                   1644: 	'<'+'image name="whirly" align="right" src="/adm/lonIcons/'+
                   1645: 	'lonanim.gif" '+
                   1646: 	'alt="animated logo" />'+
                   1647: 	'<'+'h3>Search Results Progress<'+'/h3>'+
                   1648:         '<'+'form name="popremain">'+
                   1649:         '<'+'tt>'+
                   1650: 	'<'+'br clear="all"/><i>PLEASE BE PATIENT</i>'+
                   1651: 	'<'+'br />SCANNING $servernum SERVERS'+
                   1652: 	'<'+'br clear="all" />Number of record hits found '+
                   1653: 	'<'+'input type="text" size="10" name="numhits"'+
                   1654: 	' value="0" />'+
                   1655: 	'<'+'br clear="all" />Time elapsed '+
                   1656: 	'<'+'input type="text" size="10" name="elapsetime"'+
                   1657: 	' value="0" />'+
                   1658: 	'<'+'br />'+
                   1659: 	'SERVER GRID (click on any cell for details)'+
                   1660:         $grid
                   1661:         '<'+'br />'+
                   1662: 	'Server details '+
                   1663: 	'<'+'input type="text" size="35" name="sdetails"'+
                   1664: 	' value="" />'+
                   1665: 	'<'+'br />'+
                   1666: 	' <'+'input type="button" name="button"'+
                   1667: 	' value="close this window" '+
                   1668: 	' onClick="javascript:opener.abortsearch()" />'+
                   1669: 	' <'+'input type="button" name="button"'+
                   1670: 	' value="help" onClick="javascript:opener.openhelp()" />'+
                   1671: 	'<'+'/tt>'+
                   1672:         '<'+'/form>'+
                   1673:         '<'+'/body><'+'/html>');
                   1674:     popwin.document.close();
                   1675: </script>
                   1676: ENDPOP
                   1677:     return $result;
                   1678: }
                   1679: 
                   1680: ######################################################################
                   1681: ######################################################################
                   1682: 
1.122     matthew  1683: =pod 
                   1684: 
                   1685: =item Metadata Viewing Functions
                   1686: 
                   1687: Output is a HTML-ified string.
                   1688: Input arguments are title, author, subject, url, keywords, version,
                   1689: notes, short abstract, mime, language, creation date,
1.126     matthew  1690: last revision date, owner, copyright, hostname, and
1.122     matthew  1691: extra custom metadata to show.
                   1692: 
                   1693: =over 4
                   1694: 
                   1695: =item &detailed_citation_view() 
                   1696: 
                   1697: =cut
                   1698: 
                   1699: ######################################################################
                   1700: ######################################################################
1.50      harris41 1701: sub detailed_citation_view {
1.126     matthew  1702:     my %values = @_;
1.50      harris41 1703:     my $result=<<END;
1.126     matthew  1704: <h3><a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
                   1705:     target='search_preview'>$values{'title'}</a></h3>
1.56      harris41 1706: <p>
1.130     matthew  1707: <b>$values{'author'}</b>, <i>$values{'owner'}</i><br />
                   1708: 
                   1709: <b>Subject:       </b> $values{'subject'}<br />
                   1710: <b>Keyword(s):    </b> $values{'keywords'}<br />
                   1711: <b>Notes:         </b> $values{'notes'}<br />
                   1712: <b>MIME Type:     </b> $values{'mimetag'}<br />
                   1713: <b>Language:      </b> $values{'language'}<br />
                   1714: <b>Copyright/Distribution:</b> $values{'cprtag'}<br />
1.78      harris41 1715: </p>
1.126     matthew  1716: $values{'extrashow'}
1.78      harris41 1717: <p>
1.126     matthew  1718: $values{'shortabstract'}
1.50      harris41 1719: </p>
                   1720: END
                   1721:     return $result;
                   1722: }
                   1723: 
1.122     matthew  1724: ######################################################################
                   1725: ######################################################################
                   1726: 
                   1727: =pod 
                   1728: 
                   1729: =item &summary_view() 
                   1730: 
                   1731: =cut
                   1732: 
                   1733: ######################################################################
                   1734: ######################################################################
1.50      harris41 1735: sub summary_view {
1.126     matthew  1736:     my %values = @_;
1.50      harris41 1737:     my $result=<<END;
1.126     matthew  1738: <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
                   1739:    target='search_preview'>$values{'author'}</a><br />
                   1740: $values{'title'}<br />
                   1741: $values{'owner'} -- $values{'lastrevisiondate'}<br />
                   1742: $values{'copyrighttag'}<br />
                   1743: $values{'extrashow'}
1.50      harris41 1744: </p>
                   1745: END
                   1746:     return $result;
                   1747: }
                   1748: 
1.122     matthew  1749: ######################################################################
                   1750: ######################################################################
                   1751: 
                   1752: =pod 
                   1753: 
                   1754: =item &fielded_format_view() 
                   1755: 
                   1756: =cut
                   1757: 
                   1758: ######################################################################
                   1759: ######################################################################
1.50      harris41 1760: sub fielded_format_view {
1.126     matthew  1761:     my %values = @_;
1.50      harris41 1762:     my $result=<<END;
1.126     matthew  1763: <b>URL: </b> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
                   1764:               target='search_preview'>$values{'url'}</a>
1.56      harris41 1765: <br />
1.126     matthew  1766: <b>Title:</b> $values{'title'}<br />
                   1767: <b>Author(s):</b> $values{'author'}<br />
                   1768: <b>Subject:</b> $values{'subject'}<br />
                   1769: <b>Keyword(s):</b> $values{'keywords'}<br />
                   1770: <b>Notes:</b> $values{'notes'}<br />
                   1771: <b>MIME Type:</b> $values{'mimetag'}<br />
                   1772: <b>Language:</b> $values{'language'}<br />
                   1773: <b>Creation Date:</b> $values{'creationdate'}<br />
                   1774: <b>Last Revision Date:</b> $values{'lastrevisiondate'}<br />
                   1775: <b>Publisher/Owner:</b> $values{'owner'}<br />
                   1776: <b>Copyright/Distribution:</b> $values{'copyrighttag'}<br />
                   1777: <b>Repository Location:</b> $values{'hostname'}<br />
                   1778: <b>Abstract:</b> $values{'shortabstract'}<br />
                   1779: $values{'extrashow'}
1.50      harris41 1780: </p>
                   1781: END
                   1782:     return $result;
                   1783: }
                   1784: 
1.122     matthew  1785: ######################################################################
                   1786: ######################################################################
                   1787: 
                   1788: =pod 
                   1789: 
                   1790: =item &xml_sgml_view() 
                   1791: 
                   1792: =back 
                   1793: 
                   1794: =cut
                   1795: 
                   1796: ######################################################################
                   1797: ######################################################################
1.50      harris41 1798: sub xml_sgml_view {
1.126     matthew  1799:     my %values = @_;
1.50      harris41 1800:     my $result=<<END;
1.56      harris41 1801: <pre>
                   1802: &lt;LonCapaResource&gt;
1.126     matthew  1803: &lt;url&gt;$values{'url'}&lt;/url&gt;
                   1804: &lt;title&gt;$values{'title'}&lt;/title&gt;
                   1805: &lt;author&gt;$values{'author'}&lt;/author&gt;
                   1806: &lt;subject&gt;$values{'subject'}&lt;/subject&gt;
                   1807: &lt;keywords&gt;$values{'keywords'}&lt;/keywords&gt;
                   1808: &lt;notes&gt;$values{'notes'}&lt;/notes&gt;
1.56      harris41 1809: &lt;mimeInfo&gt;
1.126     matthew  1810: &lt;mime&gt;$values{'mime'}&lt;/mime&gt;
                   1811: &lt;mimetag&gt;$values{'mimetag'}&lt;/mimetag&gt;
1.56      harris41 1812: &lt;/mimeInfo&gt;
                   1813: &lt;languageInfo&gt;
1.126     matthew  1814: &lt;language&gt;$values{'lang'}&lt;/language&gt;
                   1815: &lt;languagetag&gt;$values{'language'}&lt;/languagetag&gt;
1.56      harris41 1816: &lt;/languageInfo&gt;
1.126     matthew  1817: &lt;creationdate&gt;$values{'creationdate'}&lt;/creationdate&gt;
                   1818: &lt;lastrevisiondate&gt;$values{'lastrevisiondate'}&lt;/lastrevisiondate&gt;
                   1819: &lt;owner&gt;$values{'owner'}&lt;/owner&gt;
1.56      harris41 1820: &lt;copyrightInfo&gt;
1.126     matthew  1821: &lt;copyright&gt;$values{'copyright'}&lt;/copyright&gt;
                   1822: &lt;copyrighttag&gt;$values{'copyrighttag'}&lt;/copyrighttag&gt;
1.56      harris41 1823: &lt;/copyrightInfo&gt;
1.126     matthew  1824: &lt;repositoryLocation&gt;$values{'hostname'}&lt;/repositoryLocation&gt;
                   1825: &lt;shortabstract&gt;$values{'shortabstract'}&lt;/shortabstract&gt;
1.57      harris41 1826: &lt;/LonCapaResource&gt;
1.56      harris41 1827: </pre>
1.126     matthew  1828: $values{'extrashow'}
1.50      harris41 1829: END
                   1830:     return $result;
1.60      harris41 1831: }
                   1832: 
1.122     matthew  1833: ######################################################################
                   1834: ######################################################################
                   1835: 
                   1836: =pod 
                   1837: 
                   1838: =item &filled() see if field is filled.
                   1839: 
                   1840: =cut
                   1841: 
                   1842: ######################################################################
                   1843: ######################################################################
1.98      harris41 1844: sub filled {
                   1845:     my ($field)=@_;
                   1846:     if ($field=~/\S/ && $field ne 'any') {
                   1847: 	return 1;
1.61      harris41 1848:     }
1.98      harris41 1849:     else {
                   1850: 	return 0;
1.61      harris41 1851:     }
1.60      harris41 1852: }
                   1853: 
1.122     matthew  1854: ######################################################################
                   1855: ######################################################################
                   1856: 
                   1857: =pod 
                   1858: 
                   1859: =item &output_blank_field_error()
                   1860: 
                   1861: =cut
                   1862: 
                   1863: ######################################################################
                   1864: ######################################################################
1.98      harris41 1865: sub output_blank_field_error {
                   1866:     my ($r)=@_;
                   1867:     # make query information persistent to allow for subsequent revision
1.133   ! matthew  1868:     my $persistent=&make_persistent(\%ENV);
1.98      harris41 1869: 
                   1870:     $r->print(<<BEGINNING);
                   1871: <html>
                   1872: <head>
                   1873: <title>The LearningOnline Network with CAPA</title>
                   1874: BEGINNING
                   1875:     $r->print(<<RESULTS);
                   1876: </head>
                   1877: <body bgcolor="#ffffff">
                   1878: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
                   1879: <h1>Search Catalog</h1>
                   1880: <form method="post" action="/adm/searchcat">
                   1881: $persistent
                   1882: <input type='button' value='Revise search request'
                   1883: onClick='this.form.submit();' />
                   1884: $closebutton
                   1885: <hr />
                   1886: <h3>Helpful Message</h3>
                   1887: <p>
                   1888: Incorrect search query due to blank entry fields.
                   1889: You need to fill in the relevant
                   1890: fields on the search page in order for a query to be
                   1891: processed.
                   1892: </p>
                   1893: </body>
                   1894: </html>
                   1895: RESULTS
                   1896: }
                   1897: 
1.122     matthew  1898: ######################################################################
                   1899: ######################################################################
                   1900: 
                   1901: =pod 
                   1902: 
                   1903: =item &output_date_error()
                   1904: 
                   1905: Output a full html page with an error message.
                   1906: 
                   1907: =cut
                   1908: 
                   1909: ######################################################################
                   1910: ######################################################################
1.60      harris41 1911: sub output_date_error {
                   1912:     my ($r,$message)=@_;
                   1913:     # make query information persistent to allow for subsequent revision
1.133   ! matthew  1914:     my $persistent=&make_persistent(\%ENV);
1.60      harris41 1915: 
1.122     matthew  1916:     $r->print(<<RESULTS);
1.60      harris41 1917: <html>
                   1918: <head>
                   1919: <title>The LearningOnline Network with CAPA</title>
                   1920: </head>
                   1921: <body bgcolor="#ffffff">
1.98      harris41 1922: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
1.60      harris41 1923: <h1>Search Catalog</h1>
                   1924: <form method="post" action="/adm/searchcat">
                   1925: $persistent
                   1926: <input type='button' value='Revise search request'
1.98      harris41 1927: onClick='this.form.submit();' />
1.60      harris41 1928: $closebutton
1.98      harris41 1929: <hr />
1.60      harris41 1930: <h3>Helpful Message</h3>
                   1931: <p>
                   1932: $message
                   1933: </p>
                   1934: </body>
                   1935: </html>
                   1936: RESULTS
1.101     harris41 1937: }
                   1938: 
1.122     matthew  1939: ######################################################################
                   1940: ######################################################################
                   1941: 
                   1942: =pod 
                   1943: 
                   1944: =item &start_fresh_session()
                   1945: 
                   1946: Cleans the global %hash by removing all fields which begin with
                   1947: 'pre_' or 'store'.
                   1948: 
                   1949: =cut
                   1950: 
                   1951: ######################################################################
                   1952: ######################################################################
1.101     harris41 1953: sub start_fresh_session {
                   1954:     delete $hash{'mode_catalog'};
1.109     harris41 1955:     foreach (keys %hash) {
1.101     harris41 1956:         if ($_ =~ /^pre_/) {
                   1957:             delete $hash{$_};
                   1958:         }
                   1959:         if ($_ =~ /^store/) {
                   1960: 	    delete $hash{$_};
                   1961: 	}
1.109     harris41 1962:     }
1.3       harris41 1963: }
1.117     matthew  1964: 
1.122     matthew  1965: ######################################################################
                   1966: ######################################################################
                   1967: 
                   1968: =pod 
                   1969: 
                   1970: =item &popwin_js() send javascript to popwin
                   1971: 
                   1972: =cut
                   1973: 
                   1974: ######################################################################
                   1975: ######################################################################
1.117     matthew  1976: sub popwin_js {
                   1977:     # Print javascript out to popwin, but make sure we dont generate
                   1978:     # any javascript errors in doing so.
                   1979:     my ($r,$text) = @_;
                   1980:     $r->print(<<"END");
                   1981: <script type="text/javascript">
                   1982:     if (! popwin.closed) {
                   1983: 	$text
                   1984:     }
                   1985: </script>
                   1986: END
                   1987:     $r->rflush();
                   1988: }
                   1989: 
1.122     matthew  1990: ######################################################################
                   1991: ######################################################################
                   1992: 
                   1993: =pod 
                   1994: 
                   1995: =item &popwin_imgupdate()
                   1996: 
1.125     matthew  1997: Send a given image (and its location) out to the browser.  Takes as 
                   1998: input $r, loncapa server id, and an icon URL.
                   1999: 
1.122     matthew  2000: =cut
                   2001: 
                   2002: ######################################################################
                   2003: ######################################################################
1.117     matthew  2004: sub popwin_imgupdate {
1.125     matthew  2005:     my ($r,$server,$icon) = @_;
1.127     matthew  2006:     &popwin_js($r,'popwin.document.img_'.$Apache::lonnet::hostdom{$server}.
                   2007:                '_'.$server.'.'.'src="/adm/lonIcons/'.$icon.'";');
1.117     matthew  2008: }    
1.1       www      2009: 
                   2010: 1;
1.98      harris41 2011: 
1.1       www      2012: __END__
1.105     harris41 2013: 
1.121     matthew  2014: =pod
1.105     harris41 2015: 
1.121     matthew  2016: =back 
1.105     harris41 2017: 
                   2018: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>