Bug 15352: (RM follow-up) Add missing filters
[koha.git] / C4 / ShelfBrowser.pm
blob53fdaef4b3cb79aac49fbae22fd5db2d8dd4b3c6
1 #!/usr/bin/perl
3 package C4::ShelfBrowser;
5 # Copyright 2010 Catalyst IT
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use strict;
23 use warnings;
25 use C4::Biblio;
26 use C4::Context;
27 use C4::Koha;
28 use Koha::Biblios;
29 use Koha::Libraries;
31 use vars qw(@ISA @EXPORT @EXPORT_OK);
33 BEGIN {
34 require Exporter;
35 @ISA = qw(Exporter);
36 @EXPORT = qw(
37 &GetNearbyItems
39 @EXPORT_OK = qw(
43 =head1 NAME
45 C4::ShelfBrowser - functions that deal with the shelf browser feature found in
46 the OPAC.
48 =head1 SYNOPSIS
50 use C4::ShelfBrowser;
52 =head1 DESCRIPTION
54 This module provides functions to get items nearby to another item, for use
55 in the shelf browser function.
57 'Nearby' is controlled by a handful of system preferences that specify what
58 to take into account.
60 =head1 FUNCTIONS
62 =head2 GetNearbyItems($itemnumber, [$num_each_side])
64 $nearby = GetNearbyItems($itemnumber, [$num_each_side]);
66 @items = @{ $nearby->{items} };
68 foreach (@items) {
69 # These won't format well like this, but here are the fields
70 print $_->{title};
71 print $_->{biblionumber};
72 print $_->{itemnumber};
73 print $_->{browser_normalized_upc};
74 print $_->{browser_normalized_oclc};
75 print $_->{browser_normalized_isbn};
76 print $_->{browser_normalized_ean};
79 # This is the information required to scroll the browser to the next left
80 # or right set. Can be derived from next/prev, but it's here for convenience.
81 print $nearby->{prev_item}{itemnumber};
82 print $nearby->{next_item}{itemnumber};
83 print $nearby->{prev_item}{biblionumber};
84 print $nearby->{next_item}{biblionumber};
86 # These will be undef if the values are not used to calculate the
87 # nearby items.
88 print $nearby->{starting_homebranch}->{code};
89 print $nearby->{starting_homebranch}->{description};
90 print $nearby->{starting_location}->{code};
91 print $nearby->{starting_location}->{description};
92 print $nearby->{starting_ccode}->{code};
93 print $nearby->{starting_ccode}->{description};
95 This finds the items that are nearby to the supplied item, and supplies
96 those previous and next, along with the other useful information for displaying
97 the shelf browser.
99 It automatically applies the following user preferences to work out how to
100 calculate things: C<ShelfBrowserUsesLocation>, C<ShelfBrowserUsesHomeBranch>,
101 C<ShelfBrowserUsesCcode>.
103 The option C<$num_each_side> value determines how many items will be fetched
104 each side of the supplied item. Note that the item itself is the first entry
105 in the 'next' set, and counts towards this limit (this is to keep the
106 behaviour consistent with the code that this is a refactor of.) Default is
109 This will throw an exception if something went wrong.
111 =cut
113 sub GetNearbyItems {
114 my ( $itemnumber, $num_each_side, $gap) = @_;
115 $num_each_side ||= 3;
116 $gap ||= 7; # Should be > $num_each_side
117 die "BAD CALL in C4::ShelfBrowser::GetNearbyItems, gap should be > num_each_side"
118 if $gap <= $num_each_side;
120 my $dbh = C4::Context->dbh;
122 my $sth_get_item_details = $dbh->prepare("SELECT cn_sort,homebranch,location,ccode from items where itemnumber=?");
123 $sth_get_item_details->execute($itemnumber);
124 my $item_details_result = $sth_get_item_details->fetchrow_hashref();
125 die "Unable to find item '$itemnumber' for shelf browser" if (!$sth_get_item_details);
126 my $start_cn_sort = $item_details_result->{'cn_sort'};
128 my ($start_homebranch, $start_location, $start_ccode);
129 if (C4::Context->preference('ShelfBrowserUsesHomeBranch') &&
130 defined($item_details_result->{'homebranch'})) {
131 $start_homebranch->{code} = $item_details_result->{'homebranch'};
132 $start_homebranch->{description} = Koha::Libraries->find($item_details_result->{'homebranch'})->branchname;
134 if (C4::Context->preference('ShelfBrowserUsesLocation') &&
135 defined($item_details_result->{'location'})) {
136 $start_location->{code} = $item_details_result->{'location'};
137 $start_location->{description} = GetAuthorisedValueDesc('','',$item_details_result->{'location'},'','','LOC','opac');
139 if (C4::Context->preference('ShelfBrowserUsesCcode') &&
140 defined($item_details_result->{'ccode'})) {
141 $start_ccode->{code} = $item_details_result->{'ccode'};
142 $start_ccode->{description} = GetAuthorisedValueDesc('', '', $item_details_result->{'ccode'}, '', '', 'CCODE', 'opac');
145 # Build the query for previous and next items
146 my $prev_query ='
147 SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
148 FROM items
149 WHERE
150 ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) ';
151 my $next_query ='
152 SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
153 FROM items
154 WHERE
155 ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) ';
156 my @params;
157 my $query_cond;
158 push @params, ($start_cn_sort, $itemnumber, $start_cn_sort);
159 if ($start_homebranch) {
160 $query_cond .= 'AND homebranch = ? ';
161 push @params, $start_homebranch->{code};
163 if ($start_location) {
164 $query_cond .= 'AND location = ? ';
165 push @params, $start_location->{code};
167 if ($start_ccode) {
168 $query_cond .= 'AND ccode = ? ';
169 push @params, $start_ccode->{code};
172 my @prev_items = @{
173 $dbh->selectall_arrayref(
174 $prev_query . $query_cond . ' ORDER BY cn_sort DESC, itemnumber DESC LIMIT ?',
175 { Slice => {} },
176 ( @params, $gap )
179 my @next_items = @{
180 $dbh->selectall_arrayref(
181 $next_query . $query_cond . ' ORDER BY cn_sort, itemnumber LIMIT ?',
182 { Slice => {} },
183 ( @params, $gap + 1 )
187 my $prev_item = $prev_items[-1];
188 my $next_item = $next_items[-1];
189 @next_items = splice( @next_items, 0, $num_each_side + 1 );
190 @prev_items = reverse splice( @prev_items, 0, $num_each_side );
191 my @items = ( @prev_items, @next_items );
193 $next_item = undef
194 if not $next_item
195 or ( $next_item->{itemnumber} == $items[-1]->{itemnumber}
196 and ( @prev_items or @next_items <= 1 )
198 $prev_item = undef
199 if not $prev_item
200 or ( $prev_item->{itemnumber} == $items[0]->{itemnumber}
201 and ( @next_items or @prev_items <= 1 )
204 # populate the items
205 @items = GetShelfInfo( @items );
207 return {
208 items => \@items,
209 next_item => $next_item,
210 prev_item => $prev_item,
211 starting_homebranch => $start_homebranch,
212 starting_location => $start_location,
213 starting_ccode => $start_ccode,
217 # populate an item list with its title and upc, oclc and isbn normalized.
218 # Not really intended to be exported.
219 sub GetShelfInfo {
220 my @items = @_;
221 my $marcflavour = C4::Context->preference("marcflavour");
222 my @valid_items;
223 for my $item ( @items ) {
224 my $biblio = Koha::Biblios->find( $item->{biblionumber} );
225 next unless defined $biblio;
227 $item->{biblio_object} = $biblio;
228 $item->{biblionumber} = $biblio->biblionumber;
229 $item->{title} = $biblio->title;
230 $item->{subtitle} = $biblio->subtitle;
231 $item->{medium} = $biblio->medium;
232 $item->{part_number} = $biblio->part_number;
233 $item->{part_name} = $biblio->part_name;
234 my $this_record = GetMarcBiblio({ biblionumber => $biblio->biblionumber });
235 $item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
236 $item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
237 $item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
238 $item->{'browser_normalized_ean'} = GetNormalizedEAN($this_record,$marcflavour);
239 push @valid_items, $item;
241 return @valid_items;