File Coverage

File:C4/ShelfBrowser.pm
Coverage:23.3%

linestmtbrancondsubtimecode
1#!/usr/bin/perl
2
3package C4::ShelfBrowser;
4
5# Copyright 2010 Catalyst IT
6#
7# This file is part of Koha.
8#
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
12# version.
13#
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.
17#
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.
21
22
1
1
1
278
2
21
use strict;
23
1
1
1
5
1
31
use warnings;
24
25
1
1
1
4
29
483
use C4::Biblio;
26
1
1
1
6
1
157
use C4::Branch;
27
1
1
1
5
1
11
use C4::Context;
28
1
1
1
4
1
329
use C4::Koha;
29
30
1
1
1
4
2
125
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
31
32BEGIN {
33
1
2
        $VERSION = 3.02;
34
1
4
        require Exporter;
35
1
11
        @ISA = qw(Exporter);
36
1
2
        @EXPORT = qw(
37            &GetNearbyItems
38    );
39
1
533
    @EXPORT_OK = qw(
40    );
41}
42
43 - 113
=head1 NAME

C4::ShelfBrowser - functions that deal with the shelf browser feature found in
the OPAC.

=head1 SYNOPSIS

  use C4::ShelfBrowser;

=head1 DESCRIPTION

This module provides functions to get items nearby to another item, for use
in the shelf browser function.

'Nearby' is controlled by a handful of system preferences that specify what
to take into account.

=head1 FUNCTIONS

=head2 GetNearbyItems($itemnumber, [$num_each_side])

  $nearby = GetNearbyItems($itemnumber, [$num_each_side]);

  @next = @{ $nearby->{next} };
  @prev = @{ $nearby->{prev} };

  foreach (@next) {
      # These won't format well like this, but here are the fields
  	  print $_->{title};
  	  print $_->{biblionumber};
  	  print $_->{itemnumber};
  	  print $_->{browser_normalized_upc};
  	  print $_->{browser_normalized_oclc};
  	  print $_->{browser_normalized_isbn};
  }

  # This is the information required to scroll the browser to the next left
  # or right set. Can be derived from next/prev, but it's here for convenience.
  print $nearby->{prev_itemnumber};
  print $nearby->{next_itemnumber};
  print $nearby->{prev_biblionumber};
  print $nearby->{next_biblionumber};

  # These will be undef if the values are not used to calculate the 
  # nearby items.
  print $nearby->{starting_homebranch}->{code};
  print $nearby->{starting_homebranch}->{description};
  print $nearby->{starting_location}->{code};
  print $nearby->{starting_location}->{description};
  print $nearby->{starting_ccode}->{code};
  print $nearby->{starting_ccode}->{description};

  print $nearby->{starting_itemnumber};
  
This finds the items that are nearby to the supplied item, and supplies
those previous and next, along with the other useful information for displaying
the shelf browser.

It automatically applies the following user preferences to work out how to
calculate things: C<ShelfBrowserUsesLocation>, C<ShelfBrowserUsesHomeBranch>, 
C<ShelfBrowserUsesCcode>.

The option C<$num_each_side> value determines how many items will be fetched
each side of the supplied item. Note that the item itself is the first entry
in the 'next' set, and counts towards this limit (this is to keep the
behaviour consistant with the code that this is a refactor of.) Default is
3.

This will throw an exception if something went wrong.

=cut
114
115sub GetNearbyItems {
116
0
        my ($itemnumber, $num_each_side) = @_;
117
0
        $num_each_side ||= 3;
118
119
0
    my $dbh = C4::Context->dbh;
120
0
    my $marcflavour = C4::Context->preference("marcflavour");
121
0
    my $branches = GetBranches();
122
123
0
    my $sth_get_item_details = $dbh->prepare("SELECT cn_sort,homebranch,location,ccode from items where itemnumber=?");
124
0
    $sth_get_item_details->execute($itemnumber);
125
0
    my $item_details_result = $sth_get_item_details->fetchrow_hashref();
126
0
    die "Unable to find item '$itemnumber' for shelf browser" if (!$sth_get_item_details);
127
0
    my $start_cn_sort = $item_details_result->{'cn_sort'};
128
129
0
    my ($start_homebranch, $start_location, $start_ccode);
130
0
    if (C4::Context->preference('ShelfBrowserUsesHomeBranch') &&
131     defined($item_details_result->{'homebranch'})) {
132
0
        $start_homebranch->{code} = $item_details_result->{'homebranch'};
133
0
        $start_homebranch->{description} = $branches->{$item_details_result->{'homebranch'}}{branchname};
134    }
135
0
    if (C4::Context->preference('ShelfBrowserUsesLocation') &&
136     defined($item_details_result->{'location'})) {
137
0
        $start_location->{code} = $item_details_result->{'location'};
138
0
        $start_location->{description} = GetAuthorisedValueDesc('','',$item_details_result->{'location'},'','','LOC','opac');
139    }
140
0
    if (C4::Context->preference('ShelfBrowserUsesCcode') &&
141     defined($item_details_result->{'ccode'})) {
142
0
        $start_ccode->{code} = $item_details_result->{'ccode'};
143
0
        $start_ccode->{description} = GetAuthorisedValueDesc('', '', $item_details_result->{'ccode'}, '', '', 'CCODE', 'opac');
144    }
145
146    # Build the query for previous and next items
147
0
    my $prev_query ='
148        SELECT *
149        FROM items
150        WHERE
151            ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) ';
152
0
    my $next_query ='
153        SELECT *
154        FROM items
155        WHERE
156            ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) ';
157
0
    my @params;
158
0
    my $query_cond;
159
0
    push @params, ($start_cn_sort, $itemnumber, $start_cn_sort);
160
0
    if ($start_homebranch) {
161
0
     $query_cond .= 'AND homebranch = ? ';
162
0
     push @params, $start_homebranch->{code};
163    }
164
0
    if ($start_location) {
165
0
     $query_cond .= 'AND location = ? ';
166
0
     push @params, $start_location->{code};
167    }
168
0
    if ($start_ccode) {
169
0
     $query_cond .= 'AND ccode = ? ';
170
0
     push @params, $start_ccode->{code};
171    }
172
173
0
    my $sth_prev_items = $dbh->prepare($prev_query . $query_cond . ' ORDER BY cn_sort DESC, itemnumber LIMIT ?');
174
0
    my $sth_next_items = $dbh->prepare($next_query . $query_cond . ' ORDER BY cn_sort, itemnumber LIMIT ?');
175
0
    push @params, $num_each_side;
176
0
    $sth_prev_items->execute(@params);
177
0
    $sth_next_items->execute(@params);
178
179    # Now we have the query run, suck out the data like marrow
180
0
    my @prev_items = reverse GetShelfInfo($sth_prev_items, $marcflavour);
181
0
    my @next_items = GetShelfInfo($sth_next_items, $marcflavour);
182
183
0
    my $next_itemnumber = $next_items[-1]->{itemnumber} if @next_items;
184
0
    my $next_biblionumber = $next_items[-1]->{biblionumber} if @next_items;
185
186
0
    my $prev_itemnumber = $prev_items[0]->{itemnumber} if @prev_items;
187
0
    my $prev_biblionumber = $prev_items[0]->{biblionumber} if @prev_items;
188
189
0
    my %result = (
190        next => \@next_items,
191        prev => \@prev_items,
192        next_itemnumber => $next_itemnumber,
193        next_biblionumber => $next_biblionumber,
194        prev_itemnumber => $prev_itemnumber,
195        prev_biblionumber => $prev_biblionumber,
196        starting_itemnumber => $itemnumber,
197    );
198
0
    $result{starting_homebranch} = $start_homebranch if $start_homebranch;
199
0
    $result{starting_location} = $start_location if $start_location;
200
0
    $result{starting_ccode} = $start_ccode if $start_ccode;
201
0
    return \%result;
202}
203
204# This runs through a statement handle and pulls out all the items in it, fills
205# them up with additional info that shelves want, and returns those as a list.
206# Not really intended to be exported.
207sub GetShelfInfo {
208
0
    my ($sth, $marcflavour) = @_;
209
210
0
    my @items;
211
0
    while (my $this_item = $sth->fetchrow_hashref()) {
212
0
        my $this_biblio = GetBibData($this_item->{biblionumber});
213
0
        next if (!defined($this_biblio));
214
0
        $this_item->{'title'} = $this_biblio->{'title'};
215
0
        my $this_record = GetMarcBiblio($this_biblio->{'biblionumber'});
216
0
        $this_item->{'browser_normalized_upc'} = GetNormalizedUPC($this_record,$marcflavour);
217
0
        $this_item->{'browser_normalized_oclc'} = GetNormalizedOCLCNumber($this_record,$marcflavour);
218
0
        $this_item->{'browser_normalized_isbn'} = GetNormalizedISBN(undef,$this_record,$marcflavour);
219
0
        push @items, $this_item;
220    }
221
0
    return @items;
222}
223
224# Fetches some basic biblio data needed by the shelf stuff
225sub GetBibData {
226
0
        my ($bibnum) = @_;
227
228
0
    my $dbh = C4::Context->dbh;
229
0
    my $sth = $dbh->prepare("SELECT biblionumber, title FROM biblio WHERE biblionumber=?");
230
0
    $sth->execute($bibnum);
231
0
    my $bib = $sth->fetchrow_hashref();
232
0
    return $bib;
233}
234
2351;