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