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