Bug 18999: (17.05.x followup) Koha::Acquisition::Order is not an object yet
[koha.git] / opac / opac-shelves.pl
blobecbbc6c367a28ce3f05c49ae775586a722413086
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Biblio;
25 use C4::Koha;
26 use C4::Items;
27 use C4::Members;
28 use C4::Output;
29 use C4::Tags qw( get_tags );
30 use C4::XSLT;
32 use Koha::Biblioitems;
33 use Koha::Virtualshelves;
34 use Koha::RecordProcessor;
36 my $query = new CGI;
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");
43 exit;
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
47 template_name => $template_name,
48 query => $query,
49 type => "opac",
50 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
51 });
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' ) {
59 # Nothing to do
60 } elsif ( $op eq 'edit_form' ) {
61 $shelfnumber = $query->param('shelfnumber');
62 $shelf = Koha::Virtualshelves->find($shelfnumber);
64 if ( $shelf ) {
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' };
70 $op = 'list';
72 } else {
73 push @messages, { type => 'error', code => 'does_not_exist' };
75 } elsif ( $op eq 'add' ) {
76 if ( $loggedinuser ) {
77 eval {
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,
88 $shelf->store;
89 $shelfnumber = $shelf->shelfnumber;
91 if ($@) {
92 push @messages, { type => 'error', code => ref($@), msg => $@ };
93 } elsif ( not $shelf ) {
94 push @messages, { type => 'error', code => 'error_on_insert' };
95 } else {
96 push @messages, { type => 'message', code => 'success_on_insert' };
97 $op = 'view';
99 } else {
100 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
101 $op = 'list';
103 } elsif ( $op eq 'edit' ) {
104 $shelfnumber = $query->param('shelfnumber');
105 $shelf = Koha::Virtualshelves->find($shelfnumber);
106 if ( $shelf ) {
107 $op = $referer;
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 };
119 if ($@) {
120 push @messages, { type => 'error', code => 'error_on_update' };
121 $op = 'edit_form';
122 } else {
123 push @messages, { type => 'message', code => 'success_on_update' };
125 } else {
126 push @messages, { type => 'error', code => 'unauthorized_on_update' };
128 } else {
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);
134 if ($shelf) {
135 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
136 eval { $shelf->delete; };
137 if ($@) {
138 push @messages, { type => 'error', code => ref($@), msg => $@ };
139 } else {
140 push @messages, { type => 'message', code => 'success_on_delete' };
142 } else {
143 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
145 } else {
146 push @messages, { type => 'error', code => 'does_not_exist' };
148 $op = $referer;
149 } elsif ( $op eq 'remove_share' ) {
150 $shelfnumber = $query->param('shelfnumber');
151 $shelf = Koha::Virtualshelves->find($shelfnumber);
152 if ($shelf) {
153 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
154 if ($@) {
155 push @messages, { type => 'error', code => ref($@), msg => $@ };
156 } elsif ( $removed ) {
157 push @messages, { type => 'message', code => 'success_on_remove_share' };
158 } else {
159 push @messages, { type => 'error', code => 'error_on_remove_share' };
161 } else {
162 push @messages, { type => 'error', code => 'does_not_exist' };
164 $op = $referer;
166 } elsif ( $op eq 'add_biblio' ) {
167 $shelfnumber = $query->param('shelfnumber');
168 $shelf = Koha::Virtualshelves->find($shelfnumber);
169 if ($shelf) {
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 ); };
176 if ($@) {
177 push @messages, { type => 'error', code => ref($@), msg => $@ };
178 } elsif ( $added ) {
179 push @messages, { type => 'message', code => 'success_on_add_biblio' };
180 } else {
181 push @messages, { type => 'message', code => 'error_on_add_biblio' };
183 } else {
184 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
186 } else {
187 push @messages, { type => 'error', code => 'item_does_not_exist' };
190 } else {
191 push @messages, { type => 'error', code => 'does_not_exist' };
193 $op = $referer;
194 } elsif ( $op eq 'remove_biblios' ) {
195 $shelfnumber = $query->param('shelfnumber');
196 $shelf = Koha::Virtualshelves->find($shelfnumber);
197 my @biblionumber = $query->multi_param('biblionumber');
198 if ($shelf) {
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,
208 if ($@) {
209 push @messages, { type => 'error', code => ref($@), msg => $@ };
210 } elsif ( $number_of_biblios_removed ) {
211 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
212 } else {
213 push @messages, { type => 'error', code => 'no_biblio_removed' };
215 } else {
216 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
218 } else {
219 push @messages, { type => 'error', code => 'does_not_exist' };
221 $op = 'view';
224 if ( $op eq 'view' ) {
225 $shelfnumber ||= $query->param('shelfnumber');
226 $shelf = Koha::Virtualshelves->find($shelfnumber);
227 if ( $shelf ) {
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';
234 my ( $page, $rows );
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' } } ],
244 page => $page,
245 rows => $rows,
246 order_by => { "-$direction" => $order_by },
250 # get biblionumbers stored in the cart
251 my @cart_list;
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' });
264 my @items;
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({
271 interface => 'opac',
272 frameworkcode => $framework
274 $record_processor->process($record);
276 if ( $xslfile ) {
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;
322 $template->param(
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,
331 if ( $page ) {
332 my $pager = $contents->pager;
333 $template->param(
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, }
340 } else {
341 push @messages, { type => 'error', code => 'unauthorized_on_view' };
342 undef $shelf;
344 } else {
345 push @messages, { type => 'error', code => 'does_not_exist' };
349 if ( $op eq 'list' ) {
350 my $shelves;
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, });
354 } else {
355 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
358 my $pager = $shelves->pager;
359 $template->param(
360 shelves => $shelves,
361 pagination_bar => pagination_bar(
362 q||, $pager->last_page - $pager->first_page + 1,
363 $page, "page", { op => 'list', category => $category, }
368 $template->param(
369 op => $op,
370 referer => $referer,
371 shelf => $shelf,
372 messages => \@messages,
373 category => $category,
374 print => scalar $query->param('print') || 0,
375 listsview => 1,
378 output_html_with_http_headers $query, $cookie, $template->output;