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>.
22 use C4::Auth qw( get_template_and_user haspermission );
23 use C4::Circulation qw( barcodedecode );
28 GetNormalizedOCLCNumber
32 use C4::Output qw( pagination_bar output_html_with_http_headers output_and_exit_if_error );
33 use C4::XSLT qw( XSLTParse4Display );
36 use Koha::Biblioitems;
39 use Koha::CsvProfiles;
41 use Koha::Virtualshelves;
43 use constant ANYONE => 2;
44 use constant STAFF => 3;
45 use constant PERMITTED => 4;
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50 { template_name => "virtualshelves/shelves.tt",
53 flagsrequired => { catalogue => 1 },
57 my $op = $query->param('op') || 'list';
58 my $referer = $query->param('referer') || $op;
59 my $public = $query->param('public') ? 1 : 0;
60 my ( $shelf, $shelfnumber, @messages, $allow_transfer );
62 # PART1: Perform a few actions
63 if ( $op eq 'add_form' ) {
65 $shelf = { allow_change_from_owner => 1 };
66 } elsif ( $op eq 'edit_form' ) {
67 output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
68 $shelfnumber = $query->param('shelfnumber');
69 $shelf = Koha::Virtualshelves->find($shelfnumber);
72 $public = $shelf->public;
73 my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
74 $template->param( owner => $patron, );
75 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
76 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
80 push @messages, { type => 'alert', code => 'does_not_exist' };
82 } elsif ( $op eq 'add' ) {
83 output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
84 my $allow_changes_from = $query->param('allow_changes_from');
86 $shelf = Koha::Virtualshelf->new(
87 { shelfname => scalar $query->param('shelfname'),
88 sortfield => scalar $query->param('sortfield'),
90 allow_change_from_owner => $allow_changes_from > 0,
91 allow_change_from_others => $allow_changes_from == ANYONE,
92 allow_change_from_staff => $allow_changes_from == STAFF,
93 allow_change_from_permitted_staff => $allow_changes_from == PERMITTED,
94 owner => scalar $query->param('owner'),
98 $shelfnumber = $shelf->shelfnumber;
101 push @messages, { type => 'alert', code => ref($@), msg => $@ };
102 } elsif ( not $shelf ) {
103 push @messages, { type => 'alert', code => 'error_on_insert' };
106 push @messages, { type => 'message', code => 'success_on_insert' };
109 } elsif ( $op eq 'edit' ) {
110 output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
111 $shelfnumber = $query->param('shelfnumber');
112 $shelf = Koha::Virtualshelves->find($shelfnumber);
116 my $sortfield = $query->param('sortfield');
117 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
118 if ( $shelf->can_be_managed( $loggedinuser ) ) {
119 $shelf->shelfname( scalar $query->param('shelfname') );
120 $shelf->sortfield( $sortfield );
121 my $allow_changes_from = $query->param('allow_changes_from');
122 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
123 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
124 $shelf->allow_change_from_staff( $allow_changes_from == STAFF );
125 $shelf->allow_change_from_permitted_staff( $allow_changes_from == PERMITTED );
126 $shelf->public( scalar $query->param('public') );
127 eval { $shelf->store };
130 push @messages, { type => 'alert', code => 'error_on_update' };
133 push @messages, { type => 'message', code => 'success_on_update' };
136 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
139 push @messages, { type => 'alert', code => 'does_not_exist' };
141 } elsif ( $op eq 'delete' ) {
142 output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
143 $shelfnumber = $query->param('shelfnumber');
144 $shelf = Koha::Virtualshelves->find($shelfnumber);
146 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
147 eval { $shelf->delete; };
149 push @messages, { type => 'alert', code => ref($@), msg => $@ };
151 push @messages, { type => 'message', code => 'success_on_delete' };
154 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
157 push @messages, { type => 'alert', code => 'does_not_exist' };
160 } elsif ( $op eq 'add_biblio' ) {
161 output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
162 $shelfnumber = $query->param('shelfnumber');
163 $shelf = Koha::Virtualshelves->find($shelfnumber);
165 if( my $barcodes = $query->param('barcodes') ) {
166 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
167 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
168 foreach my $barcode (@barcodes){
169 $barcode = barcodedecode( $barcode ) if $barcode;
170 next if $barcode eq '';
171 my $item = Koha::Items->find({barcode => $barcode});
173 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
175 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
177 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
179 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
182 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
186 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
189 if ( my $biblionumbers = $query->param('biblionumbers') ) {
190 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
191 my @biblionumbers = split /\n/, $biblionumbers;
192 foreach my $biblionumber (@biblionumbers) {
193 $biblionumber =~ s/\r$//; # strip any naughty return chars
194 next if $biblionumber eq '';
195 my $biblio = Koha::Biblios->find($biblionumber);
196 if (defined $biblio) {
197 my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
199 push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
201 push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
203 push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
206 push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
210 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
214 push @messages, { type => 'alert', code => 'does_not_exist' };
217 } elsif ( $op eq 'remove_biblios' ) {
218 output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' });
219 $shelfnumber = $query->param('shelfnumber');
220 $shelf = Koha::Virtualshelves->find($shelfnumber);
221 my @biblionumbers = $query->multi_param('biblionumber');
223 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
224 my $number_of_biblios_removed = eval {
225 $shelf->remove_biblios(
227 biblionumbers => \@biblionumbers,
228 borrowernumber => $loggedinuser,
233 push @messages, { type => 'alert', code => ref($@), msg => $@ };
234 } elsif ( $number_of_biblios_removed ) {
235 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
237 push @messages, { type => 'alert', code => 'no_biblio_removed' };
240 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
243 push @messages, { type => 'alert', code => 'does_not_exist' };
246 } elsif ( $op eq 'transfer' ) {
247 $shelfnumber = $query->param('shelfnumber');
248 $shelf = Koha::Virtualshelves->find($shelfnumber) if $shelfnumber;
249 my $new_owner = $query->param('new_owner'); # is a borrowernumber
250 my $error_code = $shelf
251 ? $shelf->cannot_be_transferred({ by => $loggedinuser, to => $new_owner, interface => 'intranet' })
254 if( !$new_owner && $error_code eq 'missing_to_parameter' ) {
256 } elsif( $error_code ) {
257 push @messages, { type => 'error', code => $error_code };
260 $shelf->owner($new_owner)->store;
265 # PART2: After a possible action, further prepare form
266 if ( $op eq 'view' ) {
267 $shelfnumber ||= $query->param('shelfnumber');
268 $shelf = Koha::Virtualshelves->find($shelfnumber);
270 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
271 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
272 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
273 my $direction = $query->param('direction') || 'asc';
274 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
276 unless ( $query->param('print') ) {
277 $rows = C4::Context->preference('numSearchResults') || 20;
278 $page = ( $query->param('page') ? $query->param('page') : 1 );
281 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
282 my $contents = $shelf->get_contents->search(
285 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
288 order_by => { "-$direction" => $order_by },
293 while ( my $content = $contents->next ) {
295 my $biblionumber = $content->biblionumber;
296 my $biblio = Koha::Biblios->find($biblionumber);
297 my $record = $biblio->metadata->record;
299 $this_item->{XSLTBloc} = XSLTParse4Display(
301 biblionumber => $biblionumber,
303 xsl_syspref => 'XSLTListsDisplay',
308 my $marcflavour = C4::Context->preference("marcflavour");
309 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
310 $itemtype = Koha::ItemTypes->find( $itemtype );
311 $this_item->{title} = $biblio->title;
312 $this_item->{subtitle} = $biblio->subtitle;
313 $this_item->{medium} = $biblio->medium;
314 $this_item->{part_number} = $biblio->part_number;
315 $this_item->{part_name} = $biblio->part_name;
316 $this_item->{author} = $biblio->author;
317 $this_item->{dateadded} = $content->dateadded;
318 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
319 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
320 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
321 $this_item->{'coins'} = $biblio->get_coins;
322 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
323 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
324 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
325 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
327 unless ( defined $this_item->{size} ) {
329 #TT has problems with size
330 $this_item->{size} = q||;
333 # Getting items infos for location display
334 my $items = $biblio->items;
335 $this_item->{'ITEM_RESULTS'} = $items;
336 $this_item->{biblionumber} = $biblionumber;
337 push @items, $this_item;
340 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
342 borrowernumber => $loggedinuser,
347 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
349 borrowernumber => $loggedinuser,
356 add_to_some_private_shelves => $some_private_shelves,
357 add_to_some_public_shelves => $some_public_shelves,
358 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
359 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
360 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
361 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
362 sortfield => $sortfield,
363 itemsloop => \@items,
364 sortfield => $sortfield,
365 direction => $direction,
368 my $pager = $contents->pager;
370 pagination_bar => pagination_bar(
371 q||, $pager->last_page - $pager->first_page + 1,
372 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
377 push @messages, { type => 'error', code => 'unauthorized_on_view' };
381 push @messages, { type => 'alert', code => 'does_not_exist' };
383 } elsif( $op eq 'list' ) {
384 $allow_transfer = haspermission( C4::Context->userenv->{id}, { lists => 'edit_public_lists' } ) ? 1 : 0;
385 # this check only serves for button display
392 messages => \@messages,
394 print => scalar $query->param('print') || 0,
395 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })->as_list ],
396 allow_transfer => $allow_transfer,
399 output_html_with_http_headers $query, $cookie, $template->output;