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 );
23 use C4::Biblio qw( GetMarcBiblio );
27 GetNormalizedOCLCNumber
30 use C4::Items qw( GetItemsLocationInfo );
32 use C4::Output qw( pagination_bar output_html_with_http_headers );
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;
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49 { template_name => "virtualshelves/shelves.tt",
52 flagsrequired => { catalogue => 1 },
56 my $op = $query->param('op') || 'list';
57 my $referer = $query->param('referer') || $op;
58 my $public = $query->param('public') ? 1 : 0;
59 my ( $shelf, $shelfnumber, @messages );
61 if ( $op eq 'add_form' ) {
63 $shelf = { allow_change_from_owner => 1 };
64 } elsif ( $op eq 'edit_form' ) {
65 $shelfnumber = $query->param('shelfnumber');
66 $shelf = Koha::Virtualshelves->find($shelfnumber);
69 $public = $shelf->public;
70 my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
71 $template->param( owner => $patron, );
72 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
73 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
77 push @messages, { type => 'alert', code => 'does_not_exist' };
79 } elsif ( $op eq 'add' ) {
80 my $allow_changes_from = $query->param('allow_changes_from');
82 $shelf = Koha::Virtualshelf->new(
83 { shelfname => scalar $query->param('shelfname'),
84 sortfield => scalar $query->param('sortfield'),
86 allow_change_from_owner => $allow_changes_from > 0,
87 allow_change_from_others => $allow_changes_from == ANYONE,
88 allow_change_from_staff => $allow_changes_from == STAFF,
89 owner => scalar $query->param('owner'),
93 $shelfnumber = $shelf->shelfnumber;
96 push @messages, { type => 'alert', code => ref($@), msg => $@ };
97 } elsif ( not $shelf ) {
98 push @messages, { type => 'alert', code => 'error_on_insert' };
101 push @messages, { type => 'message', code => 'success_on_insert' };
104 } elsif ( $op eq 'edit' ) {
105 $shelfnumber = $query->param('shelfnumber');
106 $shelf = Koha::Virtualshelves->find($shelfnumber);
110 my $sortfield = $query->param('sortfield');
111 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
112 if ( $shelf->can_be_managed( $loggedinuser ) ) {
113 $shelf->shelfname( scalar $query->param('shelfname') );
114 $shelf->sortfield( $sortfield );
115 my $allow_changes_from = $query->param('allow_changes_from');
116 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
117 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
118 $shelf->allow_change_from_staff( $allow_changes_from == STAFF );
119 $shelf->public( scalar $query->param('public') );
120 eval { $shelf->store };
123 push @messages, { type => 'alert', code => 'error_on_update' };
126 push @messages, { type => 'message', code => 'success_on_update' };
129 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
132 push @messages, { type => 'alert', code => 'does_not_exist' };
134 } elsif ( $op eq 'delete' ) {
135 $shelfnumber = $query->param('shelfnumber');
136 $shelf = Koha::Virtualshelves->find($shelfnumber);
138 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
139 eval { $shelf->delete; };
141 push @messages, { type => 'alert', code => ref($@), msg => $@ };
143 push @messages, { type => 'message', code => 'success_on_delete' };
146 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
149 push @messages, { type => 'alert', code => 'does_not_exist' };
152 } elsif ( $op eq 'add_biblio' ) {
153 $shelfnumber = $query->param('shelfnumber');
154 $shelf = Koha::Virtualshelves->find($shelfnumber);
156 if( my $barcodes = $query->param('barcodes') ) {
157 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
158 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
159 foreach my $barcode (@barcodes){
160 $barcode =~ s/\r$//; # strip any naughty return chars
161 next if $barcode eq '';
162 my $item = Koha::Items->find({barcode => $barcode});
164 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
166 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
168 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
170 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
173 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
177 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
180 if ( my $biblionumbers = $query->param('biblionumbers') ) {
181 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
182 my @biblionumbers = split /\n/, $biblionumbers;
183 foreach my $biblionumber (@biblionumbers) {
184 $biblionumber =~ s/\r$//; # strip any naughty return chars
185 next if $biblionumber eq '';
186 my $biblio = Koha::Biblios->find($biblionumber);
187 if (defined $biblio) {
188 my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
190 push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
192 push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
194 push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
197 push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
201 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
205 push @messages, { type => 'alert', code => 'does_not_exist' };
208 } elsif ( $op eq 'remove_biblios' ) {
209 $shelfnumber = $query->param('shelfnumber');
210 $shelf = Koha::Virtualshelves->find($shelfnumber);
211 my @biblionumbers = $query->multi_param('biblionumber');
213 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
214 my $number_of_biblios_removed = eval {
215 $shelf->remove_biblios(
217 biblionumbers => \@biblionumbers,
218 borrowernumber => $loggedinuser,
223 push @messages, { type => 'alert', code => ref($@), msg => $@ };
224 } elsif ( $number_of_biblios_removed ) {
225 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
227 push @messages, { type => 'alert', code => 'no_biblio_removed' };
230 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
233 push @messages, { type => 'alert', code => 'does_not_exist' };
238 if ( $op eq 'view' ) {
239 $shelfnumber ||= $query->param('shelfnumber');
240 $shelf = Koha::Virtualshelves->find($shelfnumber);
242 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
243 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
244 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
245 my $direction = $query->param('direction') || 'asc';
246 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
248 unless ( $query->param('print') ) {
249 $rows = C4::Context->preference('numSearchResults') || 20;
250 $page = ( $query->param('page') ? $query->param('page') : 1 );
253 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
254 my $contents = $shelf->get_contents->search(
257 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
260 order_by => { "-$direction" => $order_by },
265 while ( my $content = $contents->next ) {
267 my $biblionumber = $content->biblionumber;
268 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
270 $this_item->{XSLTBloc} = XSLTParse4Display(
272 biblionumber => $biblionumber,
274 xsl_syspref => 'XSLTListsDisplay',
279 my $marcflavour = C4::Context->preference("marcflavour");
280 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
281 $itemtype = Koha::ItemTypes->find( $itemtype );
282 my $biblio = Koha::Biblios->find( $content->biblionumber );
283 $this_item->{title} = $biblio->title;
284 $this_item->{subtitle} = $biblio->subtitle;
285 $this_item->{medium} = $biblio->medium;
286 $this_item->{part_number} = $biblio->part_number;
287 $this_item->{part_name} = $biblio->part_name;
288 $this_item->{author} = $biblio->author;
289 $this_item->{dateadded} = $content->dateadded;
290 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
291 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
292 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
293 $this_item->{'coins'} = $biblio->get_coins;
294 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
295 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
296 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
297 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
299 unless ( defined $this_item->{size} ) {
301 #TT has problems with size
302 $this_item->{size} = q||;
305 # Getting items infos for location display
306 my @items_infos = &GetItemsLocationInfo( $biblionumber );
307 $this_item->{'ITEM_RESULTS'} = \@items_infos;
308 $this_item->{biblionumber} = $biblionumber;
309 push @items, $this_item;
312 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
314 borrowernumber => $loggedinuser,
319 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
321 borrowernumber => $loggedinuser,
328 add_to_some_private_shelves => $some_private_shelves,
329 add_to_some_public_shelves => $some_public_shelves,
330 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
331 can_remove_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 sortfield => $sortfield,
335 itemsloop => \@items,
336 sortfield => $sortfield,
337 direction => $direction,
340 my $pager = $contents->pager;
342 pagination_bar => pagination_bar(
343 q||, $pager->last_page - $pager->first_page + 1,
344 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
349 push @messages, { type => 'error', code => 'unauthorized_on_view' };
353 push @messages, { type => 'alert', code => 'does_not_exist' };
361 messages => \@messages,
363 print => scalar $query->param('print') || 0,
364 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })->as_list ],
367 output_html_with_http_headers $query, $cookie, $template->output;