From 352bb8e6d1f83bb42be37f78f059aa039f7c9dc2 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Mon, 19 Jun 2017 01:00:19 +0000 Subject: [PATCH] Bug 13952: [FOLLOW-UP] Fixing authority type import This patch fixes the comments from Comment 29 and the import functionality. You should now be able to import an exported file without editing the file at all and the authority type code will be overwritten in the file (same behaviour as biblio frameworks). Sponsored-by: Catalyst IT Signed-off-by: Katrin Fischer --- C4/ImportExportFramework.pm | 64 +++++++++++++++++++++++++++------------- admin/import_export_authtype.pl | 12 ++------ admin/import_export_framework.pl | 7 +---- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/C4/ImportExportFramework.pm b/C4/ImportExportFramework.pm index afe2c35..fc4a02a 100644 --- a/C4/ImportExportFramework.pm +++ b/C4/ImportExportFramework.pm @@ -680,11 +680,21 @@ sub ImportFramework # Process both tables my $numDeleted = 0; my $numDeletedAux = 0; - if (($numDeletedAux = _import_table($dbh, $table, $frameworkcode, $dom, ['frameworkcode', 'tagfield'], $extension, $frameworktype)) >= 0) { - $numDeleted += $numDeletedAux if ($numDeletedAux > 0); - if (($numDeletedAux = _import_table($dbh, $subtable, $frameworkcode, $dom, ['frameworkcode', 'tagfield', 'tagsubfield'], $extension, $frameworktype)) >= 0) { + if ($frameworktype eq "authority"){ + if (($numDeletedAux = _import_table($dbh, $table, $frameworkcode, $dom, ['authtypecode', 'tagfield'], $extension, $frameworktype)) >= 0) { $numDeleted += $numDeletedAux if ($numDeletedAux > 0); - $ok = ($numDeleted > 0)?$numDeleted:0; + if (($numDeletedAux = _import_table($dbh, $subtable, $frameworkcode, $dom, ['authtypecode', 'tagfield', 'tagsubfield'], $extension, $frameworktype)) >= 0) { + $numDeleted += $numDeletedAux if ($numDeletedAux > 0); + $ok = ($numDeleted > 0)?$numDeleted:0; + } + } + } else { + if (($numDeletedAux = _import_table($dbh, $table, $frameworkcode, $dom, ['frameworkcode', 'tagfield'], $extension, $frameworktype)) >= 0) { + $numDeleted += $numDeletedAux if ($numDeletedAux > 0); + if (($numDeletedAux = _import_table($dbh, $subtable, $frameworkcode, $dom, ['frameworkcode', 'tagfield', 'tagsubfield'], $extension, $frameworktype)) >= 0) { + $numDeleted += $numDeletedAux if ($numDeletedAux > 0); + $ok = ($numDeleted > 0)?$numDeleted:0; + } } } } else { @@ -837,11 +847,11 @@ sub _import_table push @fieldsName, $hashRef->{Field}; } }; - $ok = _import_table_csv($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, \@fieldsName); + $ok = _import_table_csv($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, \@fieldsName, $frameworktype); } elsif ($format eq 'ods') { - $ok = _import_table_ods($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete); + $ok = _import_table_ods($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, $frameworktype); } else { - $ok = _import_table_excel($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete); + $ok = _import_table_excel($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, $frameworktype); } if ($ok) { if (($ok = scalar(keys %fields2Delete)) > 0) { @@ -912,7 +922,7 @@ sub _processRow_DB # Process the rows of a worksheet and insert/update them in a mysql table. sub _processRows_Table { - my ($dbh, $frameworkcode, $nodeR, $table, $PKArray, $format, $fields2Delete) = @_; + my ($dbh, $frameworkcode, $nodeR, $table, $PKArray, $format, $fields2Delete, $frameworktype) = @_; my $query; my @fields = (); @@ -939,7 +949,7 @@ sub _processRows_Table chop($updateStr) if ($updateStr); } else { # Get data from row - my ($dataFields, $dataFieldsR) = _getDataFields($frameworkcode, $nodeR, \@fields, $format); + my ($dataFields, $dataFieldsR) = _getDataFields($frameworkcode, $nodeR, \@fields, $format, $frameworktype); if (scalar(@fields) == scalar(@$dataFieldsR)) { $ok = _processRow_DB($dbh, $db_scheme, $table, $fields, $dataStr, $updateStr, $dataFieldsR, $dataFields, $PKArray, \@fieldsPK, $fields2Delete); } @@ -957,8 +967,7 @@ sub _processRows_Table # Import worksheet from the csv file to the mysql table sub _import_table_csv { - my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $fields) = @_; - + my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $fields, $frameworktype) = @_; my $row = ''; my $partialRow = ''; my $numFields = @$fields; @@ -1014,13 +1023,23 @@ sub _import_table_csv my $j = 0; my %dataFields = (); for (@arrData) { - if ($fields->[$j] eq 'frameworkcode' && $_ ne $frameworkcode) { - $dataFields{$fields->[$j]} = $frameworkcode; - $arrData[$j] = $frameworkcode; + if ($frameworktype eq "authority"){ + if ($fields->[$j] eq 'authtypecode' && $_ ne $frameworkcode) { + $dataFields{$fields->[$j]} = $frameworkcode; + $arrData[$j] = $frameworkcode; + } else { + $dataFields{$fields->[$j]} = $_; + } + $j++ } else { - $dataFields{$fields->[$j]} = $_; + if ($fields->[$j] eq 'frameworkcode' && $_ ne $frameworkcode) { + $dataFields{$fields->[$j]} = $frameworkcode; + $arrData[$j] = $frameworkcode; + } else { + $dataFields{$fields->[$j]} = $_; + } + $j++ } - $j++ } $ok = _processRow_DB($dbh, $db_scheme, $table, $fieldsStr, $dataStr, $updateStr, \@arrData, \%dataFields, $PKArray, \@fieldsPK, $fields2Delete); } @@ -1037,7 +1056,7 @@ sub _import_table_csv # Import worksheet from the ods content.xml file to the mysql table sub _import_table_ods { - my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete) = @_; + my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $frameworktype) = @_; my $xc = XML::LibXML::XPathContext->new($dom); $xc->registerNs('xmlns:office','urn:oasis:names:tc:opendocument:xmlns:office:1.0'); @@ -1047,7 +1066,7 @@ sub _import_table_ods @nodes = $xc->findnodes('//table:table[@table:name="' . $table . '"]'); if (@nodes == 1 && $nodes[0]->hasChildNodes()) { my $nodeR = $nodes[0]->firstChild; - return _processRows_Table($dbh, $frameworkcode, $nodeR, $table, $PKArray, 'ods', $fields2Delete); + return _processRows_Table($dbh, $frameworkcode, $nodeR, $table, $PKArray, 'ods', $fields2Delete, $frameworktype); } else { $debug and warn "Error _import_table_ods there's not worksheet for $table\n"; } @@ -1058,7 +1077,7 @@ sub _import_table_ods # Import worksheet from the excel-xml file to the mysql table sub _import_table_excel { - my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete) = @_; + my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $frameworktype) = @_; my $xc = XML::LibXML::XPathContext->new($dom); $xc->registerNs('xmlns','urn:schemas-microsoft-com:office:spreadsheet'); @@ -1071,7 +1090,7 @@ sub _import_table_excel my @nodesT = $nodes[$i]->getElementsByTagNameNS('urn:schemas-microsoft-com:office:spreadsheet', 'Table'); if (@nodesT == 1 && $nodesT[0]->hasChildNodes()) { my $nodeR = $nodesT[0]->firstChild; - return _processRows_Table($dbh, $frameworkcode, $nodeR, $table, $PKArray, undef, $fields2Delete); + return _processRows_Table($dbh, $frameworkcode, $nodeR, $table, $PKArray, undef, $fields2Delete, $frameworktype); } } } else { @@ -1108,7 +1127,7 @@ sub _getDataNodeODS # Get the data from a row of a spreadsheet sub _getDataFields { - my ($frameworkcode, $node, $fields, $format) = @_; + my ($frameworkcode, $node, $fields, $format, $frameworktype) = @_; my $dataFields = {}; my @dataFieldsA = (); @@ -1135,6 +1154,9 @@ sub _getDataFields if ($ok) { $data = '' if ($data eq '#'); $data = $frameworkcode if ($fields->[$i] eq 'frameworkcode'); + if ($frameworktype eq "authority") { + $data = $frameworkcode if ($fields->[$i] eq 'authtypecode'); + } $dataFields->{$fields->[$i]} = $data; push @dataFieldsA, $data; $i++; diff --git a/admin/import_export_authtype.pl b/admin/import_export_authtype.pl index cbf4a16..05cdb7c 100755 --- a/admin/import_export_authtype.pl +++ b/admin/import_export_authtype.pl @@ -17,9 +17,7 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . - -use strict; -use warnings; +use Modern::Perl; use CGI qw ( -utf8 ); use CGI::Cookie; use C4::Context; @@ -27,7 +25,6 @@ use C4::Auth qw/check_cookie_auth/; use C4::ImportExportFramework; my %cookies = CGI::Cookie->fetch(); -my $authenticated = 0; my ($auth_status, $sessionID); if (exists $cookies{'CGISESSID'}) { ($auth_status, $sessionID) = check_cookie_auth( @@ -35,13 +32,10 @@ if (exists $cookies{'CGISESSID'}) { { parameters => 'parameters_remaining_permissions' }, ); } -if ($auth_status eq 'ok') { - $authenticated = 1; -} my $input = new CGI; -unless ($authenticated) { +unless ($auth_status eq 'ok') { print $input->header(-type => 'text/plain', -status => '403 Forbidden'); exit 0; } @@ -88,7 +82,7 @@ if ($action eq 'export' && $input->request_method() eq 'GET') { my $extension = $1; my $uploadFd = $input->upload($fieldname); if ($uploadFd && !$input->cgi_error) { - my $tmpfilename = $input->tmpFileName($input->param($fieldname)); + my $tmpfilename = $input->tmpFileName(scalar $input->param($fieldname)); $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension $ok = ImportFramework($filename, $authtypecode, 1, 'authority') if (rename($tmpfilename, $filename)); } diff --git a/admin/import_export_framework.pl b/admin/import_export_framework.pl index 56c6efa..c0017fa 100755 --- a/admin/import_export_framework.pl +++ b/admin/import_export_framework.pl @@ -17,7 +17,6 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . - use Modern::Perl; use CGI qw ( -utf8 ); use CGI::Cookie; @@ -26,7 +25,6 @@ use C4::Auth qw/check_cookie_auth/; use C4::ImportExportFramework; my %cookies = CGI::Cookie->fetch(); -my $authenticated = 0; my ($auth_status, $sessionID); if (exists $cookies{'CGISESSID'}) { ($auth_status, $sessionID) = check_cookie_auth( @@ -34,13 +32,10 @@ if (exists $cookies{'CGISESSID'}) { { parameters => 'manage_marc_frameworks' }, ); } -if ($auth_status eq 'ok') { - $authenticated = 1; -} my $input = new CGI; -unless ($authenticated) { +unless ($auth_status eq 'ok') { print $input->header(-type => 'text/plain', -status => '403 Forbidden'); exit 0; } -- 2.1.4