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 |
if ($filefh) { |
41 |
while ( my $biblionumber = <$filefh> ) { |
42 |
$biblionumber =~ s/[\r\n]*$//g; |
43 |
push @biblionumbers, $biblionumber if $biblionumber; |
44 |
} |
45 |
} elsif ($recordslist) { |
46 |
push @biblionumbers, split( /\s\n/, $recordslist ); |
47 |
} elsif ($bib_list) { |
48 |
push @biblionumbers, split('/', $bib_list); |
49 |
} |
50 |
|
51 |
my $op = $input->param('op'); |
52 |
my ($template, $loggedinuser, $cookie); |
53 |
|
54 |
my $frameworkcode=""; |
55 |
my $tagslib = &GetMarcStructure(1,$frameworkcode); |
56 |
my %report_actions; |
57 |
|
58 |
if($input->param('field') and not defined $op){ |
59 |
($template, $loggedinuser, $cookie) |
60 |
= get_template_and_user({template_name => "acqui/ajax.tmpl", |
61 |
query => $input, |
62 |
type => "intranet", |
63 |
authnotrequired => 0, |
64 |
flagsrequired => { tools => "batchedit" }, |
65 |
}); |
66 |
|
67 |
|
68 |
my $tag = $input->param('field'); |
69 |
my $subfield = $input->param('subfield'); |
70 |
|
71 |
if($input->param('subfield')){ |
72 |
my $branches = GetBranchesLoop(); |
73 |
|
74 |
my @authorised_values; |
75 |
if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) { |
76 |
if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) { |
77 |
foreach my $thisbranch (@$branches) { |
78 |
push @authorised_values, { |
79 |
code => $thisbranch->{value}, |
80 |
value => $thisbranch->{branchname}, |
81 |
}; |
82 |
# $value = $thisbranch->{value} if $thisbranch->{selected}; |
83 |
} |
84 |
}elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { |
85 |
my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description"); |
86 |
$sth->execute(); |
87 |
while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) { |
88 |
push @authorised_values, { |
89 |
code => $itemtype, |
90 |
value => $description, |
91 |
}; |
92 |
} |
93 |
|
94 |
}else { |
95 |
# Getting the fields where the item location is |
96 |
my ($location_field, $location_subfield) = GetMarcFromKohaField('items.location', $frameworkcode); |
97 |
|
98 |
# Getting the name of the authorised values' category for item location |
99 |
my $item_location_category = $tagslib->{$location_field}->{$location_subfield}->{'authorised_value'}; |
100 |
# Are we dealing with item location ? |
101 |
my $item_location = ($tagslib->{$tag}->{$subfield}->{authorised_value} eq $item_location_category) ? 1 : 0; |
102 |
|
103 |
# If so, we sort by authorised_value, else by libelle |
104 |
my $orderby = $item_location ? 'authorised_value' : 'lib'; |
105 |
|
106 |
my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY $orderby"); |
107 |
|
108 |
$authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}); |
109 |
|
110 |
|
111 |
while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) { |
112 |
push @authorised_values, { |
113 |
code => $value, |
114 |
value => ($item_location) ? $value . " - " . $lib : $lib, |
115 |
}; |
116 |
|
117 |
} |
118 |
} |
119 |
} |
120 |
$template->param('return' => to_json(\@authorised_values)); |
121 |
}else{ |
122 |
my @modifiablesubf; |
123 |
|
124 |
foreach my $subfield (sort keys %{$tagslib->{$tag}}) { |
125 |
next if subfield_is_koha_internal_p($subfield); |
126 |
next if $subfield eq "@"; |
127 |
next if ($tagslib->{$tag}->{$subfield}->{'tab'} eq "10"); |
128 |
my %subfield_data; |
129 |
$subfield_data{subfield} = $subfield; |
130 |
push @modifiablesubf, \%subfield_data; |
131 |
} |
132 |
$template->param('return' => to_json(\@modifiablesubf)); |
133 |
} |
134 |
|
135 |
|
136 |
output_html_with_http_headers $input, $cookie, $template->output; |
137 |
exit; |
138 |
}else{ |
139 |
($template, $loggedinuser, $cookie) |
140 |
= get_template_and_user({template_name => "tools/batchedit.tmpl", |
141 |
query => $input, |
142 |
type => "intranet", |
143 |
authnotrequired => 0, |
144 |
flagsrequired => { tools => "batchedit" }, |
145 |
}); |
146 |
|
147 |
$template->param( inputform => 1, ) unless @biblionumbers; |
148 |
|
149 |
if(!defined $op) { |
150 |
my @modifiablefields; |
151 |
|
152 |
foreach my $tag (sort keys %{$tagslib}) { |
153 |
my %subfield_data; |
154 |
foreach my $subfield (sort keys %{$tagslib->{$tag}}) { |
155 |
next if $subfield_data{tag}; |
156 |
next if subfield_is_koha_internal_p($subfield); |
157 |
next if ($tagslib->{$tag}->{$subfield}->{'tab'} eq "10"); |
158 |
|
159 |
$subfield_data{tag} = $tag; |
160 |
|
161 |
push @modifiablefields, \%subfield_data; |
162 |
} |
163 |
} |
164 |
|
165 |
$template->param( marcfields => \@modifiablefields, |
166 |
bib_list => $input->param('bib_list'), |
167 |
); |
168 |
|
169 |
}else{ |
170 |
my @fields = $input->param('field'); |
171 |
my @subfields = $input->param('subfield'); |
172 |
my @actions = $input->param('action'); |
173 |
my @condvals = $input->param('condval'); |
174 |
my @nocondvals = $input->param('nocondval'); |
175 |
my @repvals = $input->param('repval'); |
176 |
foreach my $biblionumber ( @biblionumbers ){ |
177 |
my $record = GetMarcBiblio($biblionumber); |
178 |
my ($count, $biblio) = GetBiblio($biblionumber); |
179 |
my $report = 0; |
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 |
|
189 |
my ($result,$record) = BatchModField($record, $field, $subfield, $action, $condval, $nocond, $repval); |
190 |
push @failed_actions, {action=>"$field $subfield $action ".($nocond eq "true"?"all":$condval)." $repval"} if ($result<=0); |
191 |
} |
192 |
if (@failed_actions == scalar(@fields)){ |
193 |
$report_actions{$biblionumber}->{status}="No_Actions"; |
194 |
} |
195 |
elsif (@failed_actions>0 and @failed_actions < scalar(@fields)){ |
196 |
$report_actions{$biblionumber}->{status}="Actions_Failed"; |
197 |
$report_actions{$biblionumber}->{failed_actions}=\@failed_actions; |
198 |
} |
199 |
elsif (@failed_actions == 0){ |
200 |
$report_actions{$biblionumber}->{status}="OK"; |
201 |
} |
202 |
ModBiblio($record, $biblionumber, $biblio->{frameworkcode}) unless ($report); |
203 |
} |
204 |
$template->param('moddone' => 1); |
205 |
} |
206 |
|
207 |
} |
208 |
|
209 |
my @biblioinfos; |
210 |
|
211 |
for my $biblionumber (@biblionumbers){ |
212 |
my ($count,$biblio) = GetBiblio($biblionumber); |
213 |
if (defined $op){ |
214 |
$biblio->{$report_actions{$biblionumber}->{status}}=1; |
215 |
$biblio->{failed_actions}=$report_actions{$biblionumber}->{failed_actions}; |
216 |
} |
217 |
push @biblioinfos, $biblio; |
218 |
} |
219 |
|
220 |
$template->param(biblioinfos => \@biblioinfos); |
221 |
output_html_with_http_headers $input, $cookie, $template->output; |
222 |
exit; |