From 9d3962a27567c77420bfd38a756c789469a93a17 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 Content-Type: text/plain; charset=utf-8 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 --- .../prog/en/modules/tools/csv-profiles.tt | 610 ++++++++------------ tools/csv-profiles.pl | 160 ++--- 2 files changed, 323 insertions(+), 447 deletions(-) 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 index 35b7b46..5fe6a91 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 @@ -2,395 +2,263 @@ - [% INCLUDE 'header.inc' %] - [% INCLUDE 'cat-search.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 %] +
+
+
+
+ +[% 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. - - - Required -
  8. - -
  9. -
  10. - -
  11. - -
  12. + + + [% END %] + + Required + +
  13. + + +
  14. +
  15. + + +
  16. + +
  17. + + +
  18. + +
  19. + +
  20. + +
  21. + +
  22. + +
  23. + + + 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.

    +
  24. + +
  25. + + +

    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

    +
  26. +
+
+
+ + 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 %] -
  • - - - 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.

    -
  • -
  • - - -

    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

    -
  • - - -
    -
    - -
    - [% 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 'tools-menu.inc' %] +
    +
    +[% INCLUDE 'intranet-bottom.inc' %] diff --git a/tools/csv-profiles.pl b/tools/csv-profiles.pl index 7323a6e..7db0f67 100755 --- a/tools/csv-profiles.pl +++ b/tools/csv-profiles.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl # Copyright 2009 BibLibre +# Copyright 2015 Koha Development Team # # This file is part of Koha. # @@ -33,9 +34,7 @@ This script allow the user to define a new profile for CSV export =cut -use strict; -#use warnings; FIXME - Bug 2505 -use Data::Dumper; +use Modern::Perl; use Encode; use C4::Auth; @@ -43,15 +42,15 @@ use C4::Context; use C4::Output; use CGI qw ( -utf8 ); use C4::Koha; -use C4::Csv; +use Koha::CsvProfiles; -my $input = new CGI; -my $dbh = C4::Context->dbh; +my $input = new CGI; +my $export_format_id = $input->param('export_format_id'); +my $op = $input->param('op') || 'list'; +my @messages; -# open template my ( $template, $loggedinuser, $cookie ) = get_template_and_user( - { - template_name => "tools/csv-profiles.tt", + { template_name => "tools/csv-profiles.tt", query => $input, type => "intranet", authnotrequired => 0, @@ -61,78 +60,87 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( ); # 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); - - } +$template->param( encodings => [ Encode->encodings ] ); - 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 ( $op eq 'add_form' ) { + my $csv_profile; + if ($export_format_id) { + $csv_profile = Koha::CsvProfiles->find($export_format_id); } - - 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); - + $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' }; + } } - - $rows ? $template->param(success => 1) : $template->param(error => 1); - $template->param(profile_name => $profile_name); - $template->param(action => $action); - + $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 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] - ); - - } +if ( $op eq 'list' ) { + my $csv_profiles = Koha::CsvProfiles->search; + $template->param( csv_profiles => $csv_profiles, ); +} - # List of existing profiles - $template->param(existing_profiles => GetCsvProfilesLoop()); +$template->param( + messages => \@messages, + op => $op, +); output_html_with_http_headers $input, $cookie, $template->output; -- 1.7.10.4