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>.
29 use C4::Tags qw( get_tags );
32 use Koha::Biblioitems;
35 use Koha::Virtualshelves;
36 use Koha::RecordProcessor;
38 use constant ANYONE => 2;
42 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
44 # if virtualshelves is disabled, leave immediately
45 if ( ! C4::Context->preference('virtualshelves') ) {
46 print $query->redirect("/cgi-bin/koha/errors/404.pl");
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
51 template_name => $template_name,
54 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
57 my $op = $query->param('op') || 'list';
58 my $referer = $query->param('referer') || $op;
59 my $category = $query->param('category') || 1;
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 $category = $shelf->category;
71 my $patron = GetMember( 'borrowernumber' => $shelf->owner );
72 $template->param( owner => $patron, );
73 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
74 push @messages, { type => 'error', code => 'unauthorized_on_update' };
78 push @messages, { type => 'error', code => 'does_not_exist' };
80 } elsif ( $op eq 'add' ) {
81 if ( $loggedinuser ) {
82 my $allow_changes_from = $query->param('allow_changes_from');
84 $shelf = Koha::Virtualshelf->new(
85 { shelfname => scalar $query->param('shelfname'),
86 sortfield => scalar $query->param('sortfield'),
87 category => scalar $query->param('category') || 1,
88 allow_change_from_owner => $allow_changes_from > 0,
89 allow_change_from_others => $allow_changes_from == ANYONE,
90 owner => scalar $loggedinuser,
94 $shelfnumber = $shelf->shelfnumber;
97 push @messages, { type => 'error', code => ref($@), msg => $@ };
98 } elsif ( not $shelf ) {
99 push @messages, { type => 'error', code => 'error_on_insert' };
101 push @messages, { type => 'message', code => 'success_on_insert' };
105 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
108 } elsif ( $op eq 'edit' ) {
109 $shelfnumber = $query->param('shelfnumber');
110 $shelf = Koha::Virtualshelves->find($shelfnumber);
113 my $sortfield = $query->param('sortfield');
114 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
115 if ( $shelf->can_be_managed( $loggedinuser ) ) {
116 $shelf->shelfname( scalar $query->param('shelfname') );
117 $shelf->sortfield( $sortfield );
118 my $allow_changes_from = $query->param('allow_changes_from');
119 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
120 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
121 $shelf->category( scalar $query->param('category') );
122 eval { $shelf->store };
125 push @messages, { type => 'error', code => 'error_on_update' };
128 push @messages, { type => 'message', code => 'success_on_update' };
131 push @messages, { type => 'error', code => 'unauthorized_on_update' };
134 push @messages, { type => 'error', code => 'does_not_exist' };
136 } elsif ( $op eq 'delete' ) {
137 $shelfnumber = $query->param('shelfnumber');
138 $shelf = Koha::Virtualshelves->find($shelfnumber);
140 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
141 eval { $shelf->delete; };
143 push @messages, { type => 'error', code => ref($@), msg => $@ };
145 push @messages, { type => 'message', code => 'success_on_delete' };
148 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
151 push @messages, { type => 'error', code => 'does_not_exist' };
154 } elsif ( $op eq 'remove_share' ) {
155 $shelfnumber = $query->param('shelfnumber');
156 $shelf = Koha::Virtualshelves->find($shelfnumber);
158 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
160 push @messages, { type => 'error', code => ref($@), msg => $@ };
161 } elsif ( $removed ) {
162 push @messages, { type => 'message', code => 'success_on_remove_share' };
164 push @messages, { type => 'error', code => 'error_on_remove_share' };
167 push @messages, { type => 'error', code => 'does_not_exist' };
171 } elsif ( $op eq 'add_biblio' ) {
172 $shelfnumber = $query->param('shelfnumber');
173 $shelf = Koha::Virtualshelves->find($shelfnumber);
175 if( my $barcode = $query->param('barcode') ) {
176 my $item = GetItem( 0, $barcode);
177 if (defined $item && $item->{itemnumber}) {
178 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
179 my $added = eval { $shelf->add_biblio( $item->{biblionumber}, $loggedinuser ); };
181 push @messages, { type => 'error', code => ref($@), msg => $@ };
183 push @messages, { type => 'message', code => 'success_on_add_biblio' };
185 push @messages, { type => 'message', code => 'error_on_add_biblio' };
188 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
191 push @messages, { type => 'error', code => 'item_does_not_exist' };
195 push @messages, { type => 'error', code => 'does_not_exist' };
198 } elsif ( $op eq 'remove_biblios' ) {
199 $shelfnumber = $query->param('shelfnumber');
200 $shelf = Koha::Virtualshelves->find($shelfnumber);
201 my @biblionumber = $query->multi_param('biblionumber');
203 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
204 my $number_of_biblios_removed = eval {
205 $shelf->remove_biblios(
207 biblionumbers => \@biblionumber,
208 borrowernumber => $loggedinuser,
213 push @messages, { type => 'error', code => ref($@), msg => $@ };
214 } elsif ( $number_of_biblios_removed ) {
215 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
217 push @messages, { type => 'error', code => 'no_biblio_removed' };
220 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
223 push @messages, { type => 'error', code => 'does_not_exist' };
228 if ( $op eq 'view' ) {
229 $shelfnumber ||= $query->param('shelfnumber');
230 $shelf = Koha::Virtualshelves->find($shelfnumber);
232 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
233 $category = $shelf->category;
234 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
235 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
236 my $direction = $query->param('direction') || 'asc';
237 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
239 unless ( $query->param('print') or $query->param('rss') ) {
240 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
241 $page = ( $query->param('page') ? $query->param('page') : 1 );
243 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
244 my $contents = $shelf->get_contents->search(
247 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
250 order_by => { "-$direction" => $order_by },
254 # get biblionumbers stored in the cart
256 if(my $cart_list = $query->cookie('bib_list')){
257 @cart_list = split(/\//, $cart_list);
260 my $borrower = GetMember( borrowernumber => $loggedinuser );
262 # Lists display falls back to search results configuration
263 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
264 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
265 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
267 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
269 while ( my $content = $contents->next ) {
270 my $biblionumber = $content->biblionumber;
271 my $this_item = GetBiblioData($biblionumber);
272 my $record = GetMarcBiblio($biblionumber);
273 my $framework = GetFrameworkCode( $biblionumber );
274 $record_processor->options({
276 frameworkcode => $framework
278 $record_processor->process($record);
281 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
282 1, undef, $sysxml, $xslfile, $lang);
285 my $marcflavour = C4::Context->preference("marcflavour");
286 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
287 $itemtype = Koha::ItemTypes->find( $itemtype );
289 $this_item->{imageurl} = C4::Koha::getitemtypeimagelocation( 'opac', $itemtype->imageurl );
290 $this_item->{description} = $itemtype->description; #FIXME Should not it be translated_description?
291 $this_item->{notforloan} = $itemtype->notforloan;
293 $this_item->{'coins'} = GetCOinSBiblio($record);
294 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
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;
310 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
311 $this_item->{TagLoop} = get_tags({
312 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
313 limit => C4::Context->preference('TagsShowOnList'),
317 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
320 if ( grep {$_ eq $biblionumber} @cart_list) {
321 $this_item->{incart} = 1;
324 $this_item->{biblionumber} = $biblionumber;
325 push @items, $this_item;
329 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
330 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
331 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
332 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
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 => 'error', code => 'does_not_exist' };
355 if ( $op eq 'list' ) {
357 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
358 if ( $category == 1 ) {
359 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
361 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
364 my $pager = $shelves->pager;
367 pagination_bar => pagination_bar(
368 q||, $pager->last_page - $pager->first_page + 1,
369 $page, "page", { op => 'list', category => $category, }
378 messages => \@messages,
379 category => $category,
380 print => scalar $query->param('print') || 0,
384 output_html_with_http_headers $query, $cookie, $template->output;