Bug 2818 - Allow patron editing to coexist with minPasswordLength and '****'.
[koha.git] / opac / opac-detail.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 use strict;
22
23 use CGI;
24 use C4::Auth;
25 use C4::Branch;
26 use C4::Koha;
27 use C4::Serials;    #uses getsubscriptionfrom biblionumber
28 use C4::Output;
29 use C4::Biblio;
30 use C4::Items;
31 use C4::Circulation;
32 use C4::Tags qw(get_tags);
33 use C4::Dates qw/format_date/;
34 use C4::XISBN qw(get_xisbns get_biblionumber_from_isbn get_biblio_from_xisbn);
35 use C4::Amazon;
36 use C4::Review;
37 use C4::Serials;
38 use C4::Members;
39 use C4::XSLT;
40
41 BEGIN {
42         if (C4::Context->preference('BakerTaylorEnabled')) {
43                 require C4::External::BakerTaylor;
44                 import C4::External::BakerTaylor qw(&image_url &link_url);
45         }
46 }
47
48 my $query = new CGI;
49 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50     {
51         template_name   => "opac-detail.tmpl",
52         query           => $query,
53         type            => "opac",
54         authnotrequired => 1,
55         flagsrequired   => { borrow => 1 },
56     }
57 );
58
59 my $biblionumber = $query->param('biblionumber') || $query->param('bib');
60 $template->param( biblionumber => $biblionumber );
61 # XSLT processing of some stuff
62 if (C4::Context->preference("XSLTDetailsDisplay") ) {
63     my $newxmlrecord = XSLTParse4Display($biblionumber,C4::Context->config('opachtdocs')."/prog/en/xslt/MARC21slim2OPACDetail.xsl");
64     $template->param('XSLTBloc' => $newxmlrecord);
65 }
66
67 # change back when ive fixed request.pl
68 my @all_items = &GetItemsInfo( $biblionumber, 'opac' );
69 my @items;
70 @items = @all_items unless C4::Context->preference('hidelostitems');
71
72 if (C4::Context->preference('hidelostitems')) {
73     # Hide host items
74     for my $itm (@all_items) {
75         push @items, $itm unless $itm->{itemlost};
76     }
77 }
78 my $dat = &GetBiblioData($biblionumber);
79
80 if (!$dat) {
81     print $query->redirect("/cgi-bin/koha/errors/404.pl");
82     exit;
83 }
84 my $itemtypes = GetItemTypes();
85 # imageurl:
86 my $itemtype = $dat->{'itemtype'};
87 if ( $itemtype ) {
88     $dat->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{$itemtype}->{'imageurl'} );
89     $dat->{'description'} = $itemtypes->{$itemtype}->{'description'};
90 }
91 my $shelflocations =GetKohaAuthorisedValues('items.location',$dat->{'frameworkcode'});
92 my $collections =  GetKohaAuthorisedValues('items.ccode',$dat->{'frameworkcode'} );
93
94 #coping with subscriptions
95 my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber);
96 my @subscriptions       = GetSubscriptions( $dat->{title}, $dat->{issn}, $biblionumber );
97 my @subs;
98 $dat->{'serial'}=1 if $subscriptionsnumber;
99 foreach my $subscription (@subscriptions) {
100     my %cell;
101     $cell{subscriptionid}    = $subscription->{subscriptionid};
102     $cell{subscriptionnotes} = $subscription->{notes};
103     $cell{branchcode}        = $subscription->{branchcode};
104     $cell{hasalert}          = $subscription->{hasalert};
105     #get the three latest serials.
106     $cell{latestserials} =
107       GetLatestSerials( $subscription->{subscriptionid}, 3 );
108     push @subs, \%cell;
109 }
110
111 $dat->{'count'} = scalar(@items);
112
113 #adding RequestOnOpac filter to allow or not the display of plce reserve button
114 # FIXME - use me or delete me.
115 my $RequestOnOpac;
116 if (C4::Context->preference("RequestOnOpac")) {
117     $RequestOnOpac = 1;
118 }
119
120 my $biblio_authorised_value_images = C4::Items::get_authorised_value_images( C4::Biblio::get_biblio_authorised_values( $biblionumber ) );
121
122 my $norequests = 1;
123 my $branches = GetBranches();
124 my %itemfields;
125 for my $itm (@items) {
126      $norequests = 0 && $norequests
127        if ( (not $itm->{'wthdrawn'} )
128          && (not $itm->{'itemlost'} )
129          && ($itm->{'itemnotforloan'}<0 || not $itm->{'itemnotforloan'} )
130                  && (not $itemtypes->{$itm->{'itype'}}->{notforloan} )
131          && ($itm->{'itemnumber'} ) );
132
133     $itm->{ $itm->{'publictype'} } = 1;
134     $itm->{datedue} = format_date($itm->{datedue});
135     $itm->{datelastseen} = format_date($itm->{datelastseen});
136
137     #get collection code description, too
138         my $ccode= $itm->{'ccode'};
139         $itm->{'ccode'} = $collections->{$ccode} if(defined($collections) && exists($collections->{$ccode}));
140     $itm->{'location_description'} = $shelflocations->{$itm->{'location'} };
141     $itm->{'imageurl'}    = getitemtypeimagelocation( 'opac', $itemtypes->{ $itm->{itype} }->{'imageurl'} );
142     $itm->{'description'} = $itemtypes->{$itemtype}->{'description'};
143         $itemfields{ccode} = 1 if($itm->{ccode});
144         $itemfields{enumchron} = 1 if($itm->{enumchron});
145         $itemfields{copynumber} = 1 if($itm->{copynumber});
146         $itemfields{itemnotes} = 1 if($itm->{itemnotes});
147
148      # walk through the item-level authorised values and populate some images
149      my $item_authorised_value_images = C4::Items::get_authorised_value_images( C4::Items::get_item_authorised_values( $itm->{'itemnumber'} ) );
150      # warn( Data::Dumper->Dump( [ $item_authorised_value_images ], [ 'item_authorised_value_images' ] ) );
151
152      if ( $itm->{'itemlost'} ) {
153          my $lostimageinfo = List::Util::first { $_->{'category'} eq 'LOST' } @$item_authorised_value_images;
154          $itm->{'lostimageurl'}   = $lostimageinfo->{ 'imageurl' };
155          $itm->{'lostimagelabel'} = $lostimageinfo->{ 'label' };
156      }
157
158     
159      my ( $transfertwhen, $transfertfrom, $transfertto ) = GetTransfers($itm->{itemnumber});
160      if ( $transfertwhen ne '' ) {
161         $itm->{transfertwhen} = format_date($transfertwhen);
162         $itm->{transfertfrom} = $branches->{$transfertfrom}{branchname};
163         $itm->{transfertto}   = $branches->{$transfertto}{branchname};
164      }
165 }
166
167 ## get notes and subjects from MARC record
168 my $dbh              = C4::Context->dbh;
169 my $marcflavour      = C4::Context->preference("marcflavour");
170 my $record           = GetMarcBiblio($biblionumber);
171 my $marcnotesarray   = GetMarcNotes   ($record,$marcflavour);
172 my $marcauthorsarray = GetMarcAuthors ($record,$marcflavour);
173 my $marcsubjctsarray = GetMarcSubjects($record,$marcflavour);
174 my $marcseriesarray  = GetMarcSeries  ($record,$marcflavour);
175 my $marcurlsarray    = GetMarcUrls    ($record,$marcflavour);
176 my $subtitle         = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
177
178     $template->param(
179                      MARCNOTES               => $marcnotesarray,
180                      MARCSUBJCTS             => $marcsubjctsarray,
181                      MARCAUTHORS             => $marcauthorsarray,
182                      MARCSERIES              => $marcseriesarray,
183                      MARCURLS                => $marcurlsarray,
184                      norequests              => $norequests,
185                      RequestOnOpac           => $RequestOnOpac,
186                      itemdata_ccode          => $itemfields{ccode},
187                      itemdata_enumchron      => $itemfields{enumchron},
188                      itemdata_copynumber     => $itemfields{copynumber},
189                      itemdata_itemnotes          => $itemfields{itemnotes},
190                      authorised_value_images => $biblio_authorised_value_images,
191                      subtitle                => $subtitle,
192     );
193
194 foreach ( keys %{$dat} ) {
195     $template->param( "$_" => $dat->{$_} . "" );
196 }
197
198 # COinS format FIXME: for books Only
199 my $coins_format;
200 my $fmt = substr $record->leader(), 6,2;
201 my $fmts;
202 $fmts->{'am'} = 'book';
203 $coins_format = $fmts->{$fmt};
204 $template->param(
205         ocoins_format => $coins_format,
206 );
207
208 my $reviews = getreviews( $biblionumber, 1 );
209 my $loggedincommenter;
210 foreach ( @$reviews ) {
211     my $borrowerData   = GetMember($_->{borrowernumber},'borrowernumber');
212     # setting some borrower info into this hash
213     $_->{title}     = $borrowerData->{'title'};
214     $_->{surname}   = $borrowerData->{'surname'};
215     $_->{firstname} = $borrowerData->{'firstname'};
216     $_->{userid}    = $borrowerData->{'userid'};
217     $_->{datereviewed} = format_date($_->{datereviewed});
218     if ($borrowerData->{'borrowernumber'} eq $borrowernumber) {
219                 $_->{your_comment} = 1;
220                 $loggedincommenter = 1;
221         }
222 }
223
224
225 if(C4::Context->preference("ISBD")) {
226         $template->param(ISBD => 1);
227 }
228
229 $template->param(
230     ITEM_RESULTS        => \@items,
231     subscriptionsnumber => $subscriptionsnumber,
232     biblionumber        => $biblionumber,
233     subscriptions       => \@subs,
234     subscriptionsnumber => $subscriptionsnumber,
235     reviews             => $reviews,
236     loggedincommenter   => $loggedincommenter
237 );
238
239 sub isbn_cleanup ($) {
240         my $isbn=shift;
241     ($isbn) = $isbn =~ /([\d-]*[X]*)/;
242     $isbn =~ s/-//g;
243         if (
244                 $isbn =~ /\b(\d{13})\b/ or
245                 $isbn =~ /\b(\d{10})\b/ or 
246                 $isbn =~ /\b(\d{9}X)\b/i
247         ) {
248                 return $1;
249         }
250         return undef;
251 }
252
253 # XISBN Stuff
254 my $xisbn=$dat->{'isbn'};
255 (my $aisbn) = $xisbn =~ /([\d-]*[X]*)/;
256 $aisbn =~ s/-//g;
257 $template->param(amazonisbn => $aisbn);         # FIXME: so it is OK if the ISBN = 'XXXXX' ?
258 my ($clean,$clean2);
259 # these might be overkill, but they are better than the regexp above.
260 if ($clean = isbn_cleanup($xisbn)){
261         $template->param(clean_isbn => $clean);
262 }
263
264 if (C4::Context->preference("OPACFRBRizeEditions")==1) {
265     eval {
266         $template->param(
267             xisbn => $xisbn,
268             XISBNS => get_xisbns($xisbn)
269         );
270     };
271     if ($@) { warn "XISBN Failed $@"; }
272 }
273 # Amazon.com Stuff
274 if ( C4::Context->preference("OPACAmazonContent") == 1 ) {
275     my $similar_products_exist;
276     my $amazon_details = &get_amazon_details( $xisbn );
277     my $item_attributes = \%{$amazon_details->{Items}->{Item}->{ItemAttributes}};
278     my $customer_reviews = \@{$amazon_details->{Items}->{Item}->{CustomerReviews}->{Review}};
279     for my $one_review (@$customer_reviews) {
280         $one_review->{Date} = format_date($one_review->{Date});
281     }
282     my @similar_products;
283     for my $similar_product (@{$amazon_details->{Items}->{Item}->{SimilarProducts}->{SimilarProduct}}) {
284         # do we have any of these isbns in our collection?
285         my $similar_biblionumbers = get_biblionumber_from_isbn($similar_product->{ASIN});
286         # verify that there is at least one similar item
287         if (scalar(@$similar_biblionumbers)){
288             $similar_products_exist++ if ($similar_biblionumbers && $similar_biblionumbers->[0]);
289             push @similar_products, +{ similar_biblionumbers => $similar_biblionumbers, title => $similar_product->{Title}, ASIN => $similar_product->{ASIN}  };
290         }
291     }
292     my $editorial_reviews = \@{$amazon_details->{Items}->{Item}->{EditorialReviews}->{EditorialReview}};
293     my $average_rating = $amazon_details->{Items}->{Item}->{CustomerReviews}->{AverageRating};
294     $template->param( OPACAmazonSimilarItems => $similar_products_exist );
295     $template->param( amazon_average_rating => $average_rating * 20);
296     $template->param( AMAZON_CUSTOMER_REVIEWS    => $customer_reviews );
297     $template->param( AMAZON_SIMILAR_PRODUCTS => \@similar_products );
298     $template->param( AMAZON_EDITORIAL_REVIEWS    => $editorial_reviews );
299 }
300 # Shelf Browser Stuff
301 if (C4::Context->preference("OPACShelfBrowser")) {
302     # pick the first itemnumber unless one was selected by the user
303     my $starting_itemnumber = $query->param('shelfbrowse_itemnumber'); # || $items[0]->{itemnumber};
304     $template->param( OpenOPACShelfBrowser => 1) if $starting_itemnumber;
305     # find the right cn_sort value for this item
306     my ($starting_cn_sort, $starting_homebranch, $starting_location);
307     my $sth_get_cn_sort = $dbh->prepare("SELECT cn_sort,homebranch,location from items where itemnumber=?");
308     $sth_get_cn_sort->execute($starting_itemnumber);
309     while (my $result = $sth_get_cn_sort->fetchrow_hashref()) {
310         $starting_cn_sort = $result->{'cn_sort'};
311         $starting_homebranch->{code} = $result->{'homebranch'};
312         $starting_homebranch->{description} = $branches->{$result->{'homebranch'}}{branchname};
313         $starting_location->{code} = $result->{'location'};
314         $starting_location->{description} = GetAuthorisedValueDesc('','',   $result->{'location'} ,'','','LOC');
315     
316     }
317     
318     ## List of Previous Items
319     # order by cn_sort, which should include everything we need for ordering purposes (though not
320     # for limits, those need to be handled separately
321     my $sth_shelfbrowse_previous = $dbh->prepare("
322         SELECT *
323         FROM items
324         WHERE
325             ((cn_sort = ? AND itemnumber < ?) OR cn_sort < ?) AND
326             homebranch = ? AND location = ?
327         ORDER BY cn_sort DESC, itemnumber LIMIT 3
328         ");
329     $sth_shelfbrowse_previous->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code}, $starting_location->{code});
330     my @previous_items;
331     while (my $this_item = $sth_shelfbrowse_previous->fetchrow_hashref()) {
332         my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
333         $sth_get_biblio->execute($this_item->{biblionumber});
334         while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
335             $this_item->{'title'} = $this_biblio->{'title'};
336             if ($clean2 = isbn_cleanup($this_biblio->{'isbn'})) {
337                 $this_item->{'isbn'} = $clean2;
338             } else { 
339                 $this_item->{'isbn'} = $this_biblio->{'isbn'};
340             }
341         }
342         unshift @previous_items, $this_item;
343     }
344     
345     ## List of Next Items; this also intentionally catches the current item
346     my $sth_shelfbrowse_next = $dbh->prepare("
347         SELECT *
348         FROM items
349         WHERE
350             ((cn_sort = ? AND itemnumber >= ?) OR cn_sort > ?) AND
351             homebranch = ? AND location = ?
352         ORDER BY cn_sort, itemnumber LIMIT 3
353         ");
354     $sth_shelfbrowse_next->execute($starting_cn_sort, $starting_itemnumber, $starting_cn_sort, $starting_homebranch->{code}, $starting_location->{code});
355     my @next_items;
356     while (my $this_item = $sth_shelfbrowse_next->fetchrow_hashref()) {
357         my $sth_get_biblio = $dbh->prepare("SELECT biblio.*,biblioitems.isbn AS isbn FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=?");
358         $sth_get_biblio->execute($this_item->{biblionumber});
359         while (my $this_biblio = $sth_get_biblio->fetchrow_hashref()) {
360             $this_item->{'title'} = $this_biblio->{'title'};
361             if ($clean2 = isbn_cleanup($this_biblio->{'isbn'})) {
362                 $this_item->{'isbn'} = $clean2;
363             } else { 
364                 $this_item->{'isbn'} = $this_biblio->{'isbn'};
365             }
366         }
367         push @next_items, $this_item;
368     }
369     
370     # alas, these won't auto-vivify, see http://www.perlmonks.org/?node_id=508481
371     my $shelfbrowser_next_itemnumber = $next_items[-1]->{itemnumber} if @next_items;
372     my $shelfbrowser_next_biblionumber = $next_items[-1]->{biblionumber} if @next_items;
373     
374     $template->param(
375         starting_homebranch => $starting_homebranch->{description},
376         starting_location => $starting_location->{description},
377         starting_itemnumber => $starting_itemnumber,
378         shelfbrowser_prev_itemnumber => (@previous_items ? $previous_items[0]->{itemnumber} : 0),
379         shelfbrowser_next_itemnumber => $shelfbrowser_next_itemnumber,
380         shelfbrowser_prev_biblionumber => (@previous_items ? $previous_items[0]->{biblionumber} : 0),
381         shelfbrowser_next_biblionumber => $shelfbrowser_next_biblionumber,
382         PREVIOUS_SHELF_BROWSE => \@previous_items,
383         NEXT_SHELF_BROWSE => \@next_items,
384     );
385 }
386
387 if (C4::Context->preference("BakerTaylorEnabled")) {
388         $template->param(
389                 BakerTaylorEnabled  => 1,
390                 BakerTaylorImageURL => &image_url(),
391                 BakerTaylorLinkURL  => &link_url(),
392                 BakerTaylorBookstoreURL => C4::Context->preference('BakerTaylorBookstoreURL'),
393         );
394         my ($bt_user, $bt_pass);
395         if ($clean and
396                 $bt_user = C4::Context->preference('BakerTaylorUsername') and
397                 $bt_pass = C4::Context->preference('BakerTaylorPassword')    )
398         {
399                 $template->param(
400                 BakerTaylorContentURL   =>
401                 sprintf("http://contentcafe2.btol.com/ContentCafeClient/ContentCafe.aspx?UserID=%s&Password=%s&ItemKey=%s&Options=Y",
402                                 $bt_user,$bt_pass,$clean)
403                 );
404         }
405 }
406
407 my $tag_quantity;
408 if (C4::Context->preference('TagsEnabled') and $tag_quantity = C4::Context->preference('TagsShowOnDetail')) {
409         $template->param(
410                 TagsEnabled => 1,
411                 TagsShowOnDetail => $tag_quantity,
412                 TagsInputOnDetail => C4::Context->preference('TagsInputOnDetail')
413         );
414         $template->param(TagLoop => get_tags({biblionumber=>$biblionumber, approved=>1,
415                                                                 'sort'=>'-weight', limit=>$tag_quantity}));
416 }
417
418 output_html_with_http_headers $query, $cookie, $template->output;