From 3d06472310855be4e543c5cb9dde9bca2821274b Mon Sep 17 00:00:00 2001 From: Aleisha Date: Tue, 26 Jan 2016 23:39:26 +0000 Subject: [PATCH] Bug 13952: Import and export authority types This patch amends C4::ImportExportFramework to work for authority types as well as MARC frameworks. New file: admin/import_export_authtype.pl To test: 1) Go to Admin -> Authority types 2) Confirm there are two new columns 'Export' and 'Import' in the table 3) Click 'Export' on an existing authority type and choose a file type, click 'Export' 4) Confirm that the authority type is exported as your chosen file type. Save the file 5) Create a new authority type 6) Click 'Import' into your new authority type (so you don't overwrite any important existing authority types) 7) Select the file you just exported and click 'Import' 8) Confirm you are taken to auth_tag_structure.pl 9) Go back to Authority types 10) Click 'Import' next to any existing authority type and attempt to import a file that is not XML, CSV or ODS. Confirm that this fails and you are asked to import a file of the correct file type 11) Go to Admin -> MARC bibliographic framework 12) Confirm that the 'Export' and 'Import' functions still work here as well --- C4/ImportExportFramework.pm | 37 +++--- admin/import_export_authtype.pl | 97 ++++++++++++++++ .../prog/en/modules/admin/authtypes.tt | 121 +++++++++++++++++++- 3 files changed, 240 insertions(+), 15 deletions(-) create mode 100755 admin/import_export_authtype.pl diff --git a/C4/ImportExportFramework.pm b/C4/ImportExportFramework.pm index f829f0c..8b10eef 100644 --- a/C4/ImportExportFramework.pm +++ b/C4/ImportExportFramework.pm @@ -194,7 +194,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 @@ -203,7 +203,7 @@ succes sub ExportFramework { - my ($frameworkcode, $xmlStrRef, $mode) = @_; + my ($frameworkcode, $xmlStrRef, $mode, $frameworktype) = @_; my $dbh = C4::Context->dbh; if ($dbh) { @@ -230,8 +230,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 && $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)) { + if (_export_table($subtable, $dbh, ($mode eq 'csv')?$xmlStrRef:$dom, ($mode eq 'ods')?$elementSS:$root, $frameworkcode, $mode)) { $$xmlStrRef = $dom->toString(1) if ($mode eq 'ods' || $mode eq 'excel'); return 1; } @@ -429,12 +436,6 @@ sub _export_table_excel return 1; }#_export_table_excel - - - - - - # Format chars problematics to a correct format for xml. sub _parseContent2Xml { @@ -619,7 +620,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 @@ -628,7 +629,7 @@ success sub ImportFramework { - my ($filename, $frameworkcode, $deleteFilename) = @_; + my ($filename, $frameworkcode, $deleteFilename, $frameworktype) = @_; my $tempdir; my $ok = -1; @@ -658,13 +659,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 && $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)) >= 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)) >= 0) { $numDeleted += $numDeletedAux if ($numDeletedAux > 0); $ok = ($numDeleted > 0)?$numDeleted:0; } diff --git a/admin/import_export_authtype.pl b/admin/import_export_authtype.pl new file mode 100755 index 0000000..bf8bcef --- /dev/null +++ b/admin/import_export_authtype.pl @@ -0,0 +1,97 @@ +#!/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') || ''; +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); + 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); + } +} diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt index 5a82fbf..a7e3e59 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authtypes.tt @@ -11,15 +11,83 @@ [% INCLUDE 'datatables.inc' %] @@ -153,6 +221,8 @@   Edit Delete + Export + Import @@ -165,6 +235,55 @@ MARC structure Edit Delete + + + Export + + + + + + Import + + + [% END %] -- 1.7.10.4