Bug 14868 - Update swagger.min.json
[koha.git] / catalogue / itemsearch.pl
index cb025a5..2e6608b 100755 (executable)
@@ -27,9 +27,9 @@ use C4::Items;
 use C4::Biblio;
 use C4::Branch;
 use C4::Koha;
-use C4::ItemType;
 
 use Koha::Item::Search::Field qw(GetItemSearchFields);
+use Koha::ItemTypes;
 
 my $cgi = new CGI;
 my %params = $cgi->Vars;
@@ -37,24 +37,24 @@ my %params = $cgi->Vars;
 my $format = $cgi->param('format');
 my ($template_name, $content_type);
 if (defined $format and $format eq 'json') {
-    $template_name = 'catalogue/itemsearch.json.tt';
+    $template_name = 'catalogue/itemsearch_json.tt';
     $content_type = 'json';
 
     # Map DataTables parameters with 'regular' parameters
     $cgi->param('rows', $cgi->param('iDisplayLength'));
     $cgi->param('page', ($cgi->param('iDisplayStart') / $cgi->param('iDisplayLength')) + 1);
-    my @columns = split /,/, $cgi->param('sColumns');
+    my @columns = split /,/, $cgi->multi_param('sColumns');
     $cgi->param('sortby', $columns[ $cgi->param('iSortCol_0') ]);
     $cgi->param('sortorder', $cgi->param('sSortDir_0'));
 
-    my @f = $cgi->param('f');
-    my @q = $cgi->param('q');
+    my @f = $cgi->multi_param('f');
+    my @q = $cgi->multi_param('q');
     push @q, '' if @q == 0;
-    my @op = $cgi->param('op');
-    my @c = $cgi->param('c');
+    my @op = $cgi->multi_param('op');
+    my @c = $cgi->multi_param('c');
     foreach my $i (0 .. ($cgi->param('iColumns') - 1)) {
         my $sSearch = $cgi->param("sSearch_$i");
-        if ($sSearch) {
+        if (defined $sSearch and $sSearch ne '') {
             my @words = split /\s+/, $sSearch;
             foreach my $word (@words) {
                 push @f, $columns[$i];
@@ -69,7 +69,7 @@ if (defined $format and $format eq 'json') {
     $cgi->param('op', @op);
     $cgi->param('c', @c);
 } elsif (defined $format and $format eq 'csv') {
-    $template_name = 'catalogue/itemsearch.csv.tt';
+    $template_name = 'catalogue/itemsearch_csv.tt';
 
     # Retrieve all results
     $cgi->param('rows', 0);
@@ -90,6 +90,9 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
 my $notforloan_avcode = GetAuthValCode('items.notforloan');
 my $notforloan_values = GetAuthorisedValues($notforloan_avcode);
 
+my $location_avcode = GetAuthValCode('items.location');
+my $location_values = GetAuthorisedValues($location_avcode);
+
 if (scalar keys %params > 0) {
     # Parameters given, it's a search
 
@@ -98,8 +101,8 @@ if (scalar keys %params > 0) {
         filters => [],
     };
 
-    foreach my $p (qw(homebranch location itype ccode issues datelastborrowed)) {
-        if (my @q = $cgi->param($p)) {
+    foreach my $p (qw(homebranch location itype ccode issues datelastborrowed notforloan)) {
+        if (my @q = $cgi->multi_param($p)) {
             if ($q[0] ne '') {
                 my $f = {
                     field => $p,
@@ -113,10 +116,10 @@ if (scalar keys %params > 0) {
         }
     }
 
-    my @c = $cgi->param('c');
-    my @fields = $cgi->param('f');
-    my @q = $cgi->param('q');
-    my @op = $cgi->param('op');
+    my @c = $cgi->multi_param('c');
+    my @fields = $cgi->multi_param('f');
+    my @q = $cgi->multi_param('q');
+    my @op = $cgi->multi_param('op');
 
     my $f;
     for (my $i = 0; $i < @fields; $i++) {
@@ -125,6 +128,9 @@ if (scalar keys %params > 0) {
         my $op = shift @op;
         if (defined $q and $q ne '') {
             if ($i == 0) {
+                if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
+                    $field = 'copyrightdate';
+                }
                 $f = {
                     field => $field,
                     query => $q,
@@ -178,11 +184,15 @@ if (scalar keys %params > 0) {
         };
     }
 
+    my $sortby = $cgi->param('sortby') || 'itemnumber';
+    if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
+        $sortby = 'copyrightdate';
+    }
     my $search_params = {
-        rows => $cgi->param('rows') // 20,
-        page => $cgi->param('page') || 1,
-        sortby => $cgi->param('sortby') || 'itemnumber',
-        sortorder => $cgi->param('sortorder') || 'asc',
+        rows => scalar $cgi->param('rows') // 20,
+        page => scalar $cgi->param('page') || 1,
+        sortby => $sortby,
+        sortorder => scalar $cgi->param('sortorder') || 'asc',
     };
 
     my ($results, $total_rows) = SearchItems($filter, $search_params);
@@ -193,10 +203,19 @@ if (scalar keys %params > 0) {
             $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
         }
 
+        # Get location labels
+        my $location_map = {};
+        foreach my $loc_value (@$location_values) {
+            $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
+        }
+
         foreach my $item (@$results) {
             $item->{biblio} = GetBiblio($item->{biblionumber});
             ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
             $item->{status} = $notforloan_map->{$item->{notforloan}};
+            if (defined $item->{location}) {
+                $item->{location} = $location_map->{$item->{location}};
+            }
         }
     }
 
@@ -210,10 +229,10 @@ if (scalar keys %params > 0) {
 
     if ($format eq 'html') {
         # Build pagination bar
-        my $url = $cgi->url(-absolute => 1);
+        my $url = '/cgi-bin/koha/catalogue/itemsearch.pl';
         my @params;
         foreach my $p (keys %params) {
-            my @v = $cgi->param($p);
+            my @v = $cgi->multi_param($p);
             push @params, map { "$p=" . $_ } @v;
         }
         $url .= '?' . join ('&', @params);
@@ -230,24 +249,25 @@ if ($format eq 'html') {
 
     my $branches = GetBranches();
     my @branches;
-    foreach my $branchcode (keys %$branches) {
+    foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches) {
         push @branches, {
             value => $branchcode,
             label => $branches->{$branchcode}->{branchname},
         };
     }
-    my $locations = GetAuthorisedValues('LOC');
     my @locations;
-    foreach my $location (@$locations) {
+    foreach my $location (@$location_values) {
         push @locations, {
             value => $location->{authorised_value},
-            label => $location->{lib},
+            label => $location->{lib} // $location->{authorised_value},
         };
     }
-    my @itemtypes = C4::ItemType->all();
-    foreach my $itemtype (@itemtypes) {
-        $itemtype->{value} = $itemtype->{itemtype};
-        $itemtype->{label} = $itemtype->{description};
+    my @itemtypes;
+    foreach my $itemtype ( Koha::ItemTypes->search ) {
+        push @itemtypes, {
+            value => $itemtype->itemtype,
+            label => $itemtype->translated_description,
+        };
     }
     my $ccode_avcode = GetAuthValCode('items.ccode') || 'CCODE';
     my $ccodes = GetAuthorisedValues($ccode_avcode);
@@ -292,7 +312,10 @@ if ($format eq 'csv') {
         type => 'text/csv',
         attachment => 'items.csv',
     });
-    print $template->output;
+
+    for my $line ( split '\n', $template->output ) {
+        print "$line\n" unless $line =~ m|^\s*$|;
+    }
 } else {
     output_with_http_headers $cgi, $cookie, $template->output, $content_type;
 }