From 36d4cfaf82b5dd6e1e97ef7d348b9d3f3c602974 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 24 Oct 2019 18:28:19 +0200 Subject: [PATCH] Bug 23892: wip - does not work --- C4/ImportExportFramework.pm | 173 +++++--------------------------------------- 1 file changed, 17 insertions(+), 156 deletions(-) diff --git a/C4/ImportExportFramework.pm b/C4/ImportExportFramework.pm index d4a11cabc5..bfd27e0148 100644 --- a/C4/ImportExportFramework.pm +++ b/C4/ImportExportFramework.pm @@ -40,41 +40,6 @@ BEGIN { ); } - -use constant XMLSTR => ' - - - - - - - - - - - - -'; - - use constant ODSSTR => ' @@ -171,7 +136,7 @@ use constant ODS_MANIFEST_STR => ' =head1 NAME -C4::ImportExportFramework - Import/Export Framework to Excel-xml/ODS Module Functions +C4::ImportExportFramework - Import/Export Framework to CSV or ODS Module Functions =head1 SYNOPSIS @@ -179,10 +144,10 @@ C4::ImportExportFramework - Import/Export Framework to Excel-xml/ODS Module Func =head1 DESCRIPTION -Module to Import/Export Framework to Excel-xml/ODS on intranet administration - MARC Frameworks section +Module to Import/Export Framework to CSV or ODS on intranet administration - MARC Frameworks section -Module to Import/Export Framework to Excel-xml/ODS on intranet administration - MARC Frameworks section -exporting the tables marc_tag_structure, marc_subfield_structure to excel-xml/ods or viceversa +Module to Import/Export Framework to CSV or ODS on intranet administration - MARC Frameworks section +exporting the tables marc_tag_structure, marc_subfield_structure to csv/ods or viceversa Functions for handling import/export. @@ -193,7 +158,7 @@ Functions for handling import/export. =head2 ExportFramework -Export all the information of a Framework to an excel "xml" file or OpenDocument SpreadSheet "ods" file. +Export all the information of a Framework to a CSV file or OpenDocument SpreadSheet "ods" file. return : succes @@ -209,18 +174,15 @@ sub ExportFramework my $dom; my $root; my $elementSS; - if ($mode eq 'ods' || $mode eq 'excel') { + if ($mode eq 'ods') { eval { my $parser = XML::LibXML->new(); - $dom = $parser->parse_string(($mode && $mode eq 'ods')?ODSSTR:XMLSTR); + $dom = $parser->parse_string(ODSSTR); if ($dom) { - $root = $dom->documentElement(); - if ($mode && $mode eq 'ods') { - my $elementBody = $dom->createElement('office:body'); - $root->appendChild($elementBody); - $elementSS = $dom->createElement('office:spreadsheet'); - $elementBody->appendChild($elementSS); - } + my $elementBody = $dom->createElement('office:body'); + $root->appendChild($elementBody); + $elementSS = $dom->createElement('office:spreadsheet'); + $elementBody->appendChild($elementSS); } }; if ($@) { @@ -229,9 +191,9 @@ sub ExportFramework } } - if (_export_table('marc_tag_structure', $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, ($mode eq 'ods')?$elementSS:$root, $frameworkcode, $mode)) { - if (_export_table('marc_subfield_structure', $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, ($mode eq 'ods')?$elementSS:$root, $frameworkcode, $mode)) { - $$xmlStrRef = $dom->toString(1) if ($mode eq 'ods' || $mode eq 'excel'); + if (_export_table('marc_tag_structure', $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, $elementSS, $frameworkcode, $mode)) { + if (_export_table('marc_subfield_structure', $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, $elementSS, $frameworkcode, $mode)) { + $$xmlStrRef = $dom->toString(1) if ($mode eq 'ods'); return 1; } } @@ -250,8 +212,6 @@ sub _export_table _export_table_csv($table, $dbh, $dom, $root, $frameworkcode); } elsif ($mode eq 'ods') { _export_table_ods($table, $dbh, $dom, $root, $frameworkcode); - } else { - _export_table_excel($table, $dbh, $dom, $root, $frameworkcode); } } @@ -369,76 +329,6 @@ sub _export_table_ods }#_export_table_ods -# Export the mysql table to an excel-xml (openoffice/libreoffice compatible) file -sub _export_table_excel -{ - my ($table, $dbh, $dom, $root, $frameworkcode) = @_; - - eval { - my $elementWS = $dom->createElement('Worksheet'); - $elementWS->setAttribute('ss:Name', $table); - $root->appendChild($elementWS); - my $elementTable = $dom->createElement('ss:Table'); - $elementWS->appendChild($elementTable); - my $elementRow = $dom->createElement('ss:Row'); - $elementTable->appendChild($elementRow); - - # First row with the name of the columns - my $elementCell; - my $elementData; - my $query = 'SHOW COLUMNS FROM ' . $table; - my $sth = $dbh->prepare($query); - $sth->execute(); - my @fields = (); - while (my $hashRef = $sth->fetchrow_hashref) { - $elementCell = $dom->createElement('ss:Cell'); - $elementCell->setAttribute('ss:StyleID', 's27'); - $elementRow->appendChild($elementCell); - $elementData = $dom->createElement('ss:Data'); - $elementData->setAttribute('ss:Type', 'String'); - $elementCell->appendChild($elementData); - $elementData->appendTextNode($hashRef->{Field}); - push @fields, {name => $hashRef->{Field}, type => ($hashRef->{Type} =~ /int/i)?'Number':'String'}; - } - # Populate rows with the data from mysql - $query = 'SELECT * FROM ' . $table . ' WHERE frameworkcode=?'; - $sth = $dbh->prepare($query); - $sth->execute($frameworkcode); - my $data; - while (my $hashRef = $sth->fetchrow_hashref) { - $elementRow = $dom->createElement('ss:Row'); - $elementTable->appendChild($elementRow); - for (@fields) { - $elementCell = $dom->createElement('ss:Cell'); - $elementRow->appendChild($elementCell); - $elementData = $dom->createElement('ss:Data'); - $elementData->setAttribute('ss:Type', $_->{type}); - $elementCell->appendChild($elementData); - $data = $hashRef->{$_->{name}}; - if ($_->{type} eq 'Number' && !defined($data)) { - $data = '0'; - } elsif ($_->{type} eq 'String' && !defined($data)) { - $data = q{}; - } elsif ($_->{type} eq 'String' && (!$data && $data ne '0')) { - $data = '#'; - } - $elementData->appendTextNode(($_->{type} eq 'String')?_parseContent2Xml($data):$data); - } - } - }; - if ($@) { - $debug and warn "Error _export_table_excel $@\n"; - return 0; - } - return 1; -}#_export_table_excel - - - - - - - # Format chars problematics to a correct format for xml. sub _parseContent2Xml { @@ -623,7 +513,7 @@ sub _getMeta =head2 ImportFramework -Import all the information of a Framework from a excel-xml/ods file. +Import all the information of a Framework from a csv or ods file. return : success @@ -786,7 +676,7 @@ sub _check_validity_worksheet }#_check_validity_worksheet -# Import the data from an excel-xml/ods to mysql tables. +# Import the data from a csv or ods to mysql tables. sub _import_table { my ($dbh, $table, $frameworkcode, $dom, $PKArray, $format) = @_; @@ -823,8 +713,6 @@ sub _import_table $ok = _import_table_csv($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete, \@fieldsName); } elsif ($format eq 'ods') { $ok = _import_table_ods($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete); - } else { - $ok = _import_table_excel($dbh, $table, $frameworkcode, $dom, $PKArray, \%fields2Delete); } if ($ok) { if (($ok = scalar(keys %fields2Delete)) > 0) { @@ -908,7 +796,7 @@ sub _processRows_Table my @fieldsPK = @$PKArray; shift @fieldsPK; while ($nodeR) { - if ($nodeR->nodeType == 1 && (($format && $format eq 'ods' && $nodeR->nodeName =~ /(?:table:)?table-row/) || ($nodeR->nodeName =~ /(?:ss:)?Row/)) && $nodeR->hasChildNodes()) { + if ($nodeR->nodeType == 1 && (($nodeR->nodeName =~ /(?:table:)?table-row/) || ($nodeR->nodeName =~ /(?:ss:)?Row/)) && $nodeR->hasChildNodes()) { if ($j == 0) { # Get name columns _getFields($nodeR, \@fields, $format); @@ -1037,33 +925,6 @@ sub _import_table_ods return 0; }#_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 $xc = XML::LibXML::XPathContext->new($dom); - $xc->registerNs('xmlns','urn:schemas-microsoft-com:office:spreadsheet'); - $xc->registerNs('xmlns:ss','urn:schemas-microsoft-com:office:spreadsheet'); - $xc->registerNs('xmlns:x','urn:schemas-microsoft-com:office:excel'); - my @nodes; - @nodes = $xc->findnodes('//ss:Worksheet[@ss:Name="' . $table . '"]'); - if (@nodes > 0) { - for (my $i=0; $i < @nodes; $i++) { - 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); - } - } - } else { - $debug and warn "Error _import_table_excel there's not worksheet for $table\n"; - } - return 0; -}#_import_table_excel - - # Get the data from a cell on a ods file through the value attribute or the text node sub _getDataNodeODS { -- 2.11.0