Bug 18869: [FOLLOW-UP] Make page reload after club or template delete
[koha.git] / opac / opac-shelves.pl
blobe5b9cc62a19eb37e843be75195c94ea2975adabf
1 #!/usr/bin/perl
3 # Copyright 2015 Koha Team
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
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;
32 use Koha::Biblioitems;
33 use Koha::Items;
34 use Koha::ItemTypes;
35 use Koha::Patrons;
36 use Koha::Virtualshelves;
37 use Koha::RecordProcessor;
39 use constant ANYONE => 2;
41 my $query = new CGI;
43 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
45 # if virtualshelves is disabled, leave immediately
46 if ( ! C4::Context->preference('virtualshelves') ) {
47 print $query->redirect("/cgi-bin/koha/errors/404.pl");
48 exit;
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
52 template_name => $template_name,
53 query => $query,
54 type => "opac",
55 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
56 });
58 my $op = $query->param('op') || 'list';
59 my $referer = $query->param('referer') || $op;
60 my $category = $query->param('category') || 1;
61 my ( $shelf, $shelfnumber, @messages );
63 if ( $op eq 'add_form' ) {
64 # Only pass default
65 $shelf = { allow_change_from_owner => 1 };
66 } elsif ( $op eq 'edit_form' ) {
67 $shelfnumber = $query->param('shelfnumber');
68 $shelf = Koha::Virtualshelves->find($shelfnumber);
70 if ( $shelf ) {
71 $category = $shelf->category;
72 my $patron = Koha::Patrons->find( $shelf->owner );
73 $template->param( owner => $patron, );
74 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
75 push @messages, { type => 'error', code => 'unauthorized_on_update' };
76 $op = 'list';
78 } else {
79 push @messages, { type => 'error', code => 'does_not_exist' };
81 } elsif ( $op eq 'add' ) {
82 if ( $loggedinuser ) {
83 my $allow_changes_from = $query->param('allow_changes_from');
84 eval {
85 $shelf = Koha::Virtualshelf->new(
86 { shelfname => scalar $query->param('shelfname'),
87 sortfield => scalar $query->param('sortfield'),
88 category => scalar $query->param('category') || 1,
89 allow_change_from_owner => $allow_changes_from > 0,
90 allow_change_from_others => $allow_changes_from == ANYONE,
91 owner => scalar $loggedinuser,
94 $shelf->store;
95 $shelfnumber = $shelf->shelfnumber;
97 if ($@) {
98 push @messages, { type => 'error', code => ref($@), msg => $@ };
99 } elsif ( not $shelf ) {
100 push @messages, { type => 'error', code => 'error_on_insert' };
101 } else {
102 push @messages, { type => 'message', code => 'success_on_insert' };
103 $op = 'view';
105 } else {
106 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
107 $op = 'list';
109 } elsif ( $op eq 'edit' ) {
110 $shelfnumber = $query->param('shelfnumber');
111 $shelf = Koha::Virtualshelves->find($shelfnumber);
112 if ( $shelf ) {
113 $op = $referer;
114 my $sortfield = $query->param('sortfield');
115 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
116 if ( $shelf->can_be_managed( $loggedinuser ) ) {
117 $shelf->shelfname( scalar $query->param('shelfname') );
118 $shelf->sortfield( $sortfield );
119 my $allow_changes_from = $query->param('allow_changes_from');
120 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
121 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
122 $shelf->category( scalar $query->param('category') );
123 eval { $shelf->store };
125 if ($@) {
126 push @messages, { type => 'error', code => 'error_on_update' };
127 $op = 'edit_form';
128 } else {
129 push @messages, { type => 'message', code => 'success_on_update' };
131 } else {
132 push @messages, { type => 'error', code => 'unauthorized_on_update' };
134 } else {
135 push @messages, { type => 'error', code => 'does_not_exist' };
137 } elsif ( $op eq 'delete' ) {
138 $shelfnumber = $query->param('shelfnumber');
139 $shelf = Koha::Virtualshelves->find($shelfnumber);
140 if ($shelf) {
141 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
142 eval { $shelf->delete; };
143 if ($@) {
144 push @messages, { type => 'error', code => ref($@), msg => $@ };
145 } else {
146 push @messages, { type => 'message', code => 'success_on_delete' };
148 } else {
149 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
151 } else {
152 push @messages, { type => 'error', code => 'does_not_exist' };
154 $op = $referer;
155 } elsif ( $op eq 'remove_share' ) {
156 $shelfnumber = $query->param('shelfnumber');
157 $shelf = Koha::Virtualshelves->find($shelfnumber);
158 if ($shelf) {
159 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
160 if ($@) {
161 push @messages, { type => 'error', code => ref($@), msg => $@ };
162 } elsif ( $removed ) {
163 push @messages, { type => 'message', code => 'success_on_remove_share' };
164 } else {
165 push @messages, { type => 'error', code => 'error_on_remove_share' };
167 } else {
168 push @messages, { type => 'error', code => 'does_not_exist' };
170 $op = $referer;
172 } elsif ( $op eq 'add_biblio' ) {
173 $shelfnumber = $query->param('shelfnumber');
174 $shelf = Koha::Virtualshelves->find($shelfnumber);
175 if ($shelf) {
176 if( my $barcode = $query->param('barcode') ) {
177 my $item = GetItem( 0, $barcode);
178 if (defined $item && $item->{itemnumber}) {
179 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
180 my $added = eval { $shelf->add_biblio( $item->{biblionumber}, $loggedinuser ); };
181 if ($@) {
182 push @messages, { type => 'error', code => ref($@), msg => $@ };
183 } elsif ( $added ) {
184 push @messages, { type => 'message', code => 'success_on_add_biblio' };
185 } else {
186 push @messages, { type => 'message', code => 'error_on_add_biblio' };
188 } else {
189 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
191 } else {
192 push @messages, { type => 'error', code => 'item_does_not_exist' };
195 } else {
196 push @messages, { type => 'error', code => 'does_not_exist' };
198 $op = $referer;
199 } elsif ( $op eq 'remove_biblios' ) {
200 $shelfnumber = $query->param('shelfnumber');
201 $shelf = Koha::Virtualshelves->find($shelfnumber);
202 my @biblionumber = $query->multi_param('biblionumber');
203 if ($shelf) {
204 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
205 my $number_of_biblios_removed = eval {
206 $shelf->remove_biblios(
208 biblionumbers => \@biblionumber,
209 borrowernumber => $loggedinuser,
213 if ($@) {
214 push @messages, { type => 'error', code => ref($@), msg => $@ };
215 } elsif ( $number_of_biblios_removed ) {
216 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
217 } else {
218 push @messages, { type => 'error', code => 'no_biblio_removed' };
220 } else {
221 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
223 } else {
224 push @messages, { type => 'error', code => 'does_not_exist' };
226 $op = 'view';
229 if ( $op eq 'view' ) {
230 $shelfnumber ||= $query->param('shelfnumber');
231 $shelf = Koha::Virtualshelves->find($shelfnumber);
232 if ( $shelf ) {
233 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
234 $category = $shelf->category;
235 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
236 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
237 my $direction = $query->param('direction') || 'asc';
238 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
239 my ( $page, $rows );
240 unless ( $query->param('print') or $query->param('rss') ) {
241 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
242 $page = ( $query->param('page') ? $query->param('page') : 1 );
244 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
245 my $contents = $shelf->get_contents->search(
248 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
249 page => $page,
250 rows => $rows,
251 order_by => { "-$direction" => $order_by },
255 # get biblionumbers stored in the cart
256 my @cart_list;
257 if(my $cart_list = $query->cookie('bib_list')){
258 @cart_list = split(/\//, $cart_list);
261 my $patron = Koha::Patrons->find( $loggedinuser );
263 # Lists display falls back to search results configuration
264 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
265 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
266 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
268 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
269 my @items;
270 while ( my $content = $contents->next ) {
271 my $biblionumber = $content->biblionumber;
272 my $this_item = GetBiblioData($biblionumber);
273 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
274 my $framework = GetFrameworkCode( $biblionumber );
275 $record_processor->options({
276 interface => 'opac',
277 frameworkcode => $framework
279 $record_processor->process($record);
281 if ( $xslfile ) {
282 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
283 1, undef, $sysxml, $xslfile, $lang);
286 my $marcflavour = C4::Context->preference("marcflavour");
287 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
288 $itemtype = Koha::ItemTypes->find( $itemtype );
289 if( $itemtype ) {
290 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
291 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
292 $this_item->{notforloan} = $itemtype->notforloan;
294 $this_item->{'coins'} = GetCOinSBiblio($record);
295 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
296 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
297 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
298 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
299 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
301 unless ( defined $this_item->{size} ) {
303 #TT has problems with size
304 $this_item->{size} = q||;
307 # Getting items infos for location display
308 my @items_infos = &GetItemsLocationInfo( $biblionumber );
309 $this_item->{'ITEM_RESULTS'} = \@items_infos;
311 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
312 $this_item->{TagLoop} = get_tags({
313 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
314 limit => C4::Context->preference('TagsShowOnList'),
318 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $patron);
321 if ( grep {$_ eq $biblionumber} @cart_list) {
322 $this_item->{incart} = 1;
325 $this_item->{biblionumber} = $biblionumber;
326 push @items, $this_item;
329 $template->param(
330 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
331 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
332 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
333 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
334 itemsloop => \@items,
335 sortfield => $sortfield,
336 direction => $direction,
338 if ( $page ) {
339 my $pager = $contents->pager;
340 $template->param(
341 pagination_bar => pagination_bar(
342 q||, $pager->last_page - $pager->first_page + 1,
343 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
347 } else {
348 push @messages, { type => 'error', code => 'unauthorized_on_view' };
349 undef $shelf;
351 } else {
352 push @messages, { type => 'error', code => 'does_not_exist' };
356 if ( $op eq 'list' ) {
357 my $shelves;
358 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
359 if ( $category == 1 ) {
360 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
361 } else {
362 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
365 my $pager = $shelves->pager;
366 $template->param(
367 shelves => $shelves,
368 pagination_bar => pagination_bar(
369 q||, $pager->last_page - $pager->first_page + 1,
370 $page, "page", { op => 'list', category => $category, }
375 $template->param(
376 op => $op,
377 referer => $referer,
378 shelf => $shelf,
379 messages => \@messages,
380 category => $category,
381 print => scalar $query->param('print') || 0,
382 listsview => 1,
385 output_html_with_http_headers $query, $cookie, $template->output;