MT2116: Addons to the CSV export
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 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 Data::Dumper;
39
40 use C4::Auth;
41 use C4::Context;
42 use C4::Output;
43 use CGI;
44 use C4::Koha;
45 use C4::Csv;
46
47 my $input        = new CGI;
48 my $dbh          = C4::Context->dbh;
49
50 # open template
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {
53         template_name   => "tools/csv-profiles.tmpl",
54         query           => $input,
55         type            => "intranet",
56         authnotrequired => 0,
57         flagsrequired   => { tools => 'manage_csv_profiles' },
58         debug           => 1,
59     }
60 );
61
62
63 my $profile_name        = $input->param("profile_name");
64 my $profile_description = $input->param("profile_description");
65 my $profile_content     = $input->param("profile_content");
66 my $csv_separator       = $input->param("csv_separator");
67 my $field_separator     = $input->param("field_separator");
68 my $subfield_separator  = $input->param("subfield_separator");
69 my $action              = $input->param("action");
70 my $delete              = $input->param("delete");
71 my $id                  = $input->param("id");
72 if ($delete) { $action = "delete"; }
73
74 if ($profile_name && $profile_content && $action) {
75     my $rows;
76
77     if ($action eq "create") {
78         my $query = "INSERT INTO export_format(export_format_id, profile, description, marcfields, csv_separator, field_separator, subfield_separator) VALUES (NULL, ?, ?, ?, ?, ?, ?)";
79         my $sth   = $dbh->prepare($query);
80         $rows  = $sth->execute($profile_name, $profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator);
81     
82     }
83
84     if ($action eq "edit") {
85         my $query = "UPDATE export_format SET description=?, marcfields=?, csv_separator=?, field_separator=?, subfield_separator=? WHERE export_format_id=? LIMIT 1";
86         my $sth   = $dbh->prepare($query);
87         $rows  = $sth->execute($profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $profile_name);
88 warn "id $id";
89     }
90
91     if ($action eq "delete") {
92         my $query = "DELETE FROM export_format WHERE export_format_id=? LIMIT 1";
93         my $sth   = $dbh->prepare($query);
94         $rows  = $sth->execute($profile_name);
95
96     }
97
98     $rows ? $template->param(success => 1) : $template->param(error => 1);
99     $template->param(profile_name => $profile_name);
100     $template->param(action => $action);
101
102 }
103
104     # If a profile has been selected for modification
105     if ($id) {
106         my $query = "SELECT export_format_id, profile, description, marcfields, csv_separator, field_separator, subfield_separator FROM export_format WHERE export_format_id = ?";
107         my $sth;
108         $sth = $dbh->prepare($query);
109
110         $sth->execute($id);
111         my $selected_profile = $sth->fetchrow_arrayref();
112         warn "value : " . $selected_profile->[4];
113         $template->param(
114             selected_profile_id          => $selected_profile->[0],
115             selected_profile_name        => $selected_profile->[1],
116             selected_profile_description => $selected_profile->[2],
117             selected_profile_marcfields  => $selected_profile->[3],
118             selected_csv_separator       => $selected_profile->[4],
119             selected_field_separator     => $selected_profile->[5],
120             selected_subfield_separator  => $selected_profile->[6]
121         );
122
123     }
124
125     # List of existing profiles
126     $template->param(existing_profiles => GetCsvProfilesLoop());
127
128 output_html_with_http_headers $input, $cookie, $template->output;