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>.
28 use C4
::Tags
qw( get_tags );
30 use Koha
::Virtualshelves
;
34 my $template_name = $query->param('rss') ?
"opac-shelves-rss.tt" : "opac-shelves.tt";
36 # if virtualshelves is disabled, leave immediately
37 if ( ! C4
::Context
->preference('virtualshelves') ) {
38 print $query->redirect("/cgi-bin/koha/errors/404.pl");
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
43 template_name
=> $template_name,
46 authnotrequired
=> ( C4
::Context
->preference("OpacPublic") ?
1 : 0 ),
49 my $op = $query->param('op') || 'list';
50 my $referer = $query->param('referer') || $op;
51 my $category = $query->param('category') || 1;
52 my ( $shelf, $shelfnumber, @messages );
54 if ( $op eq 'add_form' ) {
56 } elsif ( $op eq 'edit_form' ) {
57 $shelfnumber = $query->param('shelfnumber');
58 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
61 $category = $shelf->category;
62 my $patron = GetMember
( 'borrowernumber' => $shelf->owner );
63 $template->param( owner
=> $patron, );
64 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
65 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
69 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
71 } elsif ( $op eq 'add' ) {
72 if ( $loggedinuser ) {
74 $shelf = Koha
::Virtualshelf
->new(
75 { shelfname
=> scalar $query->param('shelfname'),
76 sortfield
=> scalar $query->param('sortfield'),
77 category
=> scalar $query->param('category') || 1,
78 allow_add
=> scalar $query->param('allow_add'),
79 allow_delete_own
=> scalar $query->param('allow_delete_own'),
80 allow_delete_other
=> scalar $query->param('allow_delete_other'),
81 owner
=> scalar $loggedinuser,
85 $shelfnumber = $shelf->shelfnumber;
88 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
89 } elsif ( not $shelf ) {
90 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
92 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
96 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
99 } elsif ( $op eq 'edit' ) {
100 $shelfnumber = $query->param('shelfnumber');
101 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
104 if ( $shelf->can_be_managed( $loggedinuser ) ) {
105 $shelf->shelfname( $query->param('shelfname') );
106 $shelf->sortfield( $query->param('sortfield') );
107 $shelf->allow_add( $query->param('allow_add') );
108 $shelf->allow_delete_own( $query->param('allow_delete_own') );
109 $shelf->allow_delete_other( $query->param('allow_delete_other') );
110 $shelf->category( $query->param('category') );
111 eval { $shelf->store };
114 push @messages, { type
=> 'error', code
=> 'error_on_update' };
117 push @messages, { type
=> 'message', code
=> 'success_on_update' };
120 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
123 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
125 } elsif ( $op eq 'delete' ) {
126 $shelfnumber = $query->param('shelfnumber');
127 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
129 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
130 eval { $shelf->delete; };
132 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
134 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
137 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
140 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
143 } elsif ( $op eq 'remove_share' ) {
144 $shelfnumber = $query->param('shelfnumber');
145 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
147 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
149 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
150 } elsif ( $removed ) {
151 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
153 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
156 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
160 } elsif ( $op eq 'add_biblio' ) {
161 $shelfnumber = $query->param('shelfnumber');
162 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
164 if( my $barcode = $query->param('barcode') ) {
165 my $item = GetItem
( 0, $barcode);
166 if (defined $item && $item->{itemnumber
}) {
167 my $biblio = GetBiblioFromItemNumber
( $item->{itemnumber
} );
168 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
169 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber
}, $loggedinuser ); };
171 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
173 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
175 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
178 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
181 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
185 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
188 } elsif ( $op eq 'remove_biblios' ) {
189 $shelfnumber = $query->param('shelfnumber');
190 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
191 my @biblionumber = $query->multi_param('biblionumber');
193 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
194 my $number_of_biblios_removed = eval {
195 $shelf->remove_biblios(
197 biblionumbers
=> \
@biblionumber,
198 borrowernumber
=> $loggedinuser,
203 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
204 } elsif ( $number_of_biblios_removed ) {
205 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
207 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
210 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
213 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
218 if ( $op eq 'view' ) {
219 $shelfnumber ||= $query->param('shelfnumber');
220 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
222 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
223 $category = $shelf->category;
224 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
225 my $direction = $query->param('direction') || 'asc';
226 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
228 unless ( $query->param('print') or $query->param('rss') ) {
229 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
230 $page = ( $query->param('page') ?
$query->param('page') : 1 );
232 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
233 my $contents = $shelf->get_contents->search(
236 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
239 order_by
=> { "-$direction" => $order_by },
243 # get biblionumbers stored in the cart
245 if(my $cart_list = $query->cookie('bib_list')){
246 @cart_list = split(/\//, $cart_list);
249 my $borrower = GetMember
( borrowernumber
=> $loggedinuser );
251 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
252 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
253 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
256 while ( my $content = $contents->next ) {
257 my $biblionumber = $content->biblionumber->biblionumber;
258 my $this_item = GetBiblioData
($biblionumber);
259 my $record = GetMarcBiblio
($biblionumber);
262 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
263 1, undef, $sysxml, $xslfile, $lang);
266 my $marcflavour = C4
::Context
->preference("marcflavour");
267 my $itemtypeinfo = getitemtypeinfo
( $content->biblionumber->biblioitems->first->itemtype, 'opac' );
268 $this_item->{imageurl
} = $itemtypeinfo->{imageurl
};
269 $this_item->{description
} = $itemtypeinfo->{description
};
270 $this_item->{notforloan
} = $itemtypeinfo->{notforloan
};
271 $this_item->{'coins'} = GetCOinSBiblio
($record);
272 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
273 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
274 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
275 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
276 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
278 unless ( defined $this_item->{size
} ) {
280 #TT has problems with size
281 $this_item->{size
} = q
||;
284 # Getting items infos for location display
285 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
286 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
288 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
289 $this_item->{TagLoop
} = get_tags
({
290 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
291 limit
=> C4
::Context
->preference('TagsShowOnList'),
295 $this_item->{allow_onshelf_holds
} = C4
::Reserves
::OnShelfHoldsAllowed
($this_item, $borrower);
298 if ( grep {$_ eq $biblionumber} @cart_list) {
299 $this_item->{incart
} = 1;
302 if ( $query->param('rss') ) {
303 $this_item->{title
} = $content->biblionumber->title;
304 $this_item->{author
} = $content->biblionumber->author;
307 $this_item->{biblionumber
} = $biblionumber;
308 push @items, $this_item;
312 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
313 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
314 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
315 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
316 sortfield
=> $sortfield,
317 itemsloop
=> \
@items,
318 sortfield
=> $sortfield,
319 direction
=> $direction,
322 my $pager = $contents->pager;
324 pagination_bar
=> pagination_bar
(
325 q
||, $pager->last_page - $pager->first_page + 1,
326 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
331 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
334 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
338 if ( $op eq 'list' ) {
340 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
341 if ( $category == 1 ) {
342 $shelves = Koha
::Virtualshelves
->get_private_shelves({ page
=> $page, rows
=> $rows, borrowernumber
=> $loggedinuser, });
344 $shelves = Koha
::Virtualshelves
->get_public_shelves({ page
=> $page, rows
=> $rows, });
347 my $pager = $shelves->pager;
350 pagination_bar
=> pagination_bar
(
351 q
||, $pager->last_page - $pager->first_page + 1,
352 $page, "page", { op
=> 'list', category
=> $category, }
361 messages
=> \
@messages,
362 category
=> $category,
363 print => scalar $query->param('print') || 0,
367 output_html_with_http_headers
$query, $cookie, $template->output;