Bug 22420: Remove wrong calculation on approved tags
[koha.git] / virtualshelves / shelves.pl
blob3b5944459a500ef6f6ec1016fe146e7473370f08
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 $category = 2 if $category ne "1";
55 my ( $shelf, $shelfnumber, @messages );
57 if ( $op eq 'add_form' ) {
58 # Only pass default
59 $shelf = { allow_change_from_owner => 1 };
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 = Koha::Patrons->find( $shelf->owner )->unblessed;
67 $template->param( owner => $patron, );
68 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
69 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
70 $op = 'list';
72 } else {
73 push @messages, { type => 'alert', code => 'does_not_exist' };
75 } elsif ( $op eq 'add' ) {
76 my $allow_changes_from = $query->param('allow_changes_from');
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'),
82 allow_change_from_owner => $allow_changes_from > 0,
83 allow_change_from_others => $allow_changes_from == ANYONE,
84 owner => scalar $query->param('owner'),
87 $shelf->store;
88 $shelfnumber = $shelf->shelfnumber;
90 if ($@) {
91 push @messages, { type => 'alert', code => ref($@), msg => $@ };
92 } elsif ( not $shelf ) {
93 push @messages, { type => 'alert', code => 'error_on_insert' };
95 } else {
96 push @messages, { type => 'message', code => 'success_on_insert' };
97 $op = 'view';
99 } elsif ( $op eq 'edit' ) {
100 $shelfnumber = $query->param('shelfnumber');
101 $shelf = Koha::Virtualshelves->find($shelfnumber);
103 if ( $shelf ) {
104 $op = $referer;
105 my $sortfield = $query->param('sortfield');
106 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
107 if ( $shelf->can_be_managed( $loggedinuser ) ) {
108 $shelf->shelfname( scalar $query->param('shelfname') );
109 $shelf->sortfield( $sortfield );
110 my $allow_changes_from = $query->param('allow_changes_from');
111 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
112 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
113 $shelf->category( scalar $query->param('category') );
114 eval { $shelf->store };
116 if ($@) {
117 push @messages, { type => 'alert', code => 'error_on_update' };
118 $op = 'edit_form';
119 } else {
120 push @messages, { type => 'message', code => 'success_on_update' };
122 } else {
123 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
125 } else {
126 push @messages, { type => 'alert', code => 'does_not_exist' };
128 } elsif ( $op eq 'delete' ) {
129 $shelfnumber = $query->param('shelfnumber');
130 $shelf = Koha::Virtualshelves->find($shelfnumber);
131 if ($shelf) {
132 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
133 eval { $shelf->delete; };
134 if ($@) {
135 push @messages, { type => 'alert', code => ref($@), msg => $@ };
136 } else {
137 push @messages, { type => 'message', code => 'success_on_delete' };
139 } else {
140 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
142 } else {
143 push @messages, { type => 'alert', code => 'does_not_exist' };
145 $op = 'list';
146 } elsif ( $op eq 'add_biblio' ) {
147 $shelfnumber = $query->param('shelfnumber');
148 $shelf = Koha::Virtualshelves->find($shelfnumber);
149 if ($shelf) {
150 if( my $barcodes = $query->param('barcodes') ) {
151 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
152 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
153 foreach my $barcode (@barcodes){
154 $barcode =~ s/\r$//; # strip any naughty return chars
155 next if $barcode eq '';
156 my $item = Koha::Items->find({barcode => $barcode});
157 if ( $item ) {
158 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
159 if ($@) {
160 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
161 } elsif ( $added ) {
162 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
163 } else {
164 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
166 } else {
167 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
170 } else {
171 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
174 if ( my $biblionumbers = $query->param('biblionumbers') ) {
175 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
176 my @biblionumbers = split /\n/, $biblionumbers;
177 foreach my $biblionumber (@biblionumbers) {
178 $biblionumber =~ s/\r$//; # strip any naughty return chars
179 next if $biblionumber eq '';
180 my $biblio = Koha::Biblios->find($biblionumber);
181 if (defined $biblio) {
182 my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
183 if ($@) {
184 push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
185 } elsif ( $added ) {
186 push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
187 } else {
188 push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
190 } else {
191 push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
194 } else {
195 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
198 } else {
199 push @messages, { type => 'alert', code => 'does_not_exist' };
201 $op = $referer;
202 } elsif ( $op eq 'remove_biblios' ) {
203 $shelfnumber = $query->param('shelfnumber');
204 $shelf = Koha::Virtualshelves->find($shelfnumber);
205 my @biblionumbers = $query->multi_param('biblionumber');
206 if ($shelf) {
207 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
208 my $number_of_biblios_removed = eval {
209 $shelf->remove_biblios(
211 biblionumbers => \@biblionumbers,
212 borrowernumber => $loggedinuser,
216 if ($@) {
217 push @messages, { type => 'alert', code => ref($@), msg => $@ };
218 } elsif ( $number_of_biblios_removed ) {
219 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
220 } else {
221 push @messages, { type => 'alert', code => 'no_biblio_removed' };
223 } else {
224 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
226 } else {
227 push @messages, { type => 'alert', code => 'does_not_exist' };
229 $op = $referer;
232 if ( $op eq 'view' ) {
233 $shelfnumber ||= $query->param('shelfnumber');
234 $shelf = Koha::Virtualshelves->find($shelfnumber);
235 if ( $shelf ) {
236 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
237 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
238 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
239 my $direction = $query->param('direction') || 'asc';
240 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
241 my ( $rows, $page );
242 unless ( $query->param('print') ) {
243 $rows = C4::Context->preference('numSearchResults') || 20;
244 $page = ( $query->param('page') ? $query->param('page') : 1 );
247 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
248 my $contents = $shelf->get_contents->search(
251 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
252 page => $page,
253 rows => $rows,
254 order_by => { "-$direction" => $order_by },
258 my $xslfile = C4::Context->preference('XSLTListsDisplay');
259 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
260 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
262 my @items;
263 while ( my $content = $contents->next ) {
264 my $this_item;
265 my $biblionumber = $content->biblionumber;
266 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
268 if ( $xslfile ) {
269 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
270 1, undef, $sysxml, $xslfile, $lang);
273 my $marcflavour = C4::Context->preference("marcflavour");
274 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
275 $itemtype = Koha::ItemTypes->find( $itemtype );
276 my $biblio = Koha::Biblios->find( $content->biblionumber );
277 $this_item->{title} = $biblio->title;
278 $this_item->{author} = $biblio->author;
279 $this_item->{dateadded} = $content->dateadded;
280 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
281 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
282 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
283 $this_item->{'coins'} = $biblio->get_coins;
284 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
285 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
286 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
287 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
288 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
290 unless ( defined $this_item->{size} ) {
292 #TT has problems with size
293 $this_item->{size} = q||;
296 # Getting items infos for location display
297 my @items_infos = &GetItemsLocationInfo( $biblionumber );
298 $this_item->{'ITEM_RESULTS'} = \@items_infos;
299 $this_item->{biblionumber} = $biblionumber;
300 push @items, $this_item;
303 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
305 borrowernumber => $loggedinuser,
306 add_allowed => 1,
307 category => 1,
310 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
312 borrowernumber => $loggedinuser,
313 add_allowed => 1,
314 category => 2,
318 $template->param(
319 add_to_some_private_shelves => $some_private_shelves,
320 add_to_some_public_shelves => $some_public_shelves,
321 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
322 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
323 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
324 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
325 sortfield => $sortfield,
326 itemsloop => \@items,
327 sortfield => $sortfield,
328 direction => $direction,
330 if ( $page ) {
331 my $pager = $contents->pager;
332 $template->param(
333 pagination_bar => pagination_bar(
334 q||, $pager->last_page - $pager->first_page + 1,
335 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
339 } else {
340 push @messages, { type => 'error', code => 'unauthorized_on_view' };
341 undef $shelf;
343 } else {
344 push @messages, { type => 'alert', code => 'does_not_exist' };
348 $template->param(
349 op => $op,
350 referer => $referer,
351 shelf => $shelf,
352 messages => \@messages,
353 category => $category,
354 print => scalar $query->param('print') || 0,
355 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
358 output_html_with_http_headers $query, $cookie, $template->output;