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