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;
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 owner => scalar $query->param('owner'),
92 $shelfnumber = $shelf->shelfnumber;
95 push @messages, { type => 'alert', code => ref($@), msg => $@ };
96 } elsif ( not $shelf ) {
97 push @messages, { type => 'alert', code => 'error_on_insert' };
100 push @messages, { type => 'message', code => 'success_on_insert' };
103 } elsif ( $op eq 'edit' ) {
104 $shelfnumber = $query->param('shelfnumber');
105 $shelf = Koha::Virtualshelves->find($shelfnumber);
109 my $sortfield = $query->param('sortfield');
110 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
111 if ( $shelf->can_be_managed( $loggedinuser ) ) {
112 $shelf->shelfname( scalar $query->param('shelfname') );
113 $shelf->sortfield( $sortfield );
114 my $allow_changes_from = $query->param('allow_changes_from');
115 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
116 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
117 $shelf->public( scalar $query->param('public') );
118 eval { $shelf->store };
121 push @messages, { type => 'alert', code => 'error_on_update' };
124 push @messages, { type => 'message', code => 'success_on_update' };
127 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
130 push @messages, { type => 'alert', code => 'does_not_exist' };
132 } elsif ( $op eq 'delete' ) {
133 $shelfnumber = $query->param('shelfnumber');
134 $shelf = Koha::Virtualshelves->find($shelfnumber);
136 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
137 eval { $shelf->delete; };
139 push @messages, { type => 'alert', code => ref($@), msg => $@ };
141 push @messages, { type => 'message', code => 'success_on_delete' };
144 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
147 push @messages, { type => 'alert', code => 'does_not_exist' };
150 } elsif ( $op eq 'add_biblio' ) {
151 $shelfnumber = $query->param('shelfnumber');
152 $shelf = Koha::Virtualshelves->find($shelfnumber);
154 if( my $barcodes = $query->param('barcodes') ) {
155 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
156 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
157 foreach my $barcode (@barcodes){
158 $barcode = barcodedecode( $barcode ) if $barcode;
159 next if $barcode eq '';
160 my $item = Koha::Items->find({barcode => $barcode});
162 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
164 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
166 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
168 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
171 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
175 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
178 if ( my $biblionumbers = $query->param('biblionumbers') ) {
179 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
180 my @biblionumbers = split /\n/, $biblionumbers;
181 foreach my $biblionumber (@biblionumbers) {
182 $biblionumber =~ s/\r$//; # strip any naughty return chars
183 next if $biblionumber eq '';
184 my $biblio = Koha::Biblios->find($biblionumber);
185 if (defined $biblio) {
186 my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
188 push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
190 push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
192 push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
195 push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
199 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
203 push @messages, { type => 'alert', code => 'does_not_exist' };
206 } elsif ( $op eq 'remove_biblios' ) {
207 $shelfnumber = $query->param('shelfnumber');
208 $shelf = Koha::Virtualshelves->find($shelfnumber);
209 my @biblionumbers = $query->multi_param('biblionumber');
211 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
212 my $number_of_biblios_removed = eval {
213 $shelf->remove_biblios(
215 biblionumbers => \@biblionumbers,
216 borrowernumber => $loggedinuser,
221 push @messages, { type => 'alert', code => ref($@), msg => $@ };
222 } elsif ( $number_of_biblios_removed ) {
223 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
225 push @messages, { type => 'alert', code => 'no_biblio_removed' };
228 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
231 push @messages, { type => 'alert', code => 'does_not_exist' };
236 if ( $op eq 'view' ) {
237 $shelfnumber ||= $query->param('shelfnumber');
238 $shelf = Koha::Virtualshelves->find($shelfnumber);
240 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
241 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
242 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
243 my $direction = $query->param('direction') || 'asc';
244 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
246 unless ( $query->param('print') ) {
247 $rows = C4::Context->preference('numSearchResults') || 20;
248 $page = ( $query->param('page') ? $query->param('page') : 1 );
251 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
252 my $contents = $shelf->get_contents->search(
255 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
258 order_by => { "-$direction" => $order_by },
263 while ( my $content = $contents->next ) {
265 my $biblionumber = $content->biblionumber;
266 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
268 $this_item->{XSLTBloc} = XSLTParse4Display(
270 biblionumber => $biblionumber,
272 xsl_syspref => 'XSLTListsDisplay',
277 my $marcflavour = C4::Context->preference("marcflavour");
278 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
279 $itemtype = Koha::ItemTypes->find( $itemtype );
280 my $biblio = Koha::Biblios->find( $content->biblionumber );
281 $this_item->{title} = $biblio->title;
282 $this_item->{subtitle} = $biblio->subtitle;
283 $this_item->{medium} = $biblio->medium;
284 $this_item->{part_number} = $biblio->part_number;
285 $this_item->{part_name} = $biblio->part_name;
286 $this_item->{author} = $biblio->author;
287 $this_item->{dateadded} = $content->dateadded;
288 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
289 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
290 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
291 $this_item->{'coins'} = $biblio->get_coins;
292 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
293 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
294 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
295 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
297 unless ( defined $this_item->{size} ) {
299 #TT has problems with size
300 $this_item->{size} = q||;
303 # Getting items infos for location display
304 my @items_infos = &GetItemsLocationInfo( $biblionumber );
305 $this_item->{'ITEM_RESULTS'} = \@items_infos;
306 $this_item->{biblionumber} = $biblionumber;
307 push @items, $this_item;
310 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
312 borrowernumber => $loggedinuser,
317 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
319 borrowernumber => $loggedinuser,
326 add_to_some_private_shelves => $some_private_shelves,
327 add_to_some_public_shelves => $some_public_shelves,
328 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
329 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
330 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
331 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
332 sortfield => $sortfield,
333 itemsloop => \@items,
334 sortfield => $sortfield,
335 direction => $direction,
338 my $pager = $contents->pager;
340 pagination_bar => pagination_bar(
341 q||, $pager->last_page - $pager->first_page + 1,
342 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
347 push @messages, { type => 'error', code => 'unauthorized_on_view' };
351 push @messages, { type => 'alert', code => 'does_not_exist' };
359 messages => \@messages,
361 print => scalar $query->param('print') || 0,
362 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
365 output_html_with_http_headers $query, $cookie, $template->output;