Add dependency on apache2-mpm-itk for koha-common.
[koha.git] / tools / csv-profiles.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 csv-profile.pl : Defines a CSV export profile
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script allow the user to define a new profile for CSV export
30
31 =head1 FUNCTIONS
32
33 =over 2
34
35 =cut
36
37 use strict;
38 #use warnings; FIXME - Bug 2505
39 use Data::Dumper;
40 use Encode;
41
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
45 use CGI;
46 use C4::Koha;
47 use C4::Csv;
48
49 my $input        = new CGI;
50 my $dbh          = C4::Context->dbh;
51
52 # open template
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {
55         template_name   => "tools/csv-profiles.tmpl",
56         query           => $input,
57         type            => "intranet",
58         authnotrequired => 0,
59         flagsrequired   => { tools => 'manage_csv_profiles' },
60         debug           => 1,
61     }
62 );
63
64 # Getting available encodings list
65 my @encodings = Encode->encodings();
66 my @encodings_loop = map{{encoding => $_}} @encodings;
67 $template->param(encodings => \@encodings_loop);
68
69 my $profile_name        = $input->param("profile_name");
70 my $profile_description = $input->param("profile_description");
71 my $profile_content     = $input->param("profile_content");
72 my $csv_separator       = $input->param("csv_separator");
73 my $field_separator     = $input->param("field_separator");
74 my $subfield_separator  = $input->param("subfield_separator");
75 my $encoding            = $input->param("encoding");
76 my $action              = $input->param("action");
77 my $delete              = $input->param("delete");
78 my $id                  = $input->param("id");
79 if ($delete) { $action = "delete"; }
80
81 if ($profile_name && $profile_content && $action) {
82     my $rows;
83
84     if ($action eq "create") {
85         my $query = "INSERT INTO export_format(export_format_id, profile, description, marcfields, csv_separator, field_separator, subfield_separator, encoding) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)";
86         my $sth   = $dbh->prepare($query);
87         $rows  = $sth->execute($profile_name, $profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding);
88     
89     }
90
91     if ($action eq "edit") {
92         my $query = "UPDATE export_format SET description=?, marcfields=?, csv_separator=?, field_separator=?, subfield_separator=?, encoding=? WHERE export_format_id=? LIMIT 1";
93         my $sth   = $dbh->prepare($query);
94         $rows  = $sth->execute($profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding, $profile_name);
95     }
96
97     if ($action eq "delete") {
98         my $query = "DELETE FROM export_format WHERE export_format_id=? LIMIT 1";
99         my $sth   = $dbh->prepare($query);
100         $rows  = $sth->execute($profile_name);
101
102     }
103
104     $rows ? $template->param(success => 1) : $template->param(error => 1);
105     $template->param(profile_name => $profile_name);
106     $template->param(action => $action);
107
108 }
109
110     # If a profile has been selected for modification
111     if ($id) {
112         my $query = "SELECT export_format_id, profile, description, marcfields, csv_separator, field_separator, subfield_separator, encoding FROM export_format WHERE export_format_id = ?";
113         my $sth;
114         $sth = $dbh->prepare($query);
115
116         $sth->execute($id);
117         my $selected_profile = $sth->fetchrow_arrayref();
118         $template->param(
119             selected_profile_id          => $selected_profile->[0],
120             selected_profile_name        => $selected_profile->[1],
121             selected_profile_description => $selected_profile->[2],
122             selected_profile_marcfields  => $selected_profile->[3],
123             selected_csv_separator       => $selected_profile->[4],
124             selected_field_separator     => $selected_profile->[5],
125             selected_subfield_separator  => $selected_profile->[6],
126             selected_encoding            => $selected_profile->[7]
127         );
128
129     }
130
131     # List of existing profiles
132     $template->param(existing_profiles => GetCsvProfilesLoop());
133
134 output_html_with_http_headers $input, $cookie, $template->output;