]> git.koha-community.org Git - koha.git/blob - opac/opac-shelves.pl
Bug 18276: Remove GetBiblioFromItemNumber - Easy ones
[koha.git] / opac / opac-shelves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Biblio;
25 use C4::Koha;
26 use C4::Items;
27 use C4::Members;
28 use C4::Output;
29 use C4::Tags qw( get_tags );
30 use C4::XSLT;
31
32 use Koha::Biblioitems;
33 use Koha::Items;
34 use Koha::ItemTypes;
35 use Koha::Virtualshelves;
36 use Koha::RecordProcessor;
37
38 use constant ANYONE => 2;
39
40 my $query = new CGI;
41
42 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
43
44 # if virtualshelves is disabled, leave immediately
45 if ( ! C4::Context->preference('virtualshelves') ) {
46     print $query->redirect("/cgi-bin/koha/errors/404.pl");
47     exit;
48 }
49
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
51         template_name   => $template_name,
52         query           => $query,
53         type            => "opac",
54         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
55     });
56
57 my $op       = $query->param('op')       || 'list';
58 my $referer  = $query->param('referer')  || $op;
59 my $category = $query->param('category') || 1;
60 my ( $shelf, $shelfnumber, @messages );
61
62 if ( $op eq 'add_form' ) {
63     # Only pass default
64     $shelf = { allow_change_from_owner => 1 };
65 } elsif ( $op eq 'edit_form' ) {
66     $shelfnumber = $query->param('shelfnumber');
67     $shelf       = Koha::Virtualshelves->find($shelfnumber);
68
69     if ( $shelf ) {
70         $category = $shelf->category;
71         my $patron = GetMember( 'borrowernumber' => $shelf->owner );
72         $template->param( owner => $patron, );
73         unless ( $shelf->can_be_managed( $loggedinuser ) ) {
74             push @messages, { type => 'error', code => 'unauthorized_on_update' };
75             $op = 'list';
76         }
77     } else {
78         push @messages, { type => 'error', code => 'does_not_exist' };
79     }
80 } elsif ( $op eq 'add' ) {
81     if ( $loggedinuser ) {
82         my $allow_changes_from = $query->param('allow_changes_from');
83         eval {
84             $shelf = Koha::Virtualshelf->new(
85                 {   shelfname          => scalar $query->param('shelfname'),
86                     sortfield          => scalar $query->param('sortfield'),
87                     category           => scalar $query->param('category') || 1,
88                     allow_change_from_owner => $allow_changes_from > 0,
89                     allow_change_from_others => $allow_changes_from == ANYONE,
90                     owner              => scalar $loggedinuser,
91                 }
92             );
93             $shelf->store;
94             $shelfnumber = $shelf->shelfnumber;
95         };
96         if ($@) {
97             push @messages, { type => 'error', code => ref($@), msg => $@ };
98         } elsif ( not $shelf ) {
99             push @messages, { type => 'error', code => 'error_on_insert' };
100         } else {
101             push @messages, { type => 'message', code => 'success_on_insert' };
102             $op = 'view';
103         }
104     } else {
105         push @messages, { type => 'error', code => 'unauthorized_on_insert' };
106         $op = 'list';
107     }
108 } elsif ( $op eq 'edit' ) {
109     $shelfnumber = $query->param('shelfnumber');
110     $shelf       = Koha::Virtualshelves->find($shelfnumber);
111     if ( $shelf ) {
112         $op = $referer;
113         my $sortfield = $query->param('sortfield');
114         $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
115         if ( $shelf->can_be_managed( $loggedinuser ) ) {
116             $shelf->shelfname( scalar $query->param('shelfname') );
117             $shelf->sortfield( $sortfield );
118             my $allow_changes_from = $query->param('allow_changes_from');
119             $shelf->allow_change_from_owner( $allow_changes_from > 0 );
120             $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
121             $shelf->category( scalar $query->param('category') );
122             eval { $shelf->store };
123
124             if ($@) {
125                 push @messages, { type => 'error', code => 'error_on_update' };
126                 $op = 'edit_form';
127             } else {
128                 push @messages, { type => 'message', code => 'success_on_update' };
129             }
130         } else {
131             push @messages, { type => 'error', code => 'unauthorized_on_update' };
132         }
133     } else {
134         push @messages, { type => 'error', code => 'does_not_exist' };
135     }
136 } elsif ( $op eq 'delete' ) {
137     $shelfnumber = $query->param('shelfnumber');
138     $shelf       = Koha::Virtualshelves->find($shelfnumber);
139     if ($shelf) {
140         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
141             eval { $shelf->delete; };
142             if ($@) {
143                 push @messages, { type => 'error', code => ref($@), msg => $@ };
144             } else {
145                 push @messages, { type => 'message', code => 'success_on_delete' };
146             }
147         } else {
148             push @messages, { type => 'error', code => 'unauthorized_on_delete' };
149         }
150     } else {
151         push @messages, { type => 'error', code => 'does_not_exist' };
152     }
153     $op = $referer;
154 } elsif ( $op eq 'remove_share' ) {
155     $shelfnumber = $query->param('shelfnumber');
156     $shelf = Koha::Virtualshelves->find($shelfnumber);
157     if ($shelf) {
158         my $removed = eval { $shelf->remove_share( $loggedinuser ); };
159         if ($@) {
160             push @messages, { type => 'error', code => ref($@), msg => $@ };
161         } elsif ( $removed ) {
162             push @messages, { type => 'message', code => 'success_on_remove_share' };
163         } else {
164             push @messages, { type => 'error', code => 'error_on_remove_share' };
165         }
166     } else {
167         push @messages, { type => 'error', code => 'does_not_exist' };
168     }
169     $op = $referer;
170
171 } elsif ( $op eq 'add_biblio' ) {
172     $shelfnumber = $query->param('shelfnumber');
173     $shelf = Koha::Virtualshelves->find($shelfnumber);
174     if ($shelf) {
175         if( my $barcode = $query->param('barcode') ) {
176             my $item = GetItem( 0, $barcode);
177             if (defined $item && $item->{itemnumber}) {
178                 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
179                     my $added = eval { $shelf->add_biblio( $item->{biblionumber}, $loggedinuser ); };
180                     if ($@) {
181                         push @messages, { type => 'error', code => ref($@), msg => $@ };
182                     } elsif ( $added ) {
183                         push @messages, { type => 'message', code => 'success_on_add_biblio' };
184                     } else {
185                         push @messages, { type => 'message', code => 'error_on_add_biblio' };
186                     }
187                 } else {
188                     push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
189                 }
190             } else {
191                 push @messages, { type => 'error', code => 'item_does_not_exist' };
192             }
193         }
194     } else {
195         push @messages, { type => 'error', code => 'does_not_exist' };
196     }
197     $op = $referer;
198 } elsif ( $op eq 'remove_biblios' ) {
199     $shelfnumber = $query->param('shelfnumber');
200     $shelf = Koha::Virtualshelves->find($shelfnumber);
201     my @biblionumber = $query->multi_param('biblionumber');
202     if ($shelf) {
203         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
204             my $number_of_biblios_removed = eval {
205                 $shelf->remove_biblios(
206                     {
207                         biblionumbers => \@biblionumber,
208                         borrowernumber => $loggedinuser,
209                     }
210                 );
211             };
212             if ($@) {
213                 push @messages, { type => 'error', code => ref($@), msg => $@ };
214             } elsif ( $number_of_biblios_removed ) {
215                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
216             } else {
217                 push @messages, { type => 'error', code => 'no_biblio_removed' };
218             }
219         } else {
220             push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
221         }
222     } else {
223         push @messages, { type => 'error', code => 'does_not_exist' };
224     }
225     $op = 'view';
226 }
227
228 if ( $op eq 'view' ) {
229     $shelfnumber ||= $query->param('shelfnumber');
230     $shelf = Koha::Virtualshelves->find($shelfnumber);
231     if ( $shelf ) {
232         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
233             $category = $shelf->category;
234             my $sortfield = $query->param('sortfield') || $shelf->sortfield;    # Passed in sorting overrides default sorting
235             $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
236             my $direction = $query->param('direction') || 'asc';
237             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
238             my ( $page, $rows );
239             unless ( $query->param('print') or $query->param('rss') ) {
240                 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
241                 $page = ( $query->param('page') ? $query->param('page') : 1 );
242             }
243             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
244             my $contents = $shelf->get_contents->search(
245                 {},
246                 {
247                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
248                     page     => $page,
249                     rows     => $rows,
250                     order_by => { "-$direction" => $order_by },
251                 }
252             );
253
254             # get biblionumbers stored in the cart
255             my @cart_list;
256             if(my $cart_list = $query->cookie('bib_list')){
257                 @cart_list = split(/\//, $cart_list);
258             }
259
260             my $borrower = GetMember( borrowernumber => $loggedinuser );
261
262             # Lists display falls back to search results configuration
263             my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
264             my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
265             my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
266
267             my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
268             my @items;
269             while ( my $content = $contents->next ) {
270                 my $biblionumber = $content->biblionumber;
271                 my $this_item    = GetBiblioData($biblionumber);
272                 my $record = GetMarcBiblio($biblionumber);
273                 my $framework = GetFrameworkCode( $biblionumber );
274                 $record_processor->options({
275                     interface => 'opac',
276                     frameworkcode => $framework
277                 });
278                 $record_processor->process($record);
279
280                 if ( $xslfile ) {
281                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
282                                                                 1, undef, $sysxml, $xslfile, $lang);
283                 }
284
285                 my $marcflavour = C4::Context->preference("marcflavour");
286                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
287                 $itemtype = Koha::ItemTypes->find( $itemtype );
288                 if( $itemtype ) {
289                     $this_item->{imageurl}          = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
290                     $this_item->{description}       = $itemtype->description; #FIXME Should not it be translated_description?
291                     $this_item->{notforloan}        = $itemtype->notforloan;
292                 }
293                 $this_item->{'coins'}           = GetCOinSBiblio($record);
294                 $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
295                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
296                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
297                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
298                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
299
300                 unless ( defined $this_item->{size} ) {
301
302                     #TT has problems with size
303                     $this_item->{size} = q||;
304                 }
305
306                 # Getting items infos for location display
307                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
308                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
309
310                 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
311                     $this_item->{TagLoop} = get_tags({
312                         biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
313                         limit => C4::Context->preference('TagsShowOnList'),
314                     });
315                 }
316
317                 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
318
319
320                 if ( grep {$_ eq $biblionumber} @cart_list) {
321                     $this_item->{incart} = 1;
322                 }
323
324                 $this_item->{biblionumber} = $biblionumber;
325                 push @items, $this_item;
326             }
327
328             $template->param(
329                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
330                 can_delete_shelf   => $shelf->can_be_deleted($loggedinuser),
331                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
332                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
333                 itemsloop          => \@items,
334                 sortfield          => $sortfield,
335                 direction          => $direction,
336             );
337             if ( $page ) {
338                 my $pager = $contents->pager;
339                 $template->param(
340                     pagination_bar => pagination_bar(
341                         q||, $pager->last_page - $pager->first_page + 1,
342                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
343                     ),
344                 );
345             }
346         } else {
347             push @messages, { type => 'error', code => 'unauthorized_on_view' };
348             undef $shelf;
349         }
350     } else {
351         push @messages, { type => 'error', code => 'does_not_exist' };
352     }
353 }
354
355 if ( $op eq 'list' ) {
356     my $shelves;
357     my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
358     if ( $category == 1 ) {
359         $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
360     } else {
361         $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
362     }
363
364     my $pager = $shelves->pager;
365     $template->param(
366         shelves => $shelves,
367         pagination_bar => pagination_bar(
368             q||, $pager->last_page - $pager->first_page + 1,
369             $page, "page", { op => 'list', category => $category, }
370         ),
371     );
372 }
373
374 $template->param(
375     op       => $op,
376     referer  => $referer,
377     shelf    => $shelf,
378     messages => \@messages,
379     category => $category,
380     print    => scalar $query->param('print') || 0,
381     listsview => 1,
382 );
383
384 output_html_with_http_headers $query, $cookie, $template->output;