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