View | Details | Raw Unified | Return to bug 10538
Collapse All | Expand All

(-)a/C4/ImportExportFramework.pm (-76 / +84 lines)
Lines 23-33 use XML::LibXML; Link Here
23
use XML::LibXML::XPathContext;
23
use XML::LibXML::XPathContext;
24
use Digest::MD5 qw();
24
use Digest::MD5 qw();
25
use POSIX qw(strftime);
25
use POSIX qw(strftime);
26
use Text::CSV;
26
27
27
use C4::Context;
28
use C4::Context;
28
use C4::Debug;
29
use C4::Debug;
29
30
30
31
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32
32
33
BEGIN {
33
BEGIN {
Lines 1127-1222 sub _processRows_Table Link Here
1127
    return 1;
1127
    return 1;
1128
}#_processRows_Table
1128
}#_processRows_Table
1129
1129
1130
1131
1132
1133
# Import worksheet from the csv file to the mysql table
1130
# Import worksheet from the csv file to the mysql table
1134
sub _import_table_csv
1131
sub _import_table_csv {
1135
{
1132
1136
    my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $fields) = @_;
1133
    my ( $dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $fields )
1134
      = @_;
1137
1135
1138
    my $row = '';
1136
    my $numFields      = @$fields;
1139
    my $partialRow = '';
1140
    my $numFields = @$fields;
1141
    my $fieldsNameRead = 0;
1137
    my $fieldsNameRead = 0;
1142
    my @arrData;
1138
    my @arrData;
1143
    my ($fieldsStr, $dataStr, $updateStr);
1139
    my ( $fieldsStr, $dataStr, $updateStr );
1144
    my $db_scheme = C4::Context->config("db_scheme");
1140
    my $db_scheme = C4::Context->config("db_scheme");
1145
    my @fieldsPK = @$PKArray;
1141
    my @fieldsPK  = @$PKArray;
1146
    shift @fieldsPK;
1142
    shift @fieldsPK;
1147
    my $ok = 0;
1143
    my $ok  = 0;
1148
    my $numRow = 0;
1149
    my $pos = 0;
1144
    my $pos = 0;
1150
    while (<$dom>) {
1145
1151
        $row = $_;
1146
    my $csv = Text::CSV->new( { binary => 1, eol => $/ } );
1152
        # Check whether the line has an unfinished field, i.e., a field with CR/LF in its data
1147
1153
        if ($row =~ /,"[^"]*[\r\n]+$/ || $row =~ /^[^"]+[\r\n]+$/) {
1148
    while ( my $row = $csv->getline($dom) ) {
1154
            $row =~ s/[\r\n]+$//;
1149
1155
            $partialRow .= $row;
1150
        @arrData = @$row;
1156
            next;
1151
1152
        # trim empty columns from row
1153
        my @a2  = reverse(@arrData);
1154
        my $idx = 0;
1155
        foreach my $i (@a2) {
1156
            last if $i;
1157
            $idx++;
1157
        }
1158
        }
1158
        if ($partialRow) {
1159
1159
            $row = $partialRow . $row;
1160
        my $col_count = scalar @arrData - $idx;
1160
            $partialRow = '';
1161
        @arrData = @arrData[ 0 .. $col_count - 1 ];
1162
1163
        # skip glitchy rows
1164
        next unless scalar @arrData == scalar @$fields;
1165
1166
        if ( $arrData[0] eq '#-#' ) {
1167
1168
            # Change of table with separators #-#
1169
            return 1;
1170
        }
1171
        elsif ( $fieldsNameRead && $arrData[0] eq 'tagfield' ) {
1172
1173
 # Change of table because we begin with field name with former field names read
1174
            seek( $dom, $pos, 0 );
1175
1176
            return 1;
1161
        }
1177
        }
1162
        # Line OK, process it
1178
        if ( scalar(@$fields) == scalar(@arrData) ) {
1163
        if ($row =~ /(?:".*?",?)+/) {
1179
            if ( !$fieldsNameRead ) {
1164
            @arrData = split('","', $row);
1180
1165
            $arrData[0] = substr($arrData[0], 1) if ($arrData[0] =~ /^"/);
1181
                # New table, we read the field names
1166
            $arrData[$#arrData] =~ s/[\r\n]+$//;
1182
                $fieldsNameRead = 1;
1167
            chop $arrData[$#arrData] if ($arrData[$#arrData] =~ /"$/);
1183
                for ( my $i = 0 ; $i < @arrData ; $i++ ) {
1168
            if (@arrData) {
1184
                    if ( $arrData[$i] ne $fields->[$i] ) {
1169
                if ($arrData[0] eq '#-#' && $arrData[$#arrData] eq '#-#') {
1185
                        $fieldsNameRead = 0;
1170
                    # Change of table with separators #-#
1186
                        last;
1171
                    return 1;
1187
                    }
1172
                } elsif ($fieldsNameRead && $arrData[0] eq 'tagfield') {
1173
                    # Change of table because we begin with field name with former field names read
1174
                    seek($dom, $pos, 0);
1175
                    return 1;
1176
                }
1188
                }
1177
                if (scalar(@$fields) == scalar(@arrData)) {
1189
                if ($fieldsNameRead) {
1178
                    if (!$fieldsNameRead) {
1190
                    $fieldsStr = join( ',', @$fields );
1179
                        # New table, we read the field names
1191
                    $dataStr = '';
1180
                        $fieldsNameRead = 1;
1192
                    map { $dataStr .= '?,'; } @$fields;
1181
                        for (my $i=0; $i < @arrData; $i++) {
1193
                    chop($dataStr) if ($dataStr);
1182
                            if ($arrData[$i] ne $fields->[$i]) {
1194
                    $updateStr = '';
1183
                                $fieldsNameRead = 0;
1195
                    map { $updateStr .= $_ . '=?,'; } @$fields;
1184
                                last;
1196
                    chop($updateStr) if ($updateStr);
1185
                            }
1197
                }
1186
                        }
1198
            }
1187
                        if ($fieldsNameRead) {
1199
            else {
1188
                            $fieldsStr = join(',', @$fields);
1200
1189
                            $dataStr = '';
1201
                # Read data
1190
                            map { $dataStr .= '?,';} @$fields;
1202
                my $j          = 0;
1191
                            chop($dataStr) if ($dataStr);
1203
                my %dataFields = ();
1192
                            $updateStr = '';
1204
                for (@arrData) {
1193
                            map { $updateStr .= $_ . '=?,';} @$fields;
1205
                    if (   $fields->[$j] eq 'frameworkcode'
1194
                            chop($updateStr) if ($updateStr);
1206
                        && $_ ne $frameworkcode )
1195
                        }
1207
                    {
1196
                    } else {
1208
                        $dataFields{ $fields->[$j] } = $frameworkcode;
1197
                        # Read data
1209
                        $arrData[$j] = $frameworkcode;
1198
                        my $j = 0;
1210
                    }
1199
                        my %dataFields = ();
1211
                    else {
1200
                        for (@arrData) {
1212
                        $dataFields{ $fields->[$j] } = $_;
1201
                            if ($fields->[$j] eq 'frameworkcode' && $_ ne $frameworkcode) {
1202
                                $dataFields{$fields->[$j]} = $frameworkcode;
1203
                                $arrData[$j] = $frameworkcode;
1204
                            } else {
1205
                                $dataFields{$fields->[$j]} = $_;
1206
                            }
1207
                            $j++
1208
                        }
1209
                        $ok = _processRow_DB($dbh, $db_scheme, $table, $fieldsStr, $dataStr, $updateStr, \@arrData, \%dataFields, $PKArray, \@fieldsPK, $fields2Delete);
1210
                    }
1213
                    }
1214
                    $j++;
1211
                }
1215
                }
1212
                $pos = tell($dom);
1216
1217
                $ok = _processRow_DB(
1218
                    $dbh,       $db_scheme,   $table,
1219
                    $fieldsStr, $dataStr,     $updateStr,
1220
                    \@arrData,  \%dataFields, $PKArray,
1221
                    \@fieldsPK, $fields2Delete
1222
                );
1213
            }
1223
            }
1214
            @arrData = ();
1215
        }
1224
        }
1216
        $numRow++;
1225
        $pos = tell($dom);
1217
    }
1226
    }
1218
    return $ok;
1227
    return $ok;
1219
}#_import_table_csv
1228
}    #_import_table_csv
1220
1229
1221
1230
1222
# Import worksheet from the ods content.xml file to the mysql table
1231
# Import worksheet from the ods content.xml file to the mysql table
1223
- 

Return to bug 10538