Bug 18136: Fix ExportRemoveFields pre-fill behaviour
[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 @items;
255             while ( my $content = $contents->next ) {
256                 my $biblionumber = $content->biblionumber->biblionumber;
257                 my $this_item    = GetBiblioData($biblionumber);
258                 my $record       = GetMarcBiblio($biblionumber);
259
260                 if ( C4::Context->preference("OPACXSLTResultsDisplay") ) {
261                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTResultsDisplay" );
262                 }
263
264                 my $marcflavour = C4::Context->preference("marcflavour");
265                 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'opac' );
266                 $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
267                 $this_item->{description}       = $itemtypeinfo->{description};
268                 $this_item->{notforloan}        = $itemtypeinfo->{notforloan};
269                 $this_item->{'coins'}           = GetCOinSBiblio($record);
270                 $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
271                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
272                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
273                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
274                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
275
276                 unless ( defined $this_item->{size} ) {
277
278                     #TT has problems with size
279                     $this_item->{size} = q||;
280                 }
281
282                 # Getting items infos for location display
283                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
284                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
285
286                 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
287                     $this_item->{TagLoop} = get_tags({
288                         biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
289                         limit => C4::Context->preference('TagsShowOnList'),
290                     });
291                 }
292
293                 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
294
295
296                 if ( grep {$_ eq $biblionumber} @cart_list) {
297                     $this_item->{incart} = 1;
298                 }
299
300                 if ( $query->param('rss') ) {
301                     $this_item->{title} = $content->biblionumber->title;
302                     $this_item->{author} = $content->biblionumber->author;
303                 }
304
305                 $this_item->{biblionumber} = $biblionumber;
306                 push @items, $this_item;
307             }
308
309             $template->param(
310                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
311                 can_delete_shelf   => $shelf->can_be_deleted($loggedinuser),
312                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
313                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
314                 itemsloop          => \@items,
315                 sortfield          => $sortfield,
316                 direction          => $direction,
317             );
318             if ( $page ) {
319                 my $pager = $contents->pager;
320                 $template->param(
321                     pagination_bar => pagination_bar(
322                         q||, $pager->last_page - $pager->first_page + 1,
323                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
324                     ),
325                 );
326             }
327         } else {
328             push @messages, { type => 'error', code => 'unauthorized_on_view' };
329             undef $shelf;
330         }
331     } else {
332         push @messages, { type => 'error', code => 'does_not_exist' };
333     }
334 }
335
336 if ( $op eq 'list' ) {
337     my $shelves;
338     my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
339     if ( $category == 1 ) {
340         $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
341     } else {
342         $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
343     }
344
345     my $pager = $shelves->pager;
346     $template->param(
347         shelves => $shelves,
348         pagination_bar => pagination_bar(
349             q||, $pager->last_page - $pager->first_page + 1,
350             $page, "page", { op => 'list', category => $category, }
351         ),
352     );
353 }
354
355 $template->param(
356     op       => $op,
357     referer  => $referer,
358     shelf    => $shelf,
359     messages => \@messages,
360     category => $category,
361     print    => scalar $query->param('print') || 0,
362     listsview => 1,
363 );
364
365 output_html_with_http_headers $query, $cookie, $template->output;