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 );
24 use C4::Circulation qw( barcodedecode );
28 GetNormalizedOCLCNumber
31 use C4::Items qw( GetItemsLocationInfo );
33 use C4::Output qw( pagination_bar output_html_with_http_headers );
34 use C4::XSLT qw( XSLTParse4Display );
37 use Koha::Biblioitems;
40 use Koha::CsvProfiles;
42 use Koha::Virtualshelves;
44 use constant ANYONE => 2;
45 use constant STAFF => 3;
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 );
62 if ( $op eq 'add_form' ) {
64 $shelf = { allow_change_from_owner => 1 };
65 } elsif ( $op eq 'edit_form' ) {
66 $shelfnumber = $query->param('shelfnumber');
67 $shelf = Koha::Virtualshelves->find($shelfnumber);
70 $public = $shelf->public;
71 my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
72 $template->param( owner => $patron, );
73 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
74 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
78 push @messages, { type => 'alert', code => 'does_not_exist' };
80 } elsif ( $op eq 'add' ) {
81 my $allow_changes_from = $query->param('allow_changes_from');
83 $shelf = Koha::Virtualshelf->new(
84 { shelfname => scalar $query->param('shelfname'),
85 sortfield => scalar $query->param('sortfield'),
87 allow_change_from_owner => $allow_changes_from > 0,
88 allow_change_from_others => $allow_changes_from == ANYONE,
89 allow_change_from_staff => $allow_changes_from == STAFF,
90 owner => scalar $query->param('owner'),
94 $shelfnumber = $shelf->shelfnumber;
97 push @messages, { type => 'alert', code => ref($@), msg => $@ };
98 } elsif ( not $shelf ) {
99 push @messages, { type => 'alert', code => 'error_on_insert' };
102 push @messages, { type => 'message', code => 'success_on_insert' };
105 } elsif ( $op eq 'edit' ) {
106 $shelfnumber = $query->param('shelfnumber');
107 $shelf = Koha::Virtualshelves->find($shelfnumber);
111 my $sortfield = $query->param('sortfield');
112 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
113 if ( $shelf->can_be_managed( $loggedinuser ) ) {
114 $shelf->shelfname( scalar $query->param('shelfname') );
115 $shelf->sortfield( $sortfield );
116 my $allow_changes_from = $query->param('allow_changes_from');
117 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
118 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
119 $shelf->allow_change_from_staff( $allow_changes_from == STAFF );
120 $shelf->public( scalar $query->param('public') );
121 eval { $shelf->store };
124 push @messages, { type => 'alert', code => 'error_on_update' };
127 push @messages, { type => 'message', code => 'success_on_update' };
130 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
133 push @messages, { type => 'alert', code => 'does_not_exist' };
135 } elsif ( $op eq 'delete' ) {
136 $shelfnumber = $query->param('shelfnumber');
137 $shelf = Koha::Virtualshelves->find($shelfnumber);
139 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
140 eval { $shelf->delete; };
142 push @messages, { type => 'alert', code => ref($@), msg => $@ };
144 push @messages, { type => 'message', code => 'success_on_delete' };
147 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
150 push @messages, { type => 'alert', code => 'does_not_exist' };
153 } elsif ( $op eq 'add_biblio' ) {
154 $shelfnumber = $query->param('shelfnumber');
155 $shelf = Koha::Virtualshelves->find($shelfnumber);
157 if( my $barcodes = $query->param('barcodes') ) {
158 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
159 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
160 foreach my $barcode (@barcodes){
161 $barcode = barcodedecode( $barcode ) if $barcode;
162 next if $barcode eq '';
163 my $item = Koha::Items->find({barcode => $barcode});
165 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
167 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
169 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
171 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
174 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
178 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
181 if ( my $biblionumbers = $query->param('biblionumbers') ) {
182 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
183 my @biblionumbers = split /\n/, $biblionumbers;
184 foreach my $biblionumber (@biblionumbers) {
185 $biblionumber =~ s/\r$//; # strip any naughty return chars
186 next if $biblionumber eq '';
187 my $biblio = Koha::Biblios->find($biblionumber);
188 if (defined $biblio) {
189 my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
191 push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
193 push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
195 push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
198 push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
202 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
206 push @messages, { type => 'alert', code => 'does_not_exist' };
209 } elsif ( $op eq 'remove_biblios' ) {
210 $shelfnumber = $query->param('shelfnumber');
211 $shelf = Koha::Virtualshelves->find($shelfnumber);
212 my @biblionumbers = $query->multi_param('biblionumber');
214 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
215 my $number_of_biblios_removed = eval {
216 $shelf->remove_biblios(
218 biblionumbers => \@biblionumbers,
219 borrowernumber => $loggedinuser,
224 push @messages, { type => 'alert', code => ref($@), msg => $@ };
225 } elsif ( $number_of_biblios_removed ) {
226 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
228 push @messages, { type => 'alert', code => 'no_biblio_removed' };
231 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
234 push @messages, { type => 'alert', code => 'does_not_exist' };
239 if ( $op eq 'view' ) {
240 $shelfnumber ||= $query->param('shelfnumber');
241 $shelf = Koha::Virtualshelves->find($shelfnumber);
243 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
244 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
245 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
246 my $direction = $query->param('direction') || 'asc';
247 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
249 unless ( $query->param('print') ) {
250 $rows = C4::Context->preference('numSearchResults') || 20;
251 $page = ( $query->param('page') ? $query->param('page') : 1 );
254 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
255 my $contents = $shelf->get_contents->search(
258 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
261 order_by => { "-$direction" => $order_by },
266 while ( my $content = $contents->next ) {
268 my $biblionumber = $content->biblionumber;
269 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
271 $this_item->{XSLTBloc} = XSLTParse4Display(
273 biblionumber => $biblionumber,
275 xsl_syspref => 'XSLTListsDisplay',
280 my $marcflavour = C4::Context->preference("marcflavour");
281 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
282 $itemtype = Koha::ItemTypes->find( $itemtype );
283 my $biblio = Koha::Biblios->find( $content->biblionumber );
284 $this_item->{title} = $biblio->title;
285 $this_item->{subtitle} = $biblio->subtitle;
286 $this_item->{medium} = $biblio->medium;
287 $this_item->{part_number} = $biblio->part_number;
288 $this_item->{part_name} = $biblio->part_name;
289 $this_item->{author} = $biblio->author;
290 $this_item->{dateadded} = $content->dateadded;
291 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
292 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
293 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
294 $this_item->{'coins'} = $biblio->get_coins;
295 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
296 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
297 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
298 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
300 unless ( defined $this_item->{size} ) {
302 #TT has problems with size
303 $this_item->{size} = q||;
306 # Getting items infos for location display
307 my @items_infos = &GetItemsLocationInfo( $biblionumber );
308 $this_item->{'ITEM_RESULTS'} = \@items_infos;
309 $this_item->{biblionumber} = $biblionumber;
310 push @items, $this_item;
313 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
315 borrowernumber => $loggedinuser,
320 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
322 borrowernumber => $loggedinuser,
329 add_to_some_private_shelves => $some_private_shelves,
330 add_to_some_public_shelves => $some_public_shelves,
331 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
332 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
333 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
334 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
335 sortfield => $sortfield,
336 itemsloop => \@items,
337 sortfield => $sortfield,
338 direction => $direction,
341 my $pager = $contents->pager;
343 pagination_bar => pagination_bar(
344 q||, $pager->last_page - $pager->first_page + 1,
345 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
350 push @messages, { type => 'error', code => 'unauthorized_on_view' };
354 push @messages, { type => 'alert', code => 'does_not_exist' };
362 messages => \@messages,
364 print => scalar $query->param('print') || 0,
365 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' })->as_list ],
368 output_html_with_http_headers $query, $cookie, $template->output;