3 # Copyright 2009 BibLibre
4 # Copyright 2015 Koha Development Team
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 csv-profile.pl : Defines a CSV export profile
31 This script allow the user to define a new profile for CSV export
40 use C4::Auth qw( get_template_and_user );
42 use C4::Output qw( output_html_with_http_headers );
44 use Koha::CsvProfiles;
47 my $export_format_id = $input->param('export_format_id');
48 my $op = $input->param('op') || 'list';
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52 { template_name => "tools/csv-profiles.tt",
55 flagsrequired => { tools => 'manage_csv_profiles' },
59 # Getting available encodings list
60 $template->param( encodings => [ Encode->encodings ] );
62 if ( $op eq 'add_form' ) {
64 if ($export_format_id) {
65 $csv_profile = Koha::CsvProfiles->find($export_format_id);
67 $template->param( csv_profile => $csv_profile, );
68 } elsif ( $op eq 'add_validate' ) {
69 my $profile = $input->param("profile");
70 my $description = $input->param("description");
71 my $type = $input->param("type");
74 ? $input->param("used_for_marc")
75 : $input->param("used_for_sql");
78 ? $input->param("marc_content")
79 : $input->param("sql_content");
80 my $csv_separator = $input->param("csv_separator");
81 my $field_separator = $input->param("field_separator");
82 my $subfield_separator = $input->param("subfield_separator");
83 my $encoding = $input->param("encoding");
84 my $staff_only = $input->param("staff_only") ? 1 : 0;
86 if ($export_format_id) {
87 my $csv_profile = Koha::CsvProfiles->find($export_format_id)
88 or die "Something went wrong! This export_format_id does not match any existing CSV profile.";
89 $csv_profile->profile($profile);
90 $csv_profile->description($description);
91 $csv_profile->content($content);
92 $csv_profile->csv_separator($csv_separator);
93 $csv_profile->field_separator($field_separator);
94 $csv_profile->subfield_separator($subfield_separator);
95 $csv_profile->encoding($encoding);
96 $csv_profile->type($type);
97 $csv_profile->used_for($used_for);
98 $csv_profile->staff_only($staff_only);
99 eval { $csv_profile->store; };
102 push @messages, { type => 'error', code => 'error_on_update' };
104 push @messages, { type => 'message', code => 'success_on_update' };
107 my $csv_profile = Koha::CsvProfile->new(
108 { profile => $profile,
109 description => $description,
111 csv_separator => $csv_separator,
112 field_separator => $field_separator,
113 subfield_separator => $subfield_separator,
114 encoding => $encoding,
116 used_for => $used_for,
117 staff_only => $staff_only
120 eval { $csv_profile->store; };
122 push @messages, { type => 'error', code => 'error_on_insert' };
124 push @messages, { type => 'message', code => 'success_on_insert' };
128 } elsif ( $op eq 'delete_confirm' ) {
129 my $csv_profile = Koha::CsvProfiles->find($export_format_id);
130 $template->param( csv_profile => $csv_profile, );
131 } elsif ( $op eq 'delete_confirmed' ) {
132 my $csv_profile = Koha::CsvProfiles->find($export_format_id);
133 my $deleted = eval { $csv_profile->delete; };
135 if ( $@ or not $deleted ) {
136 push @messages, { type => 'error', code => 'error_on_delete' };
138 push @messages, { type => 'message', code => 'success_on_delete' };
143 if ( $op eq 'list' ) {
144 my $csv_profiles = Koha::CsvProfiles->search;
145 $template->param( csv_profiles => $csv_profiles, );
149 messages => \@messages,
153 output_html_with_http_headers $input, $cookie, $template->output;