@@ -, +, @@ Before you import, make sure that the authtypecode of the file you are going to import matches the Code of your new authority type. Use Vim or something similar to do a search and replace and change the codes in your file - do NOT use LibreOffice etc. because it tries to be too helpful and messes with the formatting of the file (this is what has broken the Import function in previous testing of this patch) --- C4/ImportExportFramework.pm | 65 ++++++---- admin/import_export_authtype.pl | 101 ++++++++++++++++ admin/import_export_framework.pl | 4 +- .../prog/en/modules/admin/authtypes.tt | 133 +++++++++++++++++++-- 4 files changed, 269 insertions(+), 34 deletions(-) create mode 100755 admin/import_export_authtype.pl --- a/C4/ImportExportFramework.pm +++ a/C4/ImportExportFramework.pm @@ -193,7 +193,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 MARC or Authority Type Framework to an excel "xml" file, comma separated values "csv" file or OpenDocument SpreadSheet "ods" file. return : succes @@ -202,7 +202,7 @@ succes sub ExportFramework { - my ($frameworkcode, $xmlStrRef, $mode) = @_; + my ($frameworkcode, $xmlStrRef, $mode, $frameworktype) = @_; my $dbh = C4::Context->dbh; if ($dbh) { @@ -229,8 +229,15 @@ 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)) { + my $table = 'marc_tag_structure'; + my $subtable = 'marc_subfield_structure'; + if ($frameworktype eq "authority") { + $table = 'auth_tag_structure'; + $subtable = 'auth_subfield_structure'; + } + + if (_export_table($table, $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, ($mode eq 'ods')?$elementSS:$root, $frameworkcode, $mode, $frameworktype)) { + if (_export_table($subtable, $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, ($mode eq 'ods')?$elementSS:$root, $frameworkcode, $mode, $frameworktype)) { $$xmlStrRef = $dom->toString(1) if ($mode eq 'ods' || $mode eq 'excel'); return 1; } @@ -245,20 +252,20 @@ sub ExportFramework # Export all the data from a mysql table to an spreadsheet. sub _export_table { - my ($table, $dbh, $dom, $root, $frameworkcode, $mode) = @_; + my ($table, $dbh, $dom, $root, $frameworkcode, $mode, $frameworktype) = @_; if ($mode eq 'csv') { - _export_table_csv($table, $dbh, $dom, $root, $frameworkcode); + _export_table_csv($table, $dbh, $dom, $root, $frameworkcode, $frameworktype); } elsif ($mode eq 'ods') { - _export_table_ods($table, $dbh, $dom, $root, $frameworkcode); + _export_table_ods($table, $dbh, $dom, $root, $frameworkcode, $frameworktype); } else { - _export_table_excel($table, $dbh, $dom, $root, $frameworkcode); + _export_table_excel($table, $dbh, $dom, $root, $frameworkcode, $frameworktype); } } # Export the mysql table to an csv file sub _export_table_csv { - my ($table, $dbh, $strCSV, $root, $frameworkcode) = @_; + my ($table, $dbh, $strCSV, $root, $frameworkcode, $frameworktype) = @_; eval { # First row with the name of the columns @@ -274,6 +281,9 @@ sub _export_table_csv $$strCSV .= chr(10); # Populate rows with the data from mysql $query = 'SELECT * FROM ' . $table . ' WHERE frameworkcode=?'; + if ($frameworktype eq "authority") { + $query = 'SELECT * FROM ' . $table . ' WHERE authtypecode=?'; + } $sth = $dbh->prepare($query); $sth->execute($frameworkcode); my $data; @@ -305,7 +315,7 @@ sub _export_table_csv # Export the mysql table to an ods file sub _export_table_ods { - my ($table, $dbh, $dom, $root, $frameworkcode) = @_; + my ($table, $dbh, $dom, $root, $frameworkcode, $frameworktype) = @_; eval { my $elementTable = $dom->createElement('table:table'); @@ -334,6 +344,9 @@ sub _export_table_ods } # Populate rows with the data from mysql $query = 'SELECT * FROM ' . $table . ' WHERE frameworkcode=?'; + if ($frameworktype eq "authority") { + $query = 'SELECT * FROM ' . $table . ' WHERE authtypecode=?'; + } $sth = $dbh->prepare($query); $sth->execute($frameworkcode); my $data; @@ -369,7 +382,7 @@ sub _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) = @_; + my ($table, $dbh, $dom, $root, $frameworkcode, $frameworktype) = @_; eval { my $elementWS = $dom->createElement('Worksheet'); @@ -399,6 +412,9 @@ sub _export_table_excel } # Populate rows with the data from mysql $query = 'SELECT * FROM ' . $table . ' WHERE frameworkcode=?'; + if ($frameworktype eq "authority") { + $query = 'SELECT * FROM ' . $table . ' WHERE authtypecode=?'; + } $sth = $dbh->prepare($query); $sth->execute($frameworkcode); my $data; @@ -428,12 +444,6 @@ sub _export_table_excel return 1; }#_export_table_excel - - - - - - # Format chars problematics to a correct format for xml. sub _parseContent2Xml { @@ -618,7 +628,7 @@ sub _getMeta =head2 ImportFramework -Import all the information of a Framework from a excel-xml/ods file. +Import all the information of a MARC or Authority Type Framework from a excel-xml/ods file. return : success @@ -627,7 +637,7 @@ success sub ImportFramework { - my ($filename, $frameworkcode, $deleteFilename) = @_; + my ($filename, $frameworkcode, $deleteFilename, $frameworktype) = @_; my $tempdir; my $ok = -1; @@ -657,13 +667,21 @@ sub ImportFramework # They are text files, so open it to read open($dom, '<', $filename); } + + my $table = 'marc_tag_structure'; + my $subtable = 'marc_subfield_structure'; + if ($frameworktype eq "authority") { + $table = 'auth_tag_structure'; + $subtable = 'auth_subfield_structure'; + } + if ($dom) { # Process both tables my $numDeleted = 0; my $numDeletedAux = 0; - if (($numDeletedAux = _import_table($dbh, 'marc_tag_structure', $frameworkcode, $dom, ['frameworkcode', 'tagfield'], $extension)) >= 0) { + if (($numDeletedAux = _import_table($dbh, $table, $frameworkcode, $dom, ['frameworkcode', 'tagfield'], $extension, $frameworktype)) >= 0) { $numDeleted += $numDeletedAux if ($numDeletedAux > 0); - if (($numDeletedAux = _import_table($dbh, 'marc_subfield_structure', $frameworkcode, $dom, ['frameworkcode', 'tagfield', 'tagsubfield'], $extension)) >= 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; } @@ -784,7 +802,7 @@ sub _check_validity_worksheet # Import the data from an excel-xml/ods to mysql tables. sub _import_table { - my ($dbh, $table, $frameworkcode, $dom, $PKArray, $format) = @_; + my ($dbh, $table, $frameworkcode, $dom, $PKArray, $format, $frameworktype) = @_; my %fields2Delete; my $query; my @fields; @@ -793,6 +811,9 @@ sub _import_table @fields = @$PKArray; shift @fields; $query = 'SELECT ' . join(',', @fields) . ' FROM ' . $table . ' WHERE frameworkcode=?'; + if ($frameworktype eq "authority") { + $query = 'SELECT ' . join(',', @fields) . ' FROM ' . $table . ' WHERE authtypecode=?'; + } my $sth = $dbh->prepare($query); $sth->execute($frameworkcode); my $field; --- a/admin/import_export_authtype.pl +++ a/admin/import_export_authtype.pl @@ -0,0 +1,101 @@ +#!/usr/bin/perl + +# Copyright 2016 Aleisha Amohia +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + + +use strict; +use warnings; +use CGI qw ( -utf8 ); +use CGI::Cookie; +use C4::Context; +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( + $cookies{'CGISESSID'}->value, + { parameters => 'parameters_remaining_permissions' }, + ); +} +if ($auth_status eq 'ok') { + $authenticated = 1; +} + +my $input = new CGI; + +unless ($authenticated) { + print $input->header(-type => 'text/plain', -status => '403 Forbidden'); + exit 0; +} + +my $authtypecode = $input->param('authtypecode') || 'default'; +my $action = $input->param('action') || 'export'; + +## Exporting +if ($action eq 'export' && $input->request_method() eq 'GET') { + my $strXml = ''; + my $format = $input->param('type_export_' . $authtypecode); + if ($authtypecode == 'default') { + ExportFramework('', \$strXml, $format, 'authority'); + } else { + ExportFramework($authtypecode, \$strXml, $format, 'authority'); + } + + if ($format eq 'csv') { + # CSV file + + # Correctly set the encoding to output plain text in UTF-8 + binmode(STDOUT,':encoding(UTF-8)'); + print $input->header(-type => 'application/vnd.ms-excel', -attachment => 'export_' . $authtypecode . '.csv'); + print $strXml; + } elsif ($format eq 'excel') { + # Excel-xml file + print $input->header(-type => 'application/excel', -attachment => 'export_' . $authtypecode . '.xml'); + print $strXml; + } else { + # ODS file + my $strODS = ''; + createODS($strXml, 'en', \$strODS); + print $input->header(-type => 'application/vnd.oasis.opendocument.spreadsheet', -attachment => 'export_' . $authtypecode . '.ods'); + print $strODS; + } +## Importing +} elsif ($input->request_method() eq 'POST') { + my $ok = -1; + my $fieldname = 'file_import_' . $authtypecode; + my $filename = $input->param($fieldname); + # upload the input file + if ($filename && $filename =~ /\.(csv|ods|xml)$/i) { + my $extension = $1; + my $uploadFd = $input->upload($fieldname); + if ($uploadFd && !$input->cgi_error) { + my $tmpfilename = $input->tmpFileName($input->param($fieldname)); + $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension + $ok = ImportFramework($filename, $authtypecode, 1, 'authority') if (rename($tmpfilename, $filename)); + } + } + if ($ok >= 0) { # If everything went ok go to the authority type marc structure + print $input->redirect( -location => '/cgi-bin/koha/admin/auth_tag_structure.pl?authtypecode=' . $authtypecode); + } else { + # If something failed go to the list of authority types and show message + print $input->redirect( -location => '/cgi-bin/koha/admin/authtypes.pl?error_import_export=' . $authtypecode); + } +} --- a/admin/import_export_framework.pl +++ a/admin/import_export_framework.pl @@ -53,7 +53,7 @@ my $action = $input->param('action') || 'export'; if ($action eq 'export' && $input->request_method() eq 'GET') { my $strXml = ''; my $format = $input->param('type_export_' . $frameworkcode); - ExportFramework($frameworkcode, \$strXml, $format); + ExportFramework($frameworkcode, \$strXml, $format, 'biblio'); if ($format eq 'csv') { # CSV file @@ -85,7 +85,7 @@ if ($action eq 'export' && $input->request_method() eq 'GET') { if ($uploadFd && !$input->cgi_error) { my $tmpfilename = $input->tmpFileName($input->param($fieldname)); $filename = $tmpfilename . '.' . $extension; # rename the tmp file with the extension - $ok = ImportFramework($filename, $frameworkcode, 1) if (rename($tmpfilename, $filename)); + $ok = ImportFramework($filename, $frameworkcode, 1, 'biblio') if (rename($tmpfilename, $filename)); } } if ($ok >= 0) { # If everything went ok go to the framework marc structure --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt @@ -11,15 +11,83 @@ [% INCLUDE 'datatables.inc' %] @@ -161,15 +229,60 @@ [% authority_type.summary %] [% authority_type.auth_tag_to_report %] - + + + + + [% END %] --