"Fossies" - the Fresh Open Source Software Archive

Member "koha-19.11.15/catalogue/itemsearch.pl" (23 Feb 2021, 11703 Bytes) of package /linux/misc/koha-19.11.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "itemsearch.pl" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 20.05.06_vs_20.11.00.

    1 #!/usr/bin/perl
    2 # Copyright 2013 BibLibre
    3 #
    4 # This file is part of Koha
    5 #
    6 # Koha is free software; you can redistribute it and/or modify it under the
    7 # terms of the GNU General Public License as published by the Free Software
    8 # Foundation; either version 3 of the License, or (at your option) any later
    9 # version.
   10 #
   11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
   12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
   14 #
   15 # You should have received a copy of the GNU General Public License along
   16 # with Koha; if not, write to the Free Software Foundation, Inc.,
   17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   18 
   19 use Modern::Perl;
   20 use CGI;
   21 
   22 use JSON;
   23 
   24 use C4::Auth;
   25 use C4::Output;
   26 use C4::Items;
   27 use C4::Biblio;
   28 use C4::Koha;
   29 
   30 use Koha::AuthorisedValues;
   31 use Koha::Biblios;
   32 use Koha::Item::Search::Field qw(GetItemSearchFields);
   33 use Koha::ItemTypes;
   34 use Koha::Libraries;
   35 
   36 my $cgi = new CGI;
   37 my %params = $cgi->Vars;
   38 
   39 my $format = $cgi->param('format');
   40 my $template_name = 'catalogue/itemsearch.tt';
   41 
   42 if (defined $format and $format eq 'json') {
   43     $template_name = 'catalogue/itemsearch_json.tt';
   44 
   45     # Map DataTables parameters with 'regular' parameters
   46     $cgi->param('rows', scalar $cgi->param('iDisplayLength'));
   47     $cgi->param('page', (scalar $cgi->param('iDisplayStart') / scalar $cgi->param('iDisplayLength')) + 1);
   48     my @columns = split /,/, scalar $cgi->param('sColumns');
   49     $cgi->param('sortby', $columns[ scalar $cgi->param('iSortCol_0') ]);
   50     $cgi->param('sortorder', scalar $cgi->param('sSortDir_0'));
   51 
   52     my @f = $cgi->multi_param('f');
   53     my @q = $cgi->multi_param('q');
   54     push @q, '' if @q == 0;
   55     my @op = $cgi->multi_param('op');
   56     my @c = $cgi->multi_param('c');
   57     my $iColumns = $cgi->param('iColumns');
   58     foreach my $i (0 .. ($iColumns - 1)) {
   59         my $sSearch = $cgi->param("sSearch_$i");
   60         if (defined $sSearch and $sSearch ne '') {
   61             my @words = split /\s+/, $sSearch;
   62             foreach my $word (@words) {
   63                 push @f, $columns[$i];
   64                 push @c, 'and';
   65 
   66                 if ( grep /^$columns[$i]$/, qw( ccode homebranch holdingbranch location itype notforloan itemlost ) ) {
   67                     push @q, "$word";
   68                     push @op, '=';
   69                 } else {
   70                     push @q, "%$word%";
   71                     push @op, 'like';
   72                 }
   73             }
   74         }
   75     }
   76     $cgi->param('f', @f);
   77     $cgi->param('q', @q);
   78     $cgi->param('op', @op);
   79     $cgi->param('c', @c);
   80 } elsif (defined $format and $format eq 'csv') {
   81     $template_name = 'catalogue/itemsearch_csv.tt';
   82 
   83     # Retrieve all results
   84     $cgi->param('rows', 0);
   85 } elsif (defined $format and $format eq 'barcodes') {
   86     # Retrieve all results
   87     $cgi->param('rows', 0);
   88 } elsif (defined $format) {
   89     die "Unsupported format $format";
   90 }
   91 
   92 my ($template, $borrowernumber, $cookie) = get_template_and_user({
   93     template_name => $template_name,
   94     query => $cgi,
   95     type => 'intranet',
   96     flagsrequired   => { catalogue => 1 },
   97 });
   98 
   99 my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
  100 my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
  101 
  102 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
  103 my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
  104 
  105 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.itemlost', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
  106 my $itemlost_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
  107 
  108 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.withdrawn', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
  109 my $withdrawn_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
  110 
  111 if (scalar keys %params > 0) {
  112     # Parameters given, it's a search
  113 
  114     my $filter = {
  115         conjunction => 'AND',
  116         filters => [],
  117     };
  118 
  119     foreach my $p (qw(homebranch holdingbranch location itype ccode issues datelastborrowed notforloan itemlost withdrawn)) {
  120         if (my @q = $cgi->multi_param($p)) {
  121             if ($q[0] ne '') {
  122                 my $f = {
  123                     field => $p,
  124                     query => \@q,
  125                 };
  126                 if (my $op = scalar $cgi->param($p . '_op')) {
  127                     $f->{operator} = $op;
  128                 }
  129                 push @{ $filter->{filters} }, $f;
  130             }
  131         }
  132     }
  133 
  134     my @c = $cgi->multi_param('c');
  135     my @fields = $cgi->multi_param('f');
  136     my @q = $cgi->multi_param('q');
  137     my @op = $cgi->multi_param('op');
  138 
  139     my $f;
  140     for (my $i = 0; $i < @fields; $i++) {
  141         my $field = $fields[$i];
  142         my $q = shift @q;
  143         my $op = shift @op;
  144         if (defined $q and $q ne '') {
  145             if (C4::Context->preference("marcflavour") ne "UNIMARC" && $field eq 'publicationyear') {
  146                 $field = 'copyrightdate';
  147             }
  148 
  149             if ($i == 0) {
  150                 $f = {
  151                     field => $field,
  152                     query => $q,
  153                     operator => $op,
  154                 };
  155             } else {
  156                 my $c = shift @c;
  157                 $f = {
  158                     conjunction => $c,
  159                     filters => [
  160                         $f, {
  161                             field => $field,
  162                             query => $q,
  163                             operator => $op,
  164                         }
  165                     ],
  166                 };
  167             }
  168         }
  169     }
  170     push @{ $filter->{filters} }, $f;
  171 
  172     # Yes/No parameters
  173     foreach my $p (qw( damaged )) {
  174         my $v = $cgi->param($p) // '';
  175         my $f = {
  176             field => $p,
  177             query => 0,
  178         };
  179         if ($v eq 'yes') {
  180             $f->{operator} = '!=';
  181             push @{ $filter->{filters} }, $f;
  182         } elsif ($v eq 'no') {
  183             $f->{operator} = '=';
  184             push @{ $filter->{filters} }, $f;
  185         }
  186     }
  187 
  188     if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
  189         push @{ $filter->{filters} }, {
  190             field => 'itemcallnumber',
  191             query => $itemcallnumber_from,
  192             operator => '>=',
  193         };
  194     }
  195     if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
  196         push @{ $filter->{filters} }, {
  197             field => 'itemcallnumber',
  198             query => $itemcallnumber_to,
  199             operator => '<=',
  200         };
  201     }
  202 
  203     my $sortby = $cgi->param('sortby') || 'itemnumber';
  204     if (C4::Context->preference("marcflavour") ne "UNIMARC" && $sortby eq 'publicationyear') {
  205         $sortby = 'copyrightdate';
  206     }
  207     my $search_params = {
  208         rows => scalar $cgi->param('rows') // 20,
  209         page => scalar $cgi->param('page') || 1,
  210         sortby => $sortby,
  211         sortorder => scalar $cgi->param('sortorder') || 'asc',
  212     };
  213 
  214     my ($results, $total_rows) = SearchItems($filter, $search_params);
  215 
  216     if ($format eq 'barcodes') {
  217         print $cgi->header({
  218             type => 'text/plain',
  219             attachment => 'barcodes.txt',
  220         });
  221 
  222         foreach my $item (@$results) {
  223             print $item->{barcode} . "\n";
  224         }
  225         exit;
  226     }
  227 
  228     if ($results) {
  229         # Get notforloan labels
  230         my $notforloan_map = {};
  231         foreach my $nfl_value (@$notforloan_values) {
  232             $notforloan_map->{$nfl_value->{authorised_value}} = $nfl_value->{lib};
  233         }
  234 
  235         # Get location labels
  236         my $location_map = {};
  237         foreach my $loc_value (@$location_values) {
  238             $location_map->{$loc_value->{authorised_value}} = $loc_value->{lib};
  239         }
  240 
  241         # Get itemlost labels
  242         my $itemlost_map = {};
  243         foreach my $il_value (@$itemlost_values) {
  244             $itemlost_map->{$il_value->{authorised_value}} = $il_value->{lib};
  245         }
  246 
  247         # Get withdrawn labels
  248         my $withdrawn_map = {};
  249         foreach my $wd_value (@$withdrawn_values) {
  250             $withdrawn_map->{$wd_value->{authorised_value}} = $wd_value->{lib};
  251         }
  252 
  253         foreach my $item (@$results) {
  254             my $biblio = Koha::Biblios->find( $item->{biblionumber} );
  255             $item->{biblio} = $biblio;
  256             $item->{biblioitem} = $biblio->biblioitem->unblessed;
  257             $item->{status} = $notforloan_map->{$item->{notforloan}};
  258             if (defined $item->{location}) {
  259                 $item->{location} = $location_map->{$item->{location}};
  260             }
  261         }
  262     }
  263 
  264     $template->param(
  265         filter => $filter,
  266         search_params => $search_params,
  267         results => $results,
  268         total_rows => $total_rows,
  269     );
  270 
  271     if ($format eq 'csv') {
  272         print $cgi->header({
  273             type => 'text/csv',
  274             attachment => 'items.csv',
  275         });
  276 
  277         for my $line ( split '\n', $template->output ) {
  278             print "$line\n" unless $line =~ m|^\s*$|;
  279         }
  280     } elsif ($format eq 'json') {
  281         $template->param(sEcho => scalar $cgi->param('sEcho'));
  282         output_with_http_headers $cgi, $cookie, $template->output, 'json';
  283     }
  284 
  285     exit;
  286 }
  287 
  288 # Display the search form
  289 
  290 my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
  291 my @locations;
  292 foreach my $location (@$location_values) {
  293     push @locations, {
  294         value => $location->{authorised_value},
  295         label => $location->{lib} // $location->{authorised_value},
  296     };
  297 }
  298 my @itemtypes;
  299 foreach my $itemtype ( Koha::ItemTypes->search ) {
  300     push @itemtypes, {
  301         value => $itemtype->itemtype,
  302         label => $itemtype->translated_description,
  303     };
  304 }
  305 
  306 $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
  307 my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
  308 my $ccodes = GetAuthorisedValues($ccode_avcode);
  309 my @ccodes;
  310 foreach my $ccode (@$ccodes) {
  311     push @ccodes, {
  312         value => $ccode->{authorised_value},
  313         label => $ccode->{lib},
  314     };
  315 }
  316 
  317 my @notforloans;
  318 foreach my $value (@$notforloan_values) {
  319     push @notforloans, {
  320         value => $value->{authorised_value},
  321         label => $value->{lib},
  322     };
  323 }
  324 
  325 my @itemlosts;
  326 foreach my $value (@$itemlost_values) {
  327     push @itemlosts, {
  328         value => $value->{authorised_value},
  329         label => $value->{lib},
  330     };
  331 }
  332 
  333 my @withdrawns;
  334 foreach my $value (@$withdrawn_values) {
  335     push @withdrawns, {
  336         value => $value->{authorised_value},
  337         label => $value->{lib},
  338     };
  339 }
  340 
  341 my @items_search_fields = GetItemSearchFields();
  342 
  343 my $authorised_values = {};
  344 foreach my $field (@items_search_fields) {
  345     if (my $category = ($field->{authorised_values_category})) {
  346         $authorised_values->{$category} = GetAuthorisedValues($category);
  347     }
  348 }
  349 
  350 $template->param(
  351     branches => \@branches,
  352     locations => \@locations,
  353     itemtypes => \@itemtypes,
  354     ccodes => \@ccodes,
  355     notforloans => \@notforloans,
  356     itemlosts => \@itemlosts,
  357     withdrawns => \@withdrawns,
  358     items_search_fields => \@items_search_fields,
  359     authorised_values_json => to_json($authorised_values),
  360 );
  361 
  362 output_html_with_http_headers $cgi, $cookie, $template->output;