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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK);
33 $VERSION = 3.07.00.049;
45 C4::ShelfBrowser - functions that deal with the shelf browser feature found in
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
62 =head2 GetNearbyItems($itemnumber, [$num_each_side])
64 $nearby = GetNearbyItems($itemnumber, [$num_each_side]);
66 @items = @{ $nearby->{items} };
69 # These won't format well like this, but here are the fields
71 print $_->{biblionumber};
72 print $_->{itemnumber};
73 print $_->{browser_normalized_upc};
74 print $_->{browser_normalized_oclc};
75 print $_->{browser_normalized_isbn};
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
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
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 consistant with the code that this is a refactor of.) Default is
108 This will throw an exception if something went wrong.
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;
120 my $branches = GetBranches
();
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
} = $branches->{$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
147 SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
150 ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) ';
152 SELECT itemnumber, biblionumber, cn_sort, itemcallnumber
155 ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) ';
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
};
168 $query_cond .= 'AND ccode = ? ';
169 push @params, $start_ccode->{code
};
173 $dbh->selectall_arrayref(
174 $prev_query . $query_cond . ' ORDER BY cn_sort DESC, itemnumber DESC LIMIT ?',
180 $dbh->selectall_arrayref(
181 $next_query . $query_cond . ' ORDER BY cn_sort, itemnumber LIMIT ?',
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 );
195 or ( $next_item->{itemnumber
} == $items[-1]->{itemnumber
}
196 and ( @prev_items or @next_items <= 1 )
200 or ( $prev_item->{itemnumber
} == $items[0]->{itemnumber
}
201 and ( @next_items or @prev_items <= 1 )
205 @items = GetShelfInfo
( @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.
221 my $marcflavour = C4
::Context
->preference("marcflavour");
223 for my $item ( @items ) {
224 my $this_biblio = GetBibData
($item->{biblionumber
});
225 next unless defined $this_biblio;
226 $item->{'title'} = $this_biblio->{'title'};
227 my $this_record = GetMarcBiblio
($this_biblio->{'biblionumber'});
228 $item->{'browser_normalized_upc'} = GetNormalizedUPC
($this_record,$marcflavour);
229 $item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber
($this_record,$marcflavour);
230 $item->{'browser_normalized_isbn'} = GetNormalizedISBN
(undef,$this_record,$marcflavour);
231 push @valid_items, $item;
236 # Fetches some basic biblio data needed by the shelf stuff
240 my $dbh = C4
::Context
->dbh;
241 my $sth = $dbh->prepare("SELECT biblionumber, title FROM biblio WHERE biblionumber=?");
242 $sth->execute($bibnum);
243 my $bib = $sth->fetchrow_hashref();