Bug 17081 Incorrect comparison operator used in edifactmsgs.pl
[koha.git] / opac / opac-shelves.pl
blob54d93a1af1bf8f6919f1feadb58b7dcc757cc7e2
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;
21 use CGI qw ( -utf8 );
22 use C4::Auth;
23 use C4::Biblio;
24 use C4::Koha;
25 use C4::Items;
26 use C4::Members;
27 use C4::Output;
28 use C4::Tags qw( get_tags );
29 use C4::XSLT;
30 use Koha::Virtualshelves;
32 my $query = new CGI;
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");
39 exit;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
43 template_name => $template_name,
44 query => $query,
45 type => "opac",
46 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
47 });
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' ) {
55 # Nothing to do
56 } elsif ( $op eq 'edit_form' ) {
57 $shelfnumber = $query->param('shelfnumber');
58 $shelf = Koha::Virtualshelves->find($shelfnumber);
60 if ( $shelf ) {
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' };
66 $op = 'list';
68 } else {
69 push @messages, { type => 'error', code => 'does_not_exist' };
71 } elsif ( $op eq 'add' ) {
72 if ( $loggedinuser ) {
73 eval {
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,
84 $shelf->store;
85 $shelfnumber = $shelf->shelfnumber;
87 if ($@) {
88 push @messages, { type => 'error', code => ref($@), msg => $@ };
89 } elsif ( not $shelf ) {
90 push @messages, { type => 'error', code => 'error_on_insert' };
91 } else {
92 push @messages, { type => 'message', code => 'success_on_insert' };
93 $op = 'view';
95 } else {
96 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
97 $op = 'list';
99 } elsif ( $op eq 'edit' ) {
100 $shelfnumber = $query->param('shelfnumber');
101 $shelf = Koha::Virtualshelves->find($shelfnumber);
102 if ( $shelf ) {
103 $op = $referer;
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 };
113 if ($@) {
114 push @messages, { type => 'error', code => 'error_on_update' };
115 $op = 'edit_form';
116 } else {
117 push @messages, { type => 'message', code => 'success_on_update' };
119 } else {
120 push @messages, { type => 'error', code => 'unauthorized_on_update' };
122 } else {
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);
128 if ($shelf) {
129 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
130 eval { $shelf->delete; };
131 if ($@) {
132 push @messages, { type => 'error', code => ref($@), msg => $@ };
133 } else {
134 push @messages, { type => 'message', code => 'success_on_delete' };
136 } else {
137 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
139 } else {
140 push @messages, { type => 'error', code => 'does_not_exist' };
142 $op = $referer;
143 } elsif ( $op eq 'remove_share' ) {
144 $shelfnumber = $query->param('shelfnumber');
145 $shelf = Koha::Virtualshelves->find($shelfnumber);
146 if ($shelf) {
147 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
148 if ($@) {
149 push @messages, { type => 'error', code => ref($@), msg => $@ };
150 } elsif ( $removed ) {
151 push @messages, { type => 'message', code => 'success_on_remove_share' };
152 } else {
153 push @messages, { type => 'error', code => 'error_on_remove_share' };
155 } else {
156 push @messages, { type => 'error', code => 'does_not_exist' };
158 $op = $referer;
160 } elsif ( $op eq 'add_biblio' ) {
161 $shelfnumber = $query->param('shelfnumber');
162 $shelf = Koha::Virtualshelves->find($shelfnumber);
163 if ($shelf) {
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 ); };
170 if ($@) {
171 push @messages, { type => 'error', code => ref($@), msg => $@ };
172 } elsif ( $added ) {
173 push @messages, { type => 'message', code => 'success_on_add_biblio' };
174 } else {
175 push @messages, { type => 'message', code => 'error_on_add_biblio' };
177 } else {
178 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
180 } else {
181 push @messages, { type => 'error', code => 'item_does_not_exist' };
184 } else {
185 push @messages, { type => 'error', code => 'does_not_exist' };
187 $op = $referer;
188 } elsif ( $op eq 'remove_biblios' ) {
189 $shelfnumber = $query->param('shelfnumber');
190 $shelf = Koha::Virtualshelves->find($shelfnumber);
191 my @biblionumber = $query->multi_param('biblionumber');
192 if ($shelf) {
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,
202 if ($@) {
203 push @messages, { type => 'error', code => ref($@), msg => $@ };
204 } elsif ( $number_of_biblios_removed ) {
205 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
206 } else {
207 push @messages, { type => 'error', code => 'no_biblio_removed' };
209 } else {
210 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
212 } else {
213 push @messages, { type => 'error', code => 'does_not_exist' };
215 $op = 'view';
218 if ( $op eq 'view' ) {
219 $shelfnumber ||= $query->param('shelfnumber');
220 $shelf = Koha::Virtualshelves->find($shelfnumber);
221 if ( $shelf ) {
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';
227 my ( $page, $rows );
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' } } ],
237 page => $page,
238 rows => $rows,
239 order_by => { "-$direction" => $order_by },
243 # get biblionumbers stored in the cart
244 my @cart_list;
245 if(my $cart_list = $query->cookie('bib_list')){
246 @cart_list = split(/\//, $cart_list);
249 my $borrower = GetMember( borrowernumber => $loggedinuser );
251 # Lists display falls back to search results configuration
252 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
253 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
254 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
256 my @items;
257 while ( my $content = $contents->next ) {
258 my $biblionumber = $content->biblionumber->biblionumber;
259 my $this_item = GetBiblioData($biblionumber);
260 my $record = GetMarcBiblio($biblionumber);
262 if ( $xslfile ) {
263 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
264 1, undef, $sysxml, $xslfile, $lang);
267 my $marcflavour = C4::Context->preference("marcflavour");
268 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'opac' );
269 $this_item->{imageurl} = $itemtypeinfo->{imageurl};
270 $this_item->{description} = $itemtypeinfo->{description};
271 $this_item->{notforloan} = $itemtypeinfo->{notforloan};
272 $this_item->{'coins'} = GetCOinSBiblio($record);
273 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
274 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
275 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
276 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
277 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
279 unless ( defined $this_item->{size} ) {
281 #TT has problems with size
282 $this_item->{size} = q||;
285 # Getting items infos for location display
286 my @items_infos = &GetItemsLocationInfo( $biblionumber );
287 $this_item->{'ITEM_RESULTS'} = \@items_infos;
289 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
290 $this_item->{TagLoop} = get_tags({
291 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
292 limit => C4::Context->preference('TagsShowOnList'),
296 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
299 if ( grep {$_ eq $biblionumber} @cart_list) {
300 $this_item->{incart} = 1;
303 if ( $query->param('rss') ) {
304 $this_item->{title} = $content->biblionumber->title;
305 $this_item->{author} = $content->biblionumber->author;
308 $this_item->{biblionumber} = $biblionumber;
309 push @items, $this_item;
312 $template->param(
313 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
314 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
315 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
316 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
317 sortfield => $sortfield,
318 itemsloop => \@items,
319 sortfield => $sortfield,
320 direction => $direction,
322 if ( $page ) {
323 my $pager = $contents->pager;
324 $template->param(
325 pagination_bar => pagination_bar(
326 q||, $pager->last_page - $pager->first_page + 1,
327 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
331 } else {
332 push @messages, { type => 'error', code => 'unauthorized_on_view' };
334 } else {
335 push @messages, { type => 'error', code => 'does_not_exist' };
339 if ( $op eq 'list' ) {
340 my $shelves;
341 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
342 if ( $category == 1 ) {
343 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
344 } else {
345 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
348 my $pager = $shelves->pager;
349 $template->param(
350 shelves => $shelves,
351 pagination_bar => pagination_bar(
352 q||, $pager->last_page - $pager->first_page + 1,
353 $page, "page", { op => 'list', category => $category, }
358 $template->param(
359 op => $op,
360 referer => $referer,
361 shelf => $shelf,
362 messages => \@messages,
363 category => $category,
364 print => scalar $query->param('print') || 0,
365 listsview => 1,
368 output_html_with_http_headers $query, $cookie, $template->output;