Bug 14544: Get rid of C4::VirtualShelves and C4::VirtualShelves::Page
[koha.git] / catalogue / search.pl
index 55d0abf..e74ceca 100755 (executable)
@@ -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 <http://www.gnu.org/licenses>.
 
 =head1 NAME
 
@@ -147,33 +147,35 @@ use C4::Search;
 use C4::Languages qw(getLanguages);
 use C4::Koha;
 use C4::Members qw(GetMember);
-use C4::VirtualShelves;
 use URI::Escape;
 use POSIX qw(ceil floor);
-use String::Random;
 use C4::Branch; # GetBranches
+use C4::Search::History;
+
+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);
-my $lang = C4::Templates::getlanguage($cgi, 'intranet');
 # 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",
@@ -181,6 +183,9 @@ else {
     flagsrequired   => { catalogue => 1 },
     }
 );
+
+my $lang = C4::Languages::getlanguage($cgi);
+
 if (C4::Context->preference("marcflavour") eq "UNIMARC" ) {
     $template->param('UNIMARC' => 1);
 }
@@ -253,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;
@@ -411,6 +418,9 @@ 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 = 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'}) {
     my $multibranch = '('.join( " or ", map { "branch: $_ " } @{ GetBranchesInCategory( $params->{'multibranchlimit'} ) } ).')';
@@ -488,7 +498,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;
     }
@@ -509,7 +519,7 @@ 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 );
 
@@ -526,7 +536,7 @@ eval {
 # 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) {
@@ -546,6 +556,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};
@@ -678,14 +712,7 @@ for (my $i=0;$i<@servers;$i++) {
 } #/end of the for loop
 #$template->param(FEDERATED_RESULTS => \@results_array);
 
-if ($cgi->param('searchid')) {
-       $template->{'VARS'}->{'searchid'} = $cgi->param('searchid');
-}
-else {
-  my $dt = DateTime->now(time_zone  => 'local');
- #We are generating a clean numeric datetime representation so we can easily compare them using the default javascript lexigraphic sorter.
-      $template->{'VARS'}->{'searchid'} = 'scs_'.$dt->ymd('').$dt->hms(''); #scs == Staff Client
-}
+$template->{'VARS'}->{'searchid'} = $cgi->param('searchid');
 
 my $gotonumber = $cgi->param('gotoNumber');
 if ($gotonumber eq 'last' || $gotonumber eq 'first') {
@@ -696,6 +723,14 @@ 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,
             total => $total,
@@ -712,14 +747,25 @@ if ($query_desc || $limit_desc) {
 
 # VI. BUILD THE TEMPLATE
 
-# Build drop-down list for 'Add To:' menu...
-my ($totalref, $pubshelves, $barshelves)=
-       C4::VirtualShelves::GetSomeShelfNames($borrowernumber,'COMBO',1);
+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(
-        addbarshelves     => $totalref->{bartotal},
-        addbarshelvesloop => $barshelves,
-       addpubshelves     => $totalref->{pubtotal},
-       addpubshelvesloop => $pubshelves,
-       );
+    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;