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 my $sortfield = $query->param('sortfield');
109 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
110 if ( $shelf->can_be_managed( $loggedinuser ) ) {
111 $shelf->shelfname( scalar $query->param('shelfname') );
112 $shelf->sortfield( $sortfield );
113 $shelf->allow_add( scalar $query->param('allow_add') );
114 $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
115 $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
116 $shelf->category( scalar $query->param('category') );
117 eval { $shelf->store };
120 push @messages, { type
=> 'error', code
=> 'error_on_update' };
123 push @messages, { type
=> 'message', code
=> 'success_on_update' };
126 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
129 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
131 } elsif ( $op eq 'delete' ) {
132 $shelfnumber = $query->param('shelfnumber');
133 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
135 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
136 eval { $shelf->delete; };
138 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
140 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
143 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
146 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
149 } elsif ( $op eq 'remove_share' ) {
150 $shelfnumber = $query->param('shelfnumber');
151 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
153 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
155 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
156 } elsif ( $removed ) {
157 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
159 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
162 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
166 } elsif ( $op eq 'add_biblio' ) {
167 $shelfnumber = $query->param('shelfnumber');
168 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
170 if( my $barcode = $query->param('barcode') ) {
171 my $item = GetItem
( 0, $barcode);
172 if (defined $item && $item->{itemnumber
}) {
173 my $biblio = GetBiblioFromItemNumber
( $item->{itemnumber
} );
174 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
175 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber
}, $loggedinuser ); };
177 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
179 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
181 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
184 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
187 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
191 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
194 } elsif ( $op eq 'remove_biblios' ) {
195 $shelfnumber = $query->param('shelfnumber');
196 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
197 my @biblionumber = $query->multi_param('biblionumber');
199 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
200 my $number_of_biblios_removed = eval {
201 $shelf->remove_biblios(
203 biblionumbers
=> \
@biblionumber,
204 borrowernumber
=> $loggedinuser,
209 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
210 } elsif ( $number_of_biblios_removed ) {
211 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
213 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
216 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
219 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
224 if ( $op eq 'view' ) {
225 $shelfnumber ||= $query->param('shelfnumber');
226 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
228 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
229 $category = $shelf->category;
230 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
231 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
232 my $direction = $query->param('direction') || 'asc';
233 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
235 unless ( $query->param('print') or $query->param('rss') ) {
236 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
237 $page = ( $query->param('page') ?
$query->param('page') : 1 );
239 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
240 my $contents = $shelf->get_contents->search(
243 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
246 order_by
=> { "-$direction" => $order_by },
250 # get biblionumbers stored in the cart
252 if(my $cart_list = $query->cookie('bib_list')){
253 @cart_list = split(/\//, $cart_list);
256 my $borrower = GetMember
( borrowernumber
=> $loggedinuser );
258 # Lists display falls back to search results configuration
259 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
260 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
261 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
263 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
265 while ( my $content = $contents->next ) {
266 my $biblionumber = $content->biblionumber;
267 my $this_item = GetBiblioData
($biblionumber);
268 my $record = GetMarcBiblio
($biblionumber);
269 my $framework = GetFrameworkCode
( $biblionumber );
270 $record_processor->options({
272 frameworkcode
=> $framework
274 $record_processor->process($record);
277 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
278 1, undef, $sysxml, $xslfile, $lang);
281 my $marcflavour = C4
::Context
->preference("marcflavour");
282 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
283 my $itemtypeinfo = getitemtypeinfo
( $itemtype, 'opac' );
284 $this_item->{imageurl
} = $itemtypeinfo->{imageurl
};
285 $this_item->{description
} = $itemtypeinfo->{description
};
286 $this_item->{notforloan
} = $itemtypeinfo->{notforloan
};
287 $this_item->{'coins'} = GetCOinSBiblio
($record);
288 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
289 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
290 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
291 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
292 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
294 unless ( defined $this_item->{size
} ) {
296 #TT has problems with size
297 $this_item->{size
} = q
||;
300 # Getting items infos for location display
301 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
302 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
304 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
305 $this_item->{TagLoop
} = get_tags
({
306 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
307 limit
=> C4
::Context
->preference('TagsShowOnList'),
311 $this_item->{allow_onshelf_holds
} = C4
::Reserves
::OnShelfHoldsAllowed
($this_item, $borrower);
314 if ( grep {$_ eq $biblionumber} @cart_list) {
315 $this_item->{incart
} = 1;
318 $this_item->{biblionumber
} = $biblionumber;
319 push @items, $this_item;
323 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
324 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
325 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
326 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
327 itemsloop
=> \
@items,
328 sortfield
=> $sortfield,
329 direction
=> $direction,
332 my $pager = $contents->pager;
334 pagination_bar
=> pagination_bar
(
335 q
||, $pager->last_page - $pager->first_page + 1,
336 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
341 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
345 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
349 if ( $op eq 'list' ) {
351 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
352 if ( $category == 1 ) {
353 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
355 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
358 my $pager = $shelves->pager;
361 pagination_bar
=> pagination_bar
(
362 q
||, $pager->last_page - $pager->first_page + 1,
363 $page, "page", { op
=> 'list', category
=> $category, }
372 messages
=> \
@messages,
373 category
=> $category,
374 print => scalar $query->param('print') || 0,
378 output_html_with_http_headers
$query, $cookie, $template->output;