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