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