Bug 15217 Remove redundant duplicate var declaration
[koha.git] / opac / opac-shelves.pl
blob067eb10f1539000ea8263910ba6fb7faaa31a22c
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 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
37 template_name => $template_name,
38 query => $query,
39 type => "opac",
40 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
41 });
43 my $op = $query->param('op') || 'list';
44 my $referer = $query->param('referer') || $op;
45 my $category = $query->param('category') || 1;
46 my ( $shelf, $shelfnumber, @messages );
48 if ( $op eq 'add_form' ) {
49 # Nothing to do
50 } elsif ( $op eq 'edit_form' ) {
51 $shelfnumber = $query->param('shelfnumber');
52 $shelf = Koha::Virtualshelves->find($shelfnumber);
54 if ( $shelf ) {
55 $category = $shelf->category;
56 my $patron = GetMember( 'borrowernumber' => $shelf->owner );
57 $template->param( owner => $patron, );
58 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
59 push @messages, { type => 'error', code => 'unauthorized_on_update' };
60 $op = 'list';
62 } else {
63 push @messages, { type => 'error', code => 'does_not_exist' };
65 } elsif ( $op eq 'add' ) {
66 if ( $loggedinuser ) {
67 eval {
68 $shelf = Koha::Virtualshelf->new(
69 { shelfname => $query->param('shelfname'),
70 sortfield => $query->param('sortfield'),
71 category => $query->param('category') || 1,
72 allow_add => $query->param('allow_add'),
73 allow_delete_own => $query->param('allow_delete_own'),
74 allow_delete_other => $query->param('allow_delete_other'),
75 owner => $loggedinuser,
78 $shelf->store;
79 $shelfnumber = $shelf->shelfnumber;
81 if ($@) {
82 push @messages, { type => 'error', code => ref($@), msg => $@ };
83 } elsif ( not $shelf ) {
84 push @messages, { type => 'error', code => 'error_on_insert' };
85 } else {
86 push @messages, { type => 'message', code => 'success_on_insert' };
87 $op = 'view';
89 } else {
90 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
91 $op = 'list';
93 } elsif ( $op eq 'edit' ) {
94 $shelfnumber = $query->param('shelfnumber');
95 $shelf = Koha::Virtualshelves->find($shelfnumber);
96 if ( $shelf ) {
97 $op = $referer;
98 if ( $shelf->can_be_managed( $loggedinuser ) ) {
99 $shelf->shelfname( $query->param('shelfname') );
100 $shelf->sortfield( $query->param('sortfield') );
101 $shelf->allow_add( $query->param('allow_add') );
102 $shelf->allow_delete_own( $query->param('allow_delete_own') );
103 $shelf->allow_delete_other( $query->param('allow_delete_other') );
104 $shelf->category( $query->param('category') );
105 eval { $shelf->store };
107 if ($@) {
108 push @messages, { type => 'error', code => 'error_on_update' };
109 $op = 'edit_form';
110 } else {
111 push @messages, { type => 'message', code => 'success_on_update' };
113 } else {
114 push @messages, { type => 'error', code => 'unauthorized_on_update' };
116 } else {
117 push @messages, { type => 'error', code => 'does_not_exist' };
119 } elsif ( $op eq 'delete' ) {
120 $shelfnumber = $query->param('shelfnumber');
121 $shelf = Koha::Virtualshelves->find($shelfnumber);
122 if ($shelf) {
123 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
124 eval { $shelf->delete; };
125 if ($@) {
126 push @messages, { type => 'error', code => ref($@), msg => $@ };
127 } else {
128 push @messages, { type => 'message', code => 'success_on_delete' };
130 } else {
131 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
133 } else {
134 push @messages, { type => 'error', code => 'does_not_exist' };
136 $op = $referer;
137 } elsif ( $op eq 'remove_share' ) {
138 $shelfnumber = $query->param('shelfnumber');
139 $shelf = Koha::Virtualshelves->find($shelfnumber);
140 if ($shelf) {
141 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
142 if ($@) {
143 push @messages, { type => 'error', code => ref($@), msg => $@ };
144 } elsif ( $removed ) {
145 push @messages, { type => 'message', code => 'success_on_remove_share' };
146 } else {
147 push @messages, { type => 'error', code => 'error_on_remove_share' };
149 } else {
150 push @messages, { type => 'error', code => 'does_not_exist' };
152 $op = $referer;
154 } elsif ( $op eq 'add_biblio' ) {
155 $shelfnumber = $query->param('shelfnumber');
156 $shelf = Koha::Virtualshelves->find($shelfnumber);
157 if ($shelf) {
158 if( my $barcode = $query->param('barcode') ) {
159 my $item = GetItem( 0, $barcode);
160 if (defined $item && $item->{itemnumber}) {
161 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
162 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
163 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
164 if ($@) {
165 push @messages, { type => 'error', code => ref($@), msg => $@ };
166 } elsif ( $added ) {
167 push @messages, { type => 'message', code => 'success_on_add_biblio' };
168 } else {
169 push @messages, { type => 'message', code => 'error_on_add_biblio' };
171 } else {
172 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
174 } else {
175 push @messages, { type => 'error', code => 'item_does_not_exist' };
178 } else {
179 push @messages, { type => 'error', code => 'does_not_exist' };
181 $op = $referer;
182 } elsif ( $op eq 'remove_biblios' ) {
183 $shelfnumber = $query->param('shelfnumber');
184 $shelf = Koha::Virtualshelves->find($shelfnumber);
185 my @biblionumber = $query->param('biblionumber');
186 if ($shelf) {
187 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
188 my $number_of_biblios_removed = eval {
189 $shelf->remove_biblios(
191 biblionumbers => \@biblionumber,
192 borrowernumber => $loggedinuser,
196 if ($@) {
197 push @messages, { type => 'error', code => ref($@), msg => $@ };
198 } elsif ( $number_of_biblios_removed ) {
199 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
200 } else {
201 push @messages, { type => 'error', code => 'no_biblio_removed' };
203 } else {
204 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
206 } else {
207 push @messages, { type => 'error', code => 'does_not_exist' };
209 $op = 'view';
212 if ( $op eq 'view' ) {
213 $shelfnumber ||= $query->param('shelfnumber');
214 $shelf = Koha::Virtualshelves->find($shelfnumber);
215 if ( $shelf ) {
216 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
217 $category = $shelf->category;
218 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
219 my $direction = $query->param('direction') || 'asc';
220 my ( $page, $rows );
221 unless ( $query->param('print') or $query->param('rss') ) {
222 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
223 $page = ( $query->param('page') ? $query->param('page') : 1 );
225 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
226 my $contents = $shelf->get_contents->search(
229 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
230 page => $page,
231 rows => $rows,
232 order_by => "$order_by $direction",
236 # get biblionumbers stored in the cart
237 my @cart_list;
238 if(my $cart_list = $query->cookie('bib_list')){
239 @cart_list = split(/\//, $cart_list);
242 my $borrower = GetMember( borrowernumber => $loggedinuser );
244 my @items;
245 while ( my $content = $contents->next ) {
246 my $this_item;
247 my $biblionumber = $content->biblionumber->biblionumber;
248 my $record = GetMarcBiblio($biblionumber);
250 if ( C4::Context->preference("OPACXSLTResultsDisplay") ) {
251 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTResultsDisplay" );
254 my $marcflavour = C4::Context->preference("marcflavour");
255 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
256 $this_item->{imageurl} = $itemtypeinfo->{imageurl};
257 $this_item->{description} = $itemtypeinfo->{description};
258 $this_item->{notforloan} = $itemtypeinfo->{notforloan};
259 $this_item->{'coins'} = GetCOinSBiblio($record);
260 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
261 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
262 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
263 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
264 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
266 unless ( defined $this_item->{size} ) {
268 #TT has problems with size
269 $this_item->{size} = q||;
272 # Getting items infos for location display
273 my @items_infos = &GetItemsLocationInfo( $biblionumber );
274 $this_item->{'ITEM_RESULTS'} = \@items_infos;
276 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
277 $this_item->{TagLoop} = get_tags({
278 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
279 limit => C4::Context->preference('TagsShowOnList'),
283 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
286 if ( grep {$_ eq $biblionumber} @cart_list) {
287 $this_item->{incart} = 1;
290 if ( $query->param('rss') ) {
291 $this_item->{title} = $content->biblionumber->title;
292 $this_item->{author} = $content->biblionumber->author;
295 $this_item->{biblionumber} = $biblionumber;
296 push @items, $this_item;
299 $template->param(
300 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
301 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
302 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
303 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
304 sortfield => $sortfield,
305 itemsloop => \@items,
306 sortfield => $sortfield,
307 direction => $direction,
309 if ( $page ) {
310 my $pager = $contents->pager;
311 $template->param(
312 pagination_bar => pagination_bar(
313 q||, $pager->last_page - $pager->first_page + 1,
314 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
318 } else {
319 push @messages, { type => 'error', code => 'unauthorized_on_view' };
321 } else {
322 push @messages, { type => 'error', code => 'does_not_exist' };
326 if ( $op eq 'list' ) {
327 my $shelves;
328 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
329 if ( $category == 1 ) {
330 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
331 } else {
332 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
335 my $pager = $shelves->pager;
336 $template->param(
337 shelves => $shelves,
338 pagination_bar => pagination_bar(
339 q||, $pager->last_page - $pager->first_page + 1,
340 $page, "page", { op => 'list', category => $category, }
345 $template->param(
346 op => $op,
347 referer => $referer,
348 shelf => $shelf,
349 messages => \@messages,
350 category => $category,
351 print => $query->param('print') || 0,
352 listsview => 1,
355 output_html_with_http_headers $query, $cookie, $template->output;