Bug 24412: (follow-up) prevent request.pl from failing
[koha.git] / tools / csv-profiles.pl
blobe6a7bdaa0d8d7a994b558d4930909758d9e58a83
1 #!/usr/bin/perl
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>.
21 =head1 NAME
23 csv-profile.pl : Defines a CSV export profile
25 =head1 SYNOPSIS
27 =cut
29 =head1 DESCRIPTION
31 This script allow the user to define a new profile for CSV export
33 =head1 FUNCTIONS
35 =cut
37 use Modern::Perl;
38 use Encode;
40 use C4::Auth;
41 use C4::Context;
42 use C4::Output;
43 use CGI qw ( -utf8 );
44 use C4::Koha;
45 use Koha::CsvProfiles;
47 my $input = CGI->new;
48 my $export_format_id = $input->param('export_format_id');
49 my $op = $input->param('op') || 'list';
50 my @messages;
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53 { template_name => "tools/csv-profiles.tt",
54 query => $input,
55 type => "intranet",
56 flagsrequired => { tools => 'manage_csv_profiles' },
57 debug => 1,
61 # Getting available encodings list
62 $template->param( encodings => [ Encode->encodings ] );
64 if ( $op eq 'add_form' ) {
65 my $csv_profile;
66 if ($export_format_id) {
67 $csv_profile = Koha::CsvProfiles->find($export_format_id);
69 $template->param( csv_profile => $csv_profile, );
70 } elsif ( $op eq 'add_validate' ) {
71 my $profile = $input->param("profile");
72 my $description = $input->param("description");
73 my $type = $input->param("type");
74 my $used_for =
75 $type eq "marc"
76 ? $input->param("used_for_marc")
77 : $input->param("used_for_sql");
78 my $content =
79 $type eq "marc"
80 ? $input->param("marc_content")
81 : $input->param("sql_content");
82 my $csv_separator = $input->param("csv_separator");
83 my $field_separator = $input->param("field_separator");
84 my $subfield_separator = $input->param("subfield_separator");
85 my $encoding = $input->param("encoding");
86 my $staff_only = $input->param("staff_only") ? 1 : 0;
88 if ($export_format_id) {
89 my $csv_profile = Koha::CsvProfiles->find($export_format_id)
90 or die "Something went wrong! This export_format_id does not match any existing CSV profile.";
91 $csv_profile->profile($profile);
92 $csv_profile->description($description);
93 $csv_profile->content($content);
94 $csv_profile->csv_separator($csv_separator);
95 $csv_profile->field_separator($field_separator);
96 $csv_profile->subfield_separator($subfield_separator);
97 $csv_profile->encoding($encoding);
98 $csv_profile->type($type);
99 $csv_profile->used_for($used_for);
100 $csv_profile->staff_only($staff_only);
101 eval { $csv_profile->store; };
103 if ($@) {
104 push @messages, { type => 'error', code => 'error_on_update' };
105 } else {
106 push @messages, { type => 'message', code => 'success_on_update' };
108 } else {
109 my $csv_profile = Koha::CsvProfile->new(
110 { profile => $profile,
111 description => $description,
112 content => $content,
113 csv_separator => $csv_separator,
114 field_separator => $field_separator,
115 subfield_separator => $subfield_separator,
116 encoding => $encoding,
117 type => $type,
118 used_for => $used_for,
119 staff_only => $staff_only
122 eval { $csv_profile->store; };
123 if ($@) {
124 push @messages, { type => 'error', code => 'error_on_insert' };
125 } else {
126 push @messages, { type => 'message', code => 'success_on_insert' };
129 $op = 'list';
130 } elsif ( $op eq 'delete_confirm' ) {
131 my $csv_profile = Koha::CsvProfiles->find($export_format_id);
132 $template->param( csv_profile => $csv_profile, );
133 } elsif ( $op eq 'delete_confirmed' ) {
134 my $csv_profile = Koha::CsvProfiles->find($export_format_id);
135 my $deleted = eval { $csv_profile->delete; };
137 if ( $@ or not $deleted ) {
138 push @messages, { type => 'error', code => 'error_on_delete' };
139 } else {
140 push @messages, { type => 'message', code => 'success_on_delete' };
142 $op = 'list';
145 if ( $op eq 'list' ) {
146 my $csv_profiles = Koha::CsvProfiles->search;
147 $template->param( csv_profiles => $csv_profiles, );
150 $template->param(
151 messages => \@messages,
152 op => $op,
155 output_html_with_http_headers $input, $cookie, $template->output;