Bug 9302: Use patron-title.inc
[koha.git] / C4 / ShelfBrowser.pm
blobee551d2c83cf09e7a58619860cc332b14f62b557
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::Libraries;
30 use vars qw(@ISA @EXPORT @EXPORT_OK);
32 BEGIN {
33 require Exporter;
34 @ISA = qw(Exporter);
35 @EXPORT = qw(
36 &GetNearbyItems
38 @EXPORT_OK = qw(
42 =head1 NAME
44 C4::ShelfBrowser - functions that deal with the shelf browser feature found in
45 the OPAC.
47 =head1 SYNOPSIS
49 use C4::ShelfBrowser;
51 =head1 DESCRIPTION
53 This module provides functions to get items nearby to another item, for use
54 in the shelf browser function.
56 'Nearby' is controlled by a handful of system preferences that specify what
57 to take into account.
59 =head1 FUNCTIONS
61 =head2 GetNearbyItems($itemnumber, [$num_each_side])
63 $nearby = GetNearbyItems($itemnumber, [$num_each_side]);
65 @items = @{ $nearby->{items} };
67 foreach (@items) {
68 # These won't format well like this, but here are the fields
69 print $_->{title};
70 print $_->{biblionumber};
71 print $_->{itemnumber};
72 print $_->{browser_normalized_upc};
73 print $_->{browser_normalized_oclc};
74 print $_->{browser_normalized_isbn};
75 print $_->{browser_normalized_ean};
78 # This is the information required to scroll the browser to the next left
79 # or right set. Can be derived from next/prev, but it's here for convenience.
80 print $nearby->{prev_item}{itemnumber};
81 print $nearby->{next_item}{itemnumber};
82 print $nearby->{prev_item}{biblionumber};
83 print $nearby->{next_item}{biblionumber};
85 # These will be undef if the values are not used to calculate the
86 # nearby items.
87 print $nearby->{starting_homebranch}->{code};
88 print $nearby->{starting_homebranch}->{description};
89 print $nearby->{starting_location}->{code};
90 print $nearby->{starting_location}->{description};
91 print $nearby->{starting_ccode}->{code};
92 print $nearby->{starting_ccode}->{description};
94 This finds the items that are nearby to the supplied item, and supplies
95 those previous and next, along with the other useful information for displaying
96 the shelf browser.
98 It automatically applies the following user preferences to work out how to
99 calculate things: C<ShelfBrowserUsesLocation>, C<ShelfBrowserUsesHomeBranch>,
100 C<ShelfBrowserUsesCcode>.
102 The option C<$num_each_side> value determines how many items will be fetched
103 each side of the supplied item. Note that the item itself is the first entry
104 in the 'next' set, and counts towards this limit (this is to keep the
105 behaviour consistent with the code that this is a refactor of.) Default is
108 This will throw an exception if something went wrong.
110 =cut
112 sub GetNearbyItems {
113 my ( $itemnumber, $num_each_side, $gap) = @_;
114 $num_each_side ||= 3;
115 $gap ||= 7; # Should be > $num_each_side
116 die "BAD CALL in C4::ShelfBrowser::GetNearbyItems, gap should be > num_each_side"
117 if $gap <= $num_each_side;
119 my $dbh = C4::Context->dbh;
121 my $sth_get_item_details = $dbh->prepare("SELECT cn_sort,homebranch,location,ccode from items where itemnumber=?");
122 $sth_get_item_details->execute($itemnumber);
123 my $item_details_result = $sth_get_item_details->fetchrow_hashref();
124 die "Unable to find item '$itemnumber' for shelf browser" if (!$sth_get_item_details);
125 my $start_cn_sort = $item_details_result->{'cn_sort'};
127 my ($start_homebranch, $start_location, $start_ccode);
128 if (C4::Context->preference('ShelfBrowserUsesHomeBranch') &&
129 defined($item_details_result->{'homebranch'})) {
130 $start_homebranch->{code} = $item_details_result->{'homebranch'};
131 $start_homebranch->{description} = Koha::Libraries->find($item_details_result->{'homebranch'})->branchname;
133 if (C4::Context->preference('ShelfBrowserUsesLocation') &&
134 defined($item_details_result->{'location'})) {
135 $start_location->{code} = $item_details_result->{'location'};
136 $start_location->{description} = GetAuthorisedValueDesc('','',$item_details_result->{'location'},'','','LOC','opac');
138 if (C4::Context->preference('ShelfBrowserUsesCcode') &&
139 defined($item_details_result->{'ccode'})) {
140 $start_ccode->{code} = $item_details_result->{'ccode'};
141 $start_ccode->{description} = GetAuthorisedValueDesc('', '', $item_details_result->{'ccode'}, '', '', 'CCODE', 'opac');
144 # Build the query for previous and next items
145 my $prev_query ='
146 SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
147 FROM items
148 WHERE
149 ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) ';
150 my $next_query ='
151 SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
152 FROM items
153 WHERE
154 ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) ';
155 my @params;
156 my $query_cond;
157 push @params, ($start_cn_sort, $itemnumber, $start_cn_sort);
158 if ($start_homebranch) {
159 $query_cond .= 'AND homebranch = ? ';
160 push @params, $start_homebranch->{code};
162 if ($start_location) {
163 $query_cond .= 'AND location = ? ';
164 push @params, $start_location->{code};
166 if ($start_ccode) {
167 $query_cond .= 'AND ccode = ? ';
168 push @params, $start_ccode->{code};
171 my @prev_items = @{
172 $dbh->selectall_arrayref(
173 $prev_query . $query_cond . ' ORDER BY cn_sort DESC, itemnumber DESC LIMIT ?',
174 { Slice => {} },
175 ( @params, $gap )
178 my @next_items = @{
179 $dbh->selectall_arrayref(
180 $next_query . $query_cond . ' ORDER BY cn_sort, itemnumber LIMIT ?',
181 { Slice => {} },
182 ( @params, $gap + 1 )
186 my $prev_item = $prev_items[-1];
187 my $next_item = $next_items[-1];
188 @next_items = splice( @next_items, 0, $num_each_side + 1 );
189 @prev_items = reverse splice( @prev_items, 0, $num_each_side );
190 my @items = ( @prev_items, @next_items );
192 $next_item = undef
193 if not $next_item
194 or ( $next_item->{itemnumber} == $items[-1]->{itemnumber}
195 and ( @prev_items or @next_items <= 1 )
197 $prev_item = undef
198 if not $prev_item
199 or ( $prev_item->{itemnumber} == $items[0]->{itemnumber}
200 and ( @next_items or @prev_items <= 1 )
203 # populate the items
204 @items = GetShelfInfo( @items );
206 return {
207 items => \@items,
208 next_item => $next_item,
209 prev_item => $prev_item,
210 starting_homebranch => $start_homebranch,
211 starting_location => $start_location,
212 starting_ccode => $start_ccode,
216 # populate an item list with its title and upc, oclc and isbn normalized.
217 # Not really intended to be exported.
218 sub GetShelfInfo {
219 my @items = @_;
220 my $marcflavour = C4::Context->preference("marcflavour");
221 my @valid_items;
222 for my $item ( @items ) {
223 my $this_biblio = GetBibData($item->{biblionumber});
224 next unless defined $this_biblio;
225 $item->{'title'} = $this_biblio->{'title'};
226 my $this_record = GetMarcBiblio({ biblionumber => $this_biblio->{'biblionumber'} });
227 $item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
228 $item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
229 $item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
230 $item->{'browser_normalized_ean'} = GetNormalizedEAN($this_record,$marcflavour);
231 $item->{'subtitle'} = GetRecordValue('subtitle', $this_record, GetFrameworkCode( $item->{biblionumber} ));
232 push @valid_items, $item;
234 return @valid_items;
237 # Fetches some basic biblio data needed by the shelf stuff
238 sub GetBibData {
239 my ($bibnum) = @_;
241 my $dbh = C4::Context->dbh;
242 my $sth = $dbh->prepare("SELECT biblionumber, title FROM biblio WHERE biblionumber=?");
243 $sth->execute($bibnum);
244 my $bib = $sth->fetchrow_hashref();
245 return $bib;