From 6d75e385c818212cc10a9832b5a5525cbef6e6b2 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 31 Dec 2015 10:23:24 +0000 Subject: [PATCH] Bug 15451: Rewrite the csv profile tool script This page was inconsistent with the other admin and tool pages. The 2 tabs 'New profile' and 'Edit existing profile' were useless, the ergonomic needs to be revisited. This patch applies the same script/page structure as others: by default a table containing all csv profiles is displayed with 2 action links: edit and delete. Test plan: 1/ Create 1+ CSV profiles, with different types (marc and sql) 2/ Update some values using the Edit link 3/ Delete a CSV profile Note: When deleting a CSV profile, it would be great to warn the user if it is used. Signed-off-by: Bernardo Gonzalez Kriegel No problems on create, update and delete. No errors Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall --- .../prog/en/modules/tools/csv-profiles.tt | 660 +++++++++------------ tools/csv-profiles.pl | 284 ++++----- 2 files changed, 410 insertions(+), 534 deletions(-) rewrite koha-tmpl/intranet-tmpl/prog/en/modules/tools/csv-profiles.tt (97%) rewrite tools/csv-profiles.pl (71%) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/csv-profiles.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/csv-profiles.tt dissimilarity index 97% index 35b7b46132..5fe6a9152d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/csv-profiles.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/csv-profiles.tt @@ -1,396 +1,264 @@ -[% INCLUDE 'doc-head-open.inc' %]Koha › Tools › CSV export profiles[% INCLUDE 'doc-head-close.inc' %] - - - - - [% INCLUDE 'header.inc' %] - [% INCLUDE 'cat-search.inc' %] - - - -
-
-
-
- [% IF ( success || error ) %] - [% IF ( success ) %] - [% IF ( action == 'create' ) %]
The new CSV profile "[% profile_name %]" has been successfully created.
[% END %] - [% IF ( action == 'edit' ) %]
The CSV profile has been successfully modified.
[% END %] - [% IF ( action == 'delete' ) %]
The CSV profile has been successfully deleted.
[% END %] - [% ELSE %] - [% IF ( action == 'create' ) %]
The new CSV profile "[% profile_name %]" has not been created.
[% END %] - [% IF ( action == 'edit' ) %]
The CSV profile has not been modified.
[% END %] - [% IF ( action == 'delete' ) %]
The CSV profile has not been deleted.
[% END %] - [% END %] - [% END %] -
- -
-

New CSV export profile

- -
-
-
    -
  1. - - - Required -
  2. - -
  3. - - - Required -
  4. - -
  5. -
  6. - -
  7. - -
  8. - -
  9. - -
  10. - -
  11. - -
  12. - -
  13. -
  14. - - -
  15. - - - Required -

    You have to define which fields or subfields you want to export, separated by pipes.

    -

    You can also use your own headers (instead of the ones from Koha) by prefixing the field number with an header, followed by the equal sign.

    -

    Example: Personal name=200|Entry element=210$a|300|009

    -

    You can use Template Toolkit tags too. See the help page for more information.

    -
  16. -
  17. - - -

    You have to define which fields you want to export, separated by pipes.

    -

    You can also use your own headers (instead of the ones from Koha) by prefixing the field name with an header, followed by the equal sign.

    -

    Example: Name=subscription.name|Title=subscription.title|Issue number=serial.serialseq

    -
  18. -
-
-
-
-
-
-
- [% IF ( existing_profiles ) %] -
-

Modify or delete a CSV export profile

- -
-
-
    -
  1. - - Required -
  2. - -
  3. - - - Required -
  4. - - -
  5. -
  6. - -
  7. -
  8. - -
  9. -
  10. - - -
  11. -
  12. - -
  13. -
  14. - -
  15. - - - Required -
  16. - -
  17. - - -
  18. - -
  19. -
  20. -
- -
- -
- - Cancel
-
-
- [% END %] - -
-
-
-
- [% INCLUDE 'tools-menu.inc' %] -
-
- [% INCLUDE 'intranet-bottom.inc' %] +[% INCLUDE 'doc-head-open.inc' %]Koha › Tools › CSV export profiles[% INCLUDE 'doc-head-close.inc' %] + + + + +[% INCLUDE 'header.inc' %] +[% INCLUDE 'cat-search.inc' %] + + + +
+
+
+
+ +[% FOR m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'error_on_update' %] + An error occurred when updating this CSV profile. Perhaps it already exists. + [% CASE 'error_on_insert' %] + An error occurred when adding this CSV profile. + [% CASE 'error_on_delete' %] + An error occurred when deleting this CSV profile. Check the logs. + [% CASE 'success_on_update' %] + CSV profile updated successfully. + [% CASE 'success_on_insert' %] + CSV profile added successfully. + [% CASE 'success_on_delete' %] + CSV profile deleted successfully. + [% CASE 'already_exists' %] + This CSV profile already exists. + [% CASE %] + [% m.code %] + [% END %] +
+[% END %] + +[% BLOCK list_separator_options %] + [% IF selected_separator == ',' %] + + [% ELSE %] + + [% END %] + [% IF selected_separator == '|' %] + + [% ELSE %] + + [% END %] + [% IF selected_separator == ';' %] + + [% ELSE %] + + [% END %] + [% IF selected_separator == '#' %] + + [% ELSE %] + + [% END %] + [% IF selected_separator == ' ' %] + + [% ELSE %] + + [% END %] + [% IF selected_separator == '\t' %] + + [% ELSE %] + + [% END %] + [% IF selected_separator == '\n' %] + + [% ELSE %] + + [% END %] +[% END %] + +[% IF op == 'add_form' %] + [% IF csv_profile %] +

Modify a CSV profile

+ [% ELSE %] +

New CSV profile

+ [% END %] + +
+ + +
+
    + [% IF csv_profile %] +
  1. Profile ID: [% csv_profile.export_format_id %]
  2. + [% END %] +
  3. + + + Required +
  4. +
  5. + + + Required +
  6. +
  7. + + +
  8. +
  9. + + +
  10. + +
  11. + + +
  12. + +
  13. + +
  14. + +
  15. + +
  16. + +
  17. + + + Required +

    You have to define which fields or subfields you want to export, separated by pipes.

    +

    You can also use your own headers (instead of the ones from Koha) by prefixing the field number with an header, followed by the equal sign.

    +

    Example: Personal name=200|Entry element=210$a|300|009

    +

    You can use Template Toolkit tags too. See the help page for more information.

    +
  18. + +
  19. + + +

    You have to define which fields you want to export, separated by pipes.

    +

    You can also use your own headers (instead of the ones from Koha) by prefixing the field name with an header, followed by the equal sign.

    +

    Example: Name=subscription.name|Title=subscription.title|Issue number=serial.serialseq

    +
  20. +
+
+
+ + Cancel +
+
+[% END %] + +[% IF op == 'delete_confirm' %] +
+

Delete CSV Profile "[% csv_profile.profile %]?"

+
+ + + +
+
+ +
+
+[% END %] + +[% IF op == 'list' %] + + + +

CSV profiles

+ + [% IF csv_profiles%] + + + + + + + + + + + + + [% FOREACH csv_profile IN csv_profiles %] + + + + + + + [% IF csv_profile.type == 'sql' %] + + [% ELSE %] + + [% END %] + + + + [% END %] + +
CSV profile IDNameDescriptionContentCSV separatorCSV type  
[% csv_profile.export_format_id %][% csv_profile.profile %][% csv_profile.description %][% csv_profile.content %][% csv_profile.csv_separator %]SQLMARCEditDelete
+ [% ELSE %] + There is no CSV profile defined. Create a new CSV profile. + [% END %] +[% END %] + +
+
+
+[% INCLUDE 'tools-menu.inc' %] +
+
+[% INCLUDE 'intranet-bottom.inc' %] diff --git a/tools/csv-profiles.pl b/tools/csv-profiles.pl dissimilarity index 71% index 7323a6e10f..7db0f67417 100755 --- a/tools/csv-profiles.pl +++ b/tools/csv-profiles.pl @@ -1,138 +1,146 @@ -#!/usr/bin/perl - -# Copyright 2009 BibLibre -# -# 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 . - -=head1 NAME - -csv-profile.pl : Defines a CSV export profile - -=head1 SYNOPSIS - -=cut - -=head1 DESCRIPTION - -This script allow the user to define a new profile for CSV export - -=head1 FUNCTIONS - -=cut - -use strict; -#use warnings; FIXME - Bug 2505 -use Data::Dumper; -use Encode; - -use C4::Auth; -use C4::Context; -use C4::Output; -use CGI qw ( -utf8 ); -use C4::Koha; -use C4::Csv; - -my $input = new CGI; -my $dbh = C4::Context->dbh; - -# open template -my ( $template, $loggedinuser, $cookie ) = get_template_and_user( - { - template_name => "tools/csv-profiles.tt", - query => $input, - type => "intranet", - authnotrequired => 0, - flagsrequired => { tools => 'manage_csv_profiles' }, - debug => 1, - } -); - -# Getting available encodings list -my @encodings = Encode->encodings(); -my @encodings_loop = map{{encoding => $_}} @encodings; -$template->param(encodings => \@encodings_loop); - -my $profile_name = $input->param("profile_name"); -my $profile_description = $input->param("profile_description"); -my $csv_separator = $input->param("csv_separator"); -my $field_separator = $input->param("field_separator"); -my $subfield_separator = $input->param("subfield_separator"); -my $encoding = $input->param("encoding"); -my $type = $input->param("profile_type"); -my $action = $input->param("action"); -my $delete = $input->param("delete"); -my $id = $input->param("id"); -if ($delete) { $action = "delete"; } - -my $profile_content = $type eq "marc" - ? $input->param("profile_marc_content") - : $input->param("profile_sql_content"); - -if ($profile_name && $profile_content && $action) { - my $rows; - - if ($action eq "create") { - my $query = "INSERT INTO export_format(export_format_id, profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?)"; - my $sth = $dbh->prepare($query); - $rows = $sth->execute($profile_name, $profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding, $type); - - } - - if ($action eq "edit") { - my $query = "UPDATE export_format SET description=?, content=?, csv_separator=?, field_separator=?, subfield_separator=?, encoding=?, type=? WHERE export_format_id=? LIMIT 1"; - my $sth = $dbh->prepare($query); - $rows = $sth->execute($profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding, $type, $profile_name); - } - - if ($action eq "delete") { - my $query = "DELETE FROM export_format WHERE export_format_id=? LIMIT 1"; - my $sth = $dbh->prepare($query); - $rows = $sth->execute($profile_name); - - } - - $rows ? $template->param(success => 1) : $template->param(error => 1); - $template->param(profile_name => $profile_name); - $template->param(action => $action); - -} - - # If a profile has been selected for modification - if ($id) { - my $query = "SELECT export_format_id, profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type FROM export_format WHERE export_format_id = ?"; - my $sth; - $sth = $dbh->prepare($query); - - $sth->execute($id); - my $selected_profile = $sth->fetchrow_arrayref(); - $template->param( - selected_profile_id => $selected_profile->[0], - selected_profile_name => $selected_profile->[1], - selected_profile_description => $selected_profile->[2], - selected_profile_content => $selected_profile->[3], - selected_csv_separator => $selected_profile->[4], - selected_field_separator => $selected_profile->[5], - selected_subfield_separator => $selected_profile->[6], - selected_encoding => $selected_profile->[7], - selected_profile_type => $selected_profile->[8] - ); - - } - - # List of existing profiles - $template->param(existing_profiles => GetCsvProfilesLoop()); - -output_html_with_http_headers $input, $cookie, $template->output; +#!/usr/bin/perl + +# Copyright 2009 BibLibre +# Copyright 2015 Koha Development Team +# +# 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 . + +=head1 NAME + +csv-profile.pl : Defines a CSV export profile + +=head1 SYNOPSIS + +=cut + +=head1 DESCRIPTION + +This script allow the user to define a new profile for CSV export + +=head1 FUNCTIONS + +=cut + +use Modern::Perl; +use Encode; + +use C4::Auth; +use C4::Context; +use C4::Output; +use CGI qw ( -utf8 ); +use C4::Koha; +use Koha::CsvProfiles; + +my $input = new CGI; +my $export_format_id = $input->param('export_format_id'); +my $op = $input->param('op') || 'list'; +my @messages; + +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { template_name => "tools/csv-profiles.tt", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { tools => 'manage_csv_profiles' }, + debug => 1, + } +); + +# Getting available encodings list +$template->param( encodings => [ Encode->encodings ] ); + +if ( $op eq 'add_form' ) { + my $csv_profile; + if ($export_format_id) { + $csv_profile = Koha::CsvProfiles->find($export_format_id); + } + $template->param( csv_profile => $csv_profile, ); +} elsif ( $op eq 'add_validate' ) { + my $profile = $input->param("profile"); + my $description = $input->param("description"); + my $type = $input->param("type"); + my $content = + $type eq "marc" + ? $input->param("marc_content") + : $input->param("sql_content"); + my $csv_separator = $input->param("csv_separator"); + my $field_separator = $input->param("field_separator"); + my $subfield_separator = $input->param("subfield_separator"); + my $encoding = $input->param("encoding"); + + if ($export_format_id) { + my $csv_profile = Koha::CsvProfiles->find($export_format_id); + $csv_profile->profile($profile); + $csv_profile->description($description); + $csv_profile->content($content); + $csv_profile->csv_separator($csv_separator); + $csv_profile->field_separator($field_separator); + $csv_profile->subfield_separator($subfield_separator); + $csv_profile->encoding($encoding); + $csv_profile->type($type); + eval { $csv_profile->store; }; + + if ($@) { + push @messages, { type => 'error', code => 'error_on_update' }; + } else { + push @messages, { type => 'message', code => 'success_on_update' }; + } + } else { + my $csv_profile = Koha::CsvProfile->new( + { profile => $profile, + description => $description, + content => $content, + csv_separator => $csv_separator, + field_separator => $field_separator, + subfield_separator => $subfield_separator, + encoding => $encoding, + type => $type, + } + ); + eval { $csv_profile->store; }; + if ($@) { + push @messages, { type => 'error', code => 'error_on_insert' }; + } else { + push @messages, { type => 'message', code => 'success_on_insert' }; + } + } + $op = 'list'; +} elsif ( $op eq 'delete_confirm' ) { + my $csv_profile = Koha::CsvProfiles->find($export_format_id); + $template->param( csv_profile => $csv_profile, ); +} elsif ( $op eq 'delete_confirmed' ) { + my $csv_profile = Koha::CsvProfiles->find($export_format_id); + my $deleted = eval { $csv_profile->delete; }; + + if ( $@ or not $deleted ) { + push @messages, { type => 'error', code => 'error_on_delete' }; + } else { + push @messages, { type => 'message', code => 'success_on_delete' }; + } + $op = 'list'; +} + +if ( $op eq 'list' ) { + my $csv_profiles = Koha::CsvProfiles->search; + $template->param( csv_profiles => $csv_profiles, ); +} + +$template->param( + messages => \@messages, + op => $op, +); + +output_html_with_http_headers $input, $cookie, $template->output; -- 2.11.4.GIT