Bug 30966: Record Overlay Rules - can't use Z39.50 filter
[koha-ffzg.git] / catalogue / itemsearch.pl
index 7acde61..72074a8 100755 (executable)
@@ -3,29 +3,28 @@
 #
 # 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 3 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>.
 
 use Modern::Perl;
 use CGI;
 
-use JSON;
+use JSON qw( to_json );
 
-use C4::Auth;
-use C4::Output;
-use C4::Items;
-use C4::Biblio;
-use C4::Koha;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_with_http_headers output_html_with_http_headers );
+use C4::Items qw( SearchItems );
+use C4::Koha qw( GetAuthorisedValues );
 
 use Koha::AuthorisedValues;
 use Koha::Biblios;
@@ -33,7 +32,7 @@ use Koha::Item::Search::Field qw(GetItemSearchFields);
 use Koha::ItemTypes;
 use Koha::Libraries;
 
-my $cgi = new CGI;
+my $cgi = CGI->new;
 my %params = $cgi->Vars;
 
 my $format = $cgi->param('format');
@@ -63,7 +62,7 @@ if (defined $format and $format eq 'json') {
                 push @f, $columns[$i];
                 push @c, 'and';
 
-                if ( grep { $_ eq $columns[$i] } qw( ccode homebranch holdingbranch location itype notforloan itemlost ) ) {
+                if ( grep { $_ eq $columns[$i] } qw( ccode homebranch holdingbranch location itype notforloan itemlost onloan ) ) {
                     push @q, "$word";
                     push @op, '=';
                 } else {
@@ -93,7 +92,6 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
     template_name => $template_name,
     query => $cgi,
     type => 'intranet',
-    authnotrequired => 0,
     flagsrequired   => { catalogue => 1 },
 });
 
@@ -103,7 +101,11 @@ my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_v
 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
 my $withdrawn_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
 
-if (scalar keys %params > 0) {
+if ( Koha::MarcSubfieldStructures->search( { frameworkcode => '', kohafield => 'items.new_status' } )->count ) {
+    $template->param( has_new_status => 1 );
+}
+
+if ( defined $format ) {
     # Parameters given, it's a search
 
     my $filter = {
@@ -165,12 +167,15 @@ if (scalar keys %params > 0) {
     push @{ $filter->{filters} }, $f;
 
     # Yes/No parameters
-    foreach my $p (qw( damaged )) {
+    foreach my $p (qw( damaged new_status )) {
         my $v = $cgi->param($p) // '';
         my $f = {
             field => $p,
             query => 0,
         };
+        if ( $p eq 'new_status' ) {
+            $f->{ifnull} = 0;
+        }
         if ($v eq 'yes') {
             $f->{operator} = '!=';
             push @{ $filter->{filters} }, $f;
@@ -180,6 +185,21 @@ if (scalar keys %params > 0) {
         }
     }
 
+    # null/is not null parameters
+    foreach my $p (qw( onloan )) {
+        my $v = $cgi->param($p) // '';
+        my $f = {
+            field => $p,
+            operator => "is",
+        };
+        if ( $v eq 'IS NOT NULL' ) {
+            $f->{query} = "not null";
+        } elsif ( $v eq 'IS NULL' ) {
+            $f->{query} = "null";
+        }
+        push @{ $filter->{filters} }, $f unless ( $v eq "" );
+    }
+
     if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
         push @{ $filter->{filters} }, {
             field => 'itemcallnumber',
@@ -225,14 +245,17 @@ if (scalar keys %params > 0) {
             my $biblio = Koha::Biblios->find( $item->{biblionumber} );
             $item->{biblio} = $biblio;
             $item->{biblioitem} = $biblio->biblioitem->unblessed;
+            my $checkout = Koha::Checkouts->find({ itemnumber => $item->{itemnumber} });
+            $item->{checkout} = $checkout;
         }
     }
 
     $template->param(
-        filter => $filter,
+        filter        => $filter,
         search_params => $search_params,
-        results => $results,
-        total_rows => $total_rows,
+        results       => $results,
+        total_rows    => $total_rows,
+        user          => Koha::Patrons->find( $borrowernumber ),
     );
 
     if ($format eq 'csv') {
@@ -254,14 +277,8 @@ if (scalar keys %params > 0) {
 
 # Display the search form
 
-my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
-my @itemtypes;
-foreach my $itemtype ( Koha::ItemTypes->search ) {
-    push @itemtypes, {
-        value => $itemtype->itemtype,
-        label => $itemtype->translated_description,
-    };
-}
+my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } )->as_list;
+my @itemtypes = map { value => $_->itemtype, label => $_->translated_description }, Koha::ItemTypes->search_with_localization->as_list;
 
 my @ccodes = Koha::AuthorisedValues->get_descriptions_by_koha_field({ kohafield => 'items.ccode' });
 foreach my $ccode (@ccodes) {