Bug 25482: Explicitly set macros as not shard during tests
[koha.git] / virtualshelves / shelves.pl
blobaeb65c94963a227960d346d65f8ed26c1e8b7bab
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::XSLT;
30 use Koha::Biblios;
31 use Koha::Biblioitems;
32 use Koha::Items;
33 use Koha::ItemTypes;
34 use Koha::CsvProfiles;
35 use Koha::Patrons;
36 use Koha::Virtualshelves;
38 use constant ANYONE => 2;
40 my $query = new CGI;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 { template_name => "virtualshelves/shelves.tt",
44 query => $query,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { catalogue => 1 },
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' ) {
57 # Only pass default
58 $shelf = { allow_change_from_owner => 1 };
59 } elsif ( $op eq 'edit_form' ) {
60 $shelfnumber = $query->param('shelfnumber');
61 $shelf = Koha::Virtualshelves->find($shelfnumber);
63 if ( $shelf ) {
64 $category = $shelf->category;
65 my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
66 $template->param( owner => $patron, );
67 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
68 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
69 $op = 'list';
71 } else {
72 push @messages, { type => 'alert', code => 'does_not_exist' };
74 } elsif ( $op eq 'add' ) {
75 my $allow_changes_from = $query->param('allow_changes_from');
76 eval {
77 $shelf = Koha::Virtualshelf->new(
78 { shelfname => scalar $query->param('shelfname'),
79 sortfield => scalar $query->param('sortfield'),
80 category => scalar $query->param('category'),
81 allow_change_from_owner => $allow_changes_from > 0,
82 allow_change_from_others => $allow_changes_from == ANYONE,
83 owner => scalar $query->param('owner'),
86 $shelf->store;
87 $shelfnumber = $shelf->shelfnumber;
89 if ($@) {
90 push @messages, { type => 'alert', code => ref($@), msg => $@ };
91 } elsif ( not $shelf ) {
92 push @messages, { type => 'alert', code => 'error_on_insert' };
94 } else {
95 push @messages, { type => 'message', code => 'success_on_insert' };
96 $op = 'view';
98 } elsif ( $op eq 'edit' ) {
99 $shelfnumber = $query->param('shelfnumber');
100 $shelf = Koha::Virtualshelves->find($shelfnumber);
102 if ( $shelf ) {
103 $op = $referer;
104 my $sortfield = $query->param('sortfield');
105 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
106 if ( $shelf->can_be_managed( $loggedinuser ) ) {
107 $shelf->shelfname( scalar $query->param('shelfname') );
108 $shelf->sortfield( $sortfield );
109 my $allow_changes_from = $query->param('allow_changes_from');
110 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
111 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
112 $shelf->category( scalar $query->param('category') );
113 eval { $shelf->store };
115 if ($@) {
116 push @messages, { type => 'alert', code => 'error_on_update' };
117 $op = 'edit_form';
118 } else {
119 push @messages, { type => 'message', code => 'success_on_update' };
121 } else {
122 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
124 } else {
125 push @messages, { type => 'alert', code => 'does_not_exist' };
127 } elsif ( $op eq 'delete' ) {
128 $shelfnumber = $query->param('shelfnumber');
129 $shelf = Koha::Virtualshelves->find($shelfnumber);
130 if ($shelf) {
131 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
132 eval { $shelf->delete; };
133 if ($@) {
134 push @messages, { type => 'alert', code => ref($@), msg => $@ };
135 } else {
136 push @messages, { type => 'message', code => 'success_on_delete' };
138 } else {
139 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
141 } else {
142 push @messages, { type => 'alert', code => 'does_not_exist' };
144 $op = 'list';
145 } elsif ( $op eq 'add_biblio' ) {
146 $shelfnumber = $query->param('shelfnumber');
147 $shelf = Koha::Virtualshelves->find($shelfnumber);
148 if ($shelf) {
149 if( my $barcodes = $query->param('barcodes') ) {
150 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
151 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
152 foreach my $barcode (@barcodes){
153 $barcode =~ s/\r$//; # strip any naughty return chars
154 next if $barcode eq '';
155 my $item = Koha::Items->find({barcode => $barcode});
156 if ( $item ) {
157 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
158 if ($@) {
159 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
160 } elsif ( $added ) {
161 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
162 } else {
163 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
165 } else {
166 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
169 } else {
170 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
173 if ( my $biblionumbers = $query->param('biblionumbers') ) {
174 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
175 my @biblionumbers = split /\n/, $biblionumbers;
176 foreach my $biblionumber (@biblionumbers) {
177 $biblionumber =~ s/\r$//; # strip any naughty return chars
178 next if $biblionumber eq '';
179 my $biblio = Koha::Biblios->find($biblionumber);
180 if (defined $biblio) {
181 my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
182 if ($@) {
183 push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
184 } elsif ( $added ) {
185 push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
186 } else {
187 push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
189 } else {
190 push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
193 } else {
194 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
197 } else {
198 push @messages, { type => 'alert', code => 'does_not_exist' };
200 $op = $referer;
201 } elsif ( $op eq 'remove_biblios' ) {
202 $shelfnumber = $query->param('shelfnumber');
203 $shelf = Koha::Virtualshelves->find($shelfnumber);
204 my @biblionumbers = $query->multi_param('biblionumber');
205 if ($shelf) {
206 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
207 my $number_of_biblios_removed = eval {
208 $shelf->remove_biblios(
210 biblionumbers => \@biblionumbers,
211 borrowernumber => $loggedinuser,
215 if ($@) {
216 push @messages, { type => 'alert', code => ref($@), msg => $@ };
217 } elsif ( $number_of_biblios_removed ) {
218 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
219 } else {
220 push @messages, { type => 'alert', code => 'no_biblio_removed' };
222 } else {
223 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
225 } else {
226 push @messages, { type => 'alert', code => 'does_not_exist' };
228 $op = $referer;
231 if ( $op eq 'view' ) {
232 $shelfnumber ||= $query->param('shelfnumber');
233 $shelf = Koha::Virtualshelves->find($shelfnumber);
234 if ( $shelf ) {
235 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
236 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
237 $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
238 my $direction = $query->param('direction') || 'asc';
239 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
240 my ( $rows, $page );
241 unless ( $query->param('print') ) {
242 $rows = C4::Context->preference('numSearchResults') || 20;
243 $page = ( $query->param('page') ? $query->param('page') : 1 );
246 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
247 my $contents = $shelf->get_contents->search(
250 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
251 page => $page,
252 rows => $rows,
253 order_by => { "-$direction" => $order_by },
257 my $xslfile = C4::Context->preference('XSLTListsDisplay');
258 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
259 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
261 my @items;
262 while ( my $content = $contents->next ) {
263 my $this_item;
264 my $biblionumber = $content->biblionumber;
265 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
267 if ( $xslfile ) {
268 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
269 1, undef, $sysxml, $xslfile, $lang);
272 my $marcflavour = C4::Context->preference("marcflavour");
273 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
274 $itemtype = Koha::ItemTypes->find( $itemtype );
275 my $biblio = Koha::Biblios->find( $content->biblionumber );
276 $this_item->{title} = $biblio->title;
277 $this_item->{subtitle} = $biblio->subtitle;
278 $this_item->{medium} = $biblio->medium;
279 $this_item->{part_number} = $biblio->part_number;
280 $this_item->{part_name} = $biblio->part_name;
281 $this_item->{author} = $biblio->author;
282 $this_item->{dateadded} = $content->dateadded;
283 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
284 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
285 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
286 $this_item->{'coins'} = $biblio->get_coins;
287 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
288 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
289 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
290 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
292 unless ( defined $this_item->{size} ) {
294 #TT has problems with size
295 $this_item->{size} = q||;
298 # Getting items infos for location display
299 my @items_infos = &GetItemsLocationInfo( $biblionumber );
300 $this_item->{'ITEM_RESULTS'} = \@items_infos;
301 $this_item->{biblionumber} = $biblionumber;
302 push @items, $this_item;
305 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
307 borrowernumber => $loggedinuser,
308 add_allowed => 1,
309 category => 1,
312 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
314 borrowernumber => $loggedinuser,
315 add_allowed => 1,
316 category => 2,
320 $template->param(
321 add_to_some_private_shelves => $some_private_shelves,
322 add_to_some_public_shelves => $some_public_shelves,
323 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
324 can_remove_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 sortfield => $sortfield,
328 itemsloop => \@items,
329 sortfield => $sortfield,
330 direction => $direction,
332 if ( $page ) {
333 my $pager = $contents->pager;
334 $template->param(
335 pagination_bar => pagination_bar(
336 q||, $pager->last_page - $pager->first_page + 1,
337 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
341 } else {
342 push @messages, { type => 'error', code => 'unauthorized_on_view' };
343 undef $shelf;
345 } else {
346 push @messages, { type => 'alert', code => 'does_not_exist' };
350 $template->param(
351 op => $op,
352 referer => $referer,
353 shelf => $shelf,
354 messages => \@messages,
355 category => $category,
356 print => scalar $query->param('print') || 0,
357 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
360 output_html_with_http_headers $query, $cookie, $template->output;