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 );
31 use Koha
::Virtualshelves
;
32 use Koha
::RecordProcessor
;
36 my $template_name = $query->param('rss') ?
"opac-shelves-rss.tt" : "opac-shelves.tt";
38 # if virtualshelves is disabled, leave immediately
39 if ( ! C4
::Context
->preference('virtualshelves') ) {
40 print $query->redirect("/cgi-bin/koha/errors/404.pl");
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
({
45 template_name
=> $template_name,
48 authnotrequired
=> ( C4
::Context
->preference("OpacPublic") ?
1 : 0 ),
51 my $op = $query->param('op') || 'list';
52 my $referer = $query->param('referer') || $op;
53 my $category = $query->param('category') || 1;
54 my ( $shelf, $shelfnumber, @messages );
56 if ( $op eq 'add_form' ) {
58 } elsif ( $op eq 'edit_form' ) {
59 $shelfnumber = $query->param('shelfnumber');
60 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
63 $category = $shelf->category;
64 my $patron = GetMember
( 'borrowernumber' => $shelf->owner );
65 $template->param( owner
=> $patron, );
66 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
67 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
71 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
73 } elsif ( $op eq 'add' ) {
74 if ( $loggedinuser ) {
76 $shelf = Koha
::Virtualshelf
->new(
77 { shelfname
=> scalar $query->param('shelfname'),
78 sortfield
=> scalar $query->param('sortfield'),
79 category
=> scalar $query->param('category') || 1,
80 allow_add
=> scalar $query->param('allow_add'),
81 allow_delete_own
=> scalar $query->param('allow_delete_own'),
82 allow_delete_other
=> scalar $query->param('allow_delete_other'),
83 owner
=> scalar $loggedinuser,
87 $shelfnumber = $shelf->shelfnumber;
90 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
91 } elsif ( not $shelf ) {
92 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
94 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
98 push @messages, { type
=> 'error', code
=> 'unauthorized_on_insert' };
101 } elsif ( $op eq 'edit' ) {
102 $shelfnumber = $query->param('shelfnumber');
103 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
106 if ( $shelf->can_be_managed( $loggedinuser ) ) {
107 $shelf->shelfname( $query->param('shelfname') );
108 $shelf->sortfield( $query->param('sortfield') );
109 $shelf->allow_add( $query->param('allow_add') );
110 $shelf->allow_delete_own( $query->param('allow_delete_own') );
111 $shelf->allow_delete_other( $query->param('allow_delete_other') );
112 $shelf->category( $query->param('category') );
113 eval { $shelf->store };
116 push @messages, { type
=> 'error', code
=> 'error_on_update' };
119 push @messages, { type
=> 'message', code
=> 'success_on_update' };
122 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
125 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
127 } elsif ( $op eq 'delete' ) {
128 $shelfnumber = $query->param('shelfnumber');
129 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
131 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
132 eval { $shelf->delete; };
134 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
136 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
139 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
142 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
145 } elsif ( $op eq 'remove_share' ) {
146 $shelfnumber = $query->param('shelfnumber');
147 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
149 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
151 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
152 } elsif ( $removed ) {
153 push @messages, { type
=> 'message', code
=> 'success_on_remove_share' };
155 push @messages, { type
=> 'error', code
=> 'error_on_remove_share' };
158 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
162 } elsif ( $op eq 'add_biblio' ) {
163 $shelfnumber = $query->param('shelfnumber');
164 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
166 if( my $barcode = $query->param('barcode') ) {
167 my $item = GetItem
( 0, $barcode);
168 if (defined $item && $item->{itemnumber
}) {
169 my $biblio = GetBiblioFromItemNumber
( $item->{itemnumber
} );
170 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
171 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber
}, $loggedinuser ); };
173 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
175 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
177 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
180 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
183 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
187 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
190 } elsif ( $op eq 'remove_biblios' ) {
191 $shelfnumber = $query->param('shelfnumber');
192 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
193 my @biblionumber = $query->multi_param('biblionumber');
195 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
196 my $number_of_biblios_removed = eval {
197 $shelf->remove_biblios(
199 biblionumbers
=> \
@biblionumber,
200 borrowernumber
=> $loggedinuser,
205 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
206 } elsif ( $number_of_biblios_removed ) {
207 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
209 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
212 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
215 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
220 if ( $op eq 'view' ) {
221 $shelfnumber ||= $query->param('shelfnumber');
222 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
224 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
225 $category = $shelf->category;
226 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
227 my $direction = $query->param('direction') || 'asc';
228 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
230 unless ( $query->param('print') or $query->param('rss') ) {
231 $rows = C4
::Context
->preference('OPACnumSearchResults') || 20;
232 $page = ( $query->param('page') ?
$query->param('page') : 1 );
234 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
235 my $contents = $shelf->get_contents->search(
238 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
241 order_by
=> { "-$direction" => $order_by },
245 # get biblionumbers stored in the cart
247 if(my $cart_list = $query->cookie('bib_list')){
248 @cart_list = split(/\//, $cart_list);
251 my $borrower = GetMember
( borrowernumber
=> $loggedinuser );
253 # Lists display falls back to search results configuration
254 my $xslfile = C4
::Context
->preference('OPACXSLTListsDisplay');
255 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
256 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
258 my $record_processor = Koha
::RecordProcessor
->new({ filters
=> 'ViewPolicy' });
260 while ( my $content = $contents->next ) {
261 my $biblionumber = $content->biblionumber->biblionumber;
262 my $this_item = GetBiblioData
($biblionumber);
263 my $record = GetMarcBiblio
($biblionumber);
264 my $framework = GetFrameworkCode
( $biblionumber );
265 $record_processor->options({
267 frameworkcode
=> $framework
269 $record_processor->process($record);
272 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "OPACXSLTListsDisplay",
273 1, undef, $sysxml, $xslfile, $lang);
276 my $marcflavour = C4
::Context
->preference("marcflavour");
277 my $itemtypeinfo = getitemtypeinfo
( $content->biblionumber->biblioitems->first->itemtype, 'opac' );
278 $this_item->{imageurl
} = $itemtypeinfo->{imageurl
};
279 $this_item->{description
} = $itemtypeinfo->{description
};
280 $this_item->{notforloan
} = $itemtypeinfo->{notforloan
};
281 $this_item->{'coins'} = GetCOinSBiblio
($record);
282 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
283 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
284 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
285 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
286 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
288 unless ( defined $this_item->{size
} ) {
290 #TT has problems with size
291 $this_item->{size
} = q
||;
294 # Getting items infos for location display
295 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
296 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
298 if (C4
::Context
->preference('TagsEnabled') and C4
::Context
->preference('TagsShowOnList')) {
299 $this_item->{TagLoop
} = get_tags
({
300 biblionumber
=> $biblionumber, approved
=>1, 'sort'=>'-weight',
301 limit
=> C4
::Context
->preference('TagsShowOnList'),
305 $this_item->{allow_onshelf_holds
} = C4
::Reserves
::OnShelfHoldsAllowed
($this_item, $borrower);
308 if ( grep {$_ eq $biblionumber} @cart_list) {
309 $this_item->{incart
} = 1;
312 if ( $query->param('rss') ) {
313 $this_item->{title
} = $content->biblionumber->title;
314 $this_item->{author
} = $content->biblionumber->author;
317 $this_item->{biblionumber
} = $biblionumber;
318 push @items, $this_item;
322 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
323 can_delete_shelf
=> $shelf->can_be_deleted($loggedinuser),
324 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
325 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
326 sortfield
=> $sortfield,
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;