Bug 11217: The # in accountlines descriptions makes them un-writeoffable
[koha.git] / opac / opac-shelves.pl
blobb29ade648c580a5b46094f1272283b743e75c063
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 if ( $shelf->can_be_managed( $loggedinuser ) ) {
109 $shelf->shelfname( $query->param('shelfname') );
110 $shelf->sortfield( $query->param('sortfield') );
111 $shelf->allow_add( $query->param('allow_add') );
112 $shelf->allow_delete_own( $query->param('allow_delete_own') );
113 $shelf->allow_delete_other( $query->param('allow_delete_other') );
114 $shelf->category( $query->param('category') );
115 eval { $shelf->store };
117 if ($@) {
118 push @messages, { type => 'error', code => 'error_on_update' };
119 $op = 'edit_form';
120 } else {
121 push @messages, { type => 'message', code => 'success_on_update' };
123 } else {
124 push @messages, { type => 'error', code => 'unauthorized_on_update' };
126 } else {
127 push @messages, { type => 'error', code => 'does_not_exist' };
129 } elsif ( $op eq 'delete' ) {
130 $shelfnumber = $query->param('shelfnumber');
131 $shelf = Koha::Virtualshelves->find($shelfnumber);
132 if ($shelf) {
133 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
134 eval { $shelf->delete; };
135 if ($@) {
136 push @messages, { type => 'error', code => ref($@), msg => $@ };
137 } else {
138 push @messages, { type => 'message', code => 'success_on_delete' };
140 } else {
141 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
143 } else {
144 push @messages, { type => 'error', code => 'does_not_exist' };
146 $op = $referer;
147 } elsif ( $op eq 'remove_share' ) {
148 $shelfnumber = $query->param('shelfnumber');
149 $shelf = Koha::Virtualshelves->find($shelfnumber);
150 if ($shelf) {
151 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
152 if ($@) {
153 push @messages, { type => 'error', code => ref($@), msg => $@ };
154 } elsif ( $removed ) {
155 push @messages, { type => 'message', code => 'success_on_remove_share' };
156 } else {
157 push @messages, { type => 'error', code => 'error_on_remove_share' };
159 } else {
160 push @messages, { type => 'error', code => 'does_not_exist' };
162 $op = $referer;
164 } elsif ( $op eq 'add_biblio' ) {
165 $shelfnumber = $query->param('shelfnumber');
166 $shelf = Koha::Virtualshelves->find($shelfnumber);
167 if ($shelf) {
168 if( my $barcode = $query->param('barcode') ) {
169 my $item = GetItem( 0, $barcode);
170 if (defined $item && $item->{itemnumber}) {
171 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
172 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
173 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
174 if ($@) {
175 push @messages, { type => 'error', code => ref($@), msg => $@ };
176 } elsif ( $added ) {
177 push @messages, { type => 'message', code => 'success_on_add_biblio' };
178 } else {
179 push @messages, { type => 'message', code => 'error_on_add_biblio' };
181 } else {
182 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
184 } else {
185 push @messages, { type => 'error', code => 'item_does_not_exist' };
188 } else {
189 push @messages, { type => 'error', code => 'does_not_exist' };
191 $op = $referer;
192 } elsif ( $op eq 'remove_biblios' ) {
193 $shelfnumber = $query->param('shelfnumber');
194 $shelf = Koha::Virtualshelves->find($shelfnumber);
195 my @biblionumber = $query->multi_param('biblionumber');
196 if ($shelf) {
197 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
198 my $number_of_biblios_removed = eval {
199 $shelf->remove_biblios(
201 biblionumbers => \@biblionumber,
202 borrowernumber => $loggedinuser,
206 if ($@) {
207 push @messages, { type => 'error', code => ref($@), msg => $@ };
208 } elsif ( $number_of_biblios_removed ) {
209 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
210 } else {
211 push @messages, { type => 'error', code => 'no_biblio_removed' };
213 } else {
214 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
216 } else {
217 push @messages, { type => 'error', code => 'does_not_exist' };
219 $op = 'view';
222 if ( $op eq 'view' ) {
223 $shelfnumber ||= $query->param('shelfnumber');
224 $shelf = Koha::Virtualshelves->find($shelfnumber);
225 if ( $shelf ) {
226 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
227 $category = $shelf->category;
228 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
229 my $direction = $query->param('direction') || 'asc';
230 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
231 my ( $page, $rows );
232 unless ( $query->param('print') or $query->param('rss') ) {
233 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
234 $page = ( $query->param('page') ? $query->param('page') : 1 );
236 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
237 my $contents = $shelf->get_contents->search(
240 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
241 page => $page,
242 rows => $rows,
243 order_by => { "-$direction" => $order_by },
247 # get biblionumbers stored in the cart
248 my @cart_list;
249 if(my $cart_list = $query->cookie('bib_list')){
250 @cart_list = split(/\//, $cart_list);
253 my $borrower = GetMember( borrowernumber => $loggedinuser );
255 # Lists display falls back to search results configuration
256 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
257 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
258 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
260 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
261 my @items;
262 while ( my $content = $contents->next ) {
263 my $biblionumber = $content->biblionumber;
264 my $this_item = GetBiblioData($biblionumber);
265 my $record = GetMarcBiblio($biblionumber);
266 my $framework = GetFrameworkCode( $biblionumber );
267 $record_processor->options({
268 interface => 'opac',
269 frameworkcode => $framework
271 $record_processor->process($record);
273 if ( $xslfile ) {
274 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
275 1, undef, $sysxml, $xslfile, $lang);
278 my $marcflavour = C4::Context->preference("marcflavour");
279 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
280 my $itemtypeinfo = getitemtypeinfo( $itemtype, 'opac' );
281 $this_item->{imageurl} = $itemtypeinfo->{imageurl};
282 $this_item->{description} = $itemtypeinfo->{description};
283 $this_item->{notforloan} = $itemtypeinfo->{notforloan};
284 $this_item->{'coins'} = GetCOinSBiblio($record);
285 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
286 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
287 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
288 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
289 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
291 unless ( defined $this_item->{size} ) {
293 #TT has problems with size
294 $this_item->{size} = q||;
297 # Getting items infos for location display
298 my @items_infos = &GetItemsLocationInfo( $biblionumber );
299 $this_item->{'ITEM_RESULTS'} = \@items_infos;
301 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
302 $this_item->{TagLoop} = get_tags({
303 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
304 limit => C4::Context->preference('TagsShowOnList'),
308 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
311 if ( grep {$_ eq $biblionumber} @cart_list) {
312 $this_item->{incart} = 1;
315 if ( $query->param('rss') ) {
316 $this_item->{title} = $content->biblionumber->title;
317 $this_item->{author} = $content->biblionumber->author;
320 $this_item->{biblionumber} = $biblionumber;
321 push @items, $this_item;
324 $template->param(
325 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
326 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
327 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
328 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
329 sortfield => $sortfield,
330 itemsloop => \@items,
331 sortfield => $sortfield,
332 direction => $direction,
334 if ( $page ) {
335 my $pager = $contents->pager;
336 $template->param(
337 pagination_bar => pagination_bar(
338 q||, $pager->last_page - $pager->first_page + 1,
339 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
343 } else {
344 push @messages, { type => 'error', code => 'unauthorized_on_view' };
345 undef $shelf;
347 } else {
348 push @messages, { type => 'error', code => 'does_not_exist' };
352 if ( $op eq 'list' ) {
353 my $shelves;
354 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
355 if ( $category == 1 ) {
356 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
357 } else {
358 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
361 my $pager = $shelves->pager;
362 $template->param(
363 shelves => $shelves,
364 pagination_bar => pagination_bar(
365 q||, $pager->last_page - $pager->first_page + 1,
366 $page, "page", { op => 'list', category => $category, }
371 $template->param(
372 op => $op,
373 referer => $referer,
374 shelf => $shelf,
375 messages => \@messages,
376 category => $category,
377 print => scalar $query->param('print') || 0,
378 listsview => 1,
381 output_html_with_http_headers $query, $cookie, $template->output;