Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / tools / csv-profiles.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre
4 # Copyright 2015 Koha Development Team
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 =head1 NAME
22
23 csv-profile.pl : Defines a CSV export profile
24
25 =head1 SYNOPSIS
26
27 =cut
28
29 =head1 DESCRIPTION
30
31 This script allow the user to define a new profile for CSV export
32
33 =head1 FUNCTIONS
34
35 =cut
36
37 use Modern::Perl;
38 use Encode;
39
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;
46
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;
51
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     }
58 );
59
60 # Getting available encodings list
61 $template->param( encodings => [ Encode->encodings ] );
62
63 if ( $op eq 'add_form' ) {
64     my $csv_profile;
65     if ($export_format_id) {
66         $csv_profile = Koha::CsvProfiles->find($export_format_id);
67     }
68     $template->param( csv_profile => $csv_profile, );
69 } elsif ( $op eq 'add_validate' ) {
70     my $profile     = $input->param("profile");
71     my $description = $input->param("description");
72     my $type        = $input->param("type");
73     my $used_for    =
74         $type eq "marc"
75       ? $input->param("used_for_marc")
76       : $input->param("used_for_sql");
77     my $content =
78         $type eq "marc"
79       ? $input->param("marc_content")
80       : $input->param("sql_content");
81     my $csv_separator      = $input->param("csv_separator");
82     my $field_separator    = $input->param("field_separator");
83     my $subfield_separator = $input->param("subfield_separator");
84     my $encoding           = $input->param("encoding");
85     my $staff_only         = $input->param("staff_only") ? 1 : 0;
86
87     if ($export_format_id) {
88         my $csv_profile = Koha::CsvProfiles->find($export_format_id)
89             or die "Something went wrong! This export_format_id does not match any existing CSV profile.";
90         $csv_profile->profile($profile);
91         $csv_profile->description($description);
92         $csv_profile->content($content);
93         $csv_profile->csv_separator($csv_separator);
94         $csv_profile->field_separator($field_separator);
95         $csv_profile->subfield_separator($subfield_separator);
96         $csv_profile->encoding($encoding);
97         $csv_profile->type($type);
98         $csv_profile->used_for($used_for);
99         $csv_profile->staff_only($staff_only);
100         eval { $csv_profile->store; };
101
102         if ($@) {
103             push @messages, { type => 'error', code => 'error_on_update' };
104         } else {
105             push @messages, { type => 'message', code => 'success_on_update' };
106         }
107     } else {
108         my $csv_profile = Koha::CsvProfile->new(
109             {   profile            => $profile,
110                 description        => $description,
111                 content            => $content,
112                 csv_separator      => $csv_separator,
113                 field_separator    => $field_separator,
114                 subfield_separator => $subfield_separator,
115                 encoding           => $encoding,
116                 type               => $type,
117                 used_for           => $used_for,
118                 staff_only         => $staff_only
119             }
120         );
121         eval { $csv_profile->store; };
122         if ($@) {
123             push @messages, { type => 'error', code => 'error_on_insert' };
124         } else {
125             push @messages, { type => 'message', code => 'success_on_insert' };
126         }
127     }
128     $op = 'list';
129 } elsif ( $op eq 'delete_confirm' ) {
130     my $csv_profile = Koha::CsvProfiles->find($export_format_id);
131     $template->param( csv_profile => $csv_profile, );
132 } elsif ( $op eq 'delete_confirmed' ) {
133     my $csv_profile = Koha::CsvProfiles->find($export_format_id);
134     my $deleted = eval { $csv_profile->delete; };
135
136     if ( $@ or not $deleted ) {
137         push @messages, { type => 'error', code => 'error_on_delete' };
138     } else {
139         push @messages, { type => 'message', code => 'success_on_delete' };
140     }
141     $op = 'list';
142 }
143
144 if ( $op eq 'list' ) {
145     my $csv_profiles = Koha::CsvProfiles->search;
146     $template->param( csv_profiles => $csv_profiles, );
147 }
148
149 $template->param(
150     messages => \@messages,
151     op       => $op,
152 );
153
154 output_html_with_http_headers $input, $cookie, $template->output;