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
;
33 use Koha
::Virtualshelves
;
34 use Koha
::RecordProcessor
;
38 my $template_name = $query->param('rss') ?
"opac-shelves-rss.tt" : "opac-shelves.tt";
40 # if virtualshelves is disabled, leave immediately
41 if ( ! C4
::Context
->preference('virtualshelves') ) {
42 print $query->redirect("/cgi-bin/koha/errors/404.pl");
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
47 template_name
=> $template_name,
50 authnotrequired
=> ( C4
::Context
->preference("OpacPublic") ?
1 : 0 ),
53 my $op = $query->param('op') || 'list';
54 my $referer = $query->param('referer') || $op;
55 my $category = $query->param('category') || 1;
56 my ( $shelf, $shelfnumber, @messages );
58 if ( $op eq 'add_form' ) {
60 } elsif ( $op eq 'edit_form' ) {
61 $shelfnumber = $query->param('shelfnumber');
62 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
65 $category = $shelf->category;
66 my $patron = GetMember
( 'borrowernumber' => $shelf->owner );
67 $template->param( owner
=> $patron, );
68 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
69 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
73 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
75 } elsif ( $op eq 'add' ) {
76 if ( $loggedinuser ) {
78 $shelf = Koha
::Virtualshelf
->new(
79 { shelfname
=> scalar $query->param('shelfname'),
80 sortfield
=> scalar $query->param('sortfield'),
81 category
=> scalar $query->param('category') || 1,
82 allow_add
=> scalar $query->param('allow_add'),
83 allow_delete_own
=> scalar $query->param('allow_delete_own'),
84 allow_delete_other
=> scalar $query->param('allow_delete_other'),
85 owner
=> scalar $loggedinuser,
89 $shelfnumber = $shelf->shelfnumber;
92 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
93 } elsif ( not $shelf ) {
94 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
96 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
100 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
103 } elsif ( $op eq 'edit' ) {
104 $shelfnumber = $query->param('shelfnumber');
105 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
108 if ( $shelf->can_be_managed( $loggedinuser ) ) {
109 $shelf->shelfname( $query->param('shelfname') );
110 $shelf->sortfield( $query->param('sortfield') );
111 $shelf->allow_add( $query->param('allow_add') );
112 $shelf->allow_delete_own( $query->param('allow_delete_own') );
113 $shelf->allow_delete_other( $query->param('allow_delete_other') );
114 $shelf->category( $query->param('category') );
115 eval { $shelf->store };
118 push @messages, { type
=> 'error', code
=> 'error_on_update' };
121 push @messages, { type
=> 'message', code
=> 'success_on_update' };
124 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
127 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
129 } elsif ( $op eq 'delete' ) {
130 $shelfnumber = $query->param('shelfnumber');
131 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
133 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
134 eval { $shelf->delete; };
136 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
138 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
141 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
144 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
147 } elsif ( $op eq 'remove_share' ) {
148 $shelfnumber = $query->param('shelfnumber');
149 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
151 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
153 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
154 } elsif ( $removed ) {
155 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
157 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
160 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
164 } elsif ( $op eq 'add_biblio' ) {
165 $shelfnumber = $query->param('shelfnumber');
166 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
168 if( my $barcode = $query->param('barcode') ) {
169 my $item = GetItem
( 0, $barcode);
170 if (defined $item && $item->{itemnumber
}) {
171 my $biblio = GetBiblioFromItemNumber
( $item->{itemnumber
} );
172 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
173 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber
}, $loggedinuser ); };
175 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
177 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
179 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
182 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
185 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
189 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
192 } elsif ( $op eq 'remove_biblios' ) {
193 $shelfnumber = $query->param('shelfnumber');
194 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
195 my @biblionumber = $query->multi_param('biblionumber');
197 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
198 my $number_of_biblios_removed = eval {
199 $shelf->remove_biblios(
201 biblionumbers
=> \
@biblionumber,
202 borrowernumber
=> $loggedinuser,
207 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
208 } elsif ( $number_of_biblios_removed ) {
209 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
211 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
214 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
217 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
222 if ( $op eq 'view' ) {
223 $shelfnumber ||= $query->param('shelfnumber');
224 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
226 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
227 $category = $shelf->category;
228 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
229 my $direction = $query->param('direction') || 'asc';
230 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
232 unless ( $query->param('print') or $query->param('rss') ) {
233 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
234 $page = ( $query->param('page') ?
$query->param('page') : 1 );
236 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
237 my $contents = $shelf->get_contents->search(
240 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
243 order_by
=> { "-$direction" => $order_by },
247 # get biblionumbers stored in the cart
249 if(my $cart_list = $query->cookie('bib_list')){
250 @cart_list = split(/\//, $cart_list);
253 my $borrower = GetMember
( borrowernumber
=> $loggedinuser );
255 # Lists display falls back to search results configuration
256 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
257 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
258 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
260 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
262 while ( my $content = $contents->next ) {
263 my $biblionumber = $content->biblionumber;
264 my $this_item = GetBiblioData
($biblionumber);
265 my $record = GetMarcBiblio
($biblionumber);
266 my $framework = GetFrameworkCode
( $biblionumber );
267 $record_processor->options({
269 frameworkcode
=> $framework
271 $record_processor->process($record);
274 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
275 1, undef, $sysxml, $xslfile, $lang);
278 my $marcflavour = C4
::Context
->preference("marcflavour");
279 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
280 my $itemtypeinfo = getitemtypeinfo
( $itemtype, 'opac' );
281 $this_item->{imageurl
} = $itemtypeinfo->{imageurl
};
282 $this_item->{description
} = $itemtypeinfo->{description
};
283 $this_item->{notforloan
} = $itemtypeinfo->{notforloan
};
284 $this_item->{'coins'} = GetCOinSBiblio
($record);
285 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
286 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
287 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
288 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
289 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
291 unless ( defined $this_item->{size
} ) {
293 #TT has problems with size
294 $this_item->{size
} = q
||;
297 # Getting items infos for location display
298 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
299 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
301 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
302 $this_item->{TagLoop
} = get_tags
({
303 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
304 limit
=> C4
::Context
->preference('TagsShowOnList'),
308 $this_item->{allow_onshelf_holds
} = C4
::Reserves
::OnShelfHoldsAllowed
($this_item, $borrower);
311 if ( grep {$_ eq $biblionumber} @cart_list) {
312 $this_item->{incart
} = 1;
315 if ( $query->param('rss') ) {
316 $this_item->{title
} = $content->biblionumber->title;
317 $this_item->{author
} = $content->biblionumber->author;
320 $this_item->{biblionumber
} = $biblionumber;
321 push @items, $this_item;
325 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
326 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
327 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
328 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
329 sortfield
=> $sortfield,
330 itemsloop
=> \
@items,
331 sortfield
=> $sortfield,
332 direction
=> $direction,
335 my $pager = $contents->pager;
337 pagination_bar
=> pagination_bar
(
338 q
||, $pager->last_page - $pager->first_page + 1,
339 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
344 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
348 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
352 if ( $op eq 'list' ) {
354 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
355 if ( $category == 1 ) {
356 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
358 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
361 my $pager = $shelves->pager;
364 pagination_bar
=> pagination_bar
(
365 q
||, $pager->last_page - $pager->first_page + 1,
366 $page, "page", { op
=> 'list', category
=> $category, }
375 messages
=> \
@messages,
376 category
=> $category,
377 print => scalar $query->param('print') || 0,
381 output_html_with_http_headers
$query, $cookie, $template->output;