Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
|
4 |
# Copyright 2010 SARL BibLibre |
5 |
# |
6 |
# This file is part of Koha. |
7 |
# |
8 |
# Koha is free software; you can redistribute it and/or modify it under the |
9 |
# terms of the GNU General Public License as published by the Free Software |
10 |
# Foundation; either version 2 of the License, or (at your option) any later |
11 |
# version. |
12 |
# |
13 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
14 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
15 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License along with |
18 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
19 |
# Suite 330, Boston, MA 02111-1307 USA |
20 |
|
21 |
use CGI; |
22 |
use strict; |
23 |
use C4::Output; |
24 |
use C4::Auth; |
25 |
use C4::Branch; |
26 |
use C4::Koha; |
27 |
use C4::Biblio; |
28 |
use C4::Context; |
29 |
use C4::Debug; |
30 |
use JSON; |
31 |
|
32 |
my $input = new CGI; |
33 |
my $dbh = C4::Context->dbh; |
34 |
|
35 |
my $filefh = $input->param('uploadfile'); |
36 |
my $recordslist = $input->param('recordslist'); |
37 |
my $bib_list = $input->param('bib_list'); |
38 |
my @biblionumbers; |
39 |
|
40 |
#retrieve the list of biblionumber, from a file, a list entered manually or the basket |
41 |
if ($filefh) { |
42 |
while ( my $biblionumber = <$filefh> ) { |
43 |
$biblionumber =~ s/[\r\n]*$//g; |
44 |
push @biblionumbers, $biblionumber if $biblionumber; |
45 |
} |
46 |
} elsif ($recordslist) { |
47 |
push @biblionumbers, split( /\s\n/, $recordslist ); |
48 |
} elsif ($bib_list) { |
49 |
push @biblionumbers, split('/', $bib_list); |
50 |
} |
51 |
|
52 |
my $op = $input->param('op'); |
53 |
my ($template, $loggedinuser, $cookie); |
54 |
|
55 |
my $frameworkcode=""; |
56 |
my $tagslib = &GetMarcStructure(1,$frameworkcode); |
57 |
my %report_actions; |
58 |
|
59 |
# if this script is called by ajax, buid an ajax/xml answer |
60 |
if($input->param('field') and not defined $op){ |
61 |
($template, $loggedinuser, $cookie) |
62 |
= get_template_and_user({template_name => "acqui/ajax.tmpl", |
63 |
query => $input, |
64 |
type => "intranet", |
65 |
authnotrequired => 0, |
66 |
flagsrequired => { tools => "batchedit" }, |
67 |
}); |
68 |
|
69 |
|
70 |
my $tag = $input->param('field'); |
71 |
my $subfield = $input->param('subfield'); |
72 |
|
73 |
if($input->param('subfield')){ |
74 |
my $branches = GetBranchesLoop(); |
75 |
|
76 |
my @authorised_values; |
77 |
if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) { |
78 |
if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) { |
79 |
foreach my $thisbranch (@$branches) { |
80 |
push @authorised_values, { |
81 |
code => $thisbranch->{value}, |
82 |
value => $thisbranch->{branchname}, |
83 |
}; |
84 |
# $value = $thisbranch->{value} if $thisbranch->{selected}; |
85 |
} |
86 |
}elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { |
87 |
my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description"); |
88 |
$sth->execute(); |
89 |
while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { |
90 |
push @authorised_values, { |
91 |
code => $itemtype, |
92 |
value => $description, |
93 |
}; |
94 |
} |
95 |
|
96 |
}else { |
97 |
# Getting the fields where the item location is |
98 |
my ($location_field, $location_subfield) = GetMarcFromKohaField('items.location', $frameworkcode); |
99 |
|
100 |
# Getting the name of the authorised values' category for item location |
101 |
my $item_location_category = $tagslib->{$location_field}->{$location_subfield}->{'authorised_value'}; |
102 |
# Are we dealing with item location ? |
103 |
my $item_location = ($tagslib->{$tag}->{$subfield}->{authorised_value} eq $item_location_category) ? 1 : 0; |
104 |
|
105 |
# If so, we sort by authorised_value, else by libelle |
106 |
my $orderby = $item_location ? 'authorised_value' : 'lib'; |
107 |
|
108 |
my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY $orderby"); |
109 |
|
110 |
$authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}); |
111 |
|
112 |
|
113 |
while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) { |
114 |
push @authorised_values, { |
115 |
code => $value, |
116 |
value => ($item_location) ? $value . " - " . $lib : $lib, |
117 |
}; |
118 |
|
119 |
} |
120 |
} |
121 |
} |
122 |
$template->param('return' => to_json(\@authorised_values)); |
123 |
}else{ |
124 |
my @modifiablesubf; |
125 |
|
126 |
foreach my $subfield (sort keys %{$tagslib->{$tag}}) { |
127 |
next if subfield_is_koha_internal_p($subfield); |
128 |
next if $subfield eq "@"; |
129 |
next if ($tagslib->{$tag}->{$subfield}->{'tab'} eq "10"); |
130 |
my %subfield_data; |
131 |
$subfield_data{subfield} = $subfield; |
132 |
push @modifiablesubf, \%subfield_data; |
133 |
} |
134 |
$template->param('return' => to_json(\@modifiablesubf)); |
135 |
} |
136 |
|
137 |
|
138 |
output_html_with_http_headers $input, $cookie, $template->output; |
139 |
exit; |
140 |
}else{ |
141 |
# standard call (via web browser) |
142 |
($template, $loggedinuser, $cookie) |
143 |
= get_template_and_user({template_name => "tools/batchedit.tmpl", |
144 |
query => $input, |
145 |
type => "intranet", |
146 |
authnotrequired => 0, |
147 |
flagsrequired => { tools => "batchedit" }, |
148 |
}); |
149 |
|
150 |
$template->param( bib_list => join('/', @biblionumbers)); |
151 |
# if no $op, just display the form |
152 |
if(!defined $op) { |
153 |
my @modifiablefields; |
154 |
|
155 |
foreach my $tag (sort keys %{$tagslib}) { |
156 |
my %subfield_data; |
157 |
foreach my $subfield (sort keys %{$tagslib->{$tag}}) { |
158 |
next if $subfield_data{tag}; |
159 |
next if subfield_is_koha_internal_p($subfield); |
160 |
next if ($tagslib->{$tag}->{$subfield}->{'tab'} eq "10"); |
161 |
|
162 |
$subfield_data{tag} = $tag; |
163 |
|
164 |
push @modifiablefields, \%subfield_data; |
165 |
} |
166 |
} |
167 |
$template->param(marcfields => \@modifiablefields, |
168 |
); |
169 |
|
170 |
}else{ |
171 |
my @fields = $input->param('field'); |
172 |
my @subfields = $input->param('subfield'); |
173 |
my @actions = $input->param('action'); |
174 |
my @condvals = $input->param('condval'); |
175 |
my @nocondvals = $input->param('nocondval'); |
176 |
my @repvals = $input->param('repval'); |
177 |
foreach my $biblionumber ( @biblionumbers ){ |
178 |
my $record = GetMarcBiblio($biblionumber); |
179 |
my ($count, $biblio) = GetBiblio($biblionumber); |
180 |
my @failed_actions; |
181 |
for(my $i = 0 ; $i < scalar(@fields) ; $i++ ){ |
182 |
my $field = $fields[$i]; |
183 |
my $subfield = $subfields[$i]; |
184 |
my $action = $actions[$i]; |
185 |
my $condval = $condvals[$i]; |
186 |
my $nocond = $nocondvals[$i]; |
187 |
my $repval = $repvals[$i]; |
188 |
my ($result,$record) = BatchModField($record, $field, $subfield, $action, $condval, $nocond, $repval); |
189 |
push @failed_actions, {action=>"$field $subfield $action ".($nocond eq "true"?"all":$condval)." $repval"} if ($result<=0); |
190 |
} |
191 |
if (@failed_actions == scalar(@fields)){ |
192 |
$report_actions{$biblionumber}->{status}="No_Actions"; |
193 |
} |
194 |
elsif (@failed_actions>0 and @failed_actions < scalar(@fields)){ |
195 |
$report_actions{$biblionumber}->{status}="Actions_Failed"; |
196 |
$report_actions{$biblionumber}->{failed_actions}=\@failed_actions; |
197 |
} |
198 |
elsif (@failed_actions == 0){ |
199 |
$report_actions{$biblionumber}->{status}="OK"; |
200 |
} |
201 |
ModBiblio($record, $biblionumber, $biblio->{frameworkcode}); |
202 |
} |
203 |
$template->param('moddone' => 1); |
204 |
} |
205 |
|
206 |
} |
207 |
|
208 |
my @biblioinfos; |
209 |
|
210 |
for my $biblionumber (@biblionumbers){ |
211 |
my ($count,$biblio) = GetBiblio($biblionumber); |
212 |
if (defined $op){ |
213 |
$biblio->{$report_actions{$biblionumber}->{status}}=1; |
214 |
$biblio->{failed_actions}=$report_actions{$biblionumber}->{failed_actions}; |
215 |
} |
216 |
push @biblioinfos, $biblio; |
217 |
} |
218 |
|
219 |
$template->param(biblioinfos => \@biblioinfos); |
220 |
output_html_with_http_headers $input, $cookie, $template->output; |
221 |
exit; |