X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=catalogue%2Fsearch.pl;h=25b9855a91c9cf7823bd5da746b17102cbfa1d9b;hb=c60c8f0821a89b2914124b77ef6ae80421940009;hp=235198e59a59ef2d9d2867ec9dbeeec62a4f10ed;hpb=52848003e3a0e4a5fef144b8dc7b8bf3f89638e2;p=koha.git diff --git a/catalogue/search.pl b/catalogue/search.pl index 235198e59a..25b9855a91 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -7,18 +7,18 @@ # # This file is part of Koha # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . =head1 NAME @@ -144,34 +144,38 @@ use C4::Context; use C4::Output; use C4::Auth qw(:DEFAULT get_session); use C4::Search; -use C4::Languages qw(getAllLanguages); +use C4::Languages qw(getLanguages); use C4::Koha; use C4::Members qw(GetMember); -use C4::VirtualShelves qw(GetRecentShelves); +use URI::Escape; use POSIX qw(ceil floor); use C4::Branch; # GetBranches +use C4::Search::History; + +use Koha::LibraryCategories; +use Koha::Virtualshelves; + +use URI::Escape; my $DisplayMultiPlaceHold = C4::Context->preference("DisplayMultiPlaceHold"); # create a new CGI object # FIXME: no_undef_params needs to be tested -use CGI qw('-no_undef_params'); +use CGI qw('-no_undef_params' -utf8 ); my $cgi = new CGI; -my ($template,$borrowernumber,$cookie); - # decide which template to use my $template_name; my $template_type; my @params = $cgi->param("limit"); if ((@params>=1) || ($cgi->param("q")) || ($cgi->param('multibranchlimit')) || ($cgi->param('limit-yr')) ) { - $template_name = 'catalogue/results.tmpl'; + $template_name = 'catalogue/results.tt'; } else { - $template_name = 'catalogue/advsearch.tmpl'; + $template_name = 'catalogue/advsearch.tt'; $template_type = 'advsearch'; } # load the template -($template, $borrowernumber, $cookie) = get_template_and_user({ +my ($template, $borrowernumber, $cookie) = get_template_and_user({ template_name => $template_name, query => $cgi, type => "intranet", @@ -179,9 +183,15 @@ else { flagsrequired => { catalogue => 1 }, } ); + +my $lang = C4::Languages::getlanguage($cgi); + if (C4::Context->preference("marcflavour") eq "UNIMARC" ) { $template->param('UNIMARC' => 1); } +if (C4::Context->preference("IntranetNumbersPreferPhrase")) { + $template->param('numbersphr' => 1); +} if($cgi->cookie("holdfor")){ my $holdfor_patron = GetMember('borrowernumber' => $cgi->cookie("holdfor")); @@ -221,7 +231,7 @@ if($cgi->cookie("holdfor")){ my $branches = GetBranches(); # Populate branch_loop with all branches sorted by their name. If -# independantbranches is activated, set the default branch to the borrower +# IndependentBranches is activated, set the default branch to the borrower # branch, except for superlibrarian who need to search all libraries. my $user = C4::Context->userenv; my @branch_loop = map { @@ -234,7 +244,7 @@ my @branch_loop = map { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches; -my $categories = GetBranchCategories(undef,'searchdomain'); +my $categories = Koha::LibraryCategories->search( { categorytype => 'searchdomain' }, { order_by => [ 'categorytype', 'categorycode' ] } ); $template->param(branchloop => \@branch_loop, searchdomainloop => $categories); @@ -248,14 +258,16 @@ my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes") || "i my @advanced_search_types = split(/\|/, $advanced_search_types); foreach my $advanced_srch_type (@advanced_search_types) { + $advanced_srch_type =~ s/^\s*//; + $advanced_srch_type =~ s/\s*$//; if ($advanced_srch_type eq 'itemtypes') { # itemtype is a special case, since it's not defined in authorized values my @itypesloop; - foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'} } keys %$itemtypes ) { + foreach my $thisitemtype ( sort {$itemtypes->{$a}->{translated_description} cmp $itemtypes->{$b}->{translated_description} } keys %$itemtypes ) { my %row =( number=>$cnt++, ccl => "$itype_or_itemtype,phr", code => $thisitemtype, - description => $itemtypes->{$thisitemtype}->{'description'}, + description => $itemtypes->{$thisitemtype}->{translated_description}, imageurl=> getitemtypeimagelocation( 'intranet', $itemtypes->{$thisitemtype}->{'imageurl'} ), ); push @itypesloop, \%row; @@ -334,7 +346,7 @@ if ( $template_type eq 'advsearch' ) { search_boxes_loop => \@search_boxes_array); # load the language limits (for search) - my $languages_limit_loop = getAllLanguages(); + my $languages_limit_loop = getLanguages($lang, 1); $template->param(search_languages_loop => $languages_limit_loop,); # Expanded search options in advanced search: @@ -372,7 +384,7 @@ if ( C4::Context->preference('defaultSortField') . C4::Context->preference('defaultSortOrder'); } -@sort_by = split("\0",$params->{'sort_by'}) if $params->{'sort_by'}; +@sort_by = $cgi->param('sort_by'); $sort_by[0] = $default_sort_by unless $sort_by[0]; foreach my $sort (@sort_by) { $template->param($sort => 1) if $sort; @@ -380,8 +392,7 @@ foreach my $sort (@sort_by) { $template->param('sort_by' => $sort_by[0]); # Use the servers defined, or just search our local catalog(default) -my @servers; -@servers = split("\0",$params->{'server'}) if $params->{'server'}; +my @servers = $cgi->param('server'); unless (@servers) { #FIXME: this should be handled using Context.pm @servers = ("biblioserver"); @@ -389,13 +400,11 @@ unless (@servers) { } # operators include boolean and proximity operators and are used # to evaluate multiple operands -my @operators; -@operators = split("\0",$params->{'op'}) if $params->{'op'}; +my @operators = map uri_unescape($_), $cgi->param('op'); # indexes are query qualifiers, like 'title', 'author', etc. They # can be single or multiple parameters separated by comma: kw,right-Truncation -my @indexes; -@indexes = split("\0",$params->{'idx'}); +my @indexes = map uri_unescape($_), $cgi->param('idx'); # if a simple index (only one) display the index used in the top search box if ($indexes[0] && (!$indexes[1] || $params->{'scan'})) { @@ -404,17 +413,18 @@ if ($indexes[0] && (!$indexes[1] || $params->{'scan'})) { $template->param($idx => 1); } - # an operand can be a single term, a phrase, or a complete ccl query -my @operands; -@operands = split("\0",$params->{'q'}) if $params->{'q'}; +my @operands = map uri_unescape($_), $cgi->param('q'); # limits are use to limit to results to a pre-defined category such as branch or language -my @limits; -@limits = split("\0",$params->{'limit'}) if $params->{'limit'}; +my @limits = map uri_unescape($_), $cgi->param('limit'); +my @nolimits = map uri_unescape($_), $cgi->param('nolimit'); +my %is_nolimit = map { $_ => 1 } @nolimits; +@limits = grep { not $is_nolimit{$_} } @limits; if($params->{'multibranchlimit'}) { - push @limits, '('.join( " or ", map { "branch: $_ " } @{ GetBranchesInCategory( $params->{'multibranchlimit'} ) } ).')'; + my $multibranch = '('.join( " or ", map { "branch: $_ " } @{ GetBranchesInCategory( $params->{'multibranchlimit'} ) } ).')'; + push @limits, $multibranch if ($multibranch ne '()'); } my $available; @@ -452,7 +462,7 @@ my $indexes2z3950 = { }; for (my $ii = 0; $ii < @operands; ++$ii) { - my $name = $indexes2z3950->{$indexes[$ii]}; + my $name = $indexes2z3950->{$indexes[$ii] || 'kw'}; if (defined $name && defined $operands[$ii]) { $z3950par ||= {}; @@ -472,13 +482,10 @@ my $hits; my $expanded_facet = $params->{'expand'}; # Define some global variables -my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type); - -my @results; +my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type); ## I. BUILD THE QUERY -my $lang = C4::Templates::getlanguage($cgi, 'intranet'); -( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$stopwords_removed,$query_type) = buildQuery(\@operators,\@operands,\@indexes,\@limits,\@sort_by,$scan,$lang); +( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) = buildQuery(\@operators,\@operands,\@indexes,\@limits,\@sort_by,$scan,$lang); ## parse the query_cgi string and put it into a form suitable for s my @query_inputs; @@ -489,7 +496,7 @@ for my $this_cgi ( split('&',$query_cgi) ) { $this_cgi =~ m/(.*?)=(.*)/; my $input_name = $1; my $input_value = $2; - push @query_inputs, { input_name => $input_name, input_value => $input_value }; + push @query_inputs, { input_name => $input_name, input_value => Encode::decode_utf8( uri_unescape( $input_value ) ) }; if ($input_name eq 'idx') { $scan_index_to_use = $input_value; # unless $scan_index_to_use; } @@ -510,32 +517,23 @@ for my $this_cgi ( split('&',$limit_cgi) ) { my $input_name = $1; my $input_value = $2; $input_name =~ s/=$//; - push @limit_inputs, { input_name => $input_name, input_value => $input_value }; + push @limit_inputs, { input_name => $input_name, input_value => Encode::decode_utf8( uri_unescape($input_value) ) }; } $template->param ( LIMIT_INPUTS => \@limit_inputs ); ## II. DO THE SEARCH AND GET THE RESULTS my $total; # the total results for the whole set my $facets; # this object stores the faceted results that display on the left-hand of the results page -my @results_array; my $results_hashref; -if (C4::Context->preference('NoZebra')) { - $query=~s/yr(:|=)\s*([\d]{1,4})-([\d]{1,4})/(yr>=$2 and yr<=$3)/g; - $simple_query=~s/yr\s*(:|=)([\d]{1,4})-([\d]{1,4})/(yr>=$2 and yr<=$3)/g; - # warn $query; - eval { - ($error, $results_hashref, $facets) = NZgetRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan); - }; -} else { - eval { - ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan); - }; -} +eval { + ($error, $results_hashref, $facets) = getRecords($query,$simple_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$itemtypes,$query_type,$scan); +}; + # This sorts the facets into alphabetical order if ($facets) { foreach my $f (@$facets) { - $f->{facets} = [ sort { uc($a->{facet_title_value}) cmp uc($b->{facet_title_value}) } @{ $f->{facets} } ]; + $f->{facets} = [ sort { uc($a->{facet_label_value}) cmp uc($b->{facet_label_value}) } @{ $f->{facets} } ]; } } if ($@ || $error) { @@ -555,6 +553,30 @@ for (my $i=0;$i<@servers;$i++) { my @newresults = searchResults('intranet', $query_desc, $hits, $results_per_page, $offset, $scan, $results_hashref->{$server}->{"RECORDS"}); $total = $total + $results_hashref->{$server}->{"hits"}; + + # Search history + if (C4::Context->preference('EnableSearchHistory')) { + unless ( $offset ) { + my $path_info = $cgi->url(-path_info=>1); + my $query_cgi_history = $cgi->url(-query=>1); + $query_cgi_history =~ s/^$path_info\?//; + $query_cgi_history =~ s/;/&/g; + my $query_desc_history = $query_desc; + $query_desc_history .= ", $limit_desc" + if $limit_desc; + + C4::Search::History::add({ + userid => $borrowernumber, + sessionid => $cgi->cookie("CGISESSID"), + query_desc => $query_desc_history, + query_cgi => $query_cgi_history, + total => $total, + type => "biblio", + }); + } + $template->param( EnableSearchHistory => 1 ); + } + ## If there's just one result, redirect to the detail page if ($total == 1) { my $biblionumber = $newresults[0]->{biblionumber}; @@ -572,6 +594,9 @@ for (my $i=0;$i<@servers;$i++) { exit; } + # set up parameters if user wishes to re-run the search + # as a Z39.50 search + $template->param (z3950_search_params => C4::Search::z3950_search_args($z3950par || $query_desc)); if ($hits) { $template->param(total => $hits); @@ -584,11 +609,9 @@ for (my $i=0;$i<@servers;$i++) { $template->param(limit_desc => $limit_desc); $template->param(offset => $offset); $template->param(DisplayMultiPlaceHold => $DisplayMultiPlaceHold); - $template->param (z3950_search_params => C4::Search::z3950_search_args($query_desc)); if ($query_desc || $limit_desc) { $template->param(searchdesc => 1); } - $template->param(stopwords_removed => "@$stopwords_removed") if $stopwords_removed; $template->param(results_per_page => $results_per_page); # must define a value for size if not present in DB # in order to avoid problems generated by the default size value in TT @@ -659,7 +682,6 @@ for (my $i=0;$i<@servers;$i++) { # no hits else { $template->param(searchdesc => 1,query_desc => $query_desc,limit_desc => $limit_desc); - $template->param (z3950_search_params => C4::Search::z3950_search_args($z3950par || $query_desc)); } } # end of the if local @@ -668,7 +690,10 @@ for (my $i=0;$i<@servers;$i++) { elsif ($server =~/authorityserver/) { # this is the local authority server my @inner_sup_results_array; for my $sup_record ( @{$results_hashref->{$server}->{"RECORDS"}} ) { - my $marc_record_object = MARC::Record->new_from_usmarc($sup_record); + my $marc_record_object = C4::Search::new_record_from_zebra( + 'authorityserver', + $sup_record + ); # warn "Authority Found: ".$marc_record_object->as_formatted(); push @inner_sup_results_array, { 'title' => $marc_record_object->field(100)->subfield('a'), @@ -683,6 +708,24 @@ for (my $i=0;$i<@servers;$i++) { } #/end of the for loop #$template->param(FEDERATED_RESULTS => \@results_array); +$template->{'VARS'}->{'searchid'} = $cgi->param('searchid'); + +my $gotonumber = $cgi->param('gotoNumber'); +if ($gotonumber eq 'last' || $gotonumber eq 'first') { + $template->{'VARS'}->{'gotoNumber'} = $gotonumber; +} +$template->{'VARS'}->{'gotoPage'} = 'detail.pl'; +my $gotopage = $cgi->param('gotoPage'); +$template->{'VARS'}->{'gotoPage'} = $gotopage + if $gotopage =~ m/^(ISBD|labeledMARC|MARC|more)?detail.pl$/; + +for my $facet ( @$facets ) { + for my $entry ( @{ $facet->{facets} } ) { + my $index = $entry->{type_link_value}; + my $value = $entry->{facet_link_value}; + $entry->{active} = grep { $_->{input_value} eq qq{$index:$value} } @limit_inputs; + } +} $template->param( #classlist => $classlist, @@ -700,22 +743,25 @@ if ($query_desc || $limit_desc) { # VI. BUILD THE TEMPLATE -# Build drop-down list for 'Add To:' menu... - -my $row_count = 10; # FIXME:This probably should be a syspref -my ($pubshelves, $total) = GetRecentShelves(2, $row_count, undef); -my ($barshelves, $total) = GetRecentShelves(1, $row_count, $borrowernumber); - -if (@{$pubshelves}) { - $template->param( addpubshelves => scalar @{$pubshelves}); - $template->param( addpubshelvesloop => $pubshelves); -} - -if (@{$barshelves}) { - $template->param( addbarshelves => scalar @{$barshelves}); - $template->param( addbarshelvesloop => $barshelves); -} +my $some_private_shelves = Koha::Virtualshelves->get_some_shelves( + { + borrowernumber => $borrowernumber, + add_allowed => 1, + category => 1, + } +); +my $some_public_shelves = Koha::Virtualshelves->get_some_shelves( + { + borrowernumber => $borrowernumber, + add_allowed => 1, + category => 2, + } +); +$template->param( + add_to_some_private_shelves => $some_private_shelves, + add_to_some_public_shelves => $some_public_shelves, +); output_html_with_http_headers $cgi, $cookie, $template->output;