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
;
36 use Koha
::Virtualshelves
;
37 use Koha
::RecordProcessor
;
39 use constant ANYONE
=> 2;
43 my $template_name = $query->param('rss') ?
"opac-shelves-rss.tt" : "opac-shelves.tt";
45 # if virtualshelves is disabled, leave immediately
46 if ( ! C4
::Context
->preference('virtualshelves') ) {
47 print $query->redirect("/cgi-bin/koha/errors/404.pl");
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
52 template_name
=> $template_name,
55 authnotrequired
=> ( C4
::Context
->preference("OpacPublic") ?
1 : 0 ),
58 my $op = $query->param('op') || 'list';
59 my $referer = $query->param('referer') || $op;
60 my $category = $query->param('category') || 1;
61 my ( $shelf, $shelfnumber, @messages );
63 if ( $op eq 'add_form' ) {
65 $shelf = { allow_change_from_owner
=> 1 };
66 } elsif ( $op eq 'edit_form' ) {
67 $shelfnumber = $query->param('shelfnumber');
68 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
71 $category = $shelf->category;
72 my $patron = Koha
::Patrons
->find( $shelf->owner );
73 $template->param( owner
=> $patron, );
74 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
75 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
79 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
81 } elsif ( $op eq 'add' ) {
82 if ( $loggedinuser ) {
83 my $allow_changes_from = $query->param('allow_changes_from');
85 $shelf = Koha
::Virtualshelf
->new(
86 { shelfname
=> scalar $query->param('shelfname'),
87 sortfield
=> scalar $query->param('sortfield'),
88 category
=> scalar $query->param('category') || 1,
89 allow_change_from_owner
=> $allow_changes_from > 0,
90 allow_change_from_others
=> $allow_changes_from == ANYONE
,
91 owner
=> scalar $loggedinuser,
95 $shelfnumber = $shelf->shelfnumber;
98 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
99 } elsif ( not $shelf ) {
100 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
102 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
106 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
109 } elsif ( $op eq 'edit' ) {
110 $shelfnumber = $query->param('shelfnumber');
111 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
114 my $sortfield = $query->param('sortfield');
115 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
116 if ( $shelf->can_be_managed( $loggedinuser ) ) {
117 $shelf->shelfname( scalar $query->param('shelfname') );
118 $shelf->sortfield( $sortfield );
119 my $allow_changes_from = $query->param('allow_changes_from');
120 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
121 $shelf->allow_change_from_others( $allow_changes_from == ANYONE
);
122 $shelf->category( scalar $query->param('category') );
123 eval { $shelf->store };
126 push @messages, { type
=> 'error', code
=> 'error_on_update' };
129 push @messages, { type
=> 'message', code
=> 'success_on_update' };
132 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
135 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
137 } elsif ( $op eq 'delete' ) {
138 $shelfnumber = $query->param('shelfnumber');
139 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
141 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
142 eval { $shelf->delete; };
144 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
146 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
149 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
152 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
155 } elsif ( $op eq 'remove_share' ) {
156 $shelfnumber = $query->param('shelfnumber');
157 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
159 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
161 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
162 } elsif ( $removed ) {
163 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
165 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
168 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
172 } elsif ( $op eq 'add_biblio' ) {
173 $shelfnumber = $query->param('shelfnumber');
174 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
176 if( my $barcode = $query->param('barcode') ) {
177 my $item = GetItem
( 0, $barcode);
178 if (defined $item && $item->{itemnumber
}) {
179 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
180 my $added = eval { $shelf->add_biblio( $item->{biblionumber
}, $loggedinuser ); };
182 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
184 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
186 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
189 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
192 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
196 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
199 } elsif ( $op eq 'remove_biblios' ) {
200 $shelfnumber = $query->param('shelfnumber');
201 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
202 my @biblionumber = $query->multi_param('biblionumber');
204 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
205 my $number_of_biblios_removed = eval {
206 $shelf->remove_biblios(
208 biblionumbers
=> \
@biblionumber,
209 borrowernumber
=> $loggedinuser,
214 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
215 } elsif ( $number_of_biblios_removed ) {
216 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
218 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
221 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
224 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
229 if ( $op eq 'view' ) {
230 $shelfnumber ||= $query->param('shelfnumber');
231 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
233 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
234 $category = $shelf->category;
235 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
236 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
237 my $direction = $query->param('direction') || 'asc';
238 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
240 unless ( $query->param('print') or $query->param('rss') ) {
241 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
242 $page = ( $query->param('page') ?
$query->param('page') : 1 );
244 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
245 my $contents = $shelf->get_contents->search(
248 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
251 order_by
=> { "-$direction" => $order_by },
255 # get biblionumbers stored in the cart
257 if(my $cart_list = $query->cookie('bib_list')){
258 @cart_list = split(/\//, $cart_list);
261 my $patron = Koha
::Patrons
->find( $loggedinuser );
263 # Lists display falls back to search results configuration
264 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
265 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
266 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
268 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
270 while ( my $content = $contents->next ) {
271 my $biblionumber = $content->biblionumber;
272 my $this_item = GetBiblioData
($biblionumber);
273 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
274 my $framework = GetFrameworkCode
( $biblionumber );
275 $record_processor->options({
277 frameworkcode
=> $framework
279 $record_processor->process($record);
282 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
283 1, undef, $sysxml, $xslfile, $lang);
286 my $marcflavour = C4
::Context
->preference("marcflavour");
287 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
288 $itemtype = Koha
::ItemTypes
->find( $itemtype );
290 $this_item->{imageurl
} = C4
::Koha
::getitemtypeimagelocation
( 'opac', $itemtype->imageurl );
291 $this_item->{description
} = $itemtype->description; #FIXME Should not it be translated_description?
292 $this_item->{notforloan
} = $itemtype->notforloan;
294 $this_item->{'coins'} = GetCOinSBiblio
($record);
295 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
296 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
297 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
298 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
299 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
301 unless ( defined $this_item->{size
} ) {
303 #TT has problems with size
304 $this_item->{size
} = q
||;
307 # Getting items infos for location display
308 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
309 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
311 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
312 $this_item->{TagLoop
} = get_tags
({
313 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
314 limit
=> C4
::Context
->preference('TagsShowOnList'),
318 $this_item->{allow_onshelf_holds
} = C4
::Reserves
::OnShelfHoldsAllowed
($this_item, $patron);
321 if ( grep {$_ eq $biblionumber} @cart_list) {
322 $this_item->{incart
} = 1;
325 $this_item->{biblionumber
} = $biblionumber;
326 push @items, $this_item;
330 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
331 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
332 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
333 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
334 itemsloop
=> \
@items,
335 sortfield
=> $sortfield,
336 direction
=> $direction,
339 my $pager = $contents->pager;
341 pagination_bar
=> pagination_bar
(
342 q
||, $pager->last_page - $pager->first_page + 1,
343 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
348 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
352 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
356 if ( $op eq 'list' ) {
358 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
359 if ( $category == 1 ) {
360 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
362 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
365 my $pager = $shelves->pager;
368 pagination_bar
=> pagination_bar
(
369 q
||, $pager->last_page - $pager->first_page + 1,
370 $page, "page", { op
=> 'list', category
=> $category, }
379 messages
=> \
@messages,
380 category
=> $category,
381 print => scalar $query->param('print') || 0,
385 output_html_with_http_headers
$query, $cookie, $template->output;