+ + +$closebutton +
END + my $srchtype = 'Catalog'; + my $jscript; + if ($env{'form.area'} eq 'portfolio') { + $srchtype = 'Portfolio'; + $jscript = ''; + } + my $scrout= &Apache::loncommon::start_page("Advanced $srchtype Search", + $jscript); + $scrout .= $bread_crumb; + + $scrout .= ''; + + $scrout .= &Apache::loncommon::end_page(); + $r->print($scrout); + return; +} + +###################################################################### +###################################################################### + +=pod + +=item &titlefield() + +Inputs: title text + +Outputs: titletext with font wrapper + +=cut + +###################################################################### +###################################################################### +sub titlefield { + my $title=shift; + return $title; +} + +###################################################################### +###################################################################### + +=pod + +=item viewoptiontext() + +Inputs: codename for view option + +Outputs: displayed text + +=cut + +###################################################################### +###################################################################### +sub viewoptiontext { + my $code=shift; + my %desc=&Apache::lonlocal::texthash + ('detailed' => "Detailed Citation View", + 'xml' => 'XML/SGML', + 'compact' => 'Compact View', + 'fielded' => 'Fielded Format', + 'summary' => 'Summary View', + 'summarypreview' => 'Summary Preview', + 'detailedpreview' => 'Detailed Citation Preview'); + return $desc{$code}; +} + +###################################################################### +###################################################################### + +=pod + +=item viewoptions() + +Inputs: none + +Outputs: text for box with view options + +=cut + +###################################################################### +###################################################################### +sub viewoptions { + my $scrout; + if (! defined($env{'form.viewselect'})) { + $env{'form.viewselect'}='detailed'; + } + $scrout .= '' + .&mt('Type:').' ' + .&Apache::lonmeta::selectbox('viewselect', + $env{'form.viewselect'}, + \&viewoptiontext, + sort(keys(%Views))) + .''; + my $countselect = &Apache::lonmeta::selectbox('show', + $env{'form.show'}, + undef, + (10,20,50,100,1000,10000)); + $scrout .= ' ' + .&mt('Records per Page:').' '.$countselect + .''.$/; + return $scrout; +} + +###################################################################### +###################################################################### + +=pod + +=item searchhelp() + +Inputs: none + +Outputs: return little blurb on how to enter searches + +=cut + +###################################################################### +###################################################################### +sub searchhelp { + return &mt('Enter words and quoted phrases'); +} + +###################################################################### +###################################################################### + +=pod + +=item &get_persistent_form_data() + +Inputs: filename of database + +Outputs: returns undef on database errors. + +This function is the reverse of &make_persistent() for form data. +Retrieve persistent data from %persistent_db. Retrieved items will have their +values unescaped. If a form value already exists in $env, it will not be +overwritten. Form values that are array references may have values appended +to them. + +=cut + +###################################################################### +###################################################################### +sub get_persistent_form_data { + my $filename = shift; + return 0 if (! -e $filename); + return undef if (! tie(%persistent_db,'GDBM_File',$filename, + &GDBM_READER(),0640)); + # + # These make sure we do not get array references printed out as 'values'. + my %arrays_allowed = ('form.domains'=>1); + # + # Loop through the keys, looking for 'form.' + foreach my $name (keys(%persistent_db)) { + next if ($name !~ /^form./); + # Kludgification begins! + if ($name eq 'form.domains' && + $env{'form.searchmode'} eq 'basic' && + $env{'form.phase'} ne 'disp_basic') { + next; + } + # End kludge (hopefully) + next if (exists($env{$name})); + my @values = map { + &unescape($_); + } split(',',$persistent_db{$name}); + next if (@values <1); + if ($arrays_allowed{$name}) { + $env{$name} = [@values]; + } else { + $env{$name} = $values[0] if ($values[0]); + } + } + untie (%persistent_db); + return 1; +} + +###################################################################### +###################################################################### + +=pod + +=item &get_persistent_data() + +Inputs: filename of database, ref to array of values to recover. + +Outputs: array of values. Returns undef on error. + +This function is the reverse of &make_persistent(); +Retrieve persistent data from %persistent_db. Retrieved items will have their +values unescaped. If the item contains commas (before unescaping), the +returned value will be an array pointer. + +=cut + +###################################################################### +###################################################################### +sub get_persistent_data { + my $filename = shift; + my @Vars = @{shift()}; + my @Values; # Return array + return undef if (! -e $filename); + return undef if (! tie(%persistent_db,'GDBM_File',$filename, + &GDBM_READER(),0640)); + foreach my $name (@Vars) { + if (! exists($persistent_db{$name})) { + push @Values, undef; + next; + } + my @values = map { + &unescape($_); + } split(',',$persistent_db{$name}); + if (@values <= 1) { + push @Values,$values[0]; + } else { + push @Values,\@values; + } + } + untie (%persistent_db); + return @Values; +} + +###################################################################### +###################################################################### + +=pod + +=item &make_persistent() + +Inputs: Hash of values to save, filename of persistent database. + +Store variables away to the %persistent_db. +Values will be escaped. Values that are array pointers will have their +elements escaped and concatenated in a comma separated string. + +=cut + +###################################################################### +###################################################################### +sub make_persistent { + my %save = %{shift()}; + my $filename = shift; + return undef if (! tie(%persistent_db,'GDBM_File', + $filename,&GDBM_WRCREAT(),0640)); + foreach my $name (keys(%save)) { + my @values = (ref($save{$name}) ? @{$save{$name}} : ($save{$name})); + # We handle array references, but not recursively. + my $store = join(',', map { &escape($_); } @values ); + $persistent_db{$name} = $store; + } + untie(%persistent_db); + return 1; +} + +###################################################################### +###################################################################### + +=pod + +=item &make_form_data_persistent() + +Inputs: filename of persistent database. + +Store most form variables away to the %persistent_db. +Values will be escaped. Values that are array pointers will have their +elements escaped and concatenated in a comma separated string. + +=cut + +###################################################################### +###################################################################### +sub make_form_data_persistent { + my $r = shift; + my $filename = shift; + my %save; + foreach (keys(%env)) { + next if (!/^form/ || /submit/); + $save{$_} = $env{$_}; + } + return &make_persistent(\%save,$filename); +} + +###################################################################### +###################################################################### + +=pod + +=item &parse_advanced_search() + +Parse advanced search form and return the following: + +=over 4 + +=item $query Scalar containing an SQL query. + +=item $customquery Scalar containing a custom query. + +=item $customshow Scalar containing commands to show custom metadata. + +=item $libraries_to_query Reference to array of domains to search. + +=back + +=cut + +###################################################################### +###################################################################### +sub parse_advanced_search { + my ($r,$closebutton,$hidden_fields)=@_; + my @BasicFields = ('title','author','subject','keywords','url','version', + 'notes','abstract','extension','owner','authorspace', +# 'custommetadata','customshow', + 'modifyinguser','standards','mime'); + my @StatsFields = &statfields(); + my @EvalFields = &evalfields(); + my $fillflag=0; + my $pretty_search_string = ""; + # Clean up fields for safety + for my $field (@BasicFields, + 'creationdatestart_month','creationdatestart_day', + 'creationdatestart_year','creationdateend_month', + 'creationdateend_day','creationdateend_year', + 'lastrevisiondatestart_month','lastrevisiondatestart_day', + 'lastrevisiondatestart_year','lastrevisiondateend_month', + 'lastrevisiondateend_day','lastrevisiondateend_year') { + $env{'form.'.$field}=~s/[^\w\/\s\(\)\=\-\"\'.\*]//g; + } + foreach ('mode','form','element') { + # is this required? Hmmm. + next if (! exists($env{'form.'.$_})); + $env{'form.'.$_}=&unescape($env{'form.'.$_}); + $env{'form.'.$_}=~s/[^\w\/\s\(\)\=\-\"\']//g; + } + # Preprocess the category form element. + $env{'form.category'} = 'any' if (! defined($env{'form.category'}) || + ref($env{'form.category'})); + # + # Check to see if enough information was filled in + foreach my $field (@BasicFields) { + if (&filled($env{'form.'.$field})) { + $fillflag++; + } + } + foreach my $field (@StatsFields,@EvalFields) { + if (&filled($env{'form.'.$field.'_max'})) { + $fillflag++; + } + if (&filled($env{'form.'.$field.'_min'})) { + $fillflag++; + } } - %cprtag=(); - $cprtag{'any'}='Any copyright/distribution'; - { - my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab'); - map { - $_=~/(\w+)\s+([\w\s\-]+)/; - $cprtag{$1}=$2; - } <$fh>; + for my $field ('lowestgradelevel','highestgradelevel') { + if ( $env{'form.'.$field} =~ /^\d+$/ && + $env{'form.'.$field} > 0) { + $fillflag++; + } + } + if ($env{'form.area'} eq 'portfolio') { + # Added metadata fields + for (my $i=0; $i<$env{'form.numaddedfields'} ; $i++) { + my $field = $env{'form.addedfield_'.$i}; + $field =~ s/^\s*(\S*)\s*$/$1/; + $field =~ s/\W/_/g; + if ($field ne '') { + $fillflag++; + } + } + } + if (! $fillflag) { + &output_blank_field_error($r,$closebutton, + 'phase=disp_adv',$hidden_fields); + return ; + } + # Turn the form input into a SQL-based query + my $query=''; + my @queries; + my $font = ''; + # Evaluate logical expression AND/OR/NOT phrase fields. + foreach my $field (@BasicFields) { + next if (!defined($env{'form.'.$field}) || $env{'form.'.$field} eq ''); + my ($error,$SQLQuery) = + &process_phrase_input($env{'form.'.$field}, + $env{'form.'.$field.'_related'},$field); + if (defined($error)) { + &output_unparsed_phrase_error($r,$closebutton,'phase=disp_adv', + $hidden_fields,$field); + return; + } else { + $pretty_search_string .= + $font.$field.': '.$env{'form.'.$field}; + if ($env{'form.'.$field.'_related'}) { + my @Words = + &Apache::loncommon::get_related_words + ($env{'form.'.$field}); + if (@Words) { + $pretty_search_string.= ' with related words: '. + join(', ',@Words[0..4]); + } else { + $pretty_search_string.= ' with related words.'; + } + } + $pretty_search_string .= 'table: |'.$table.'|
' # SB + .''
+ .&mt('Unable to retrieve search results. '
+ .'Unable to determine the table results were saved in.')
+ .&Apache::loncommon::end_page()
+ );
+ return undef;
+ }
+ ##
+ ## Make sure we can connect and the table exists.
+ ##
+ my $connection_result = &Apache::lonmysql::connect_to_db();
+ if (!defined($connection_result)) {
+ $r->print("Unable to connect to the MySQL database where your results".
+ " are saved.".
+ &Apache::loncommon::end_page());
+ &Apache::lonnet::logthis("lonsearchcat: unable to get lonmysql to".
+ " connect to database.");
+ &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
+ return undef;
+ }
+ my $table_check = &Apache::lonmysql::check_table($table);
+ if (! defined($table_check)) {
+ $r->print("A MySQL error has occurred.".
+ &Apache::loncommon::end_page());
+ &Apache::lonnet::logthis("lonmysql was unable to determine the status".
+ " of table ".$table);
+ return undef;
+ } elsif (! $table_check) {
+ $r->print("The table of results could not be found.");
+ &Apache::lonnet::logthis("The user requested a table, ".$table.
+ ", that could not be found.");
+ return undef;
+ }
+ return 1;
+}
+
+######################################################################
+######################################################################
+
+=pod
+
+=item &print_sort_form()
+
+The sort feature is not implemented at this time. This form just prints
+a link to change the search query.
+
+=cut
+
+######################################################################
+######################################################################
+sub print_sort_form {
+ my ($r,$pretty_query_string) = @_;
+
+ ##
+ my %SortableFields=&Apache::lonlocal::texthash(
+ id => 'Default',
+ title => 'Title',
+ author => 'Author',
+ subject => 'Subject',
+ url => 'URL',
+ version => 'Version Number',
+ mime => 'Mime type',
+ lang => 'Language',
+ owner => 'Owner/Publisher',
+ copyright => 'Copyright',
+ hostname => 'Host',
+ creationdate => 'Creation Date',
+ lastrevisiondate => 'Revision Date'
+ );
+ ##
+ my $table = $env{'form.table'};
+ return if (! &ensure_db_and_table($r,$table));
+ ##
+ ## Get the number of results
+ ##
+ my $total_results = &Apache::lonmysql::number_of_rows($table);
+ if (! defined($total_results)) {
+ $r->print("A MySQL error has occurred.".
+ &Apache::loncommon::end_page());
+ &Apache::lonnet::logthis("lonmysql was unable to determine the number".
+ " of rows in table ".$table);
+ &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
+ return;
+ }
+ my $js =< '
+ .&mt('There are [_1] matches to your query.',$total_results)
+ .' '.$revise.' '.&mt('Search: ').$pretty_query_string
+ .'Sort Results
+#Sort by: \n";
+ my $revise = &revise_button();
+ $result.='
-between:
-CREATIONDATESTART
- $scrout.=&dateboxes('creationdatestart',1,1,1976,
- $ENV{'form.creationdatestart_month'},
- $ENV{'form.creationdatestart_day'},
- $ENV{'form.creationdatestart_year'},
- );
- $scrout.=<
between:
-LASTREVISIONDATESTART
- $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
- $ENV{'form.lastrevisiondatestart_month'},
- $ENV{'form.lastrevisiondatestart_day'},
- $ENV{'form.lastrevisiondatestart_year'},
- );
- $scrout.=<
-Example: grandmother=75 OR grandfather=85
-
-CUSTOMMETADATA
-$scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'});
-$scrout.=' initial users of this system do not need to worry about this option';
-
-# ---------------------------------------------------------------- Print screen
- $r->print(<Search Catalog
-
-
-