use C4::Koha;
use Koha::AuthorisedValues;
+use Koha::Biblios;
use Koha::Item::Search::Field qw(GetItemSearchFields);
use Koha::ItemTypes;
use Koha::Libraries;
my %params = $cgi->Vars;
my $format = $cgi->param('format');
-my ($template_name, $content_type);
+my $template_name = 'catalogue/itemsearch.tt';
+
if (defined $format and $format eq 'json') {
$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->multi_param('sColumns');
- $cgi->param('sortby', $columns[ $cgi->param('iSortCol_0') ]);
- $cgi->param('sortorder', $cgi->param('sSortDir_0'));
+ $cgi->param('rows', scalar $cgi->param('iDisplayLength'));
+ $cgi->param('page', (scalar $cgi->param('iDisplayStart') / scalar $cgi->param('iDisplayLength')) + 1);
+ my @columns = split /,/, scalar $cgi->param('sColumns');
+ $cgi->param('sortby', $columns[ scalar $cgi->param('iSortCol_0') ]);
+ $cgi->param('sortorder', scalar $cgi->param('sSortDir_0'));
my @f = $cgi->multi_param('f');
my @q = $cgi->multi_param('q');
push @q, '' if @q == 0;
my @op = $cgi->multi_param('op');
my @c = $cgi->multi_param('c');
- foreach my $i (0 .. ($cgi->param('iColumns') - 1)) {
+ my $iColumns = $cgi->param('iColumns');
+ foreach my $i (0 .. ($iColumns - 1)) {
my $sSearch = $cgi->param("sSearch_$i");
if (defined $sSearch and $sSearch ne '') {
my @words = split /\s+/, $sSearch;
foreach my $word (@words) {
push @f, $columns[$i];
- push @q, "%$word%";
- push @op, 'like';
push @c, 'and';
+
+ if ( grep /^$columns[$i]$/, qw( ccode homebranch holdingbranch location notforloan ) ) {
+ push @q, "$word";
+ push @op, '=';
+ } else {
+ push @q, "%$word%";
+ push @op, 'like';
+ }
}
}
}
# Retrieve all results
$cgi->param('rows', 0);
-} else {
- $format = 'html';
- $template_name = 'catalogue/itemsearch.tt';
- $content_type = 'html';
+} elsif (defined $format and $format eq 'barcodes') {
+ # Retrieve all results
+ $cgi->param('rows', 0);
+} elsif (defined $format) {
+ die "Unsupported format $format";
}
my ($template, $borrowernumber, $cookie) = get_template_and_user({
flagsrequired => { catalogue => 1 },
});
-my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan' });
+my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
my $notforloan_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
-$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location' });
+$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.location', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
my $location_values = $mss->count ? GetAuthorisedValues($mss->next->authorised_value) : [];
if (scalar keys %params > 0) {
field => $p,
query => \@q,
};
- if (my $op = $cgi->param($p . '_op')) {
+ if (my $op = scalar $cgi->param($p . '_op')) {
$f->{operator} = $op;
}
push @{ $filter->{filters} }, $f;
}
}
- if (my $itemcallnumber_from = $cgi->param('itemcallnumber_from')) {
+ if (my $itemcallnumber_from = scalar $cgi->param('itemcallnumber_from')) {
push @{ $filter->{filters} }, {
field => 'itemcallnumber',
query => $itemcallnumber_from,
operator => '>=',
};
}
- if (my $itemcallnumber_to = $cgi->param('itemcallnumber_to')) {
+ if (my $itemcallnumber_to = scalar $cgi->param('itemcallnumber_to')) {
push @{ $filter->{filters} }, {
field => 'itemcallnumber',
query => $itemcallnumber_to,
};
my ($results, $total_rows) = SearchItems($filter, $search_params);
+
+ if ($format eq 'barcodes') {
+ print $cgi->header({
+ type => 'text/plain',
+ attachment => 'barcodes.txt',
+ });
+
+ foreach my $item (@$results) {
+ print $item->{barcode} . "\n";
+ }
+ exit;
+ }
+
if ($results) {
# Get notforloan labels
my $notforloan_map = {};
}
foreach my $item (@$results) {
- $item->{biblio} = GetBiblio($item->{biblionumber});
- ($item->{biblioitem}) = GetBiblioItemByBiblioNumber($item->{biblionumber});
+ my $biblio = Koha::Biblios->find( $item->{biblionumber} );
+ $item->{biblio} = $biblio;
+ $item->{biblioitem} = $biblio->biblioitem->unblessed;
$item->{status} = $notforloan_map->{$item->{notforloan}};
if (defined $item->{location}) {
$item->{location} = $location_map->{$item->{location}};
search_params => $search_params,
results => $results,
total_rows => $total_rows,
- search_done => 1,
);
- if ($format eq 'html') {
- # Build pagination bar
- my $url = '/cgi-bin/koha/catalogue/itemsearch.pl';
- my @params;
- foreach my $p (keys %params) {
- my @v = $cgi->multi_param($p);
- push @params, map { "$p=" . $_ } @v;
- }
- $url .= '?' . join ('&', @params);
- my $nb_pages = 1 + int($total_rows / $search_params->{rows});
- my $current_page = $search_params->{page};
- my $pagination_bar = pagination_bar($url, $nb_pages, $current_page, 'page');
-
- $template->param(pagination_bar => $pagination_bar);
- }
-}
+ if ($format eq 'csv') {
+ print $cgi->header({
+ type => 'text/csv',
+ attachment => 'items.csv',
+ });
-if ($format eq 'html') {
- # Retrieve data required for the form.
-
- my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
- my @locations;
- foreach my $location (@$location_values) {
- push @locations, {
- value => $location->{authorised_value},
- label => $location->{lib} // $location->{authorised_value},
- };
- }
- my @itemtypes;
- foreach my $itemtype ( Koha::ItemTypes->search ) {
- push @itemtypes, {
- value => $itemtype->itemtype,
- label => $itemtype->translated_description,
- };
+ for my $line ( split '\n', $template->output ) {
+ print "$line\n" unless $line =~ m|^\s*$|;
+ }
+ } elsif ($format eq 'json') {
+ $template->param(sEcho => scalar $cgi->param('sEcho'));
+ output_with_http_headers $cgi, $cookie, $template->output, 'json';
}
- my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode' });
- my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
- my $ccodes = GetAuthorisedValues($ccode_avcode);
- my @ccodes;
- foreach my $ccode (@$ccodes) {
- push @ccodes, {
- value => $ccode->{authorised_value},
- label => $ccode->{lib},
- };
- }
+ exit;
+}
- my @notforloans;
- foreach my $value (@$notforloan_values) {
- push @notforloans, {
- value => $value->{authorised_value},
- label => $value->{lib},
- };
- }
+# Display the search form
- my @items_search_fields = GetItemSearchFields();
+my @branches = map { value => $_->branchcode, label => $_->branchname }, Koha::Libraries->search( {}, { order_by => 'branchname' } );
+my @locations;
+foreach my $location (@$location_values) {
+ push @locations, {
+ value => $location->{authorised_value},
+ label => $location->{lib} // $location->{authorised_value},
+ };
+}
+my @itemtypes;
+foreach my $itemtype ( Koha::ItemTypes->search ) {
+ push @itemtypes, {
+ value => $itemtype->itemtype,
+ label => $itemtype->translated_description,
+ };
+}
- my $authorised_values = {};
- foreach my $field (@items_search_fields) {
- if (my $category = ($field->{authorised_values_category})) {
- $authorised_values->{$category} = GetAuthorisedValues($category);
- }
- }
+$mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.ccode', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
+my $ccode_avcode = $mss->count ? $mss->next->authorised_value : 'CCODE';
+my $ccodes = GetAuthorisedValues($ccode_avcode);
+my @ccodes;
+foreach my $ccode (@$ccodes) {
+ push @ccodes, {
+ value => $ccode->{authorised_value},
+ label => $ccode->{lib},
+ };
+}
- $template->param(
- branches => \@branches,
- locations => \@locations,
- itemtypes => \@itemtypes,
- ccodes => \@ccodes,
- notforloans => \@notforloans,
- items_search_fields => \@items_search_fields,
- authorised_values_json => to_json($authorised_values),
- );
+my @notforloans;
+foreach my $value (@$notforloan_values) {
+ push @notforloans, {
+ value => $value->{authorised_value},
+ label => $value->{lib},
+ };
}
-if ($format eq 'csv') {
- print $cgi->header({
- type => 'text/csv',
- attachment => 'items.csv',
- });
+my @items_search_fields = GetItemSearchFields();
- for my $line ( split '\n', $template->output ) {
- print "$line\n" unless $line =~ m|^\s*$|;
+my $authorised_values = {};
+foreach my $field (@items_search_fields) {
+ if (my $category = ($field->{authorised_values_category})) {
+ $authorised_values->{$category} = GetAuthorisedValues($category);
}
-} else {
- output_with_http_headers $cgi, $cookie, $template->output, $content_type;
}
+
+$template->param(
+ branches => \@branches,
+ locations => \@locations,
+ itemtypes => \@itemtypes,
+ ccodes => \@ccodes,
+ notforloans => \@notforloans,
+ items_search_fields => \@items_search_fields,
+ authorised_values_json => to_json($authorised_values),
+);
+
+output_html_with_http_headers $cgi, $cookie, $template->output;