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>.
25 use C4::External::BakerTaylor qw( image_url link_url );
30 use C4::Tags qw( get_tags );
34 use Koha::Biblioitems;
35 use Koha::IssuingRules;
39 use Koha::Virtualshelves;
40 use Koha::RecordProcessor;
42 use constant ANYONE => 2;
46 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
48 # if virtualshelves is disabled, leave immediately
49 if ( ! C4::Context->preference('virtualshelves') ) {
50 print $query->redirect("/cgi-bin/koha/errors/404.pl");
54 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
55 template_name => $template_name,
58 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
61 my $op = $query->param('op') || 'list';
62 my $referer = $query->param('referer') || $op;
64 if ( $query->param('category') && (
65 ($query->param('category') == 1) ||
66 ($query->param('category') == 2)
69 $category = $query->param('category');
72 my ( $shelf, $shelfnumber, @messages );
74 if ( $op eq 'add_form' ) {
76 $shelf = { allow_change_from_owner => 1 };
77 } elsif ( $op eq 'edit_form' ) {
78 $shelfnumber = $query->param('shelfnumber');
79 $shelf = Koha::Virtualshelves->find($shelfnumber);
82 $category = $shelf->category;
83 my $patron = Koha::Patrons->find( $shelf->owner );
84 $template->param( owner => $patron, );
85 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
86 push @messages, { type => 'error', code => 'unauthorized_on_update' };
90 push @messages, { type => 'error', code => 'does_not_exist' };
92 } elsif ( $op eq 'add' ) {
93 if ( $loggedinuser ) {
94 my $allow_changes_from = $query->param('allow_changes_from');
96 $shelf = Koha::Virtualshelf->new(
97 { shelfname => scalar $query->param('shelfname'),
98 sortfield => scalar $query->param('sortfield'),
99 category => $category || 1,
100 allow_change_from_owner => $allow_changes_from > 0,
101 allow_change_from_others => $allow_changes_from == ANYONE,
102 owner => scalar $loggedinuser,
106 $shelfnumber = $shelf->shelfnumber;
109 push @messages, { type => 'error', code => ref($@), msg => $@ };
110 } elsif ( not $shelf ) {
111 push @messages, { type => 'error', code => 'error_on_insert' };
113 push @messages, { type => 'message', code => 'success_on_insert' };
117 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
120 } elsif ( $op eq 'edit' ) {
121 $shelfnumber = $query->param('shelfnumber');
122 $shelf = Koha::Virtualshelves->find($shelfnumber);
125 my $sortfield = $query->param('sortfield');
126 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
127 if ( $shelf->can_be_managed( $loggedinuser ) ) {
128 $shelf->shelfname( scalar $query->param('shelfname') );
129 $shelf->sortfield( $sortfield );
130 my $allow_changes_from = $query->param('allow_changes_from');
131 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
132 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
133 $shelf->category( $category );
134 eval { $shelf->store };
137 push @messages, { type => 'error', code => 'error_on_update' };
140 push @messages, { type => 'message', code => 'success_on_update' };
143 push @messages, { type => 'error', code => 'unauthorized_on_update' };
146 push @messages, { type => 'error', code => 'does_not_exist' };
148 } elsif ( $op eq 'delete' ) {
149 $shelfnumber = $query->param('shelfnumber');
150 $shelf = Koha::Virtualshelves->find($shelfnumber);
152 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
153 eval { $shelf->delete; };
155 push @messages, { type => 'error', code => ref($@), msg => $@ };
157 push @messages, { type => 'message', code => 'success_on_delete' };
160 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
163 push @messages, { type => 'error', code => 'does_not_exist' };
166 } elsif ( $op eq 'remove_share' ) {
167 $shelfnumber = $query->param('shelfnumber');
168 $shelf = Koha::Virtualshelves->find($shelfnumber);
170 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
172 push @messages, { type => 'error', code => ref($@), msg => $@ };
173 } elsif ( $removed ) {
174 push @messages, { type => 'message', code => 'success_on_remove_share' };
176 push @messages, { type => 'error', code => 'error_on_remove_share' };
179 push @messages, { type => 'error', code => 'does_not_exist' };
183 } elsif ( $op eq 'add_biblio' ) {
184 $shelfnumber = $query->param('shelfnumber');
185 $shelf = Koha::Virtualshelves->find($shelfnumber);
187 if( my $barcode = $query->param('barcode') ) {
188 my $item = Koha::Items->find({ barcode => $barcode });
190 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
191 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
193 push @messages, { type => 'error', code => ref($@), msg => $@ };
195 push @messages, { type => 'message', code => 'success_on_add_biblio' };
197 push @messages, { type => 'message', code => 'error_on_add_biblio' };
200 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
203 push @messages, { type => 'error', code => 'item_does_not_exist' };
207 push @messages, { type => 'error', code => 'does_not_exist' };
210 } elsif ( $op eq 'remove_biblios' ) {
211 $shelfnumber = $query->param('shelfnumber');
212 $shelf = Koha::Virtualshelves->find($shelfnumber);
213 my @biblionumber = $query->multi_param('biblionumber');
215 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
216 my $number_of_biblios_removed = eval {
217 $shelf->remove_biblios(
219 biblionumbers => \@biblionumber,
220 borrowernumber => $loggedinuser,
225 push @messages, { type => 'error', code => ref($@), msg => $@ };
226 } elsif ( $number_of_biblios_removed ) {
227 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
229 push @messages, { type => 'error', code => 'no_biblio_removed' };
232 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
235 push @messages, { type => 'error', code => 'does_not_exist' };
240 if ( $op eq 'view' ) {
241 $shelfnumber ||= $query->param('shelfnumber');
242 $shelf = Koha::Virtualshelves->find($shelfnumber);
244 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
245 $category = $shelf->category;
246 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
247 $sortfield = 'title' unless grep $_ eq $sortfield, qw( title author copyrightdate itemcallnumber dateadded );
248 my $direction = $query->param('direction') || 'asc';
249 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
251 unless ( $query->param('print') or $query->param('rss') ) {
252 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
253 $page = ( $query->param('page') ? $query->param('page') : 1 );
255 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
256 my $contents = $shelf->get_contents->search(
259 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
262 order_by => { "-$direction" => $order_by },
266 # get biblionumbers stored in the cart
268 if(my $cart_list = $query->cookie('bib_list')){
269 @cart_list = split(/\//, $cart_list);
272 my $patron = Koha::Patrons->find( $loggedinuser );
274 # Lists display falls back to search results configuration
275 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
276 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
277 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
279 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
281 while ( my $content = $contents->next ) {
282 my $biblionumber = $content->biblionumber;
283 my $this_item = GetBiblioData($biblionumber);
284 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
285 my $framework = GetFrameworkCode( $biblionumber );
286 my $biblio = Koha::Biblios->find( $biblionumber );
287 $record_processor->options({
289 frameworkcode => $framework
291 $record_processor->process($record);
295 anonymous_session => ($loggedinuser) ? 0 : 1
297 $this_item->{XSLTBloc} = XSLTParse4Display(
298 $biblionumber, $record,
299 "OPACXSLTListsDisplay", 1,
306 my $marcflavour = C4::Context->preference("marcflavour");
307 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
308 $itemtype = Koha::ItemTypes->find( $itemtype );
310 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
311 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
312 $this_item->{notforloan} = $itemtype->notforloan;
314 $this_item->{'coins'} = $biblio->get_coins;
315 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
316 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
317 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
318 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
320 unless ( defined $this_item->{size} ) {
322 #TT has problems with size
323 $this_item->{size} = q||;
326 # Getting items infos for location display
327 my @items_infos = &GetItemsLocationInfo( $biblionumber );
328 $this_item->{'ITEM_RESULTS'} = \@items_infos;
330 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
331 $this_item->{TagLoop} = get_tags({
332 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
333 limit => C4::Context->preference('TagsShowOnList'),
337 my $items = $biblio->items;
338 while ( my $item = $items->next ) {
339 $this_item->{allow_onshelf_holds} = Koha::IssuingRules->get_onshelfholds_policy( { item => $item, patron => $patron } );
340 last if $this_item->{allow_onshelf_holds};
343 if ( grep {$_ eq $biblionumber} @cart_list) {
344 $this_item->{incart} = 1;
347 $this_item->{biblio_object} = $biblio;
348 $this_item->{biblionumber} = $biblionumber;
349 push @items, $this_item;
353 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
354 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
355 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
356 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
357 itemsloop => \@items,
358 sortfield => $sortfield,
359 direction => $direction,
362 my $pager = $contents->pager;
364 pagination_bar => pagination_bar(
365 q||, $pager->last_page - $pager->first_page + 1,
366 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
371 push @messages, { type => 'error', code => 'unauthorized_on_view' };
375 push @messages, { type => 'error', code => 'does_not_exist' };
379 if ( $op eq 'list' ) {
381 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
382 if ( $category == 1 ) {
383 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
385 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
388 my $pager = $shelves->pager;
391 pagination_bar => pagination_bar(
392 q||, $pager->last_page - $pager->first_page + 1,
393 $page, "page", { op => 'list', category => $category, }
402 messages => \@messages,
403 category => ($category == 1 || $category == 2) ? $category : "",
404 print => scalar $query->param('print') || 0,
408 my $content_type = $query->param('rss')? 'rss' : 'html';
409 output_with_http_headers $query, $cookie, $template->output, $content_type;