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 );
33 use Koha
::Biblioitems
;
37 use Koha
::Virtualshelves
;
38 use Koha
::RecordProcessor
;
40 use constant ANYONE
=> 2;
44 my $template_name = $query->param('rss') ?
"opac-shelves-rss.tt" : "opac-shelves.tt";
46 # if virtualshelves is disabled, leave immediately
47 if ( ! C4
::Context
->preference('virtualshelves') ) {
48 print $query->redirect("/cgi-bin/koha/errors/404.pl");
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
53 template_name
=> $template_name,
56 authnotrequired
=> ( C4
::Context
->preference("OpacPublic") ?
1 : 0 ),
59 my $op = $query->param('op') || 'list';
60 my $referer = $query->param('referer') || $op;
61 my $category = $query->param('category') || 1;
62 my ( $shelf, $shelfnumber, @messages );
64 if ( $op eq 'add_form' ) {
66 $shelf = { allow_change_from_owner
=> 1 };
67 } elsif ( $op eq 'edit_form' ) {
68 $shelfnumber = $query->param('shelfnumber');
69 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
72 $category = $shelf->category;
73 my $patron = Koha
::Patrons
->find( $shelf->owner );
74 $template->param( owner
=> $patron, );
75 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
76 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
80 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
82 } elsif ( $op eq 'add' ) {
83 if ( $loggedinuser ) {
84 my $allow_changes_from = $query->param('allow_changes_from');
86 $shelf = Koha
::Virtualshelf
->new(
87 { shelfname
=> scalar $query->param('shelfname'),
88 sortfield
=> scalar $query->param('sortfield'),
89 category
=> scalar $query->param('category') || 1,
90 allow_change_from_owner
=> $allow_changes_from > 0,
91 allow_change_from_others
=> $allow_changes_from == ANYONE
,
92 owner
=> scalar $loggedinuser,
96 $shelfnumber = $shelf->shelfnumber;
99 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
100 } elsif ( not $shelf ) {
101 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
103 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
107 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
110 } elsif ( $op eq 'edit' ) {
111 $shelfnumber = $query->param('shelfnumber');
112 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
115 my $sortfield = $query->param('sortfield');
116 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
117 if ( $shelf->can_be_managed( $loggedinuser ) ) {
118 $shelf->shelfname( scalar $query->param('shelfname') );
119 $shelf->sortfield( $sortfield );
120 my $allow_changes_from = $query->param('allow_changes_from');
121 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
122 $shelf->allow_change_from_others( $allow_changes_from == ANYONE
);
123 $shelf->category( scalar $query->param('category') );
124 eval { $shelf->store };
127 push @messages, { type
=> 'error', code
=> 'error_on_update' };
130 push @messages, { type
=> 'message', code
=> 'success_on_update' };
133 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
136 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
138 } elsif ( $op eq 'delete' ) {
139 $shelfnumber = $query->param('shelfnumber');
140 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
142 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
143 eval { $shelf->delete; };
145 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
147 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
150 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
153 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
156 } elsif ( $op eq 'remove_share' ) {
157 $shelfnumber = $query->param('shelfnumber');
158 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
160 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
162 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
163 } elsif ( $removed ) {
164 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
166 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
169 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
173 } elsif ( $op eq 'add_biblio' ) {
174 $shelfnumber = $query->param('shelfnumber');
175 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
177 if( my $barcode = $query->param('barcode') ) {
178 my $item = GetItem
( 0, $barcode);
179 if (defined $item && $item->{itemnumber
}) {
180 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
181 my $added = eval { $shelf->add_biblio( $item->{biblionumber
}, $loggedinuser ); };
183 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
185 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
187 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
190 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
193 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
197 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
200 } elsif ( $op eq 'remove_biblios' ) {
201 $shelfnumber = $query->param('shelfnumber');
202 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
203 my @biblionumber = $query->multi_param('biblionumber');
205 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
206 my $number_of_biblios_removed = eval {
207 $shelf->remove_biblios(
209 biblionumbers
=> \
@biblionumber,
210 borrowernumber
=> $loggedinuser,
215 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
216 } elsif ( $number_of_biblios_removed ) {
217 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
219 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
222 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
225 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
230 if ( $op eq 'view' ) {
231 $shelfnumber ||= $query->param('shelfnumber');
232 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
234 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
235 $category = $shelf->category;
236 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
237 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
238 my $direction = $query->param('direction') || 'asc';
239 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
241 unless ( $query->param('print') or $query->param('rss') ) {
242 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
243 $page = ( $query->param('page') ?
$query->param('page') : 1 );
245 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
246 my $contents = $shelf->get_contents->search(
249 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
252 order_by
=> { "-$direction" => $order_by },
256 # get biblionumbers stored in the cart
258 if(my $cart_list = $query->cookie('bib_list')){
259 @cart_list = split(/\//, $cart_list);
262 my $patron = Koha
::Patrons
->find( $loggedinuser );
264 # Lists display falls back to search results configuration
265 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
266 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
267 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
269 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
271 while ( my $content = $contents->next ) {
272 my $biblionumber = $content->biblionumber;
273 my $this_item = GetBiblioData
($biblionumber);
274 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
275 my $framework = GetFrameworkCode
( $biblionumber );
276 my $biblio = Koha
::Biblios
->find( $biblionumber );
277 $record_processor->options({
279 frameworkcode
=> $framework
281 $record_processor->process($record);
284 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
285 1, undef, $sysxml, $xslfile, $lang);
288 my $marcflavour = C4
::Context
->preference("marcflavour");
289 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
290 $itemtype = Koha
::ItemTypes
->find( $itemtype );
292 $this_item->{imageurl
} = C4
::Koha
::getitemtypeimagelocation
( 'opac', $itemtype->imageurl );
293 $this_item->{description
} = $itemtype->description; #FIXME Should not it be translated_description?
294 $this_item->{notforloan
} = $itemtype->notforloan;
296 $this_item->{'coins'} = GetCOinSBiblio
($record);
297 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
298 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
299 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
300 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
301 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
303 unless ( defined $this_item->{size
} ) {
305 #TT has problems with size
306 $this_item->{size
} = q
||;
309 # Getting items infos for location display
310 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
311 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
313 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
314 $this_item->{TagLoop
} = get_tags
({
315 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
316 limit
=> C4
::Context
->preference('TagsShowOnList'),
320 my $items = $biblio->items;
321 while ( my $item = $items->next ) {
322 $this_item->{allow_onshelf_holds
} = C4
::Reserves
::OnShelfHoldsAllowed
($item->unblessed, $patron);
323 last if $this_item->{allow_onshelf_holds
};
326 if ( grep {$_ eq $biblionumber} @cart_list) {
327 $this_item->{incart
} = 1;
330 $this_item->{biblionumber
} = $biblionumber;
331 push @items, $this_item;
335 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
336 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
337 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
338 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
339 itemsloop
=> \
@items,
340 sortfield
=> $sortfield,
341 direction
=> $direction,
344 my $pager = $contents->pager;
346 pagination_bar
=> pagination_bar
(
347 q
||, $pager->last_page - $pager->first_page + 1,
348 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
353 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
357 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
361 if ( $op eq 'list' ) {
363 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
364 if ( $category == 1 ) {
365 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
367 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
370 my $pager = $shelves->pager;
373 pagination_bar
=> pagination_bar
(
374 q
||, $pager->last_page - $pager->first_page + 1,
375 $page, "page", { op
=> 'list', category
=> $category, }
384 messages
=> \
@messages,
385 category
=> $category,
386 print => scalar $query->param('print') || 0,
390 output_html_with_http_headers
$query, $cookie, $template->output;